@asla/yoursql 0.11.3 → 0.11.4

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.
@@ -9,7 +9,7 @@ import type { SqlLike, TransactionMode } from "./interfaces.ts";
9
9
  */
10
10
  export declare class DbPoolConnection extends DbQuery {
11
11
  #private;
12
- constructor(conn: DbQueryBase, onRelease: (conn: DbQueryBase) => void);
12
+ constructor(conn: DbQueryBase, onRelease: (conn: DbQueryBase) => void, onDispose?: (conn: DbQueryBase) => void);
13
13
  begin(mode?: TransactionMode): Promise<void>;
14
14
  query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
15
15
  query<T = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
@@ -22,6 +22,7 @@ export declare class DbPoolConnection extends DbQuery {
22
22
  get released(): boolean;
23
23
  /** 调用 release() 时,如果事务未提交,则抛出异常 */
24
24
  release(): void;
25
+ dispose(): void;
25
26
  [Symbol.dispose](): void;
26
27
  }
27
28
  //# sourceMappingURL=DbPoolConnection.d.ts.map
@@ -8,12 +8,14 @@ import { ConnectionNotAvailableError } from './errors.js';
8
8
  * @public
9
9
  */
10
10
  class DbPoolConnection extends DbQuery {
11
- constructor(conn, onRelease) {
11
+ constructor(conn, onRelease, onDispose = onRelease) {
12
12
  super();
13
13
  this.#conn = conn;
14
14
  this.#onRelease = onRelease;
15
+ this.#onDispose = onDispose;
15
16
  }
16
17
  #onRelease;
18
+ #onDispose;
17
19
  //implement
18
20
  async begin(mode) {
19
21
  await this.query("BEGIN" + (mode ? " TRANSACTION ISOLATION LEVEL " + mode : ""));
@@ -58,6 +60,13 @@ class DbPoolConnection extends DbQuery {
58
60
  this.#onRelease(conn);
59
61
  }
60
62
  }
63
+ dispose() {
64
+ const conn = this.#conn;
65
+ if (conn) {
66
+ this.#conn = undefined;
67
+ this.#onDispose(conn);
68
+ }
69
+ }
61
70
  //implement
62
71
  [Symbol.dispose]() {
63
72
  return this.release();
@@ -26,23 +26,19 @@ class DbPoolTransaction extends DbQuery {
26
26
  #conn;
27
27
  async commit() {
28
28
  if (this.#conn) {
29
- const promise = this.#conn.execute("COMMIT");
30
- this.#release(this.#conn);
29
+ const conn = this.#conn;
30
+ const promise = conn.execute("COMMIT").then(() => conn.release(), (e) => conn.dispose());
31
31
  return promise;
32
32
  }
33
- else {
34
- this.#release(undefined);
35
- }
33
+ this.#release(undefined);
36
34
  }
37
35
  async rollback() {
38
36
  if (this.#conn) {
39
- const promise = this.#conn.execute("ROLLBACK");
40
- this.#release(this.#conn);
37
+ const conn = this.#conn;
38
+ const promise = conn.execute("ROLLBACK").then(() => conn.release(), (e) => conn.dispose());
41
39
  return promise;
42
40
  }
43
- else {
44
- this.#release(undefined);
45
- }
41
+ this.#release(undefined);
46
42
  }
47
43
  savePoint(savePoint) {
48
44
  return this.execute("SAVEPOINT " + savePoint);
@@ -51,7 +47,7 @@ class DbPoolTransaction extends DbQuery {
51
47
  return this.execute("ROLLBACK TO " + savePoint);
52
48
  }
53
49
  #pending;
54
- #getConnQuery(call, callIfFirst = call, queryError) {
50
+ #getConnQuery(call, callIfFirst = call, queryErrorCatch) {
55
51
  if (this.#pending) {
56
52
  return Promise.reject(new ParallelQueryError());
57
53
  }
@@ -67,11 +63,8 @@ class DbPoolTransaction extends DbQuery {
67
63
  }
68
64
  this.#conn = conn;
69
65
  let promise = callIfFirst(conn);
70
- if (queryError) {
71
- promise = promise.catch((e) => {
72
- queryError();
73
- throw e;
74
- });
66
+ if (queryErrorCatch) {
67
+ promise = promise.catch(queryErrorCatch);
75
68
  }
76
69
  return promise;
77
70
  }, (e) => {
@@ -81,11 +74,8 @@ class DbPoolTransaction extends DbQuery {
81
74
  }
82
75
  else {
83
76
  promise = call(this.#conn);
84
- if (queryError) {
85
- promise = promise.catch((e) => {
86
- queryError();
87
- throw e;
88
- });
77
+ if (queryErrorCatch) {
78
+ promise = promise.catch(queryErrorCatch);
89
79
  }
90
80
  }
91
81
  this.#pending = promise;
@@ -94,12 +84,16 @@ class DbPoolTransaction extends DbQuery {
94
84
  });
95
85
  }
96
86
  #query(call, callIfFirst) {
97
- const onError = () => {
87
+ const onError = (e) => {
98
88
  if (this.#errorRollback) {
99
- this.rollback();
89
+ const passError = () => {
90
+ throw e;
91
+ };
92
+ return this.rollback().then(passError, passError);
100
93
  }
101
94
  else {
102
95
  this.#release(this.#conn);
96
+ throw e;
103
97
  }
104
98
  };
105
99
  return this.#getConnQuery(call, callIfFirst, onError);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asla/yoursql",
3
- "version": "0.11.3",
3
+ "version": "0.11.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/mod.d.ts",
@@ -21,7 +21,6 @@
21
21
  "rollup": "^4.22.4",
22
22
  "tslib": "^2.7.0",
23
23
  "typescript": "^5.6.2",
24
- "typescritp": "^1.0.0",
25
24
  "vitest": "^2.1.1"
26
25
  },
27
26
  "repository": {