@zhin.js/database 1.1.6 → 1.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -279,12 +279,18 @@ export class MySQLDialect extends Dialect {
279
279
  if (this.usePool) {
280
280
  // 从连接池获取一个连接用于事务
281
281
  const connection = await this.pool.getConnection();
282
- // 设置隔离级别
283
- if (options?.isolationLevel) {
284
- await connection.query(`SET TRANSACTION ISOLATION LEVEL ${this.formatIsolationLevel(options.isolationLevel)}`);
282
+ try {
283
+ // 设置隔离级别
284
+ if (options?.isolationLevel) {
285
+ await connection.query(`SET TRANSACTION ISOLATION LEVEL ${this.formatIsolationLevel(options.isolationLevel)}`);
286
+ }
287
+ // 开始事务
288
+ await connection.beginTransaction();
289
+ }
290
+ catch (error) {
291
+ connection.release();
292
+ throw error;
285
293
  }
286
- // 开始事务
287
- await connection.beginTransaction();
288
294
  return {
289
295
  async commit() {
290
296
  try {
@@ -254,7 +254,13 @@ export class PostgreSQLDialect extends Dialect {
254
254
  if (options?.isolationLevel) {
255
255
  beginSql = `BEGIN ISOLATION LEVEL ${this.formatIsolationLevel(options.isolationLevel)}`;
256
256
  }
257
- await client.query(beginSql);
257
+ try {
258
+ await client.query(beginSql);
259
+ }
260
+ catch (error) {
261
+ client.release();
262
+ throw error;
263
+ }
258
264
  return {
259
265
  async commit() {
260
266
  try {
@@ -63,8 +63,9 @@ export class SQLiteDialect extends Dialect {
63
63
  const trimmedSql = sql.trim().toLowerCase();
64
64
  const isInsert = trimmedSql.startsWith('insert');
65
65
  const isUpdateOrDelete = trimmedSql.startsWith('update') || trimmedSql.startsWith('delete');
66
- // 带 RETURNING 的 DML 有结果集,不能走 run 丢行
67
- const hasReturning = /\breturning\b/.test(trimmedSql);
66
+ // 带 RETURNING 的 DML 有结果集,不能走 run 丢行(剥离引号标识符与字符串字面量避免列名误判)
67
+ const sqlWithoutLiterals = trimmedSql.replace(/'[^']*'/g, "''").replace(/"[^"]*"/g, '""');
68
+ const hasReturning = /\breturning\b/.test(sqlWithoutLiterals);
68
69
  const args = this.prepareBindParams(params ?? []);
69
70
  const stmt = this.db.prepare(sql);
70
71
  if (isInsert && !hasReturning) {
@@ -441,8 +441,12 @@ export class RelatedDatabase extends Database {
441
441
  case '$in':
442
442
  if (isSubquery(value[op])) {
443
443
  // 子查询
444
+ const offset = parameterOffset + params.length;
444
445
  const subquery = value[op].toSQL();
445
- clauses.push(`${quotedKey} IN (${subquery.sql})`);
446
+ const subSql = offset > 0
447
+ ? subquery.sql.replace(/\$(\d+)/g, (_, n) => `$${Number(n) + offset}`)
448
+ : subquery.sql;
449
+ clauses.push(`${quotedKey} IN (${subSql})`);
446
450
  params.push(...subquery.params);
447
451
  }
448
452
  else if (Array.isArray(value[op]) && value[op].length) {
@@ -457,8 +461,12 @@ export class RelatedDatabase extends Database {
457
461
  case '$nin':
458
462
  if (isSubquery(value[op])) {
459
463
  // 子查询
464
+ const offset = parameterOffset + params.length;
460
465
  const subquery = value[op].toSQL();
461
- clauses.push(`${quotedKey} NOT IN (${subquery.sql})`);
466
+ const subSql = offset > 0
467
+ ? subquery.sql.replace(/\$(\d+)/g, (_, n) => `$${Number(n) + offset}`)
468
+ : subquery.sql;
469
+ clauses.push(`${quotedKey} NOT IN (${subSql})`);
462
470
  params.push(...subquery.params);
463
471
  }
464
472
  else if (Array.isArray(value[op]) && value[op].length) {
@@ -565,6 +573,9 @@ export class RelatedDatabase extends Database {
565
573
  * 获取所有模型名称
566
574
  */
567
575
  getModelNames() {
576
+ if (this.definitions instanceof Map) {
577
+ return Array.from(this.definitions.keys()).map(String);
578
+ }
568
579
  return Object.keys(this.definitions || {});
569
580
  }
570
581
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/database",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "Universal database abstraction layer for Zhin.js - supports SQLite, MySQL, PostgreSQL, MongoDB, Redis",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",