@asla/yoursql 0.8.4 → 0.8.6
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
CHANGED
|
@@ -77,3 +77,116 @@ const s = Selection.from("user", "u")
|
|
|
77
77
|
.where(`u.name LIKE %${v(searchName)}%`)
|
|
78
78
|
.toString();
|
|
79
79
|
```
|
|
80
|
+
|
|
81
|
+
### client 抽象类
|
|
82
|
+
|
|
83
|
+
yoursql 还导出了一些抽象类,实现抽象类后可以方便的进行数据查询
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import {
|
|
87
|
+
type DbQueryPool,
|
|
88
|
+
type DbTransaction,
|
|
89
|
+
type DbConnection,
|
|
90
|
+
DbQuery,
|
|
91
|
+
DbCursor,
|
|
92
|
+
DbPoolConnection,
|
|
93
|
+
DbPoolTransaction,
|
|
94
|
+
} from "@asla/yoursql/client";
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### DbQuery 抽象类
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
class YourQuery extends DbQuery {
|
|
101
|
+
// implement
|
|
102
|
+
}
|
|
103
|
+
const db: DbQuery = new YourQuery();
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
declare const db: DbQuery;
|
|
108
|
+
|
|
109
|
+
type Row = { name: string; age: number };
|
|
110
|
+
const sqlText = "SELECT * FROM user";
|
|
111
|
+
|
|
112
|
+
const rows: Row[] = await db.queryRows<Row>(sqlText);
|
|
113
|
+
const count: number = await db.queryCount(sqlText);
|
|
114
|
+
const rows: Map<string, Row> = await db.queryMap<Row>(sqlText, "name");
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### DbQueryPool 接口
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
class YourPool extends DbQuery implements DbQuery {
|
|
121
|
+
// implement
|
|
122
|
+
}
|
|
123
|
+
const pool: DbQueryPool = new YourPool();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
##### 普通查询
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const conn = await pool.connect();
|
|
130
|
+
try {
|
|
131
|
+
await conn.queryRows(sqlText);
|
|
132
|
+
} finally {
|
|
133
|
+
conn.release();
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
或者,使用 `using` 语法更优雅
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
using conn = await pool.connect();
|
|
141
|
+
await conn.queryRows(sqlText);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
##### 事务查询
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
const conn = pool.begin();
|
|
148
|
+
try {
|
|
149
|
+
await conn.queryRows(sqlText);
|
|
150
|
+
await conn.queryRows(sqlText);
|
|
151
|
+
await conn.commit();
|
|
152
|
+
} catch (e) {
|
|
153
|
+
await conn.rollback();
|
|
154
|
+
throw e;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
或者,使用 `using` 语法更优雅
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
await using conn = pool.begin();
|
|
162
|
+
|
|
163
|
+
await conn.queryRows(sqlText);
|
|
164
|
+
await conn.queryRows(sqlText);
|
|
165
|
+
await conn.commit();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
##### 游标查询
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const cursor = await pool.cursor(sqlText);
|
|
172
|
+
|
|
173
|
+
let rows = await cursor.read(20);
|
|
174
|
+
while (rows.length) {
|
|
175
|
+
console.log(rows);
|
|
176
|
+
rows = await cursor.read(20);
|
|
177
|
+
if (conditions) {
|
|
178
|
+
await cursor.close(); // 提前关闭游标
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
或者使用 `for await of` 更优雅
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
const cursor = await pool.cursor(sqlText);
|
|
188
|
+
for await (const element of cursor) {
|
|
189
|
+
console.log(element);
|
|
190
|
+
if (conditions) break; //提前关闭游标
|
|
191
|
+
}
|
|
192
|
+
```
|
|
@@ -3,14 +3,19 @@ import { DbQuery } from "./DbQuery.ts";
|
|
|
3
3
|
import type { MultipleQueryResult, QueryRowsResult } from "./DbQuery.ts";
|
|
4
4
|
import type { DbPoolConnection } from "./DbPoolConnection.ts";
|
|
5
5
|
import type { DbTransaction, TransactionMode } from "./interfaces.ts";
|
|
6
|
+
/** @public */
|
|
7
|
+
export type DbPoolTransactionOption = {
|
|
8
|
+
errorRollback?: boolean;
|
|
9
|
+
mode?: TransactionMode;
|
|
10
|
+
};
|
|
6
11
|
/**
|
|
7
12
|
* @public
|
|
8
13
|
* 池连接事务
|
|
9
14
|
*/
|
|
10
15
|
export declare class DbPoolTransaction extends DbQuery implements DbTransaction {
|
|
11
16
|
#private;
|
|
12
|
-
readonly mode?: TransactionMode
|
|
13
|
-
constructor(connect: () => Promise<DbPoolConnection>,
|
|
17
|
+
readonly mode?: TransactionMode;
|
|
18
|
+
constructor(connect: () => Promise<DbPoolConnection>, option?: TransactionMode | DbPoolTransactionOption);
|
|
14
19
|
commit(): Promise<void>;
|
|
15
20
|
rollback(): Promise<void>;
|
|
16
21
|
savePoint(savePoint: string): Promise<void>;
|
|
@@ -6,10 +6,18 @@ import { ParallelQueryError, ConnectionNotAvailableError } from './errors.js';
|
|
|
6
6
|
* 池连接事务
|
|
7
7
|
*/
|
|
8
8
|
class DbPoolTransaction extends DbQuery {
|
|
9
|
+
#errorRollback;
|
|
9
10
|
mode;
|
|
10
|
-
constructor(connect,
|
|
11
|
+
constructor(connect, option) {
|
|
11
12
|
super();
|
|
12
|
-
|
|
13
|
+
if (option) {
|
|
14
|
+
if (typeof option === "string")
|
|
15
|
+
this.mode = option;
|
|
16
|
+
else {
|
|
17
|
+
this.mode = option.mode;
|
|
18
|
+
this.#errorRollback = option.errorRollback;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
13
21
|
this.#query = (sql) => {
|
|
14
22
|
return new Promise((resolve, reject) => {
|
|
15
23
|
this.#pending = connect()
|
|
@@ -24,10 +32,21 @@ class DbPoolTransaction extends DbQuery {
|
|
|
24
32
|
this.#pending = undefined;
|
|
25
33
|
resolve(res[1]);
|
|
26
34
|
}, (e) => {
|
|
35
|
+
// 语法错误、查询错误、网络错误
|
|
27
36
|
this.#pending = undefined;
|
|
28
37
|
reject(e);
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
const conn = this.#conn;
|
|
39
|
+
if (!conn)
|
|
40
|
+
return;
|
|
41
|
+
if (this.#errorRollback) {
|
|
42
|
+
const onFinally = () => {
|
|
43
|
+
this.#release(conn, e);
|
|
44
|
+
};
|
|
45
|
+
return conn.rollback().then(onFinally, onFinally);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.#release(conn, e);
|
|
49
|
+
}
|
|
31
50
|
});
|
|
32
51
|
});
|
|
33
52
|
};
|
|
@@ -60,25 +79,35 @@ class DbPoolTransaction extends DbQuery {
|
|
|
60
79
|
}
|
|
61
80
|
/** 拿到连接后执行这个 */
|
|
62
81
|
#queryAfter(sql) {
|
|
63
|
-
|
|
82
|
+
const conn = this.#conn;
|
|
83
|
+
return conn.query(sql).then((res) => {
|
|
64
84
|
this.#pending = undefined;
|
|
65
85
|
return res;
|
|
66
86
|
}, (e) => {
|
|
67
87
|
this.#pending = undefined;
|
|
68
|
-
|
|
69
|
-
|
|
88
|
+
if (this.#errorRollback) {
|
|
89
|
+
const onOk = () => {
|
|
90
|
+
this.#release(conn, e);
|
|
91
|
+
throw e;
|
|
92
|
+
};
|
|
93
|
+
return conn.rollback().then(onOk, onOk);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.#release(conn, e);
|
|
97
|
+
throw e;
|
|
98
|
+
}
|
|
70
99
|
});
|
|
71
100
|
}
|
|
72
101
|
#query;
|
|
73
102
|
query(sql) {
|
|
74
103
|
if (this.#pending)
|
|
75
104
|
return Promise.reject(new ParallelQueryError());
|
|
76
|
-
return this.#query(sql
|
|
105
|
+
return this.#query(sql);
|
|
77
106
|
}
|
|
78
107
|
multipleQuery(sql) {
|
|
79
108
|
if (this.#pending)
|
|
80
109
|
return Promise.reject(new ParallelQueryError());
|
|
81
|
-
return this.#query(sql
|
|
110
|
+
return this.#query(sql);
|
|
82
111
|
}
|
|
83
112
|
#error;
|
|
84
113
|
#release(conn, error = new ConnectionNotAvailableError("Connection already release")) {
|
|
@@ -13,12 +13,16 @@ export declare class DbTable<T extends TableType> {
|
|
|
13
13
|
fromAs(as?: string): Selection;
|
|
14
14
|
/** 选择单表全部列 */
|
|
15
15
|
select(columns: "*", as?: string): ChainSelect<T>;
|
|
16
|
-
/**
|
|
17
|
-
select(columns: Constructable<
|
|
16
|
+
/** 选择单表,带提示 */
|
|
17
|
+
select(columns: Constructable<{
|
|
18
|
+
[key in keyof T]?: string | boolean;
|
|
19
|
+
} & {
|
|
20
|
+
[key: string]: string | boolean;
|
|
21
|
+
}>, as?: string): ChainSelect<Record<string, any>>;
|
|
18
22
|
/** 选择单表 */
|
|
19
23
|
select<R extends {}>(columns: Constructable<{
|
|
20
24
|
[key in keyof R]: boolean | string;
|
|
21
|
-
} | string>, as?: string): ChainSelect<R>;
|
|
25
|
+
} | string | string[]>, as?: string): ChainSelect<R>;
|
|
22
26
|
/**
|
|
23
27
|
* INSERT 语句,需要注意 SQL 注入
|
|
24
28
|
* @example
|