@asla/yoursql 0.13.0 → 0.14.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/README.md +22 -206
- package/dist/sql_gen/SqlStatement.d.ts +0 -1
- package/dist/sql_gen/SqlStatement.js +3 -5
- package/dist/sql_gen/_statement.d.ts +0 -1
- package/dist/sql_gen/_statement.js +10 -13
- package/dist/sql_gen/mod.d.ts +0 -1
- package/dist/sql_gen/mod.js +10 -19
- package/dist/sql_gen/sql_value/SqlValuesDataset.d.ts +28 -0
- package/dist/sql_gen/sql_value/SqlValuesDataset.js +51 -0
- package/dist/sql_gen/sql_value/ValueSqlTemplate.d.ts +0 -1
- package/dist/sql_gen/sql_value/ValueSqlTemplate.js +2 -5
- package/dist/sql_gen/sql_value/_to_values.d.ts +0 -1
- package/dist/sql_gen/sql_value/_to_values.js +5 -8
- package/dist/sql_gen/sql_value/db_type.d.ts +0 -1
- package/dist/sql_gen/sql_value/db_type.js +2 -5
- package/dist/sql_gen/sql_value/sql_value.d.ts +3 -25
- package/dist/sql_gen/sql_value/sql_value.js +7 -60
- package/dist/sql_gen/sql_value/type.d.ts +0 -1
- package/dist/sql_gen/sql_value/type.js +1 -0
- package/dist/sql_gen/statement/_modify.d.ts +0 -1
- package/dist/sql_gen/statement/_modify.js +1 -0
- package/dist/sql_gen/statement/cte.d.ts +0 -1
- package/dist/sql_gen/statement/cte.js +7 -10
- package/dist/sql_gen/statement/delete.d.ts +0 -1
- package/dist/sql_gen/statement/delete.js +2 -5
- package/dist/sql_gen/statement/delete_chain.d.ts +0 -1
- package/dist/sql_gen/statement/delete_chain.js +1 -0
- package/dist/sql_gen/statement/delete_impl.d.ts +0 -1
- package/dist/sql_gen/statement/delete_impl.js +3 -6
- package/dist/sql_gen/statement/insert.d.ts +0 -1
- package/dist/sql_gen/statement/insert.js +2 -5
- package/dist/sql_gen/statement/insert_chain.d.ts +8 -1
- package/dist/sql_gen/statement/insert_chain.js +1 -0
- package/dist/sql_gen/statement/insert_impl.d.ts +0 -1
- package/dist/sql_gen/statement/insert_impl.js +4 -7
- package/dist/sql_gen/statement/mod.d.ts +0 -1
- package/dist/sql_gen/statement/mod.js +9 -0
- package/dist/sql_gen/statement/select.d.ts +0 -1
- package/dist/sql_gen/statement/select.js +4 -7
- package/dist/sql_gen/statement/select_chain.d.ts +0 -1
- package/dist/sql_gen/statement/select_chain.js +1 -0
- package/dist/sql_gen/statement/select_impl.d.ts +0 -1
- package/dist/sql_gen/statement/select_impl.js +4 -7
- package/dist/sql_gen/statement/update.d.ts +0 -1
- package/dist/sql_gen/statement/update.js +2 -5
- package/dist/sql_gen/statement/update_chain.d.ts +0 -1
- package/dist/sql_gen/statement/update_chain.js +1 -0
- package/dist/sql_gen/statement/update_impl.d.ts +0 -1
- package/dist/sql_gen/statement/update_impl.js +4 -7
- package/dist/sql_gen/util.d.ts +0 -1
- package/dist/sql_gen/util.js +1 -3
- package/dist/sql_gen/your_table/checker.d.ts +0 -1
- package/dist/sql_gen/your_table/checker.js +2 -5
- package/dist/sql_gen/your_table/infer_db_type.d.ts +0 -1
- package/dist/sql_gen/your_table/infer_db_type.js +3 -5
- package/dist/sql_gen/your_table/mod.d.ts +0 -1
- package/dist/sql_gen/your_table/mod.js +3 -0
- package/dist/sql_gen/your_table/table.d.ts +0 -1
- package/dist/sql_gen/your_table/table.js +2 -5
- package/package.json +31 -34
- package/dist/client/DbCursor.d.ts +0 -14
- package/dist/client/DbCursor.js +0 -21
- package/dist/client/DbPoolConnection.d.ts +0 -5
- package/dist/client/DbPoolConnection.js +0 -74
- package/dist/client/DbPoolTransaction.d.ts +0 -9
- package/dist/client/DbPoolTransaction.js +0 -146
- package/dist/client/DbQuery.d.ts +0 -50
- package/dist/client/DbQuery.js +0 -42
- package/dist/client/DbQueryBase.d.ts +0 -38
- package/dist/client/DbQueryBase.js +0 -32
- package/dist/client/DbQueryPool.d.ts +0 -39
- package/dist/client/DbQueryPool.js +0 -56
- package/dist/client/_type.d.ts +0 -2
- package/dist/client/errors.d.ts +0 -9
- package/dist/client/errors.js +0 -14
- package/dist/client/interfaces.d.ts +0 -87
- package/dist/client/mod.d.ts +0 -9
- package/dist/client/mod.js +0 -7
package/dist/client/DbCursor.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/** @public */
|
|
2
|
-
class DbCursor {
|
|
3
|
-
// implement
|
|
4
|
-
[Symbol.asyncDispose]() {
|
|
5
|
-
return this.close();
|
|
6
|
-
}
|
|
7
|
-
async *[Symbol.asyncIterator]() {
|
|
8
|
-
let data = await this.read();
|
|
9
|
-
try {
|
|
10
|
-
while (data.length) {
|
|
11
|
-
yield* data;
|
|
12
|
-
data = await this.read();
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
finally {
|
|
16
|
-
await this.close();
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export { DbCursor };
|
|
@@ -1,5 +0,0 @@
|
|
|
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;
|
|
5
|
-
//# sourceMappingURL=DbPoolConnection.d.ts.map
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { DbQuery } from './DbQuery.js';
|
|
2
|
-
import { ConnectionNotAvailableError } from './errors.js';
|
|
3
|
-
|
|
4
|
-
class DbPoolConnectionImpl extends DbQuery {
|
|
5
|
-
constructor(conn, onRelease, onDispose = onRelease) {
|
|
6
|
-
super();
|
|
7
|
-
this.#conn = conn;
|
|
8
|
-
this.#onRelease = onRelease;
|
|
9
|
-
this.#onDispose = onDispose;
|
|
10
|
-
}
|
|
11
|
-
#onRelease;
|
|
12
|
-
#onDispose;
|
|
13
|
-
//implement
|
|
14
|
-
async begin(mode) {
|
|
15
|
-
await this.execute("BEGIN" + (mode ? " TRANSACTION ISOLATION LEVEL " + mode : ""));
|
|
16
|
-
}
|
|
17
|
-
#conn;
|
|
18
|
-
query(sql) {
|
|
19
|
-
if (!this.#conn)
|
|
20
|
-
return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
|
|
21
|
-
if (typeof sql === "function")
|
|
22
|
-
sql = sql();
|
|
23
|
-
return this.#conn.query(sql);
|
|
24
|
-
}
|
|
25
|
-
execute(sql) {
|
|
26
|
-
if (!this.#conn)
|
|
27
|
-
return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
|
|
28
|
-
if (typeof sql === "function")
|
|
29
|
-
sql = sql();
|
|
30
|
-
return this.#conn.execute(sql);
|
|
31
|
-
}
|
|
32
|
-
/** @deprecated 不建议使用 */
|
|
33
|
-
multipleQuery(sql) {
|
|
34
|
-
if (!this.#conn)
|
|
35
|
-
return Promise.reject(new ConnectionNotAvailableError("Connection already release"));
|
|
36
|
-
return this.#conn.multipleQuery(sql);
|
|
37
|
-
}
|
|
38
|
-
//implement
|
|
39
|
-
async rollback() {
|
|
40
|
-
await this.execute("ROLLBACK");
|
|
41
|
-
}
|
|
42
|
-
//implement
|
|
43
|
-
async commit() {
|
|
44
|
-
await this.execute("COMMIT");
|
|
45
|
-
}
|
|
46
|
-
get released() {
|
|
47
|
-
return !this.#conn;
|
|
48
|
-
}
|
|
49
|
-
/** 调用 release() 时,如果事务未提交,则抛出异常 */
|
|
50
|
-
release() {
|
|
51
|
-
const conn = this.#conn;
|
|
52
|
-
if (conn) {
|
|
53
|
-
this.#conn = undefined;
|
|
54
|
-
this.#onRelease(conn);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
dispose() {
|
|
58
|
-
const conn = this.#conn;
|
|
59
|
-
if (conn) {
|
|
60
|
-
this.#conn = undefined;
|
|
61
|
-
this.#onDispose(conn);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
//implement
|
|
65
|
-
[Symbol.dispose]() {
|
|
66
|
-
return this.release();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
/** @public */
|
|
70
|
-
function createDbPoolConnection(conn, onRelease, onDispose = onRelease) {
|
|
71
|
-
return new DbPoolConnectionImpl(conn, onRelease, onDispose);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export { createDbPoolConnection };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { DbPoolConnection, DbPoolTransaction, TransactionMode } from "./interfaces.ts";
|
|
2
|
-
/** @public */
|
|
3
|
-
export type DbPoolTransactionOption = {
|
|
4
|
-
errorRollback?: boolean;
|
|
5
|
-
mode?: TransactionMode;
|
|
6
|
-
};
|
|
7
|
-
/** @public */
|
|
8
|
-
export declare function createDbPoolTransaction(connect: () => Promise<DbPoolConnection>, option?: TransactionMode | DbPoolTransactionOption): DbPoolTransaction;
|
|
9
|
-
//# sourceMappingURL=DbPoolTransaction.d.ts.map
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { DbQuery } from './DbQuery.js';
|
|
2
|
-
import { ParallelQueryError, ConnectionNotAvailableError } from './errors.js';
|
|
3
|
-
|
|
4
|
-
class DbPoolTransactionImpl extends DbQuery {
|
|
5
|
-
#errorRollback;
|
|
6
|
-
mode;
|
|
7
|
-
#begin;
|
|
8
|
-
constructor(connect, option) {
|
|
9
|
-
super();
|
|
10
|
-
if (option) {
|
|
11
|
-
if (typeof option === "string")
|
|
12
|
-
this.mode = option;
|
|
13
|
-
else {
|
|
14
|
-
this.mode = option.mode;
|
|
15
|
-
this.#errorRollback = option.errorRollback;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
this.#begin = "BEGIN" + (this.mode ? " TRANSACTION ISOLATION LEVEL " + this.mode : "");
|
|
19
|
-
this.#connect = connect;
|
|
20
|
-
}
|
|
21
|
-
#connect;
|
|
22
|
-
#conn;
|
|
23
|
-
async commit() {
|
|
24
|
-
if (this.#conn) {
|
|
25
|
-
const conn = this.#conn;
|
|
26
|
-
const promise = conn.execute("COMMIT").then(() => conn.release(), (e) => conn.dispose());
|
|
27
|
-
return promise;
|
|
28
|
-
}
|
|
29
|
-
this.#release(undefined);
|
|
30
|
-
}
|
|
31
|
-
async rollback() {
|
|
32
|
-
if (this.#conn) {
|
|
33
|
-
const conn = this.#conn;
|
|
34
|
-
const promise = conn.execute("ROLLBACK").then(() => conn.release(), (e) => conn.dispose());
|
|
35
|
-
return promise;
|
|
36
|
-
}
|
|
37
|
-
this.#release(undefined);
|
|
38
|
-
}
|
|
39
|
-
savePoint(savePoint) {
|
|
40
|
-
return this.execute("SAVEPOINT " + savePoint);
|
|
41
|
-
}
|
|
42
|
-
rollbackTo(savePoint) {
|
|
43
|
-
return this.execute("ROLLBACK TO " + savePoint);
|
|
44
|
-
}
|
|
45
|
-
#pending;
|
|
46
|
-
#getConnQuery(call, callIfFirst = call, queryErrorCatch) {
|
|
47
|
-
if (this.#pending) {
|
|
48
|
-
return Promise.reject(new ParallelQueryError());
|
|
49
|
-
}
|
|
50
|
-
if (this.#error) {
|
|
51
|
-
return Promise.reject(this.#error);
|
|
52
|
-
}
|
|
53
|
-
let promise;
|
|
54
|
-
if (!this.#conn) {
|
|
55
|
-
promise = this.#connect().then((conn) => {
|
|
56
|
-
if (this.released) {
|
|
57
|
-
conn.release();
|
|
58
|
-
throw this.#error;
|
|
59
|
-
}
|
|
60
|
-
this.#conn = conn;
|
|
61
|
-
let promise = callIfFirst(conn);
|
|
62
|
-
if (queryErrorCatch) {
|
|
63
|
-
promise = promise.catch(queryErrorCatch);
|
|
64
|
-
}
|
|
65
|
-
return promise;
|
|
66
|
-
}, (e) => {
|
|
67
|
-
this.#release(undefined);
|
|
68
|
-
throw e;
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
promise = call(this.#conn);
|
|
73
|
-
if (queryErrorCatch) {
|
|
74
|
-
promise = promise.catch(queryErrorCatch);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
this.#pending = promise;
|
|
78
|
-
return promise.finally(() => {
|
|
79
|
-
this.#pending = undefined;
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
#query(call, callIfFirst) {
|
|
83
|
-
const onError = (e) => {
|
|
84
|
-
if (this.#errorRollback) {
|
|
85
|
-
const passError = () => {
|
|
86
|
-
throw e;
|
|
87
|
-
};
|
|
88
|
-
return this.rollback().then(passError, passError);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
this.#release(this.#conn);
|
|
92
|
-
throw e;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
return this.#getConnQuery(call, callIfFirst, onError);
|
|
96
|
-
}
|
|
97
|
-
query(sql) {
|
|
98
|
-
return this.#query((conn) => {
|
|
99
|
-
return conn.query(sql);
|
|
100
|
-
}, async (conn) => {
|
|
101
|
-
if (typeof sql === "function")
|
|
102
|
-
sql = sql();
|
|
103
|
-
const isArray = sql instanceof Array;
|
|
104
|
-
const result = await conn.query(isArray ? [this.#begin, ...sql] : [this.#begin, sql]);
|
|
105
|
-
if (isArray)
|
|
106
|
-
return result.slice(1);
|
|
107
|
-
else
|
|
108
|
-
return result[1];
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
async execute(sql) {
|
|
112
|
-
return this.#query((conn) => {
|
|
113
|
-
return conn.execute(sql);
|
|
114
|
-
}, (conn) => {
|
|
115
|
-
if (typeof sql === "function")
|
|
116
|
-
sql = sql();
|
|
117
|
-
return conn.execute(sql instanceof Array ? [this.#begin, ...sql] : [this.#begin, sql]);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
/** @deprecated 不建议使用 */
|
|
121
|
-
multipleQuery(sql) {
|
|
122
|
-
return this.#query((conn) => {
|
|
123
|
-
return conn.multipleQuery(sql);
|
|
124
|
-
}, (conn) => {
|
|
125
|
-
return conn.multipleQuery(sql instanceof Array ? [this.#begin, ...sql] : [this.#begin, sql]);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
#error;
|
|
129
|
-
#release(conn, error = new ConnectionNotAvailableError("Connection already release")) {
|
|
130
|
-
this.#error = error;
|
|
131
|
-
this.#conn = undefined;
|
|
132
|
-
conn?.release();
|
|
133
|
-
}
|
|
134
|
-
get released() {
|
|
135
|
-
return !!this.#error;
|
|
136
|
-
}
|
|
137
|
-
[Symbol.asyncDispose]() {
|
|
138
|
-
return this.rollback();
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/** @public */
|
|
142
|
-
function createDbPoolTransaction(connect, option) {
|
|
143
|
-
return new DbPoolTransactionImpl(connect, option);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export { createDbPoolTransaction };
|
package/dist/client/DbQuery.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { SqlLike } from "./interfaces.ts";
|
|
2
|
-
import { MultipleQueryResult, QueryRowsResult } from "./DbQueryBase.ts";
|
|
3
|
-
import { SqlStatementDataset } from "./_type.ts";
|
|
4
|
-
/** @public */
|
|
5
|
-
export type QueryInput = SqlLike | (() => SqlLike);
|
|
6
|
-
/** @public */
|
|
7
|
-
export type QueryDataInput<T> = SqlStatementDataset<T> | (() => SqlStatementDataset<T>);
|
|
8
|
-
/** @public */
|
|
9
|
-
export type MultipleQueryInput = SqlLike[] | (() => SqlLike[]);
|
|
10
|
-
/**
|
|
11
|
-
* SQL 查询相关操作
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export declare abstract class DbQuery {
|
|
15
|
-
abstract execute(sql: QueryInput | MultipleQueryInput): Promise<void>;
|
|
16
|
-
abstract query<T extends MultipleQueryResult = MultipleQueryResult>(sql: MultipleQueryInput): Promise<T>;
|
|
17
|
-
abstract query<T = any>(sql: QueryDataInput<T>): Promise<QueryRowsResult<T>>;
|
|
18
|
-
abstract query<T = any>(sql: QueryInput): Promise<QueryRowsResult<T>>;
|
|
19
|
-
/**
|
|
20
|
-
* 执行多语句的方法
|
|
21
|
-
* @deprecated 不建议使用。改用 query()
|
|
22
|
-
*/
|
|
23
|
-
abstract multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
24
|
-
/** 单语句查询受影响的行 */
|
|
25
|
-
queryCount(sql: QueryInput): Promise<number>;
|
|
26
|
-
/** 单语句查询,不应查询多语句,否则返回错误值 */
|
|
27
|
-
queryRows<T = any>(sql: QueryDataInput<T>): Promise<T[]>;
|
|
28
|
-
/** 单语句查询,不应查询多语句,否则返回错误值 */
|
|
29
|
-
queryRows<T = any>(sql: QueryInput): Promise<T[]>;
|
|
30
|
-
/** 单语句查询,只返回第一行。如果查询没有返回行,则抛出异常。 */
|
|
31
|
-
queryFirstRow<T = any>(sql: QueryDataInput<T>): Promise<T>;
|
|
32
|
-
queryFirstRow<T = any>(sql: QueryInput): Promise<T>;
|
|
33
|
-
/**
|
|
34
|
-
* 查询行
|
|
35
|
-
* 不应查询单语句,否则返回错误值
|
|
36
|
-
* @deprecated 不建议使用。
|
|
37
|
-
*/
|
|
38
|
-
multipleQueryRows<T extends any[] = any[]>(sql: SqlLike | SqlLike[]): Promise<T[]>;
|
|
39
|
-
/**
|
|
40
|
-
* 指定某一列为key,返回 key 到 row 的映射
|
|
41
|
-
* 单语句查询,不应查询多语句,否则返回错误值
|
|
42
|
-
*/
|
|
43
|
-
queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: QueryDataInput<T>, key: K): Promise<Map<T[K], T>>;
|
|
44
|
-
/**
|
|
45
|
-
* 指定某一列为key,返回 key 到 row 的映射
|
|
46
|
-
* 单语句查询,不应查询多语句,否则返回错误值
|
|
47
|
-
*/
|
|
48
|
-
queryMap<T extends Record<string, any> = Record<string, any>, K extends keyof T = string>(sql: QueryInput, key: K): Promise<Map<T[K], T>>;
|
|
49
|
-
}
|
|
50
|
-
//# sourceMappingURL=DbQuery.d.ts.map
|
package/dist/client/DbQuery.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQL 查询相关操作
|
|
3
|
-
* @public
|
|
4
|
-
*/
|
|
5
|
-
class DbQuery {
|
|
6
|
-
/** 单语句查询受影响的行 */
|
|
7
|
-
queryCount(sql) {
|
|
8
|
-
return this.query(sql).then((res) => {
|
|
9
|
-
if (res.rowCount === null)
|
|
10
|
-
return 0;
|
|
11
|
-
return res.rowCount;
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
queryRows(sql) {
|
|
15
|
-
return this.query(sql).then((res) => res.rows);
|
|
16
|
-
}
|
|
17
|
-
queryFirstRow(sql) {
|
|
18
|
-
return this.query(sql).then(({ rows, rowCount }) => {
|
|
19
|
-
if (rows.length === 0)
|
|
20
|
-
throw new Error("Query did not return any rows");
|
|
21
|
-
return rows[0];
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* 查询行
|
|
26
|
-
* 不应查询单语句,否则返回错误值
|
|
27
|
-
* @deprecated 不建议使用。
|
|
28
|
-
*/
|
|
29
|
-
multipleQueryRows(sql) {
|
|
30
|
-
return this.multipleQuery(sql).then((res) => res.map((item) => item.rows ?? []));
|
|
31
|
-
}
|
|
32
|
-
async queryMap(sql, key) {
|
|
33
|
-
const { rows } = await this.query(sql);
|
|
34
|
-
let map = new Map();
|
|
35
|
-
for (let i = 0; i < rows.length; i++) {
|
|
36
|
-
map.set(rows[i][key], rows[i]);
|
|
37
|
-
}
|
|
38
|
-
return map;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export { DbQuery };
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { SqlTemplate } from "./_type.ts";
|
|
2
|
-
import { SqlLike } from "./interfaces.ts";
|
|
3
|
-
/** @public */
|
|
4
|
-
export interface SingleQueryResult {
|
|
5
|
-
rowCount: number;
|
|
6
|
-
rows?: any[];
|
|
7
|
-
}
|
|
8
|
-
/** @public */
|
|
9
|
-
export interface QueryRowsResult<T = any> extends SingleQueryResult {
|
|
10
|
-
rowCount: number;
|
|
11
|
-
rows: T[];
|
|
12
|
-
}
|
|
13
|
-
/** @public */
|
|
14
|
-
export type MultipleQueryResult = SingleQueryResult[];
|
|
15
|
-
/**
|
|
16
|
-
* 数据库客户端的最小实现接口
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
export interface DbQueryBase {
|
|
20
|
-
/** 单语句查询, 忽略返回值 */
|
|
21
|
-
execute(sql: SqlLike | SqlLike[]): Promise<void>;
|
|
22
|
-
/** 单语句查询。单个 SqlLike 不应包含多语句,否则返回错误值 */
|
|
23
|
-
query<T = any>(sql: SqlLike): Promise<QueryRowsResult<T>>;
|
|
24
|
-
/** 多语句查询 */
|
|
25
|
-
query<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike[]): Promise<T>;
|
|
26
|
-
query(sql: SqlLike[] | SqlLike): Promise<unknown[] | unknown>;
|
|
27
|
-
/** 多语句查询 */
|
|
28
|
-
multipleQuery<T extends MultipleQueryResult = MultipleQueryResult>(sql: SqlLike | SqlLike[]): Promise<T>;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 将 SqlLike 转换为字符串
|
|
32
|
-
* @public
|
|
33
|
-
*
|
|
34
|
-
*/
|
|
35
|
-
export declare function sqlLikeToString(sqlLike: SqlLike): string;
|
|
36
|
-
/** @public */
|
|
37
|
-
export declare function isSqlTemplate(obj: any): obj is SqlTemplate;
|
|
38
|
-
//# sourceMappingURL=DbQueryBase.d.ts.map
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 将 SqlLike 转换为字符串
|
|
3
|
-
* @public
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
function sqlLikeToString(sqlLike) {
|
|
7
|
-
if (typeof sqlLike === "string") {
|
|
8
|
-
return sqlLike;
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
if (isSqlTemplate(sqlLike)) {
|
|
12
|
-
const { templates } = sqlLike;
|
|
13
|
-
const textArgs = sqlLike.toTextArgs();
|
|
14
|
-
let sql = templates[0];
|
|
15
|
-
for (let i = 1; i < templates.length; i++) {
|
|
16
|
-
sql += textArgs[i - 1] + templates[i];
|
|
17
|
-
}
|
|
18
|
-
return sql;
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
return sqlLike.genSql();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
/** @public */
|
|
26
|
-
function isSqlTemplate(obj) {
|
|
27
|
-
if (typeof obj !== "object" || obj === null)
|
|
28
|
-
return false;
|
|
29
|
-
return Array.isArray(obj.templates) && Array.isArray(obj.args) && typeof obj.toTextArgs === "function";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export { isSqlTemplate, sqlLikeToString };
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { DbCursor, DbCursorOption } from "./DbCursor.ts";
|
|
2
|
-
import { DbQuery } from "./DbQuery.ts";
|
|
3
|
-
import { DbPool, DbPoolConnection, DbTransaction, SqlLike, TransactionMode } from "./interfaces.ts";
|
|
4
|
-
import { QueryRowsResult } from "./DbQueryBase.ts";
|
|
5
|
-
import { SqlStatementDataset, InferQueryResult } from "./_type.ts";
|
|
6
|
-
/** @public */
|
|
7
|
-
export interface ExecutableSQL<T = unknown> {
|
|
8
|
-
genSql(): string;
|
|
9
|
-
then(resolve: (data: T) => void, reject: () => void): void;
|
|
10
|
-
}
|
|
11
|
-
/** @public */
|
|
12
|
-
export interface QueryableDataSQL<Raw, Res = QueryRowsResult<Raw>> extends ExecutableSQL<Res> {
|
|
13
|
-
query(): Promise<QueryRowsResult<Raw>>;
|
|
14
|
-
queryCount(): Promise<number>;
|
|
15
|
-
queryRows(): Promise<Raw[]>;
|
|
16
|
-
queryFirstRow(): Promise<Raw>;
|
|
17
|
-
queryMap<K>(key: string): Promise<Map<K, Raw>>;
|
|
18
|
-
cursor(): Promise<DbCursor<Raw>>;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* @public
|
|
22
|
-
* 池链接查询
|
|
23
|
-
*/
|
|
24
|
-
export declare abstract class DbQueryPool extends DbQuery implements DbPool {
|
|
25
|
-
abstract connect(): Promise<DbPoolConnection>;
|
|
26
|
-
/** 连接池空闲链接数量 */
|
|
27
|
-
abstract idleCount: number;
|
|
28
|
-
/** 连接池总链接数量 */
|
|
29
|
-
abstract totalCount: number;
|
|
30
|
-
abstract begin(mode?: TransactionMode): DbTransaction;
|
|
31
|
-
abstract cursor<T extends {}>(sql: SqlStatementDataset<T>): Promise<DbCursor<T>>;
|
|
32
|
-
abstract cursor<T>(sql: SqlLike, option?: DbCursorOption): Promise<DbCursor<T>>;
|
|
33
|
-
createQueryableSQL<Raw>(statement: SqlStatementDataset<Raw>): QueryableDataSQL<Raw, void>;
|
|
34
|
-
createQueryableSQL<Raw>(statement: SqlLike): QueryableDataSQL<Raw, void>;
|
|
35
|
-
createQueryableSQL<T extends SqlStatementDataset<any>, Res>(statement: T, transform: (queryable: DbQueryPool, statement: T) => Promise<Res>): QueryableDataSQL<InferQueryResult<T>, Awaited<Res>>;
|
|
36
|
-
createQueryableSQL<Raw, Res>(statement: SqlLike, transform: (queryable: DbQueryPool, statement: SqlLike) => Promise<Res>): QueryableDataSQL<Raw, Res>;
|
|
37
|
-
createExecutableSQL(statement: SqlLike): ExecutableSQL<void>;
|
|
38
|
-
}
|
|
39
|
-
//# sourceMappingURL=DbQueryPool.d.ts.map
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { DbQuery } from './DbQuery.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @public
|
|
5
|
-
* 池链接查询
|
|
6
|
-
*/
|
|
7
|
-
class DbQueryPool extends DbQuery {
|
|
8
|
-
createQueryableSQL(statement, transform) {
|
|
9
|
-
return new QueryableSqlImpl(this, statement, transform);
|
|
10
|
-
}
|
|
11
|
-
createExecutableSQL(statement) {
|
|
12
|
-
return new QueryableSqlImpl(this, statement);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
class QueryableSqlImpl {
|
|
16
|
-
queryClient;
|
|
17
|
-
statement;
|
|
18
|
-
transform;
|
|
19
|
-
constructor(queryClient, statement, transform = defaultTransform) {
|
|
20
|
-
this.queryClient = queryClient;
|
|
21
|
-
this.statement = statement;
|
|
22
|
-
this.transform = transform;
|
|
23
|
-
}
|
|
24
|
-
genSql() {
|
|
25
|
-
return this.statement.toString();
|
|
26
|
-
}
|
|
27
|
-
toString() {
|
|
28
|
-
return this.genSql();
|
|
29
|
-
}
|
|
30
|
-
query() {
|
|
31
|
-
return this.queryClient.query(this.statement);
|
|
32
|
-
}
|
|
33
|
-
queryCount() {
|
|
34
|
-
return this.queryClient.queryCount(this.statement);
|
|
35
|
-
}
|
|
36
|
-
queryRows() {
|
|
37
|
-
return this.queryClient.queryRows(this.statement);
|
|
38
|
-
}
|
|
39
|
-
queryFirstRow() {
|
|
40
|
-
return this.queryClient.queryFirstRow(this.statement);
|
|
41
|
-
}
|
|
42
|
-
queryMap(key) {
|
|
43
|
-
return this.queryClient.queryMap(this.statement, key);
|
|
44
|
-
}
|
|
45
|
-
cursor() {
|
|
46
|
-
return this.queryClient.cursor(this.statement);
|
|
47
|
-
}
|
|
48
|
-
then(resolve, reject) {
|
|
49
|
-
this.transform(this.queryClient, this.statement).then(resolve, reject);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function defaultTransform(queryClient, statement) {
|
|
53
|
-
return queryClient.execute(statement);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export { DbQueryPool };
|
package/dist/client/_type.d.ts
DELETED
package/dist/client/errors.d.ts
DELETED
package/dist/client/errors.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/** @public */
|
|
2
|
-
class ParallelQueryError extends Error {
|
|
3
|
-
constructor() {
|
|
4
|
-
super("The previous query was not completed and cannot be executed");
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
/** @public */
|
|
8
|
-
class ConnectionNotAvailableError extends Error {
|
|
9
|
-
constructor(message) {
|
|
10
|
-
super(message);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export { ConnectionNotAvailableError, ParallelQueryError };
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { DbQuery } from "./DbQuery.ts";
|
|
2
|
-
import { DbCursor, DbCursorOption } from "./DbCursor.ts";
|
|
3
|
-
import { SqlStatementDataset, SqlTemplate } from "./_type.ts";
|
|
4
|
-
/**
|
|
5
|
-
* 数据库连接
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export interface DbConnection extends DbQuery, AsyncDisposable {
|
|
9
|
-
close(): Promise<void>;
|
|
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
|
-
}
|
|
23
|
-
/** @public */
|
|
24
|
-
export type TransactionMode = "SERIALIZABLE" | "REPEATABLE READ" | "READ COMMITTED" | "READ UNCOMMITTED";
|
|
25
|
-
/**
|
|
26
|
-
* SQL 事务查询操作
|
|
27
|
-
*
|
|
28
|
-
* 使用 `await using` 语法离开作用域时,如果没有 `commit()` 或 `rollback(`) , 则调用 `rollback()`
|
|
29
|
-
*
|
|
30
|
-
* ```ts
|
|
31
|
-
* async function doSomeTransaction(){
|
|
32
|
-
* await using transaction = pool.begin()
|
|
33
|
-
* await transaction.query("SELECT * FROM user")
|
|
34
|
-
* throw new Error("error")
|
|
35
|
-
* }
|
|
36
|
-
* try{
|
|
37
|
-
* await doSomeTransaction()
|
|
38
|
-
* }catch(e){
|
|
39
|
-
* console.error(e)
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
* 下面的写法会造成连接池泄露
|
|
43
|
-
* ```ts
|
|
44
|
-
* async function doSomeTransaction(){
|
|
45
|
-
* const transaction = pool.begin()
|
|
46
|
-
* await transaction.query("SELECT * FROM user")
|
|
47
|
-
* }
|
|
48
|
-
* await doSomeTransaction() // 离开作用域后连接不会被回收
|
|
49
|
-
* console.warn("连接未被回收!")
|
|
50
|
-
*
|
|
51
|
-
* ```
|
|
52
|
-
* @public
|
|
53
|
-
*/
|
|
54
|
-
export interface DbTransaction extends DbQuery, AsyncDisposable {
|
|
55
|
-
/** 回滚,并释放连接 */
|
|
56
|
-
rollback(): Promise<void>;
|
|
57
|
-
/** 回滚到保存点 */
|
|
58
|
-
rollbackTo(savePoint: string): Promise<void>;
|
|
59
|
-
savePoint(savePoint: string): Promise<void>;
|
|
60
|
-
/** 提交,并释放连接 */
|
|
61
|
-
commit(): Promise<void>;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* @public
|
|
65
|
-
* 池连接事务
|
|
66
|
-
*/
|
|
67
|
-
export interface DbPoolTransaction extends DbTransaction {
|
|
68
|
-
readonly mode?: TransactionMode;
|
|
69
|
-
get released(): boolean;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* 数据库连接池
|
|
73
|
-
* @public
|
|
74
|
-
*/
|
|
75
|
-
export interface DbPool {
|
|
76
|
-
connect(): Promise<DbPoolConnection>;
|
|
77
|
-
idleCount: number;
|
|
78
|
-
totalCount: number;
|
|
79
|
-
begin(mode?: TransactionMode): DbTransaction;
|
|
80
|
-
cursor<T extends {}>(sql: SqlStatementDataset<T>): Promise<DbCursor<T>>;
|
|
81
|
-
cursor<T>(sql: SqlLike, option?: DbCursorOption): Promise<DbCursor<T>>;
|
|
82
|
-
}
|
|
83
|
-
/** @public */
|
|
84
|
-
export type SqlLike = {
|
|
85
|
-
genSql(): string;
|
|
86
|
-
} | SqlTemplate | string;
|
|
87
|
-
//# sourceMappingURL=interfaces.d.ts.map
|
package/dist/client/mod.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export * from "./errors.ts";
|
|
2
|
-
export * from "./interfaces.ts";
|
|
3
|
-
export * from "./DbQueryBase.ts";
|
|
4
|
-
export * from "./DbQuery.ts";
|
|
5
|
-
export * from "./DbCursor.ts";
|
|
6
|
-
export * from "./DbPoolConnection.ts";
|
|
7
|
-
export * from "./DbPoolTransaction.ts";
|
|
8
|
-
export * from "./DbQueryPool.ts";
|
|
9
|
-
//# sourceMappingURL=mod.d.ts.map
|
package/dist/client/mod.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { ConnectionNotAvailableError, ParallelQueryError } from './errors.js';
|
|
2
|
-
export { isSqlTemplate, sqlLikeToString } from './DbQueryBase.js';
|
|
3
|
-
export { DbQuery } from './DbQuery.js';
|
|
4
|
-
export { DbCursor } from './DbCursor.js';
|
|
5
|
-
export { createDbPoolConnection } from './DbPoolConnection.js';
|
|
6
|
-
export { createDbPoolTransaction } from './DbPoolTransaction.js';
|
|
7
|
-
export { DbQueryPool } from './DbQueryPool.js';
|