@asla/yoursql 0.11.3 → 0.12.0
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.
- package/dist/client/DbPoolConnection.d.ts +4 -26
- package/dist/client/DbPoolConnection.js +19 -12
- package/dist/client/DbPoolTransaction.d.ts +3 -25
- package/dist/client/DbPoolTransaction.js +23 -29
- package/dist/client/DbQuery.d.ts +4 -1
- package/dist/client/DbQueryPool.d.ts +1 -2
- package/dist/client/interfaces.d.ts +20 -1
- package/dist/client/mod.js +2 -2
- package/package.json +1 -2
|
@@ -1,27 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 池连接
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
export declare class DbPoolConnection extends DbQuery {
|
|
11
|
-
#private;
|
|
12
|
-
constructor(conn: DbQueryBase, onRelease: (conn: DbQueryBase) => void);
|
|
13
|
-
begin(mode?: TransactionMode): Promise<void>;
|
|
14
|
-
query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
|
|
15
|
-
query<T = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
|
|
16
|
-
query<T = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
|
|
17
|
-
execute(sql: QueryInput | MultipleQueryInput): Promise<void>;
|
|
18
|
-
/** @deprecated 不建议使用 */
|
|
19
|
-
multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
20
|
-
rollback(): Promise<void>;
|
|
21
|
-
commit(): Promise<void>;
|
|
22
|
-
get released(): boolean;
|
|
23
|
-
/** 调用 release() 时,如果事务未提交,则抛出异常 */
|
|
24
|
-
release(): void;
|
|
25
|
-
[Symbol.dispose](): void;
|
|
26
|
-
}
|
|
1
|
+
import { type DbQueryBase } from "./DbQueryBase.ts";
|
|
2
|
+
import type { DbPoolConnection } from "./interfaces.ts";
|
|
3
|
+
/** @public */
|
|
4
|
+
export declare function createDbPoolConnection(conn: DbQueryBase, onRelease: (conn: DbQueryBase) => void, onDispose?: (conn: DbQueryBase) => void): DbPoolConnection;
|
|
27
5
|
//# sourceMappingURL=DbPoolConnection.d.ts.map
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import { DbQuery } from './DbQuery.js';
|
|
2
2
|
import { ConnectionNotAvailableError } from './errors.js';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 池连接
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
class DbPoolConnection extends DbQuery {
|
|
11
|
-
constructor(conn, onRelease) {
|
|
4
|
+
class DbPoolConnectionImpl extends DbQuery {
|
|
5
|
+
constructor(conn, onRelease, onDispose = onRelease) {
|
|
12
6
|
super();
|
|
13
7
|
this.#conn = conn;
|
|
14
8
|
this.#onRelease = onRelease;
|
|
9
|
+
this.#onDispose = onDispose;
|
|
15
10
|
}
|
|
16
11
|
#onRelease;
|
|
12
|
+
#onDispose;
|
|
17
13
|
//implement
|
|
18
14
|
async begin(mode) {
|
|
19
|
-
await this.
|
|
15
|
+
await this.execute("BEGIN" + (mode ? " TRANSACTION ISOLATION LEVEL " + mode : ""));
|
|
20
16
|
}
|
|
21
17
|
#conn;
|
|
22
18
|
query(sql) {
|
|
@@ -41,11 +37,11 @@ class DbPoolConnection extends DbQuery {
|
|
|
41
37
|
}
|
|
42
38
|
//implement
|
|
43
39
|
async rollback() {
|
|
44
|
-
await this.
|
|
40
|
+
await this.execute("ROLLBACK");
|
|
45
41
|
}
|
|
46
42
|
//implement
|
|
47
43
|
async commit() {
|
|
48
|
-
await this.
|
|
44
|
+
await this.execute("COMMIT");
|
|
49
45
|
}
|
|
50
46
|
get released() {
|
|
51
47
|
return !this.#conn;
|
|
@@ -58,10 +54,21 @@ class DbPoolConnection extends DbQuery {
|
|
|
58
54
|
this.#onRelease(conn);
|
|
59
55
|
}
|
|
60
56
|
}
|
|
57
|
+
dispose() {
|
|
58
|
+
const conn = this.#conn;
|
|
59
|
+
if (conn) {
|
|
60
|
+
this.#conn = undefined;
|
|
61
|
+
this.#onDispose(conn);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
61
64
|
//implement
|
|
62
65
|
[Symbol.dispose]() {
|
|
63
66
|
return this.release();
|
|
64
67
|
}
|
|
65
68
|
}
|
|
69
|
+
/** @public */
|
|
70
|
+
function createDbPoolConnection(conn, onRelease, onDispose = onRelease) {
|
|
71
|
+
return new DbPoolConnectionImpl(conn, onRelease, onDispose);
|
|
72
|
+
}
|
|
66
73
|
|
|
67
|
-
export {
|
|
74
|
+
export { createDbPoolConnection };
|
|
@@ -1,31 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { MultipleQueryResult, QueryRowsResult } from "./DbQueryBase.ts";
|
|
3
|
-
import type { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
4
|
-
import type { DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
|
|
1
|
+
import type { DbPoolConnection, DbPoolTransaction, TransactionMode } from "./interfaces.ts";
|
|
5
2
|
/** @public */
|
|
6
3
|
export type DbPoolTransactionOption = {
|
|
7
4
|
errorRollback?: boolean;
|
|
8
5
|
mode?: TransactionMode;
|
|
9
6
|
};
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
* 池连接事务
|
|
13
|
-
*/
|
|
14
|
-
export declare class DbPoolTransaction extends DbQuery implements DbTransaction {
|
|
15
|
-
#private;
|
|
16
|
-
readonly mode?: TransactionMode;
|
|
17
|
-
constructor(connect: () => Promise<DbPoolConnection>, option?: TransactionMode | DbPoolTransactionOption);
|
|
18
|
-
commit(): Promise<void>;
|
|
19
|
-
rollback(): Promise<void>;
|
|
20
|
-
savePoint(savePoint: string): Promise<void>;
|
|
21
|
-
rollbackTo(savePoint: string): Promise<void>;
|
|
22
|
-
query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
|
|
23
|
-
query<T extends object = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
|
|
24
|
-
query<T extends object = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
|
|
25
|
-
execute(sql: QueryInput | MultipleQueryInput): Promise<void>;
|
|
26
|
-
/** @deprecated 不建议使用 */
|
|
27
|
-
multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
28
|
-
get released(): boolean;
|
|
29
|
-
[Symbol.asyncDispose](): Promise<void>;
|
|
30
|
-
}
|
|
7
|
+
/** @public */
|
|
8
|
+
export declare function createDbPoolTransaction(connect: () => Promise<DbPoolConnection>, option?: TransactionMode | DbPoolTransactionOption): DbPoolTransaction;
|
|
31
9
|
//# sourceMappingURL=DbPoolTransaction.d.ts.map
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { DbQuery } from './DbQuery.js';
|
|
2
2
|
import { ParallelQueryError, ConnectionNotAvailableError } from './errors.js';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
* @public
|
|
6
|
-
* 池连接事务
|
|
7
|
-
*/
|
|
8
|
-
class DbPoolTransaction extends DbQuery {
|
|
4
|
+
class DbPoolTransactionImpl extends DbQuery {
|
|
9
5
|
#errorRollback;
|
|
10
6
|
mode;
|
|
11
7
|
#begin;
|
|
@@ -26,23 +22,19 @@ class DbPoolTransaction extends DbQuery {
|
|
|
26
22
|
#conn;
|
|
27
23
|
async commit() {
|
|
28
24
|
if (this.#conn) {
|
|
29
|
-
const
|
|
30
|
-
|
|
25
|
+
const conn = this.#conn;
|
|
26
|
+
const promise = conn.execute("COMMIT").then(() => conn.release(), (e) => conn.dispose());
|
|
31
27
|
return promise;
|
|
32
28
|
}
|
|
33
|
-
|
|
34
|
-
this.#release(undefined);
|
|
35
|
-
}
|
|
29
|
+
this.#release(undefined);
|
|
36
30
|
}
|
|
37
31
|
async rollback() {
|
|
38
32
|
if (this.#conn) {
|
|
39
|
-
const
|
|
40
|
-
|
|
33
|
+
const conn = this.#conn;
|
|
34
|
+
const promise = conn.execute("ROLLBACK").then(() => conn.release(), (e) => conn.dispose());
|
|
41
35
|
return promise;
|
|
42
36
|
}
|
|
43
|
-
|
|
44
|
-
this.#release(undefined);
|
|
45
|
-
}
|
|
37
|
+
this.#release(undefined);
|
|
46
38
|
}
|
|
47
39
|
savePoint(savePoint) {
|
|
48
40
|
return this.execute("SAVEPOINT " + savePoint);
|
|
@@ -51,7 +43,7 @@ class DbPoolTransaction extends DbQuery {
|
|
|
51
43
|
return this.execute("ROLLBACK TO " + savePoint);
|
|
52
44
|
}
|
|
53
45
|
#pending;
|
|
54
|
-
#getConnQuery(call, callIfFirst = call,
|
|
46
|
+
#getConnQuery(call, callIfFirst = call, queryErrorCatch) {
|
|
55
47
|
if (this.#pending) {
|
|
56
48
|
return Promise.reject(new ParallelQueryError());
|
|
57
49
|
}
|
|
@@ -67,11 +59,8 @@ class DbPoolTransaction extends DbQuery {
|
|
|
67
59
|
}
|
|
68
60
|
this.#conn = conn;
|
|
69
61
|
let promise = callIfFirst(conn);
|
|
70
|
-
if (
|
|
71
|
-
promise = promise.catch(
|
|
72
|
-
queryError();
|
|
73
|
-
throw e;
|
|
74
|
-
});
|
|
62
|
+
if (queryErrorCatch) {
|
|
63
|
+
promise = promise.catch(queryErrorCatch);
|
|
75
64
|
}
|
|
76
65
|
return promise;
|
|
77
66
|
}, (e) => {
|
|
@@ -81,11 +70,8 @@ class DbPoolTransaction extends DbQuery {
|
|
|
81
70
|
}
|
|
82
71
|
else {
|
|
83
72
|
promise = call(this.#conn);
|
|
84
|
-
if (
|
|
85
|
-
promise = promise.catch(
|
|
86
|
-
queryError();
|
|
87
|
-
throw e;
|
|
88
|
-
});
|
|
73
|
+
if (queryErrorCatch) {
|
|
74
|
+
promise = promise.catch(queryErrorCatch);
|
|
89
75
|
}
|
|
90
76
|
}
|
|
91
77
|
this.#pending = promise;
|
|
@@ -94,12 +80,16 @@ class DbPoolTransaction extends DbQuery {
|
|
|
94
80
|
});
|
|
95
81
|
}
|
|
96
82
|
#query(call, callIfFirst) {
|
|
97
|
-
const onError = () => {
|
|
83
|
+
const onError = (e) => {
|
|
98
84
|
if (this.#errorRollback) {
|
|
99
|
-
|
|
85
|
+
const passError = () => {
|
|
86
|
+
throw e;
|
|
87
|
+
};
|
|
88
|
+
return this.rollback().then(passError, passError);
|
|
100
89
|
}
|
|
101
90
|
else {
|
|
102
91
|
this.#release(this.#conn);
|
|
92
|
+
throw e;
|
|
103
93
|
}
|
|
104
94
|
};
|
|
105
95
|
return this.#getConnQuery(call, callIfFirst, onError);
|
|
@@ -148,5 +138,9 @@ class DbPoolTransaction extends DbQuery {
|
|
|
148
138
|
return this.rollback();
|
|
149
139
|
}
|
|
150
140
|
}
|
|
141
|
+
/** @public */
|
|
142
|
+
function createDbPoolTransaction(connect, option) {
|
|
143
|
+
return new DbPoolTransactionImpl(connect, option);
|
|
144
|
+
}
|
|
151
145
|
|
|
152
|
-
export {
|
|
146
|
+
export { createDbPoolTransaction };
|
package/dist/client/DbQuery.d.ts
CHANGED
|
@@ -16,7 +16,10 @@ export declare abstract class DbQuery {
|
|
|
16
16
|
abstract query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
|
|
17
17
|
abstract query<T = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
|
|
18
18
|
abstract query<T = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* 执行多语句的方法
|
|
21
|
+
* @deprecated 不建议使用。改用 query()
|
|
22
|
+
*/
|
|
20
23
|
abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
21
24
|
/** 单语句查询受影响的行 */
|
|
22
25
|
queryCount(sql: QueryInput): Promise<number>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { DbCursor, DbCursorOption } from "./DbCursor.ts";
|
|
2
|
-
import { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
3
2
|
import { DbQuery } from "./DbQuery.ts";
|
|
4
|
-
import { DbPool, DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
|
|
3
|
+
import { DbPool, DbPoolConnection, DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
|
|
5
4
|
import { QueryRowsResult } from "./DbQueryBase.ts";
|
|
6
5
|
import { SqlStatementDataset, InferQueryResult } from "./_type.ts";
|
|
7
6
|
/** @public */
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { DbQuery } from "./DbQuery.ts";
|
|
2
|
-
import { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
3
2
|
import { DbCursor, DbCursorOption } from "./DbCursor.ts";
|
|
4
3
|
import { SqlStatementDataset, SqlTemplate } from "./_type.ts";
|
|
5
4
|
/**
|
|
@@ -9,6 +8,18 @@ import { SqlStatementDataset, SqlTemplate } from "./_type.ts";
|
|
|
9
8
|
export interface DbConnection extends DbQuery, AsyncDisposable {
|
|
10
9
|
close(): Promise<void>;
|
|
11
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* 数据库池连接
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export interface DbPoolConnection extends DbQuery, Disposable {
|
|
16
|
+
release(): void;
|
|
17
|
+
dispose(): void;
|
|
18
|
+
begin(mode?: TransactionMode): Promise<void>;
|
|
19
|
+
commit(): Promise<void>;
|
|
20
|
+
rollback(): Promise<void>;
|
|
21
|
+
get released(): boolean;
|
|
22
|
+
}
|
|
12
23
|
/** @public */
|
|
13
24
|
export type TransactionMode = "SERIALIZABLE" | "REPEATABLE READ" | "READ COMMITTED" | "READ UNCOMMITTED";
|
|
14
25
|
/**
|
|
@@ -49,6 +60,14 @@ export interface DbTransaction extends DbQuery, AsyncDisposable {
|
|
|
49
60
|
/** 提交,并释放连接 */
|
|
50
61
|
commit(): Promise<void>;
|
|
51
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* @public
|
|
65
|
+
* 池连接事务
|
|
66
|
+
*/
|
|
67
|
+
export interface DbPoolTransaction extends DbTransaction {
|
|
68
|
+
readonly mode?: TransactionMode;
|
|
69
|
+
get released(): boolean;
|
|
70
|
+
}
|
|
52
71
|
/**
|
|
53
72
|
* 数据库连接池
|
|
54
73
|
* @public
|
package/dist/client/mod.js
CHANGED
|
@@ -2,6 +2,6 @@ export { ConnectionNotAvailableError, ParallelQueryError } from './errors.js';
|
|
|
2
2
|
export { isSqlTemplate, sqlLikeToString } from './DbQueryBase.js';
|
|
3
3
|
export { DbQuery } from './DbQuery.js';
|
|
4
4
|
export { DbCursor } from './DbCursor.js';
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
5
|
+
export { createDbPoolConnection } from './DbPoolConnection.js';
|
|
6
|
+
export { createDbPoolTransaction } from './DbPoolTransaction.js';
|
|
7
7
|
export { DbQueryPool } from './DbQueryPool.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asla/yoursql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
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": {
|