@event-driven-io/dumbo 0.13.0-beta.3 → 0.13.0-beta.31
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/chunk-4SM4JJJZ.js +83 -0
- package/dist/chunk-4SM4JJJZ.js.map +1 -0
- package/dist/chunk-EEKBLOCH.cjs +706 -0
- package/dist/chunk-EEKBLOCH.cjs.map +1 -0
- package/dist/{chunk-OJ34O3Q2.cjs → chunk-GVR3PSDL.cjs} +2474 -1636
- package/dist/chunk-GVR3PSDL.cjs.map +1 -0
- package/dist/chunk-HZM7GDOT.cjs +34 -0
- package/dist/chunk-HZM7GDOT.cjs.map +1 -0
- package/dist/{chunk-N7RWT46K.js → chunk-JKE6SULM.js} +2522 -1684
- package/dist/chunk-JKE6SULM.js.map +1 -0
- package/dist/chunk-MOPKHYYU.cjs +83 -0
- package/dist/chunk-MOPKHYYU.cjs.map +1 -0
- package/dist/chunk-RA2L3FQW.js +481 -0
- package/dist/chunk-RA2L3FQW.js.map +1 -0
- package/dist/chunk-UA542GT3.cjs +481 -0
- package/dist/chunk-UA542GT3.cjs.map +1 -0
- package/dist/chunk-WLWELSA2.js +34 -0
- package/dist/chunk-WLWELSA2.js.map +1 -0
- package/dist/chunk-XFEGS3YT.js +706 -0
- package/dist/chunk-XFEGS3YT.js.map +1 -0
- package/dist/cloudflare.cjs +456 -0
- package/dist/cloudflare.cjs.map +1 -0
- package/dist/cloudflare.d.cts +92 -0
- package/dist/cloudflare.d.ts +92 -0
- package/dist/cloudflare.js +456 -0
- package/dist/cloudflare.js.map +1 -0
- package/dist/{columnProcessors-DMPpTPqM.d.ts → columnProcessors-Be2uMYjS.d.cts} +1 -1
- package/dist/{columnProcessors-BX-sH7ah.d.cts → columnProcessors-D0ivj_SU.d.ts} +1 -1
- package/dist/{connectionString-B1wm0TFc.d.cts → connectionString-Dch_MIRU.d.cts} +394 -168
- package/dist/{connectionString-B1wm0TFc.d.ts → connectionString-Dch_MIRU.d.ts} +394 -168
- package/dist/index.cjs +100 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -18
- package/dist/index.d.ts +23 -18
- package/dist/index.js +105 -9
- package/dist/index.js.map +1 -1
- package/dist/pg.cjs +383 -9
- package/dist/pg.cjs.map +1 -1
- package/dist/pg.d.cts +81 -105
- package/dist/pg.d.ts +81 -105
- package/dist/pg.js +435 -61
- package/dist/pg.js.map +1 -1
- package/dist/postgresql.cjs +50 -0
- package/dist/postgresql.cjs.map +1 -0
- package/dist/postgresql.d.cts +61 -0
- package/dist/postgresql.d.ts +61 -0
- package/dist/postgresql.js +50 -0
- package/dist/sqlite.cjs +60 -0
- package/dist/sqlite.cjs.map +1 -0
- package/dist/{index-C2z_XBn6.d.ts → sqlite.d.cts} +69 -20
- package/dist/{index-C0h0c380.d.cts → sqlite.d.ts} +69 -20
- package/dist/sqlite.js +60 -0
- package/dist/sqlite3.cjs +319 -6
- package/dist/sqlite3.cjs.map +1 -1
- package/dist/sqlite3.d.cts +45 -25
- package/dist/sqlite3.d.ts +45 -25
- package/dist/sqlite3.js +335 -22
- package/dist/sqlite3.js.map +1 -1
- package/package.json +55 -33
- package/dist/chunk-A7TC7IOP.cjs +0 -55
- package/dist/chunk-A7TC7IOP.cjs.map +0 -1
- package/dist/chunk-F7JQ7BX7.js +0 -240
- package/dist/chunk-F7JQ7BX7.js.map +0 -1
- package/dist/chunk-I34X53VL.js +0 -688
- package/dist/chunk-I34X53VL.js.map +0 -1
- package/dist/chunk-ISNF6USX.cjs +0 -688
- package/dist/chunk-ISNF6USX.cjs.map +0 -1
- package/dist/chunk-IYEHOE4S.cjs +0 -405
- package/dist/chunk-IYEHOE4S.cjs.map +0 -1
- package/dist/chunk-L2YZQAG3.cjs +0 -240
- package/dist/chunk-L2YZQAG3.cjs.map +0 -1
- package/dist/chunk-N7RWT46K.js.map +0 -1
- package/dist/chunk-OJ34O3Q2.cjs.map +0 -1
- package/dist/chunk-TXSETOGH.js +0 -55
- package/dist/chunk-TXSETOGH.js.map +0 -1
- package/dist/chunk-XVV3OOQX.js +0 -405
- package/dist/chunk-XVV3OOQX.js.map +0 -1
- package/dist/d1.cjs +0 -277
- package/dist/d1.cjs.map +0 -1
- package/dist/d1.d.cts +0 -72
- package/dist/d1.d.ts +0 -72
- package/dist/d1.js +0 -277
- package/dist/d1.js.map +0 -1
- package/dist/pg-3ACXFMU4.cjs +0 -59
- package/dist/pg-3ACXFMU4.cjs.map +0 -1
- package/dist/pg-GHOW3XSG.js +0 -59
- package/dist/sqlite3-EEIKQCJR.js +0 -25
- package/dist/sqlite3-SE4DDYZE.cjs +0 -25
- package/dist/sqlite3-SE4DDYZE.cjs.map +0 -1
- /package/dist/{pg-GHOW3XSG.js.map → postgresql.js.map} +0 -0
- /package/dist/{sqlite3-EEIKQCJR.js.map → sqlite.js.map} +0 -0
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CheckViolationError,
|
|
3
|
+
ConnectionError,
|
|
4
|
+
DataError,
|
|
5
|
+
DeadlockError,
|
|
6
|
+
DumboError,
|
|
7
|
+
ForeignKeyViolationError,
|
|
8
|
+
InsufficientResourcesError,
|
|
9
|
+
IntegrityConstraintViolationError,
|
|
10
|
+
InvalidOperationError,
|
|
11
|
+
LockNotAvailableError,
|
|
12
|
+
NotNullViolationError,
|
|
13
|
+
SQL,
|
|
14
|
+
SQLFormatter,
|
|
15
|
+
SQLProcessorsRegistry,
|
|
16
|
+
SerializationError,
|
|
17
|
+
SystemError,
|
|
18
|
+
TaskProcessor,
|
|
19
|
+
UniqueConstraintError,
|
|
20
|
+
createAlwaysNewConnectionPool,
|
|
21
|
+
createAmbientConnection,
|
|
22
|
+
createAmbientConnectionPool,
|
|
23
|
+
createBoundedConnectionPool,
|
|
24
|
+
createConnection,
|
|
25
|
+
createSingletonConnectionPool,
|
|
26
|
+
defaultProcessorsRegistry,
|
|
27
|
+
mapDefaultSQLColumnProcessors,
|
|
28
|
+
mapSQLQueryResult,
|
|
29
|
+
registerDefaultMigratorOptions,
|
|
30
|
+
registerFormatter,
|
|
31
|
+
sqlExecutor,
|
|
32
|
+
tracer
|
|
33
|
+
} from "./chunk-JKE6SULM.js";
|
|
34
|
+
|
|
35
|
+
// src/storage/sqlite/core/errors/errorMapper.ts
|
|
36
|
+
var getSqliteErrorCode = (error) => {
|
|
37
|
+
if (error instanceof Error && "code" in error && typeof error.code === "string") {
|
|
38
|
+
return error.code;
|
|
39
|
+
}
|
|
40
|
+
return void 0;
|
|
41
|
+
};
|
|
42
|
+
var getErrorMessage = (error) => error instanceof Error ? error.message : void 0;
|
|
43
|
+
var asError = (error) => error instanceof Error ? error : void 0;
|
|
44
|
+
var mapConstraintError = (message, innerError) => {
|
|
45
|
+
const upperMessage = message?.toUpperCase() ?? "";
|
|
46
|
+
if (upperMessage.includes("UNIQUE") || upperMessage.includes("PRIMARY KEY"))
|
|
47
|
+
return new UniqueConstraintError(message, innerError);
|
|
48
|
+
if (upperMessage.includes("FOREIGN KEY"))
|
|
49
|
+
return new ForeignKeyViolationError(message, innerError);
|
|
50
|
+
if (upperMessage.includes("NOT NULL"))
|
|
51
|
+
return new NotNullViolationError(message, innerError);
|
|
52
|
+
if (upperMessage.includes("CHECK"))
|
|
53
|
+
return new CheckViolationError(message, innerError);
|
|
54
|
+
return new IntegrityConstraintViolationError(message, innerError);
|
|
55
|
+
};
|
|
56
|
+
var mapSqliteError = (error) => {
|
|
57
|
+
if (DumboError.isInstanceOf(error)) return error;
|
|
58
|
+
const code = getSqliteErrorCode(error);
|
|
59
|
+
if (!code)
|
|
60
|
+
return new DumboError({
|
|
61
|
+
errorCode: 500,
|
|
62
|
+
message: getErrorMessage(error),
|
|
63
|
+
innerError: asError(error)
|
|
64
|
+
});
|
|
65
|
+
const message = getErrorMessage(error);
|
|
66
|
+
const innerError = asError(error);
|
|
67
|
+
switch (code) {
|
|
68
|
+
// ── Constraint violations (19) ──
|
|
69
|
+
// node-sqlite3 only exposes the primary code; subtype is in the message.
|
|
70
|
+
case "SQLITE_CONSTRAINT":
|
|
71
|
+
return mapConstraintError(message, innerError);
|
|
72
|
+
// ── Busy / lock contention ──
|
|
73
|
+
// SQLITE_BUSY (5): conflict with a separate database connection
|
|
74
|
+
case "SQLITE_BUSY":
|
|
75
|
+
return new LockNotAvailableError(message, innerError);
|
|
76
|
+
// SQLITE_LOCKED (6): conflict within the same connection or shared cache
|
|
77
|
+
case "SQLITE_LOCKED":
|
|
78
|
+
return new DeadlockError(message, innerError);
|
|
79
|
+
// SQLITE_PROTOCOL (15): WAL locking race condition
|
|
80
|
+
case "SQLITE_PROTOCOL":
|
|
81
|
+
return new LockNotAvailableError(message, innerError);
|
|
82
|
+
// ── Connection / open errors ──
|
|
83
|
+
// SQLITE_CANTOPEN (14): unable to open database file
|
|
84
|
+
case "SQLITE_CANTOPEN":
|
|
85
|
+
return new ConnectionError(message, innerError);
|
|
86
|
+
// SQLITE_NOTADB (26): file is not a database
|
|
87
|
+
case "SQLITE_NOTADB":
|
|
88
|
+
return new ConnectionError(message, innerError);
|
|
89
|
+
// ── Resource exhaustion ──
|
|
90
|
+
// SQLITE_NOMEM (7): out of memory
|
|
91
|
+
case "SQLITE_NOMEM":
|
|
92
|
+
return new InsufficientResourcesError(message, innerError);
|
|
93
|
+
// SQLITE_FULL (13): disk full
|
|
94
|
+
case "SQLITE_FULL":
|
|
95
|
+
return new InsufficientResourcesError(message, innerError);
|
|
96
|
+
// ── System / I/O errors ──
|
|
97
|
+
// SQLITE_IOERR (10): operating system I/O error
|
|
98
|
+
case "SQLITE_IOERR":
|
|
99
|
+
return new SystemError(message, innerError);
|
|
100
|
+
// SQLITE_CORRUPT (11): database file is corrupted
|
|
101
|
+
case "SQLITE_CORRUPT":
|
|
102
|
+
return new SystemError(message, innerError);
|
|
103
|
+
// SQLITE_INTERNAL (2): internal SQLite malfunction
|
|
104
|
+
case "SQLITE_INTERNAL":
|
|
105
|
+
return new SystemError(message, innerError);
|
|
106
|
+
// SQLITE_NOLFS (22): large file support unavailable
|
|
107
|
+
case "SQLITE_NOLFS":
|
|
108
|
+
return new SystemError(message, innerError);
|
|
109
|
+
// ── Data errors ──
|
|
110
|
+
// SQLITE_TOOBIG (18): string or BLOB too large
|
|
111
|
+
case "SQLITE_TOOBIG":
|
|
112
|
+
return new DataError(message, innerError);
|
|
113
|
+
// SQLITE_MISMATCH (20): datatype mismatch
|
|
114
|
+
case "SQLITE_MISMATCH":
|
|
115
|
+
return new DataError(message, innerError);
|
|
116
|
+
// SQLITE_RANGE (25): bind parameter index out of range
|
|
117
|
+
case "SQLITE_RANGE":
|
|
118
|
+
return new DataError(message, innerError);
|
|
119
|
+
// ── Invalid operations ──
|
|
120
|
+
// SQLITE_ERROR (1): generic SQL error (syntax errors, missing tables, etc.)
|
|
121
|
+
case "SQLITE_ERROR":
|
|
122
|
+
return new InvalidOperationError(message, innerError);
|
|
123
|
+
// SQLITE_READONLY (8): attempt to write to a read-only database
|
|
124
|
+
case "SQLITE_READONLY":
|
|
125
|
+
return new InvalidOperationError(message, innerError);
|
|
126
|
+
// SQLITE_MISUSE (21): API misuse
|
|
127
|
+
case "SQLITE_MISUSE":
|
|
128
|
+
return new InvalidOperationError(message, innerError);
|
|
129
|
+
// SQLITE_AUTH (23): authorization denied
|
|
130
|
+
case "SQLITE_AUTH":
|
|
131
|
+
return new InvalidOperationError(message, innerError);
|
|
132
|
+
// SQLITE_PERM (3): access permission denied
|
|
133
|
+
case "SQLITE_PERM":
|
|
134
|
+
return new InvalidOperationError(message, innerError);
|
|
135
|
+
// SQLITE_SCHEMA (17): schema changed, statement needs re-preparation
|
|
136
|
+
case "SQLITE_SCHEMA":
|
|
137
|
+
return new InvalidOperationError(message, innerError);
|
|
138
|
+
// ── Transaction / abort ──
|
|
139
|
+
// SQLITE_ABORT (4): operation aborted (e.g. by rollback)
|
|
140
|
+
case "SQLITE_ABORT":
|
|
141
|
+
return new SerializationError(message, innerError);
|
|
142
|
+
// SQLITE_INTERRUPT (9): operation interrupted
|
|
143
|
+
case "SQLITE_INTERRUPT":
|
|
144
|
+
return new SerializationError(message, innerError);
|
|
145
|
+
}
|
|
146
|
+
return new DumboError({
|
|
147
|
+
errorCode: 500,
|
|
148
|
+
message,
|
|
149
|
+
innerError
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// src/storage/sqlite/core/sql/processors/columProcessors.ts
|
|
154
|
+
var mapColumnType = (token, { builder }) => {
|
|
155
|
+
let columnSQL;
|
|
156
|
+
const { sqlTokenType } = token;
|
|
157
|
+
switch (sqlTokenType) {
|
|
158
|
+
case "SQL_COLUMN_AUTO_INCREMENT":
|
|
159
|
+
columnSQL = `INTEGER ${token.primaryKey ? "PRIMARY KEY" : ""} AUTOINCREMENT`;
|
|
160
|
+
break;
|
|
161
|
+
case "SQL_COLUMN_BIGINT":
|
|
162
|
+
columnSQL = "INTEGER";
|
|
163
|
+
break;
|
|
164
|
+
case "SQL_COLUMN_SERIAL":
|
|
165
|
+
columnSQL = "INTEGER";
|
|
166
|
+
break;
|
|
167
|
+
case "SQL_COLUMN_INTEGER":
|
|
168
|
+
columnSQL = "INTEGER";
|
|
169
|
+
break;
|
|
170
|
+
case "SQL_COLUMN_JSONB":
|
|
171
|
+
columnSQL = "BLOB";
|
|
172
|
+
break;
|
|
173
|
+
case "SQL_COLUMN_BIGSERIAL":
|
|
174
|
+
columnSQL = "INTEGER";
|
|
175
|
+
break;
|
|
176
|
+
case "SQL_COLUMN_TIMESTAMP":
|
|
177
|
+
columnSQL = "DATETIME";
|
|
178
|
+
break;
|
|
179
|
+
case "SQL_COLUMN_TIMESTAMPTZ":
|
|
180
|
+
columnSQL = "DATETIME";
|
|
181
|
+
break;
|
|
182
|
+
case "SQL_COLUMN_VARCHAR":
|
|
183
|
+
columnSQL = `VARCHAR ${Number.isNaN(token.length) ? "" : `(${token.length})`}`;
|
|
184
|
+
break;
|
|
185
|
+
default: {
|
|
186
|
+
const exhaustiveCheck = sqlTokenType;
|
|
187
|
+
throw new Error(`Unknown column type: ${exhaustiveCheck}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
builder.addSQL(columnSQL);
|
|
191
|
+
};
|
|
192
|
+
var sqliteColumnProcessors = mapDefaultSQLColumnProcessors(mapColumnType);
|
|
193
|
+
|
|
194
|
+
// src/storage/sqlite/core/sql/formatter/index.ts
|
|
195
|
+
var sqliteSQLProcessorsRegistry = SQLProcessorsRegistry({
|
|
196
|
+
from: defaultProcessorsRegistry
|
|
197
|
+
}).register(sqliteColumnProcessors);
|
|
198
|
+
var sqliteFormatter = SQLFormatter({
|
|
199
|
+
processorsRegistry: sqliteSQLProcessorsRegistry
|
|
200
|
+
});
|
|
201
|
+
registerFormatter("SQLite", sqliteFormatter);
|
|
202
|
+
|
|
203
|
+
// src/storage/sqlite/core/execute/execute.ts
|
|
204
|
+
var sqliteExecute = async (database, handle) => {
|
|
205
|
+
try {
|
|
206
|
+
return await handle(database);
|
|
207
|
+
} finally {
|
|
208
|
+
await database.close();
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
var sqliteSQLExecutor = (driverType, serializer, formatter, errorMapper) => ({
|
|
212
|
+
driverType,
|
|
213
|
+
query: async (client, sql, options) => {
|
|
214
|
+
tracer.info("db:sql:query", {
|
|
215
|
+
query: (formatter ?? sqliteFormatter).format(sql, { serializer }).query,
|
|
216
|
+
params: (formatter ?? sqliteFormatter).format(sql, { serializer }).params,
|
|
217
|
+
debugSQL: (formatter ?? sqliteFormatter).describe(sql, { serializer })
|
|
218
|
+
});
|
|
219
|
+
try {
|
|
220
|
+
let result = await client.query(sql, options);
|
|
221
|
+
if (options?.mapping) {
|
|
222
|
+
result = {
|
|
223
|
+
...result,
|
|
224
|
+
rows: result.rows.map(
|
|
225
|
+
(row) => mapSQLQueryResult(row, options.mapping)
|
|
226
|
+
)
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return result;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
tracer.error("db:sql:query:execute:error", { error });
|
|
232
|
+
throw (errorMapper ?? mapSqliteError)(error);
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
batchQuery: async (client, sqls, options) => {
|
|
236
|
+
try {
|
|
237
|
+
const results = await client.batchQuery(sqls, options);
|
|
238
|
+
if (options?.mapping) {
|
|
239
|
+
return results.map((result) => ({
|
|
240
|
+
...result,
|
|
241
|
+
rows: result.rows.map(
|
|
242
|
+
(row) => mapSQLQueryResult(row, options.mapping)
|
|
243
|
+
)
|
|
244
|
+
}));
|
|
245
|
+
}
|
|
246
|
+
return results;
|
|
247
|
+
} catch (error) {
|
|
248
|
+
tracer.error("db:sql:batch_query:execute:error", { error });
|
|
249
|
+
throw (errorMapper ?? mapSqliteError)(error);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
command: async (client, sql, options) => {
|
|
253
|
+
tracer.info("db:sql:command", {
|
|
254
|
+
query: (formatter ?? sqliteFormatter).format(sql, { serializer }).query,
|
|
255
|
+
params: (formatter ?? sqliteFormatter).format(sql, { serializer }).params,
|
|
256
|
+
debugSQL: (formatter ?? sqliteFormatter).describe(sql, { serializer })
|
|
257
|
+
});
|
|
258
|
+
try {
|
|
259
|
+
return await client.command(sql, options);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
tracer.error("db:sql:command:execute:error", { error });
|
|
262
|
+
throw (errorMapper ?? mapSqliteError)(error);
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
batchCommand: async (client, sqls, options) => {
|
|
266
|
+
try {
|
|
267
|
+
return await client.batchCommand(sqls, options);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
tracer.error("db:sql:batch_command:execute:error", { error });
|
|
270
|
+
throw (errorMapper ?? mapSqliteError)(error);
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
formatter: formatter ?? sqliteFormatter
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// src/storage/sqlite/core/pool/dualPool.ts
|
|
277
|
+
import { cpus } from "os";
|
|
278
|
+
var sqliteDualConnectionPool = (options) => {
|
|
279
|
+
const { sqliteConnectionFactory, connectionOptions } = options;
|
|
280
|
+
const readerPoolSize = options.readerPoolSize ?? Math.max(4, cpus().length);
|
|
281
|
+
let databaseInitPromise = null;
|
|
282
|
+
const initTaskProcessor = new TaskProcessor({
|
|
283
|
+
maxActiveTasks: 1,
|
|
284
|
+
maxQueueSize: 1e3
|
|
285
|
+
});
|
|
286
|
+
const ensureDatabaseInitialized = async (connectionOptions2, retryCount = 0) => {
|
|
287
|
+
if (databaseInitPromise !== null) {
|
|
288
|
+
return databaseInitPromise;
|
|
289
|
+
}
|
|
290
|
+
return initTaskProcessor.enqueue(
|
|
291
|
+
async ({ ack }) => {
|
|
292
|
+
if (databaseInitPromise !== null) {
|
|
293
|
+
ack();
|
|
294
|
+
return databaseInitPromise;
|
|
295
|
+
}
|
|
296
|
+
const initConnection = sqliteConnectionFactory({
|
|
297
|
+
...connectionOptions2,
|
|
298
|
+
skipDatabasePragmas: false,
|
|
299
|
+
readonly: false
|
|
300
|
+
});
|
|
301
|
+
const initPromise = initConnection.open();
|
|
302
|
+
databaseInitPromise = initPromise;
|
|
303
|
+
try {
|
|
304
|
+
await initPromise;
|
|
305
|
+
await initConnection.close();
|
|
306
|
+
ack();
|
|
307
|
+
} catch (error) {
|
|
308
|
+
databaseInitPromise = null;
|
|
309
|
+
await initConnection.close();
|
|
310
|
+
ack();
|
|
311
|
+
if (retryCount < 3) {
|
|
312
|
+
return ensureDatabaseInitialized(connectionOptions2, retryCount + 1);
|
|
313
|
+
}
|
|
314
|
+
throw mapSqliteError(error);
|
|
315
|
+
}
|
|
316
|
+
},
|
|
317
|
+
{ taskGroupId: "db-init" }
|
|
318
|
+
);
|
|
319
|
+
};
|
|
320
|
+
const wrappedConnectionFactory = async (readonly, connectionOptions2) => {
|
|
321
|
+
await ensureDatabaseInitialized(connectionOptions2);
|
|
322
|
+
const connection = sqliteConnectionFactory({
|
|
323
|
+
...connectionOptions2,
|
|
324
|
+
skipDatabasePragmas: true,
|
|
325
|
+
readonly
|
|
326
|
+
});
|
|
327
|
+
await connection.open();
|
|
328
|
+
return connection;
|
|
329
|
+
};
|
|
330
|
+
const writerPool = createSingletonConnectionPool({
|
|
331
|
+
driverType: options.driverType,
|
|
332
|
+
getConnection: () => wrappedConnectionFactory(false, connectionOptions)
|
|
333
|
+
});
|
|
334
|
+
const readerPool = createBoundedConnectionPool({
|
|
335
|
+
driverType: options.driverType,
|
|
336
|
+
getConnection: () => wrappedConnectionFactory(true, connectionOptions),
|
|
337
|
+
maxConnections: readerPoolSize
|
|
338
|
+
});
|
|
339
|
+
return {
|
|
340
|
+
driverType: options.driverType,
|
|
341
|
+
connection: (connectionOptions2) => connectionOptions2?.readonly ? readerPool.connection(connectionOptions2) : writerPool.connection(connectionOptions2),
|
|
342
|
+
execute: {
|
|
343
|
+
query: (...args) => readerPool.execute.query(...args),
|
|
344
|
+
batchQuery: (...args) => readerPool.execute.batchQuery(...args),
|
|
345
|
+
command: (...args) => writerPool.execute.command(...args),
|
|
346
|
+
batchCommand: (...args) => writerPool.execute.batchCommand(...args)
|
|
347
|
+
},
|
|
348
|
+
withConnection: (handle, connectionOptions2) => connectionOptions2?.readonly ? readerPool.withConnection(handle, connectionOptions2) : writerPool.withConnection(handle, connectionOptions2),
|
|
349
|
+
transaction: writerPool.transaction,
|
|
350
|
+
withTransaction: writerPool.withTransaction,
|
|
351
|
+
close: () => Promise.all([writerPool.close(), readerPool.close()]).then(() => {
|
|
352
|
+
})
|
|
353
|
+
};
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
// src/storage/sqlite/core/pool/pool.ts
|
|
357
|
+
var isInMemoryDatabase = (options) => {
|
|
358
|
+
if ("fileName" in options) {
|
|
359
|
+
return options.fileName === InMemorySQLiteDatabase;
|
|
360
|
+
}
|
|
361
|
+
if ("connectionString" in options) {
|
|
362
|
+
return options.connectionString === InMemorySQLiteDatabase;
|
|
363
|
+
}
|
|
364
|
+
return false;
|
|
365
|
+
};
|
|
366
|
+
var sqliteAmbientConnectionPool = (options) => {
|
|
367
|
+
const { connection, driverType } = options;
|
|
368
|
+
return createAmbientConnectionPool({
|
|
369
|
+
driverType,
|
|
370
|
+
connection
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
var sqliteSingletonConnectionPool = (options) => {
|
|
374
|
+
const { driverType, sqliteConnectionFactory, connectionOptions } = options;
|
|
375
|
+
return createSingletonConnectionPool({
|
|
376
|
+
driverType,
|
|
377
|
+
getConnection: () => sqliteConnectionFactory(connectionOptions)
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
var sqliteAlwaysNewConnectionPool = (options) => {
|
|
381
|
+
const { driverType, sqliteConnectionFactory, connectionOptions } = options;
|
|
382
|
+
return createAlwaysNewConnectionPool({
|
|
383
|
+
driverType,
|
|
384
|
+
getConnection: () => sqliteConnectionFactory(connectionOptions)
|
|
385
|
+
});
|
|
386
|
+
};
|
|
387
|
+
var toSqlitePoolOptions = (options) => {
|
|
388
|
+
const { singleton, ...rest } = options;
|
|
389
|
+
const isInMemory = isInMemoryDatabase(options);
|
|
390
|
+
if ("client" in options && options.client) {
|
|
391
|
+
return { ...rest, singleton: true };
|
|
392
|
+
}
|
|
393
|
+
if (isInMemory) {
|
|
394
|
+
return { ...rest, singleton: true };
|
|
395
|
+
}
|
|
396
|
+
if (singleton === true) {
|
|
397
|
+
return { ...rest, singleton: true };
|
|
398
|
+
}
|
|
399
|
+
return { ...rest, dual: true };
|
|
400
|
+
};
|
|
401
|
+
function sqlitePool(options) {
|
|
402
|
+
const { driverType } = options;
|
|
403
|
+
if (options.connection)
|
|
404
|
+
return createAmbientConnectionPool({
|
|
405
|
+
driverType,
|
|
406
|
+
connection: options.connection
|
|
407
|
+
});
|
|
408
|
+
if ("dual" in options && options.dual) {
|
|
409
|
+
return sqliteDualConnectionPool(
|
|
410
|
+
options
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
if (options.singleton === true && options.sqliteConnectionFactory) {
|
|
414
|
+
return createSingletonConnectionPool({
|
|
415
|
+
driverType,
|
|
416
|
+
getConnection: () => options.sqliteConnectionFactory(
|
|
417
|
+
options.connectionOptions
|
|
418
|
+
)
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
return createAlwaysNewConnectionPool({
|
|
422
|
+
driverType,
|
|
423
|
+
getConnection: () => options.sqliteConnectionFactory(
|
|
424
|
+
options.connectionOptions
|
|
425
|
+
)
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/storage/sqlite/core/schema/migrations.ts
|
|
430
|
+
var DefaultSQLiteMigratorOptions = {};
|
|
431
|
+
registerDefaultMigratorOptions("SQLite", DefaultSQLiteMigratorOptions);
|
|
432
|
+
|
|
433
|
+
// src/storage/sqlite/core/index.ts
|
|
434
|
+
var SQLiteDatabaseName = "SQLite";
|
|
435
|
+
|
|
436
|
+
// src/storage/sqlite/core/connections/connectionString.ts
|
|
437
|
+
var SQLiteConnectionString = (connectionString) => {
|
|
438
|
+
if (!connectionString.startsWith("file:") && connectionString !== ":memory:" && !connectionString.startsWith("/") && !connectionString.startsWith("./")) {
|
|
439
|
+
throw new Error(
|
|
440
|
+
`Invalid SQLite connection string: ${connectionString}. It should start with "file:", ":memory:", "/", or "./".`
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
return connectionString;
|
|
444
|
+
};
|
|
445
|
+
var parsePragmasFromConnectionString = (connectionString) => {
|
|
446
|
+
const str = String(connectionString);
|
|
447
|
+
if (!str.startsWith("file:")) {
|
|
448
|
+
return {};
|
|
449
|
+
}
|
|
450
|
+
const url = new URL(str);
|
|
451
|
+
const params = url.searchParams;
|
|
452
|
+
const pragmas = {};
|
|
453
|
+
const journalMode = params.get("journal_mode");
|
|
454
|
+
if (journalMode !== null) {
|
|
455
|
+
pragmas.journal_mode = journalMode;
|
|
456
|
+
}
|
|
457
|
+
const synchronous = params.get("synchronous");
|
|
458
|
+
if (synchronous !== null) {
|
|
459
|
+
pragmas.synchronous = synchronous;
|
|
460
|
+
}
|
|
461
|
+
const cacheSize = params.get("cache_size");
|
|
462
|
+
if (cacheSize !== null) {
|
|
463
|
+
pragmas.cache_size = parseInt(cacheSize, 10);
|
|
464
|
+
}
|
|
465
|
+
const foreignKeys = params.get("foreign_keys");
|
|
466
|
+
if (foreignKeys !== null) {
|
|
467
|
+
const val = foreignKeys.toLowerCase();
|
|
468
|
+
pragmas.foreign_keys = val === "true" || val === "on" || val === "1";
|
|
469
|
+
}
|
|
470
|
+
const tempStore = params.get("temp_store");
|
|
471
|
+
if (tempStore !== null) {
|
|
472
|
+
pragmas.temp_store = tempStore.toUpperCase();
|
|
473
|
+
}
|
|
474
|
+
const busyTimeout = params.get("busy_timeout");
|
|
475
|
+
if (busyTimeout !== null) {
|
|
476
|
+
pragmas.busy_timeout = parseInt(busyTimeout, 10);
|
|
477
|
+
}
|
|
478
|
+
return pragmas;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
// src/storage/sqlite/core/connections/index.ts
|
|
482
|
+
var isSQLiteError = (error) => {
|
|
483
|
+
if (error instanceof Error && "code" in error) {
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
return false;
|
|
487
|
+
};
|
|
488
|
+
var transactionNestingCounter = () => {
|
|
489
|
+
let transactionLevel = 0;
|
|
490
|
+
return {
|
|
491
|
+
reset: () => {
|
|
492
|
+
transactionLevel = 0;
|
|
493
|
+
},
|
|
494
|
+
increment: () => {
|
|
495
|
+
transactionLevel++;
|
|
496
|
+
},
|
|
497
|
+
decrement: () => {
|
|
498
|
+
transactionLevel--;
|
|
499
|
+
if (transactionLevel < 0) {
|
|
500
|
+
throw new Error("Transaction level is out of bounds");
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
get level() {
|
|
504
|
+
return transactionLevel;
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
};
|
|
508
|
+
var sqliteAmbientClientConnection = (options) => {
|
|
509
|
+
const {
|
|
510
|
+
client,
|
|
511
|
+
driverType,
|
|
512
|
+
initTransaction,
|
|
513
|
+
allowNestedTransactions,
|
|
514
|
+
defaultTransactionMode,
|
|
515
|
+
serializer,
|
|
516
|
+
errorMapper
|
|
517
|
+
} = options;
|
|
518
|
+
return createAmbientConnection({
|
|
519
|
+
driverType,
|
|
520
|
+
client,
|
|
521
|
+
initTransaction: initTransaction ?? ((connection) => sqliteTransaction(
|
|
522
|
+
driverType,
|
|
523
|
+
connection,
|
|
524
|
+
allowNestedTransactions ?? false,
|
|
525
|
+
serializer,
|
|
526
|
+
defaultTransactionMode
|
|
527
|
+
)),
|
|
528
|
+
executor: ({ serializer: serializer2 }) => sqliteSQLExecutor(driverType, serializer2, void 0, errorMapper),
|
|
529
|
+
serializer
|
|
530
|
+
});
|
|
531
|
+
};
|
|
532
|
+
var sqliteClientConnection = (options) => {
|
|
533
|
+
const { connectionOptions, sqliteClientFactory, serializer } = options;
|
|
534
|
+
let client = null;
|
|
535
|
+
const connect = async () => {
|
|
536
|
+
if (client) return Promise.resolve(client);
|
|
537
|
+
client = sqliteClientFactory(connectionOptions);
|
|
538
|
+
if (client && "connect" in client && typeof client.connect === "function") {
|
|
539
|
+
try {
|
|
540
|
+
await client.connect();
|
|
541
|
+
} catch (error) {
|
|
542
|
+
throw mapSqliteError(error);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return client;
|
|
546
|
+
};
|
|
547
|
+
return createConnection({
|
|
548
|
+
driverType: options.driverType,
|
|
549
|
+
connect,
|
|
550
|
+
close: async () => {
|
|
551
|
+
if (client && "close" in client && typeof client.close === "function")
|
|
552
|
+
await client.close();
|
|
553
|
+
else if (client && "release" in client && typeof client.release === "function")
|
|
554
|
+
client.release();
|
|
555
|
+
},
|
|
556
|
+
initTransaction: (connection) => sqliteTransaction(
|
|
557
|
+
options.driverType,
|
|
558
|
+
connection,
|
|
559
|
+
connectionOptions.transactionOptions?.allowNestedTransactions ?? false,
|
|
560
|
+
serializer,
|
|
561
|
+
connectionOptions.defaultTransactionMode
|
|
562
|
+
),
|
|
563
|
+
executor: ({ serializer: serializer2 }) => sqliteSQLExecutor(options.driverType, serializer2),
|
|
564
|
+
serializer
|
|
565
|
+
});
|
|
566
|
+
};
|
|
567
|
+
var sqlitePoolClientConnection = (options) => {
|
|
568
|
+
const { connectionOptions, sqliteClientFactory, serializer } = options;
|
|
569
|
+
let client = null;
|
|
570
|
+
const connect = async () => {
|
|
571
|
+
if (client) return Promise.resolve(client);
|
|
572
|
+
client = sqliteClientFactory(connectionOptions);
|
|
573
|
+
try {
|
|
574
|
+
await client.connect();
|
|
575
|
+
} catch (error) {
|
|
576
|
+
throw mapSqliteError(error);
|
|
577
|
+
}
|
|
578
|
+
return client;
|
|
579
|
+
};
|
|
580
|
+
return createConnection({
|
|
581
|
+
driverType: options.driverType,
|
|
582
|
+
connect,
|
|
583
|
+
close: () => client !== null ? Promise.resolve(client.release()) : Promise.resolve(),
|
|
584
|
+
initTransaction: (connection) => sqliteTransaction(
|
|
585
|
+
options.driverType,
|
|
586
|
+
connection,
|
|
587
|
+
connectionOptions.transactionOptions?.allowNestedTransactions ?? false,
|
|
588
|
+
serializer,
|
|
589
|
+
connectionOptions.defaultTransactionMode
|
|
590
|
+
),
|
|
591
|
+
executor: ({ serializer: serializer2 }) => sqliteSQLExecutor(options.driverType, serializer2),
|
|
592
|
+
serializer
|
|
593
|
+
});
|
|
594
|
+
};
|
|
595
|
+
function sqliteConnection(options) {
|
|
596
|
+
return options.type === "Client" ? sqliteClientConnection(options) : sqlitePoolClientConnection(options);
|
|
597
|
+
}
|
|
598
|
+
var InMemorySQLiteDatabase = SQLiteConnectionString(":memory:");
|
|
599
|
+
var DEFAULT_SQLITE_PRAGMA_OPTIONS = {
|
|
600
|
+
journal_mode: "WAL",
|
|
601
|
+
synchronous: "NORMAL",
|
|
602
|
+
cache_size: -1e6,
|
|
603
|
+
foreign_keys: true,
|
|
604
|
+
temp_store: "MEMORY",
|
|
605
|
+
busy_timeout: 5e3
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
// src/storage/sqlite/core/transactions/index.ts
|
|
609
|
+
var sqliteTransaction = (driverType, connection, allowNestedTransactions, serializer, defaultTransactionMode) => (getClient, options) => {
|
|
610
|
+
const transactionCounter = transactionNestingCounter();
|
|
611
|
+
allowNestedTransactions = options?.allowNestedTransactions ?? allowNestedTransactions;
|
|
612
|
+
const transaction = {
|
|
613
|
+
connection: connection(),
|
|
614
|
+
driverType,
|
|
615
|
+
begin: async function() {
|
|
616
|
+
const client = await getClient;
|
|
617
|
+
if (allowNestedTransactions) {
|
|
618
|
+
if (transactionCounter.level >= 1) {
|
|
619
|
+
transactionCounter.increment();
|
|
620
|
+
if (options?.useSavepoints) {
|
|
621
|
+
await client.query(
|
|
622
|
+
SQL`SAVEPOINT transaction${SQL.plain(transactionCounter.level.toString())}`
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
transactionCounter.increment();
|
|
628
|
+
}
|
|
629
|
+
const mode = options?.mode ?? defaultTransactionMode ?? "IMMEDIATE";
|
|
630
|
+
await client.query(SQL`BEGIN ${SQL.plain(mode)} TRANSACTION`);
|
|
631
|
+
},
|
|
632
|
+
commit: async function() {
|
|
633
|
+
const client = await getClient;
|
|
634
|
+
try {
|
|
635
|
+
if (allowNestedTransactions) {
|
|
636
|
+
if (transactionCounter.level > 1) {
|
|
637
|
+
if (options?.useSavepoints) {
|
|
638
|
+
await client.query(
|
|
639
|
+
SQL`RELEASE transaction${SQL.plain(transactionCounter.level.toString())}`
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
transactionCounter.decrement();
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
transactionCounter.reset();
|
|
646
|
+
}
|
|
647
|
+
await client.query(SQL`COMMIT`);
|
|
648
|
+
} finally {
|
|
649
|
+
if (options?.close)
|
|
650
|
+
await options?.close(
|
|
651
|
+
client
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
},
|
|
655
|
+
rollback: async function(error) {
|
|
656
|
+
const client = await getClient;
|
|
657
|
+
try {
|
|
658
|
+
if (allowNestedTransactions) {
|
|
659
|
+
if (transactionCounter.level > 1) {
|
|
660
|
+
transactionCounter.decrement();
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
await client.query(SQL`ROLLBACK`);
|
|
665
|
+
} finally {
|
|
666
|
+
if (options?.close)
|
|
667
|
+
await options?.close(
|
|
668
|
+
client,
|
|
669
|
+
error
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
},
|
|
673
|
+
execute: sqlExecutor(sqliteSQLExecutor(driverType, serializer), {
|
|
674
|
+
connect: () => getClient
|
|
675
|
+
}),
|
|
676
|
+
_transactionOptions: options ?? {}
|
|
677
|
+
};
|
|
678
|
+
return transaction;
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
export {
|
|
682
|
+
mapSqliteError,
|
|
683
|
+
sqliteFormatter,
|
|
684
|
+
sqliteExecute,
|
|
685
|
+
sqliteSQLExecutor,
|
|
686
|
+
sqliteTransaction,
|
|
687
|
+
SQLiteConnectionString,
|
|
688
|
+
parsePragmasFromConnectionString,
|
|
689
|
+
isSQLiteError,
|
|
690
|
+
transactionNestingCounter,
|
|
691
|
+
sqliteAmbientClientConnection,
|
|
692
|
+
sqliteClientConnection,
|
|
693
|
+
sqlitePoolClientConnection,
|
|
694
|
+
sqliteConnection,
|
|
695
|
+
InMemorySQLiteDatabase,
|
|
696
|
+
DEFAULT_SQLITE_PRAGMA_OPTIONS,
|
|
697
|
+
isInMemoryDatabase,
|
|
698
|
+
sqliteAmbientConnectionPool,
|
|
699
|
+
sqliteSingletonConnectionPool,
|
|
700
|
+
sqliteAlwaysNewConnectionPool,
|
|
701
|
+
toSqlitePoolOptions,
|
|
702
|
+
sqlitePool,
|
|
703
|
+
DefaultSQLiteMigratorOptions,
|
|
704
|
+
SQLiteDatabaseName
|
|
705
|
+
};
|
|
706
|
+
//# sourceMappingURL=chunk-XFEGS3YT.js.map
|