baja-lite 1.8.8 → 1.8.9
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/boot-remote.d.ts +1 -1
- package/boot-remote.js +6 -1
- package/boot.d.ts +1 -1
- package/boot.js +6 -1
- package/cache.d.ts +91 -0
- package/cache.js +515 -0
- package/const/index.d.ts +2 -0
- package/const/index.js +2 -0
- package/const/symbols.d.ts +73 -0
- package/const/symbols.js +78 -0
- package/const/types.d.ts +504 -0
- package/const/types.js +127 -0
- package/db/dao/format-dialects.d.ts +6 -0
- package/db/dao/format-dialects.js +8 -0
- package/db/dao/mysql.d.ts +44 -0
- package/db/dao/mysql.js +303 -0
- package/db/dao/postgresql.d.ts +44 -0
- package/db/dao/postgresql.js +322 -0
- package/db/dao/sqlite-remote.d.ts +46 -0
- package/db/dao/sqlite-remote.js +226 -0
- package/db/dao/sqlite.d.ts +41 -0
- package/db/dao/sqlite.js +237 -0
- package/db/index.d.ts +8 -0
- package/db/index.js +8 -0
- package/db/service.d.ts +778 -0
- package/db/service.js +1651 -0
- package/db/sql-template.d.ts +46 -0
- package/db/sql-template.js +660 -0
- package/db/stream-query.d.ts +607 -0
- package/db/stream-query.js +1626 -0
- package/index.d.ts +4 -1
- package/index.js +4 -1
- package/logger.d.ts +46 -0
- package/logger.js +35 -0
- package/package.json +1 -1
- package/sqlite.d.ts +1 -1
- package/sqlite.js +2 -1
- package/{test-mysql.js → test/test-mysql.js} +3 -2
- package/{test-postgresql.js → test/test-postgresql.js} +3 -2
- package/{test-sqlite.js → test/test-sqlite.js} +3 -2
- package/sql.d.ts +0 -2222
- package/sql.js +0 -5503
- /package/{test-mysql.d.ts → test/test-mysql.d.ts} +0 -0
- /package/{test-postgresql.d.ts → test/test-postgresql.d.ts} +0 -0
- /package/{test-sqlite.d.ts → test/test-sqlite.d.ts} +0 -0
- /package/{test-xml.d.ts → test/test-xml.d.ts} +0 -0
- /package/{test-xml.js → test/test-xml.js} +0 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { _daoConnection, _daoDB, _GlobalSqlOption, _inTransaction, _LoggerService, _MysqlKeepAliveTime, DEFAULT_KEEPALIVE_INTERVAL } from '../../const/symbols.js';
|
|
3
|
+
import { SyncMode } from '../../const/types.js';
|
|
4
|
+
import { replacePlaceholders } from '../../string.js';
|
|
5
|
+
export class PostgresqlConnection {
|
|
6
|
+
constructor(conn) {
|
|
7
|
+
this[_a] = false;
|
|
8
|
+
this[_daoConnection] = conn;
|
|
9
|
+
}
|
|
10
|
+
execute(sync, sql, params) {
|
|
11
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
12
|
+
if (!sql) {
|
|
13
|
+
return { affectedRows: 0, insertId: 0n };
|
|
14
|
+
}
|
|
15
|
+
;
|
|
16
|
+
if (sync === SyncMode.Sync) {
|
|
17
|
+
globalThis[_LoggerService].warn('Postgresql not supported sync mode');
|
|
18
|
+
return { affectedRows: 0, insertId: 0n };
|
|
19
|
+
}
|
|
20
|
+
;
|
|
21
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
22
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
23
|
+
}
|
|
24
|
+
return (async () => {
|
|
25
|
+
try {
|
|
26
|
+
const { rowCount } = await this[_daoConnection].query({
|
|
27
|
+
text: replacePlaceholders(sql),
|
|
28
|
+
values: params
|
|
29
|
+
});
|
|
30
|
+
const result = rowCount;
|
|
31
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
32
|
+
globalThis[_LoggerService].verbose?.(result);
|
|
33
|
+
}
|
|
34
|
+
return { affectedRows: rowCount || 0, insertId: 0n };
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
globalThis[_LoggerService].error(`
|
|
38
|
+
error: ${error},
|
|
39
|
+
sql: ${sql},
|
|
40
|
+
params: ${params}
|
|
41
|
+
`);
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
})();
|
|
45
|
+
}
|
|
46
|
+
pluck(sync, sql, params) {
|
|
47
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
48
|
+
if (!sql) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
;
|
|
52
|
+
if (sync === SyncMode.Sync) {
|
|
53
|
+
globalThis[_LoggerService].warn('Postgresql not supported sync mode');
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
;
|
|
57
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
58
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
59
|
+
}
|
|
60
|
+
return (async () => {
|
|
61
|
+
try {
|
|
62
|
+
const { rows } = await this[_daoConnection].query({
|
|
63
|
+
text: replacePlaceholders(sql),
|
|
64
|
+
values: params
|
|
65
|
+
});
|
|
66
|
+
// 修复 fall-through:原代码 if 分支 resolve 后没 return,导致 resolve(null)
|
|
67
|
+
// 紧跟其后(虽然第二次 resolve 是 no-op,但写法有歧义)。
|
|
68
|
+
if (rows && rows[0]) {
|
|
69
|
+
const r = Object.values(rows[0])[0];
|
|
70
|
+
return r === null ? null : r;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
globalThis[_LoggerService].error(`
|
|
76
|
+
error: ${error},
|
|
77
|
+
sql: ${sql},
|
|
78
|
+
params: ${params}
|
|
79
|
+
`);
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
})();
|
|
83
|
+
}
|
|
84
|
+
get(sync, sql, params) {
|
|
85
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
86
|
+
if (!sql) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
;
|
|
90
|
+
if (sync === SyncMode.Sync) {
|
|
91
|
+
globalThis[_LoggerService].warn('Postgresql not supported sync mode');
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
;
|
|
95
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
96
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
97
|
+
}
|
|
98
|
+
return (async () => {
|
|
99
|
+
try {
|
|
100
|
+
const { rows } = await this[_daoConnection].query({
|
|
101
|
+
text: replacePlaceholders(sql),
|
|
102
|
+
values: params
|
|
103
|
+
});
|
|
104
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
105
|
+
globalThis[_LoggerService].verbose?.(rows);
|
|
106
|
+
}
|
|
107
|
+
if (rows && rows[0]) {
|
|
108
|
+
return rows[0];
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
globalThis[_LoggerService].error(`
|
|
114
|
+
error: ${error},
|
|
115
|
+
sql: ${sql},
|
|
116
|
+
params: ${params}
|
|
117
|
+
`);
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
})();
|
|
121
|
+
}
|
|
122
|
+
raw(sync, sql, params) {
|
|
123
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
124
|
+
if (!sql) {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
;
|
|
128
|
+
if (sync === SyncMode.Sync) {
|
|
129
|
+
globalThis[_LoggerService].warn('Postgresql not supported sync mode');
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
;
|
|
133
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
134
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
135
|
+
}
|
|
136
|
+
return (async () => {
|
|
137
|
+
try {
|
|
138
|
+
const { rows } = await this[_daoConnection].query({
|
|
139
|
+
text: replacePlaceholders(sql),
|
|
140
|
+
values: params
|
|
141
|
+
});
|
|
142
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
143
|
+
globalThis[_LoggerService].verbose?.(rows);
|
|
144
|
+
}
|
|
145
|
+
if (rows) {
|
|
146
|
+
return rows.map((i) => Object.values(i)[0]);
|
|
147
|
+
}
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
globalThis[_LoggerService].error(`
|
|
152
|
+
error: ${error},
|
|
153
|
+
sql: ${sql},
|
|
154
|
+
params: ${params}
|
|
155
|
+
`);
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
})();
|
|
159
|
+
}
|
|
160
|
+
query(sync, sql, params) {
|
|
161
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
162
|
+
if (!sql) {
|
|
163
|
+
return [];
|
|
164
|
+
}
|
|
165
|
+
;
|
|
166
|
+
if (sync === SyncMode.Sync) {
|
|
167
|
+
globalThis[_LoggerService].warn('Postgresql not supported sync mode');
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
;
|
|
171
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
172
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
173
|
+
}
|
|
174
|
+
return (async () => {
|
|
175
|
+
try {
|
|
176
|
+
const { rows } = await this[_daoConnection].query({
|
|
177
|
+
text: replacePlaceholders(sql),
|
|
178
|
+
values: params
|
|
179
|
+
});
|
|
180
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
181
|
+
globalThis[_LoggerService].verbose?.(rows);
|
|
182
|
+
}
|
|
183
|
+
return rows;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
globalThis[_LoggerService].error(`
|
|
187
|
+
error: ${error},
|
|
188
|
+
sql: ${sql},
|
|
189
|
+
params: ${params}
|
|
190
|
+
`);
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
})();
|
|
194
|
+
}
|
|
195
|
+
release(sync) {
|
|
196
|
+
try {
|
|
197
|
+
this[_daoConnection]?.release();
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
}
|
|
201
|
+
if (sync === SyncMode.Async) {
|
|
202
|
+
return Promise.resolve();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
_a = _inTransaction;
|
|
207
|
+
export class Postgresql {
|
|
208
|
+
constructor(pool) {
|
|
209
|
+
this.isClosing = false;
|
|
210
|
+
this[_daoDB] = pool;
|
|
211
|
+
this.keepAlive();
|
|
212
|
+
}
|
|
213
|
+
async keepAlive() {
|
|
214
|
+
if (this.isClosing)
|
|
215
|
+
return;
|
|
216
|
+
let connection = null;
|
|
217
|
+
try {
|
|
218
|
+
connection = await this.createConnection(SyncMode.Async);
|
|
219
|
+
if (connection) {
|
|
220
|
+
await connection.query(SyncMode.Async, 'SELECT 1 FROM DUAL');
|
|
221
|
+
// (globalThis[_LoggerService]! as LoggerService).debug?.('keepAlive->', data?.[0]?.[1]);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
globalThis[_LoggerService].error('keepAlive error', error);
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
if (connection) {
|
|
229
|
+
await connection.release(SyncMode.Async);
|
|
230
|
+
}
|
|
231
|
+
if (!this.isClosing) {
|
|
232
|
+
this.keepAliveTimer = setTimeout(() => this.keepAlive(), globalThis[_MysqlKeepAliveTime] ?? DEFAULT_KEEPALIVE_INTERVAL);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
createConnection(sync) {
|
|
237
|
+
if (sync === SyncMode.Sync) {
|
|
238
|
+
globalThis[_LoggerService].error('Postgresql not supported sync mode');
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
;
|
|
242
|
+
return (async () => {
|
|
243
|
+
try {
|
|
244
|
+
const connection = await this[_daoDB].connect();
|
|
245
|
+
globalThis[_LoggerService].debug?.('create new connection!');
|
|
246
|
+
return new PostgresqlConnection(connection);
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
})();
|
|
252
|
+
}
|
|
253
|
+
transaction(sync, fn, conn) {
|
|
254
|
+
if (sync === SyncMode.Sync) {
|
|
255
|
+
globalThis[_LoggerService].warn('Postgresql not supported sync mode');
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
;
|
|
259
|
+
return (async () => {
|
|
260
|
+
let needCommit = false;
|
|
261
|
+
let newConn = false;
|
|
262
|
+
if (!conn) {
|
|
263
|
+
conn = await this.createConnection(SyncMode.Async) ?? undefined;
|
|
264
|
+
newConn = true;
|
|
265
|
+
}
|
|
266
|
+
if (conn?.[_inTransaction] !== true) {
|
|
267
|
+
needCommit = true;
|
|
268
|
+
globalThis[_LoggerService].debug?.('beginTransaction begin!');
|
|
269
|
+
await conn[_daoConnection].query('BEGIN');
|
|
270
|
+
globalThis[_LoggerService].debug?.('beginTransaction end!');
|
|
271
|
+
}
|
|
272
|
+
conn[_inTransaction] = true;
|
|
273
|
+
try {
|
|
274
|
+
const result = await fn(conn);
|
|
275
|
+
if (needCommit) {
|
|
276
|
+
globalThis[_LoggerService].debug?.('commit begin!');
|
|
277
|
+
await conn[_daoConnection].query('COMMIT');
|
|
278
|
+
globalThis[_LoggerService].debug?.('commit end!');
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
if (needCommit) {
|
|
284
|
+
globalThis[_LoggerService].debug?.('rollback begin!');
|
|
285
|
+
await conn[_daoConnection].query('ROLLBACK');
|
|
286
|
+
globalThis[_LoggerService].debug?.('rollback end!');
|
|
287
|
+
}
|
|
288
|
+
globalThis[_LoggerService].error(error.message, { cause: error });
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
try {
|
|
293
|
+
if (needCommit) {
|
|
294
|
+
conn[_inTransaction] = false;
|
|
295
|
+
}
|
|
296
|
+
if (newConn) {
|
|
297
|
+
globalThis[_LoggerService].debug?.('release begin!');
|
|
298
|
+
conn[_daoConnection].release();
|
|
299
|
+
globalThis[_LoggerService].debug?.('release end!');
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
// 释放连接失败通常不影响业务逻辑,记录日志即可
|
|
304
|
+
globalThis[_LoggerService].warn?.('Failed to release connection in finally block', error);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
})();
|
|
308
|
+
}
|
|
309
|
+
close(sync) {
|
|
310
|
+
this.isClosing = true;
|
|
311
|
+
if (this.keepAliveTimer) {
|
|
312
|
+
clearTimeout(this.keepAliveTimer);
|
|
313
|
+
}
|
|
314
|
+
this[_daoDB]?.end();
|
|
315
|
+
}
|
|
316
|
+
backup(sync, name) {
|
|
317
|
+
}
|
|
318
|
+
remove(sync) {
|
|
319
|
+
}
|
|
320
|
+
restore(sync, name) {
|
|
321
|
+
}
|
|
322
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ExtensionCodec } from "@msgpack/msgpack";
|
|
2
|
+
import { _daoConnection, _daoDB, _inTransaction, _sqliteRemoteName } from '../../const/symbols.js';
|
|
3
|
+
import { Connection, Dao, SqliteRemoteInterface, SyncMode } from '../../const/types.js';
|
|
4
|
+
export declare const extensionCodec: ExtensionCodec<undefined>;
|
|
5
|
+
export declare class SqliteRemoteConnection implements Connection {
|
|
6
|
+
[_daoConnection]: SqliteRemoteInterface;
|
|
7
|
+
[_sqliteRemoteName]: string;
|
|
8
|
+
[_inTransaction]: boolean;
|
|
9
|
+
constructor(conn: SqliteRemoteInterface, name: string);
|
|
10
|
+
execute(sync: SyncMode.Sync, sql?: string, params?: any): {
|
|
11
|
+
affectedRows: number;
|
|
12
|
+
insertId: bigint;
|
|
13
|
+
};
|
|
14
|
+
execute(sync: SyncMode.Async, sql?: string, params?: any): Promise<{
|
|
15
|
+
affectedRows: number;
|
|
16
|
+
insertId: bigint;
|
|
17
|
+
}>;
|
|
18
|
+
pluck<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T | null;
|
|
19
|
+
pluck<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T | null>;
|
|
20
|
+
get<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T | null;
|
|
21
|
+
get<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T | null>;
|
|
22
|
+
raw<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T[];
|
|
23
|
+
raw<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T[]>;
|
|
24
|
+
query<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T[];
|
|
25
|
+
query<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T[]>;
|
|
26
|
+
release(sync: SyncMode.Sync): void;
|
|
27
|
+
release(sync: SyncMode.Async): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export declare class SqliteRemote implements Dao {
|
|
30
|
+
[_sqliteRemoteName]: string;
|
|
31
|
+
[_daoDB]: SqliteRemoteInterface;
|
|
32
|
+
private connection?;
|
|
33
|
+
constructor(db: SqliteRemoteInterface, name: string);
|
|
34
|
+
createConnection(sync: SyncMode.Sync): Connection | null;
|
|
35
|
+
createConnection(sync: SyncMode.Async): Promise<Connection | null>;
|
|
36
|
+
transaction<T = any>(sync: SyncMode.Sync, fn: (conn: Connection) => T, conn?: Connection | null): T | null;
|
|
37
|
+
transaction<T = any>(sync: SyncMode.Async, fn: (conn: Connection) => Promise<T>, conn?: Connection | null): Promise<T | null>;
|
|
38
|
+
close(sync: SyncMode.Sync): void;
|
|
39
|
+
close(sync: SyncMode.Async): Promise<void>;
|
|
40
|
+
backup(sync: SyncMode.Sync, exportPath: string): void;
|
|
41
|
+
backup(sync: SyncMode.Async, exportPath: string): Promise<void>;
|
|
42
|
+
remove(sync: SyncMode.Sync): void;
|
|
43
|
+
remove(sync: SyncMode.Async): Promise<void>;
|
|
44
|
+
restore(sync: SyncMode.Sync, importPath: string): void;
|
|
45
|
+
restore(sync: SyncMode.Async, importPath: string): Promise<void>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { decode, DecodeError, encode, ExtensionCodec } from "@msgpack/msgpack";
|
|
3
|
+
import { _daoConnection, _daoDB, _GlobalSqlOption, _inTransaction, _LoggerService, _sqliteRemoteName } from '../../const/symbols.js';
|
|
4
|
+
import { SyncMode } from '../../const/types.js';
|
|
5
|
+
const BIGINT_EXT_TYPE = 0;
|
|
6
|
+
export const extensionCodec = new ExtensionCodec();
|
|
7
|
+
extensionCodec.register({
|
|
8
|
+
type: BIGINT_EXT_TYPE,
|
|
9
|
+
encode(input) {
|
|
10
|
+
if (typeof input === "bigint") {
|
|
11
|
+
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
|
|
12
|
+
return encode(Number(input));
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return encode(String(input));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
decode(data) {
|
|
23
|
+
const val = decode(data);
|
|
24
|
+
if (!(typeof val === "string" || typeof val === "number")) {
|
|
25
|
+
throw new DecodeError(`unexpected BigInt source: ${val} (${typeof val})`);
|
|
26
|
+
}
|
|
27
|
+
return BigInt(val);
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
export class SqliteRemoteConnection {
|
|
31
|
+
constructor(conn, name) {
|
|
32
|
+
this[_a] = false;
|
|
33
|
+
this[_daoConnection] = conn;
|
|
34
|
+
this[_sqliteRemoteName] = name;
|
|
35
|
+
}
|
|
36
|
+
execute(sync, sql, params) {
|
|
37
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
38
|
+
if (!sql) {
|
|
39
|
+
return { affectedRows: 0, insertId: 0n };
|
|
40
|
+
}
|
|
41
|
+
;
|
|
42
|
+
if (sync === SyncMode.Sync) {
|
|
43
|
+
globalThis[_LoggerService].warn('SqliteRemote not supported sync mode');
|
|
44
|
+
return { affectedRows: 0, insertId: 0n };
|
|
45
|
+
}
|
|
46
|
+
;
|
|
47
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
48
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
49
|
+
}
|
|
50
|
+
return (async () => {
|
|
51
|
+
try {
|
|
52
|
+
const data = await this[_daoConnection].execute(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
53
|
+
const { affectedRows, insertId } = decode(data, { extensionCodec });
|
|
54
|
+
return { affectedRows, insertId: insertId ? BigInt(insertId) : 0n };
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
globalThis[_LoggerService].error(`
|
|
58
|
+
error: ${error},
|
|
59
|
+
sql: ${sql},
|
|
60
|
+
params: ${params}
|
|
61
|
+
`);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
})();
|
|
65
|
+
}
|
|
66
|
+
pluck(sync, sql, params) {
|
|
67
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
68
|
+
if (!sql) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
;
|
|
72
|
+
if (sync === SyncMode.Sync) {
|
|
73
|
+
globalThis[_LoggerService].warn('SqliteRemote not supported sync mode');
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
;
|
|
77
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
78
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
79
|
+
}
|
|
80
|
+
return (async () => {
|
|
81
|
+
try {
|
|
82
|
+
const data = await this[_daoConnection].pluck(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
83
|
+
return decode(data, { extensionCodec });
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
globalThis[_LoggerService].error(`
|
|
87
|
+
error: ${error},
|
|
88
|
+
sql: ${sql},
|
|
89
|
+
params: ${params}
|
|
90
|
+
`);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
})();
|
|
94
|
+
}
|
|
95
|
+
get(sync, sql, params) {
|
|
96
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
97
|
+
if (!sql) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
;
|
|
101
|
+
if (sync === SyncMode.Sync) {
|
|
102
|
+
globalThis[_LoggerService].warn('SqliteRemote not supported sync mode');
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
;
|
|
106
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
107
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
108
|
+
}
|
|
109
|
+
return (async () => {
|
|
110
|
+
try {
|
|
111
|
+
const data = await this[_daoConnection].get(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
112
|
+
return decode(data, { extensionCodec });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
globalThis[_LoggerService].error(`
|
|
116
|
+
error: ${error},
|
|
117
|
+
sql: ${sql},
|
|
118
|
+
params: ${params}
|
|
119
|
+
`);
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
})();
|
|
123
|
+
}
|
|
124
|
+
raw(sync, sql, params) {
|
|
125
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
126
|
+
if (!sql) {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
;
|
|
130
|
+
if (sync === SyncMode.Sync) {
|
|
131
|
+
globalThis[_LoggerService].warn('SqliteRemote not supported sync mode');
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
;
|
|
135
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
136
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
137
|
+
}
|
|
138
|
+
return (async () => {
|
|
139
|
+
try {
|
|
140
|
+
const data = await this[_daoConnection].raw(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
141
|
+
return decode(data, { extensionCodec });
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
globalThis[_LoggerService].error(`
|
|
145
|
+
error: ${error},
|
|
146
|
+
sql: ${sql},
|
|
147
|
+
params: ${params}
|
|
148
|
+
`);
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
})();
|
|
152
|
+
}
|
|
153
|
+
query(sync, sql, params) {
|
|
154
|
+
globalThis[_LoggerService].debug?.(sql, params ?? '');
|
|
155
|
+
if (!sql) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
;
|
|
159
|
+
if (sync === SyncMode.Sync) {
|
|
160
|
+
globalThis[_LoggerService].warn('SqliteRemote not supported sync mode');
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
;
|
|
164
|
+
if (globalThis[_GlobalSqlOption].log === 'trace') {
|
|
165
|
+
globalThis[_LoggerService].verbose?.(`${sql}\n,${JSON.stringify(params ?? '')}`);
|
|
166
|
+
}
|
|
167
|
+
return (async () => {
|
|
168
|
+
try {
|
|
169
|
+
const data = await this[_daoConnection].query(encode([this[_sqliteRemoteName], sql, params], { extensionCodec }));
|
|
170
|
+
return decode(data, { extensionCodec });
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
globalThis[_LoggerService].error(`
|
|
174
|
+
error: ${error},
|
|
175
|
+
sql: ${sql},
|
|
176
|
+
params: ${params}
|
|
177
|
+
`);
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
})();
|
|
181
|
+
}
|
|
182
|
+
release(sync) {
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
_a = _inTransaction;
|
|
186
|
+
export class SqliteRemote {
|
|
187
|
+
constructor(db, name) {
|
|
188
|
+
this[_daoDB] = db;
|
|
189
|
+
this[_sqliteRemoteName] = name;
|
|
190
|
+
}
|
|
191
|
+
createConnection(sync) {
|
|
192
|
+
if (sync === SyncMode.Sync) {
|
|
193
|
+
globalThis[_LoggerService].error('SQLITEREMOTE not supported sync mode');
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
;
|
|
197
|
+
if (!this.connection) {
|
|
198
|
+
this.connection = new SqliteRemoteConnection(this[_daoDB], this[_sqliteRemoteName]);
|
|
199
|
+
}
|
|
200
|
+
return Promise.resolve(this.connection);
|
|
201
|
+
}
|
|
202
|
+
transaction(sync, fn, conn) {
|
|
203
|
+
globalThis[_LoggerService].warn(`SQLITEREMOTE not supported transaction`);
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
close(sync) {
|
|
207
|
+
if (sync === SyncMode.Async) {
|
|
208
|
+
return this[_daoDB]?.close(this[_sqliteRemoteName]);
|
|
209
|
+
}
|
|
210
|
+
;
|
|
211
|
+
}
|
|
212
|
+
backup(sync, exportPath) {
|
|
213
|
+
if (sync === SyncMode.Async) {
|
|
214
|
+
return this[_daoDB]?.export(this[_sqliteRemoteName], exportPath);
|
|
215
|
+
}
|
|
216
|
+
;
|
|
217
|
+
}
|
|
218
|
+
remove(sync) {
|
|
219
|
+
}
|
|
220
|
+
restore(sync, importPath) {
|
|
221
|
+
if (sync === SyncMode.Async) {
|
|
222
|
+
return this[_daoDB]?.restore(this[_sqliteRemoteName], importPath);
|
|
223
|
+
}
|
|
224
|
+
;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { _daoConnection, _daoDB, _inTransaction } from '../../const/symbols.js';
|
|
2
|
+
import { Connection, Dao, SyncMode } from '../../const/types.js';
|
|
3
|
+
export declare class SqliteConnection implements Connection {
|
|
4
|
+
[_daoConnection]: any;
|
|
5
|
+
[_inTransaction]: boolean;
|
|
6
|
+
constructor(conn: any);
|
|
7
|
+
execute(sync: SyncMode.Sync, sql?: string, params?: any): {
|
|
8
|
+
affectedRows: number;
|
|
9
|
+
insertId: bigint;
|
|
10
|
+
};
|
|
11
|
+
execute(sync: SyncMode.Async, sql?: string, params?: any): Promise<{
|
|
12
|
+
affectedRows: number;
|
|
13
|
+
insertId: bigint;
|
|
14
|
+
}>;
|
|
15
|
+
pluck<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T | null;
|
|
16
|
+
pluck<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T | null>;
|
|
17
|
+
get<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T | null;
|
|
18
|
+
get<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T | null>;
|
|
19
|
+
raw<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T[];
|
|
20
|
+
raw<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T[]>;
|
|
21
|
+
query<T = any>(sync: SyncMode.Sync, sql?: string, params?: any): T[];
|
|
22
|
+
query<T = any>(sync: SyncMode.Async, sql?: string, params?: any): Promise<T[]>;
|
|
23
|
+
release(sync: SyncMode.Sync): void;
|
|
24
|
+
release(sync: SyncMode.Async): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export declare class Sqlite implements Dao {
|
|
27
|
+
[_daoDB]: any;
|
|
28
|
+
constructor(db: any);
|
|
29
|
+
createConnection(sync: SyncMode.Sync): Connection | null;
|
|
30
|
+
createConnection(sync: SyncMode.Async): Promise<Connection | null>;
|
|
31
|
+
transaction<T = any>(sync: SyncMode.Sync, fn: (conn: Connection) => T, conn?: Connection | null): T | null;
|
|
32
|
+
transaction<T = any>(sync: SyncMode.Async, fn: (conn: Connection) => Promise<T>, conn?: Connection | null): Promise<T | null>;
|
|
33
|
+
close(sync: SyncMode.Sync): void;
|
|
34
|
+
close(sync: SyncMode.Async): Promise<void>;
|
|
35
|
+
backup(sync: SyncMode.Sync, name: string): void;
|
|
36
|
+
backup(sync: SyncMode.Async, name: string): Promise<void>;
|
|
37
|
+
remove(sync: SyncMode.Sync): void;
|
|
38
|
+
remove(sync: SyncMode.Async): Promise<void>;
|
|
39
|
+
restore(sync: SyncMode.Sync, name: string): void;
|
|
40
|
+
restore(sync: SyncMode.Async, name: string): Promise<void>;
|
|
41
|
+
}
|