@minatojs/driver-mysql
Advanced tools
Comparing version 2.7.1 to 2.7.2
@@ -44,2 +44,3 @@ import type { Pool, PoolConfig } from 'mysql'; | ||
sql: MySQLBuilder; | ||
private session?; | ||
private _compat; | ||
@@ -71,4 +72,6 @@ private _queryTasks; | ||
remove(sel: Selection.Mutable): Promise<{ | ||
matched?: undefined; | ||
removed?: undefined; | ||
} | { | ||
matched: any; | ||
removed: any; | ||
@@ -86,3 +89,4 @@ }>; | ||
}>; | ||
withTransaction(callback: (session: Driver) => Promise<void>): Promise<void>; | ||
} | ||
export default MySQLDriver; |
@@ -231,2 +231,3 @@ "use strict"; | ||
sql; | ||
session; | ||
_compat = {}; | ||
@@ -424,5 +425,6 @@ _queryTasks = []; | ||
return new Promise((resolve, reject) => { | ||
var _a; | ||
if (debug) | ||
logger.debug("> %s", sql); | ||
this.pool.query(sql, (err, results) => { | ||
((_a = this.session) != null ? _a : this.pool).query(sql, (err, results) => { | ||
if (!err) | ||
@@ -441,3 +443,3 @@ return resolve(results); | ||
sql = (0, import_mysql.format)(sql, values); | ||
if (!this.config.multipleStatements) { | ||
if (this.session || !this.config.multipleStatements) { | ||
return this.query(sql); | ||
@@ -532,3 +534,3 @@ } | ||
const result = await this.query(`DELETE FROM ${(0, import_sql_utils.escapeId)(table)} WHERE ` + filter); | ||
return { removed: result.affectedRows }; | ||
return { matched: result.affectedRows, removed: result.affectedRows }; | ||
} | ||
@@ -602,2 +604,21 @@ async create(sel, data) { | ||
} | ||
async withTransaction(callback) { | ||
return new Promise((resolve, reject) => { | ||
this.pool.getConnection((err, conn) => { | ||
if (err) { | ||
logger.warn("getConnection failed: ", err); | ||
return; | ||
} | ||
const driver = new Proxy(this, { | ||
get(target, p, receiver) { | ||
if (p === "session") | ||
return conn; | ||
else | ||
return Reflect.get(target, p, receiver); | ||
} | ||
}); | ||
conn.beginTransaction(() => callback(driver).then(() => conn.commit(() => resolve()), (e) => conn.rollback(() => reject(e))).finally(() => conn.release())); | ||
}); | ||
}); | ||
} | ||
}; | ||
@@ -604,0 +625,0 @@ __name(_MySQLDriver, "MySQLDriver"); |
{ | ||
"name": "@minatojs/driver-mysql", | ||
"version": "2.7.1", | ||
"version": "2.7.2", | ||
"description": "MySQL Driver for Minato", | ||
@@ -29,6 +29,6 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.6.1" | ||
"@minatojs/core": "^2.7.0" | ||
}, | ||
"devDependencies": { | ||
"@minatojs/tests": "^1.8.0", | ||
"@minatojs/tests": "^1.8.1", | ||
"@types/mysql": "^2.15.23" | ||
@@ -35,0 +35,0 @@ }, |
import { createPool, format } from '@vlasky/mysql' | ||
import type { OkPacket, Pool, PoolConfig } from 'mysql' | ||
import type { OkPacket, Pool, PoolConfig, PoolConnection } from 'mysql' | ||
import { Dict, difference, makeArray, pick, Time } from 'cosmokit' | ||
@@ -228,2 +228,3 @@ import { Database, Driver, Eval, executeUpdate, Field, isEvalExpr, Model, RuntimeError, Selection } from '@minatojs/core' | ||
private session?: PoolConnection | ||
private _compat: Compat = {} | ||
@@ -447,3 +448,3 @@ private _queryTasks: QueryTask[] = [] | ||
if (debug) logger.debug('> %s', sql) | ||
this.pool.query(sql, (err: Error, results) => { | ||
;(this.session ?? this.pool).query(sql, (err: Error, results) => { | ||
if (!err) return resolve(results) | ||
@@ -462,3 +463,3 @@ logger.warn('> %s', sql) | ||
sql = format(sql, values) | ||
if (!this.config.multipleStatements) { | ||
if (this.session || !this.config.multipleStatements) { | ||
return this.query(sql) | ||
@@ -557,3 +558,3 @@ } | ||
const result = await this.query(`DELETE FROM ${escapeId(table)} WHERE ` + filter) | ||
return { removed: result.affectedRows } | ||
return { matched: result.affectedRows, removed: result.affectedRows } | ||
} | ||
@@ -630,4 +631,24 @@ | ||
} | ||
async withTransaction(callback: (session: Driver) => Promise<void>) { | ||
return new Promise<void>((resolve, reject) => { | ||
this.pool.getConnection((err, conn) => { | ||
if (err) { | ||
logger.warn('getConnection failed: ', err) | ||
return | ||
} | ||
const driver = new Proxy(this, { | ||
get(target, p, receiver) { | ||
if (p === 'session') return conn | ||
else return Reflect.get(target, p, receiver) | ||
}, | ||
}) | ||
conn.beginTransaction(() => callback(driver) | ||
.then(() => conn.commit(() => resolve()), (e) => conn.rollback(() => reject(e))) | ||
.finally(() => conn.release())) | ||
}) | ||
}) | ||
} | ||
} | ||
export default MySQLDriver |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70754
1292