@event-driven-io/dumbo 0.13.0-beta.34 → 0.13.0-beta.36
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/cloudflare.cjs +379 -434
- package/dist/cloudflare.cjs.map +1 -1
- package/dist/cloudflare.d.cts +36 -29
- package/dist/cloudflare.d.ts +36 -29
- package/dist/cloudflare.js +343 -434
- package/dist/cloudflare.js.map +1 -1
- package/dist/core-BeyC5tHn.js +519 -0
- package/dist/core-BeyC5tHn.js.map +1 -0
- package/dist/core-Bf2ujUFy.js +379 -0
- package/dist/core-Bf2ujUFy.js.map +1 -0
- package/dist/core-BgYtAxCm.cjs +3237 -0
- package/dist/core-BgYtAxCm.cjs.map +1 -0
- package/dist/core-DDo4m0Z_.js +2262 -0
- package/dist/core-DDo4m0Z_.js.map +1 -0
- package/dist/core-DmbB4qMn.cjs +656 -0
- package/dist/core-DmbB4qMn.cjs.map +1 -0
- package/dist/core-Go_IA78L.cjs +450 -0
- package/dist/core-Go_IA78L.cjs.map +1 -0
- package/dist/{connectionString-Dch_MIRU.d.cts → index-BDSQvDH2.d.cts} +1010 -922
- package/dist/index-BdWFmDhJ.d.cts +68 -0
- package/dist/index-D1jryNqo.d.ts +198 -0
- package/dist/index-DaP2fTEX.d.ts +67 -0
- package/dist/index-DqVvUav9.d.cts +198 -0
- package/dist/index-S_wp0Eaf.d.cts +67 -0
- package/dist/index-cxtbLiju.d.ts +68 -0
- package/dist/{connectionString-Dch_MIRU.d.ts → index-zkszkJqP.d.ts} +1010 -922
- package/dist/index.cjs +212 -359
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -132
- package/dist/index.d.ts +3 -132
- package/dist/index.js +52 -361
- package/dist/index.js.map +1 -1
- package/dist/pg.cjs +366 -431
- package/dist/pg.cjs.map +1 -1
- package/dist/pg.d.cts +83 -72
- package/dist/pg.d.ts +83 -72
- package/dist/pg.js +320 -431
- package/dist/pg.js.map +1 -1
- package/dist/postgreSQLMetadata-LqX-Z_wo.cjs +118 -0
- package/dist/postgreSQLMetadata-LqX-Z_wo.cjs.map +1 -0
- package/dist/postgreSQLMetadata-zMq8yJo_.js +65 -0
- package/dist/postgreSQLMetadata-zMq8yJo_.js.map +1 -0
- package/dist/postgresql.cjs +25 -50
- package/dist/postgresql.d.cts +3 -61
- package/dist/postgresql.d.ts +3 -61
- package/dist/postgresql.js +4 -50
- package/dist/sqlite.cjs +30 -60
- package/dist/sqlite.d.cts +3 -190
- package/dist/sqlite.d.ts +3 -190
- package/dist/sqlite.js +4 -60
- package/dist/sqlite3.cjs +312 -380
- package/dist/sqlite3.cjs.map +1 -1
- package/dist/sqlite3.d.cts +79 -77
- package/dist/sqlite3.d.ts +79 -77
- package/dist/sqlite3.js +276 -378
- package/dist/sqlite3.js.map +1 -1
- package/dist/sqliteMetadata-BoQ7Sc9I.js +29 -0
- package/dist/sqliteMetadata-BoQ7Sc9I.js.map +1 -0
- package/dist/sqliteMetadata-D_2pKeCC.cjs +46 -0
- package/dist/sqliteMetadata-D_2pKeCC.cjs.map +1 -0
- package/package.json +12 -12
- package/dist/chunk-33P5766L.js +0 -83
- package/dist/chunk-33P5766L.js.map +0 -1
- package/dist/chunk-4MMMEDQ7.cjs +0 -2944
- package/dist/chunk-4MMMEDQ7.cjs.map +0 -1
- package/dist/chunk-6HAHSSPW.js +0 -701
- package/dist/chunk-6HAHSSPW.js.map +0 -1
- package/dist/chunk-GNH5XC6D.cjs +0 -481
- package/dist/chunk-GNH5XC6D.cjs.map +0 -1
- package/dist/chunk-H2JBS7HM.js +0 -2944
- package/dist/chunk-H2JBS7HM.js.map +0 -1
- package/dist/chunk-JIZGCEPE.cjs +0 -701
- package/dist/chunk-JIZGCEPE.cjs.map +0 -1
- package/dist/chunk-NJI6PJFZ.cjs +0 -83
- package/dist/chunk-NJI6PJFZ.cjs.map +0 -1
- package/dist/chunk-RQ3KKMTP.cjs +0 -34
- package/dist/chunk-RQ3KKMTP.cjs.map +0 -1
- package/dist/chunk-UK7MXVS2.js +0 -34
- package/dist/chunk-UK7MXVS2.js.map +0 -1
- package/dist/chunk-Y5TD53QE.js +0 -481
- package/dist/chunk-Y5TD53QE.js.map +0 -1
- package/dist/columnProcessors-Be2uMYjS.d.cts +0 -8
- package/dist/columnProcessors-D0ivj_SU.d.ts +0 -8
- package/dist/postgresql.cjs.map +0 -1
- package/dist/postgresql.js.map +0 -1
- package/dist/sqlite.cjs.map +0 -1
- package/dist/sqlite.js.map +0 -1
package/dist/cloudflare.js
CHANGED
|
@@ -1,456 +1,365 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
InMemorySQLiteDatabase,
|
|
5
|
-
SQLiteConnectionString,
|
|
6
|
-
SQLiteDatabaseName,
|
|
7
|
-
isInMemoryDatabase,
|
|
8
|
-
isSQLiteError,
|
|
9
|
-
mapSqliteError,
|
|
10
|
-
parsePragmasFromConnectionString,
|
|
11
|
-
sqliteAlwaysNewConnectionPool,
|
|
12
|
-
sqliteAmbientClientConnection,
|
|
13
|
-
sqliteAmbientConnectionPool,
|
|
14
|
-
sqliteClientConnection,
|
|
15
|
-
sqliteConnection,
|
|
16
|
-
sqliteExecute,
|
|
17
|
-
sqliteFormatter,
|
|
18
|
-
sqlitePool,
|
|
19
|
-
sqlitePoolClientConnection,
|
|
20
|
-
sqliteSQLExecutor,
|
|
21
|
-
sqliteSingletonConnectionPool,
|
|
22
|
-
sqliteTransaction,
|
|
23
|
-
toSqlitePoolOptions,
|
|
24
|
-
transactionNestingCounter
|
|
25
|
-
} from "./chunk-6HAHSSPW.js";
|
|
26
|
-
import {
|
|
27
|
-
defaultSQLiteDatabase,
|
|
28
|
-
sqliteMetadata,
|
|
29
|
-
tableExists
|
|
30
|
-
} from "./chunk-UK7MXVS2.js";
|
|
31
|
-
import {
|
|
32
|
-
BatchCommandNoChangesError,
|
|
33
|
-
CheckViolationError,
|
|
34
|
-
ConnectionError,
|
|
35
|
-
DataError,
|
|
36
|
-
DeadlockError,
|
|
37
|
-
DumboError,
|
|
38
|
-
ForeignKeyViolationError,
|
|
39
|
-
InsufficientResourcesError,
|
|
40
|
-
IntegrityConstraintViolationError,
|
|
41
|
-
InvalidOperationError,
|
|
42
|
-
JSONSerializer,
|
|
43
|
-
LockNotAvailableError,
|
|
44
|
-
NotNullViolationError,
|
|
45
|
-
SerializationError,
|
|
46
|
-
SystemError,
|
|
47
|
-
UniqueConstraintError,
|
|
48
|
-
createSingletonConnectionPool,
|
|
49
|
-
dumboDatabaseDriverRegistry,
|
|
50
|
-
sqlExecutor,
|
|
51
|
-
tracer
|
|
52
|
-
} from "./chunk-H2JBS7HM.js";
|
|
1
|
+
import { B as LockNotAvailableError, I as ForeignKeyViolationError, K as UniqueConstraintError, L as InsufficientResourcesError, M as DataError, N as DeadlockError, On as dumboDatabaseDriverRegistry, P as DumboError, R as IntegrityConstraintViolationError, T as sqlExecutor, U as SerializationError, V as NotNullViolationError, W as SystemError, _ as BatchCommandNoChangesError, j as ConnectionError, k as CheckViolationError, o as createSingletonConnectionPool, rn as JSONSerializer, rt as tracer, z as InvalidOperationError } from "./core-DDo4m0Z_.js";
|
|
2
|
+
import { n as defaultSQLiteDatabase, r as tableExists, t as sqliteMetadata } from "./sqliteMetadata-BoQ7Sc9I.js";
|
|
3
|
+
import { C as mapSqliteError, S as sqliteFormatter, _ as SQLiteConnectionString, a as sqliteAmbientConnectionPool, b as sqliteExecute, c as toSqlitePoolOptions, d as isSQLiteError, f as sqliteAmbientClientConnection, g as transactionNestingCounter, h as sqlitePoolClientConnection, i as sqliteAlwaysNewConnectionPool, l as DEFAULT_SQLITE_PRAGMA_OPTIONS, m as sqliteConnection, n as DefaultSQLiteMigratorOptions, o as sqlitePool, p as sqliteClientConnection, r as isInMemoryDatabase, s as sqliteSingletonConnectionPool, t as SQLiteDatabaseName, u as InMemorySQLiteDatabase, v as parsePragmasFromConnectionString, x as sqliteSQLExecutor, y as sqliteTransaction } from "./core-BeyC5tHn.js";
|
|
53
4
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
return qr;
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
};
|
|
5
|
+
//#region src/storage/sqlite/d1/connections/d1Client.ts
|
|
6
|
+
const d1Client = (options) => {
|
|
7
|
+
const { database, session, serializer } = options;
|
|
8
|
+
const execute = session ?? database;
|
|
9
|
+
return {
|
|
10
|
+
database,
|
|
11
|
+
session,
|
|
12
|
+
connect: () => Promise.resolve(),
|
|
13
|
+
close: () => Promise.resolve(),
|
|
14
|
+
withSession: async (constraintOrBookmark) => {
|
|
15
|
+
const newSession = constraintOrBookmark ? database.withSession(constraintOrBookmark) : database.withSession();
|
|
16
|
+
return Promise.resolve(d1Client({
|
|
17
|
+
database,
|
|
18
|
+
session: newSession,
|
|
19
|
+
serializer
|
|
20
|
+
}));
|
|
21
|
+
},
|
|
22
|
+
query: async (sql, _options) => {
|
|
23
|
+
const { query, params } = sqliteFormatter.format(sql, { serializer });
|
|
24
|
+
const stmt = execute.prepare(query);
|
|
25
|
+
const { results } = await (params?.length ? stmt.bind(...params) : stmt).all();
|
|
26
|
+
return {
|
|
27
|
+
rowCount: results?.length ?? 0,
|
|
28
|
+
rows: results ?? []
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
batchQuery: async (sqls, _options) => {
|
|
32
|
+
const statements = sqls.map((sql) => {
|
|
33
|
+
const { query, params } = sqliteFormatter.format(sql, { serializer });
|
|
34
|
+
const stmt = execute.prepare(query);
|
|
35
|
+
return params?.length ? stmt.bind(...params) : stmt;
|
|
36
|
+
});
|
|
37
|
+
return (await execute.batch(statements)).map((result) => ({
|
|
38
|
+
rowCount: result.results?.length ?? 0,
|
|
39
|
+
rows: result.results ?? []
|
|
40
|
+
}));
|
|
41
|
+
},
|
|
42
|
+
command: async (sql, _options) => {
|
|
43
|
+
const { query, params } = sqliteFormatter.format(sql, { serializer });
|
|
44
|
+
const stmt = execute.prepare(query);
|
|
45
|
+
const result = await (params?.length ? stmt.bind(...params) : stmt).run();
|
|
46
|
+
return {
|
|
47
|
+
rowCount: result.meta?.changes ?? 0,
|
|
48
|
+
rows: result.results ?? []
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
batchCommand: async (sqls, options) => {
|
|
52
|
+
const statements = sqls.map((sql) => {
|
|
53
|
+
const { query, params } = sqliteFormatter.format(sql, { serializer });
|
|
54
|
+
const stmt = execute.prepare(query);
|
|
55
|
+
return params?.length ? stmt.bind(...params) : stmt;
|
|
56
|
+
});
|
|
57
|
+
return (await execute.batch(statements)).map((result, i) => {
|
|
58
|
+
const qr = {
|
|
59
|
+
rowCount: result.meta?.changes ?? 0,
|
|
60
|
+
rows: result.results ?? []
|
|
61
|
+
};
|
|
62
|
+
if (options?.assertChanges && (qr.rowCount ?? 0) === 0) throw new BatchCommandNoChangesError(i);
|
|
63
|
+
return qr;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
121
67
|
};
|
|
122
68
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/storage/sqlite/d1/errors/errorMapper.ts
|
|
71
|
+
const getErrorMessage = (error) => error instanceof Error ? error.message : void 0;
|
|
72
|
+
const asError = (error) => error instanceof Error ? error : void 0;
|
|
73
|
+
/**
|
|
74
|
+
* Determines the constraint subtype from the error message.
|
|
75
|
+
*
|
|
76
|
+
* D1 embeds the SQLite constraint detail in the message string, e.g.:
|
|
77
|
+
* "D1_ERROR: UNIQUE constraint failed: users.email"
|
|
78
|
+
* "D1_ERROR: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed"
|
|
79
|
+
*/
|
|
80
|
+
const mapConstraintError = (message, innerError) => {
|
|
81
|
+
const upperMessage = message?.toUpperCase() ?? "";
|
|
82
|
+
if (upperMessage.includes("UNIQUE") || upperMessage.includes("PRIMARY KEY")) return new UniqueConstraintError(message, innerError);
|
|
83
|
+
if (upperMessage.includes("FOREIGN KEY")) return new ForeignKeyViolationError(message, innerError);
|
|
84
|
+
if (upperMessage.includes("NOT NULL")) return new NotNullViolationError(message, innerError);
|
|
85
|
+
if (upperMessage.includes("CHECK")) return new CheckViolationError(message, innerError);
|
|
86
|
+
return new IntegrityConstraintViolationError(message, innerError);
|
|
137
87
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
88
|
+
const isConstraintMessage = (upper) => upper.includes("CONSTRAINT") || upper.includes("UNIQUE") || upper.includes("PRIMARY KEY") || upper.includes("FOREIGN KEY") || upper.includes("NOT NULL");
|
|
89
|
+
/** Extracts the first `SQLITE_<CODE>` token from a message string. */
|
|
90
|
+
const SQLITE_CODE_RE = /SQLITE_([A-Z]+)/;
|
|
91
|
+
const extractEmbeddedSqliteCode = (message) => {
|
|
92
|
+
const match = SQLITE_CODE_RE.exec(message);
|
|
93
|
+
return match ? `SQLITE_${match[1]}` : void 0;
|
|
143
94
|
};
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return new InvalidOperationError(message, innerError);
|
|
180
|
-
case "SQLITE_MISUSE":
|
|
181
|
-
return new InvalidOperationError(message, innerError);
|
|
182
|
-
case "SQLITE_AUTH":
|
|
183
|
-
return new InvalidOperationError(message, innerError);
|
|
184
|
-
case "SQLITE_PERM":
|
|
185
|
-
return new InvalidOperationError(message, innerError);
|
|
186
|
-
case "SQLITE_SCHEMA":
|
|
187
|
-
return new InvalidOperationError(message, innerError);
|
|
188
|
-
case "SQLITE_ABORT":
|
|
189
|
-
return new SerializationError(message, innerError);
|
|
190
|
-
case "SQLITE_INTERRUPT":
|
|
191
|
-
return new SerializationError(message, innerError);
|
|
192
|
-
}
|
|
193
|
-
return void 0;
|
|
95
|
+
/**
|
|
96
|
+
* Maps a `SQLITE_*` code found in the D1 message to a DumboError.
|
|
97
|
+
*
|
|
98
|
+
* D1 sometimes forwards raw SQLite result codes from the C++ layer, e.g.:
|
|
99
|
+
* "D1_ERROR: SQLITE_BUSY: database is locked"
|
|
100
|
+
* "SQLITE_READONLY: attempt to write a readonly database"
|
|
101
|
+
*
|
|
102
|
+
* See https://www.sqlite.org/rescode.html for the full code list.
|
|
103
|
+
*/
|
|
104
|
+
const mapEmbeddedSqliteCode = (code, message, innerError) => {
|
|
105
|
+
switch (code) {
|
|
106
|
+
case "SQLITE_CONSTRAINT": return mapConstraintError(message, innerError);
|
|
107
|
+
case "SQLITE_BUSY": return new LockNotAvailableError(message, innerError);
|
|
108
|
+
case "SQLITE_LOCKED": return new DeadlockError(message, innerError);
|
|
109
|
+
case "SQLITE_PROTOCOL": return new LockNotAvailableError(message, innerError);
|
|
110
|
+
case "SQLITE_CANTOPEN": return new ConnectionError(message, innerError);
|
|
111
|
+
case "SQLITE_NOTADB": return new ConnectionError(message, innerError);
|
|
112
|
+
case "SQLITE_NOMEM": return new InsufficientResourcesError(message, innerError);
|
|
113
|
+
case "SQLITE_FULL": return new InsufficientResourcesError(message, innerError);
|
|
114
|
+
case "SQLITE_IOERR": return new SystemError(message, innerError);
|
|
115
|
+
case "SQLITE_CORRUPT": return new SystemError(message, innerError);
|
|
116
|
+
case "SQLITE_INTERNAL": return new SystemError(message, innerError);
|
|
117
|
+
case "SQLITE_NOLFS": return new SystemError(message, innerError);
|
|
118
|
+
case "SQLITE_TOOBIG": return new DataError(message, innerError);
|
|
119
|
+
case "SQLITE_MISMATCH": return new DataError(message, innerError);
|
|
120
|
+
case "SQLITE_RANGE": return new DataError(message, innerError);
|
|
121
|
+
case "SQLITE_ERROR": return new InvalidOperationError(message, innerError);
|
|
122
|
+
case "SQLITE_READONLY": return new InvalidOperationError(message, innerError);
|
|
123
|
+
case "SQLITE_MISUSE": return new InvalidOperationError(message, innerError);
|
|
124
|
+
case "SQLITE_AUTH": return new InvalidOperationError(message, innerError);
|
|
125
|
+
case "SQLITE_PERM": return new InvalidOperationError(message, innerError);
|
|
126
|
+
case "SQLITE_SCHEMA": return new InvalidOperationError(message, innerError);
|
|
127
|
+
case "SQLITE_ABORT": return new SerializationError(message, innerError);
|
|
128
|
+
case "SQLITE_INTERRUPT": return new SerializationError(message, innerError);
|
|
129
|
+
}
|
|
194
130
|
};
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
131
|
+
/**
|
|
132
|
+
* Maps a Cloudflare D1 error to a typed DumboError.
|
|
133
|
+
*
|
|
134
|
+
* Unlike node-sqlite3, D1 throws **plain `Error` objects** with no `code`
|
|
135
|
+
* property. All error information is embedded in `error.message`, using
|
|
136
|
+
* well-known prefixes from the `workerd` runtime:
|
|
137
|
+
*
|
|
138
|
+
* | Prefix | Meaning |
|
|
139
|
+
* |-----------------------|----------------------------------------------|
|
|
140
|
+
* | `D1_ERROR:` | General D1 / SQLite error |
|
|
141
|
+
* | `D1_EXEC_ERROR:` | Error during `.exec()` batch |
|
|
142
|
+
* | `D1_TYPE_ERROR:` | Type mismatch (e.g. `undefined` vs `null`) |
|
|
143
|
+
* | `D1_COLUMN_NOTFOUND` | Referenced column does not exist |
|
|
144
|
+
* | `D1_DUMP_ERROR:` | Error during database dump |
|
|
145
|
+
* | `D1_SESSION_ERROR:` | Session bookmark / constraint error |
|
|
146
|
+
*
|
|
147
|
+
* D1 also surfaces platform-level messages (no prefix) for transient errors:
|
|
148
|
+
* - "Network connection lost."
|
|
149
|
+
* - "D1 DB is overloaded. Too many requests queued."
|
|
150
|
+
* - "Memory limit would be exceeded by this operation."
|
|
151
|
+
* - "Cannot resolve D1 DB due to transient issue on remote node."
|
|
152
|
+
* - "D1 DB reset because its code was updated."
|
|
153
|
+
*
|
|
154
|
+
* Reference:
|
|
155
|
+
* - https://developers.cloudflare.com/d1/platform/client-api/
|
|
156
|
+
* - https://github.com/cloudflare/workerd (src/cloudflare/internal/d1-api.ts)
|
|
157
|
+
*
|
|
158
|
+
* Falls back to a generic DumboError (500) if the error is not a recognized D1 error.
|
|
159
|
+
*/
|
|
160
|
+
const mapD1Error = (error) => {
|
|
161
|
+
if (DumboError.isInstanceOf(error)) return error;
|
|
162
|
+
const message = getErrorMessage(error);
|
|
163
|
+
if (!message) return new DumboError({
|
|
164
|
+
errorCode: 500,
|
|
165
|
+
message: error instanceof Error ? error.message : String(error),
|
|
166
|
+
innerError: asError(error)
|
|
167
|
+
});
|
|
168
|
+
const innerError = asError(error);
|
|
169
|
+
const upper = message.toUpperCase();
|
|
170
|
+
if (upper.startsWith("D1_TYPE_ERROR")) return new DataError(message, innerError);
|
|
171
|
+
if (upper.startsWith("D1_COLUMN_NOTFOUND")) return new DataError(message, innerError);
|
|
172
|
+
if (upper.startsWith("D1_DUMP_ERROR")) return new SystemError(message, innerError);
|
|
173
|
+
if (upper.startsWith("D1_SESSION_ERROR")) return new ConnectionError(message, innerError);
|
|
174
|
+
if (isConstraintMessage(upper)) return mapConstraintError(message, innerError);
|
|
175
|
+
if (upper.includes("NETWORK CONNECTION LOST") || upper.includes("CANNOT RESOLVE D1 DB DUE TO TRANSIENT ISSUE") || upper.includes("D1 DB RESET BECAUSE")) return new ConnectionError(message, innerError);
|
|
176
|
+
if (upper.includes("D1 DB IS OVERLOADED") || upper.includes("TOO MANY REQUESTS") || upper.includes("MEMORY LIMIT WOULD BE EXCEEDED")) return new InsufficientResourcesError(message, innerError);
|
|
177
|
+
if (upper.startsWith("D1_ERROR") || upper.startsWith("D1_EXEC_ERROR")) {
|
|
178
|
+
const embeddedCode = extractEmbeddedSqliteCode(message);
|
|
179
|
+
if (embeddedCode) {
|
|
180
|
+
const mapped = mapEmbeddedSqliteCode(embeddedCode, message, innerError);
|
|
181
|
+
if (mapped) return mapped;
|
|
182
|
+
}
|
|
183
|
+
return new InvalidOperationError(message, innerError);
|
|
184
|
+
}
|
|
185
|
+
const embeddedCode = extractEmbeddedSqliteCode(message);
|
|
186
|
+
if (embeddedCode) {
|
|
187
|
+
const mapped = mapEmbeddedSqliteCode(embeddedCode, message, innerError);
|
|
188
|
+
if (mapped) return mapped;
|
|
189
|
+
}
|
|
190
|
+
return new DumboError({
|
|
191
|
+
errorCode: 500,
|
|
192
|
+
message,
|
|
193
|
+
innerError
|
|
194
|
+
});
|
|
239
195
|
};
|
|
240
196
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/storage/sqlite/d1/execute/d1SqlExecutor.ts
|
|
199
|
+
const d1SQLExecutor = () => ({
|
|
200
|
+
driverType: "SQLite:d1",
|
|
201
|
+
formatter: sqliteFormatter,
|
|
202
|
+
query: async (client, sql, options) => {
|
|
203
|
+
try {
|
|
204
|
+
return await client.query(sql, options);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
tracer.error("db:sql:query:execute:error", { error });
|
|
207
|
+
throw mapD1Error(error);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
batchQuery: async (client, sqls, options) => {
|
|
211
|
+
try {
|
|
212
|
+
return await client.batchQuery(sqls, options);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
tracer.error("db:sql:batch_query:execute:error", { error });
|
|
215
|
+
throw mapD1Error(error);
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
command: async (client, sql, options) => {
|
|
219
|
+
try {
|
|
220
|
+
return await client.command(sql, options);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
tracer.error("db:sql:command:execute:error", { error });
|
|
223
|
+
throw mapD1Error(error);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
batchCommand: async (client, sqls, options) => {
|
|
227
|
+
try {
|
|
228
|
+
return await client.batchCommand(sqls, options);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
tracer.error("db:sql:batch_command:execute:error", { error });
|
|
231
|
+
throw mapD1Error(error);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
277
234
|
});
|
|
278
235
|
|
|
279
|
-
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/storage/sqlite/d1/transactions/d1Transaction.ts
|
|
280
238
|
var D1TransactionNotSupportedError = class extends Error {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
this.name = "D1TransactionNotSupportedError";
|
|
286
|
-
}
|
|
239
|
+
constructor() {
|
|
240
|
+
super("D1 does not support SQL transactions (BEGIN/COMMIT/ROLLBACK/SAVEPOINT). Use { mode: \"session_based\" } to opt-in to session+batch semantics, or use connection.execute.batchCommand() for atomic multi-statement execution.");
|
|
241
|
+
this.name = "D1TransactionNotSupportedError";
|
|
242
|
+
}
|
|
287
243
|
};
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
"Transaction has not been started. Call begin() first."
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
return Promise.resolve(sessionClient);
|
|
353
|
-
}
|
|
354
|
-
}),
|
|
355
|
-
_transactionOptions: options ?? {}
|
|
356
|
-
};
|
|
244
|
+
const d1Transaction = (connection, serializer, defaultOptions) => (getClient, options) => {
|
|
245
|
+
const transactionCounter = transactionNestingCounter();
|
|
246
|
+
const allowNestedTransactions = options?.allowNestedTransactions ?? defaultOptions?.allowNestedTransactions;
|
|
247
|
+
const mode = options?.mode ?? defaultOptions?.mode;
|
|
248
|
+
let client = null;
|
|
249
|
+
let sessionClient = null;
|
|
250
|
+
const getDatabaseClient = async () => {
|
|
251
|
+
if (client) return Promise.resolve(client);
|
|
252
|
+
client = await getClient;
|
|
253
|
+
return client;
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
connection: connection(),
|
|
257
|
+
driverType: D1DriverType,
|
|
258
|
+
begin: async function() {
|
|
259
|
+
if (mode !== "session_based") throw new D1TransactionNotSupportedError();
|
|
260
|
+
const client = await getDatabaseClient();
|
|
261
|
+
if (allowNestedTransactions) {
|
|
262
|
+
if (transactionCounter.level >= 1) {
|
|
263
|
+
transactionCounter.increment();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
transactionCounter.increment();
|
|
267
|
+
}
|
|
268
|
+
sessionClient = await client.withSession(options?.d1Session);
|
|
269
|
+
},
|
|
270
|
+
commit: async function() {
|
|
271
|
+
const client = await getDatabaseClient();
|
|
272
|
+
try {
|
|
273
|
+
if (allowNestedTransactions) {
|
|
274
|
+
if (transactionCounter.level > 1) {
|
|
275
|
+
transactionCounter.decrement();
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
transactionCounter.reset();
|
|
279
|
+
}
|
|
280
|
+
sessionClient = null;
|
|
281
|
+
} finally {
|
|
282
|
+
if (options?.close) await options?.close(client);
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
rollback: async function(error) {
|
|
286
|
+
const client = await getDatabaseClient();
|
|
287
|
+
try {
|
|
288
|
+
if (allowNestedTransactions) {
|
|
289
|
+
if (transactionCounter.level > 1) {
|
|
290
|
+
transactionCounter.decrement();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
sessionClient = null;
|
|
295
|
+
} finally {
|
|
296
|
+
if (options?.close) await options?.close(client, error);
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
execute: sqlExecutor(d1SQLExecutor(), { connect: () => {
|
|
300
|
+
if (!sessionClient) throw new Error("Transaction has not been started. Call begin() first.");
|
|
301
|
+
return Promise.resolve(sessionClient);
|
|
302
|
+
} }),
|
|
303
|
+
_transactionOptions: options ?? {}
|
|
304
|
+
};
|
|
357
305
|
};
|
|
358
306
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
return await handle(sessionConnection);
|
|
387
|
-
} finally {
|
|
388
|
-
await sessionConnection.close();
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
return connection;
|
|
307
|
+
//#endregion
|
|
308
|
+
//#region src/storage/sqlite/d1/connections/d1Connection.ts
|
|
309
|
+
const D1DriverType = "SQLite:d1";
|
|
310
|
+
const d1Connection = (options) => {
|
|
311
|
+
const connection = options.connection ?? options.transaction?.connection ?? { ...sqliteAmbientClientConnection({
|
|
312
|
+
driverType: "SQLite:d1",
|
|
313
|
+
client: options.client ?? d1Client(options),
|
|
314
|
+
initTransaction: (connection) => d1Transaction(connection, options.serializer, options.transactionOptions),
|
|
315
|
+
serializer: options.serializer,
|
|
316
|
+
errorMapper: mapD1Error
|
|
317
|
+
}) };
|
|
318
|
+
connection.d1Session = async (constraintOrBookmark) => {
|
|
319
|
+
const sessionClient = await (await connection.open()).withSession(constraintOrBookmark);
|
|
320
|
+
return d1Connection({
|
|
321
|
+
...options,
|
|
322
|
+
client: sessionClient
|
|
323
|
+
});
|
|
324
|
+
};
|
|
325
|
+
connection.withD1Session = async (handle, options) => {
|
|
326
|
+
const sessionConnection = await connection.d1Session(options);
|
|
327
|
+
try {
|
|
328
|
+
return await handle(sessionConnection);
|
|
329
|
+
} finally {
|
|
330
|
+
await sessionConnection.close();
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
return connection;
|
|
392
334
|
};
|
|
393
335
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
336
|
+
//#endregion
|
|
337
|
+
//#region src/storage/sqlite/d1/pool/d1ConnectionPool.ts
|
|
338
|
+
const d1Pool = (options) => createSingletonConnectionPool({
|
|
339
|
+
driverType: D1DriverType,
|
|
340
|
+
getConnection: () => d1Connection({
|
|
341
|
+
...options,
|
|
342
|
+
serializer: JSONSerializer.from(options)
|
|
343
|
+
})
|
|
401
344
|
});
|
|
402
345
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
346
|
+
//#endregion
|
|
347
|
+
//#region src/storage/sqlite/d1/index.ts
|
|
348
|
+
const d1DumboDriver = {
|
|
349
|
+
driverType: D1DriverType,
|
|
350
|
+
createPool: (options) => d1Pool(options),
|
|
351
|
+
sqlFormatter: sqliteFormatter,
|
|
352
|
+
defaultMigratorOptions: DefaultSQLiteMigratorOptions,
|
|
353
|
+
canHandle: (options) => {
|
|
354
|
+
return options.driverType === "SQLite:d1" && "database" in options;
|
|
355
|
+
},
|
|
356
|
+
databaseMetadata: sqliteMetadata
|
|
413
357
|
};
|
|
414
|
-
|
|
415
|
-
|
|
358
|
+
const useD1DumboDriver = () => {
|
|
359
|
+
dumboDatabaseDriverRegistry.register(D1DriverType, d1DumboDriver);
|
|
416
360
|
};
|
|
417
361
|
useD1DumboDriver();
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
DEFAULT_SQLITE_PRAGMA_OPTIONS,
|
|
422
|
-
DefaultSQLiteMigratorOptions,
|
|
423
|
-
InMemorySQLiteDatabase,
|
|
424
|
-
SQLiteConnectionString,
|
|
425
|
-
SQLiteDatabaseName,
|
|
426
|
-
d1Client,
|
|
427
|
-
d1Connection,
|
|
428
|
-
d1DumboDriver,
|
|
429
|
-
d1Pool,
|
|
430
|
-
d1SQLExecutor,
|
|
431
|
-
d1Transaction,
|
|
432
|
-
defaultSQLiteDatabase,
|
|
433
|
-
isInMemoryDatabase,
|
|
434
|
-
isSQLiteError,
|
|
435
|
-
mapD1Error,
|
|
436
|
-
mapSqliteError,
|
|
437
|
-
parsePragmasFromConnectionString,
|
|
438
|
-
sqliteAlwaysNewConnectionPool,
|
|
439
|
-
sqliteAmbientClientConnection,
|
|
440
|
-
sqliteAmbientConnectionPool,
|
|
441
|
-
sqliteClientConnection,
|
|
442
|
-
sqliteConnection,
|
|
443
|
-
sqliteExecute,
|
|
444
|
-
sqliteFormatter,
|
|
445
|
-
sqliteMetadata,
|
|
446
|
-
sqlitePool,
|
|
447
|
-
sqlitePoolClientConnection,
|
|
448
|
-
sqliteSQLExecutor,
|
|
449
|
-
sqliteSingletonConnectionPool,
|
|
450
|
-
sqliteTransaction,
|
|
451
|
-
tableExists,
|
|
452
|
-
toSqlitePoolOptions,
|
|
453
|
-
transactionNestingCounter,
|
|
454
|
-
useD1DumboDriver
|
|
455
|
-
};
|
|
362
|
+
|
|
363
|
+
//#endregion
|
|
364
|
+
export { D1DriverType, D1TransactionNotSupportedError, DEFAULT_SQLITE_PRAGMA_OPTIONS, DefaultSQLiteMigratorOptions, InMemorySQLiteDatabase, SQLiteConnectionString, SQLiteDatabaseName, d1Client, d1Connection, d1DumboDriver, d1Pool, d1SQLExecutor, d1Transaction, defaultSQLiteDatabase, isInMemoryDatabase, isSQLiteError, mapD1Error, mapSqliteError, parsePragmasFromConnectionString, sqliteAlwaysNewConnectionPool, sqliteAmbientClientConnection, sqliteAmbientConnectionPool, sqliteClientConnection, sqliteConnection, sqliteExecute, sqliteFormatter, sqliteMetadata, sqlitePool, sqlitePoolClientConnection, sqliteSQLExecutor, sqliteSingletonConnectionPool, sqliteTransaction, tableExists, toSqlitePoolOptions, transactionNestingCounter, useD1DumboDriver };
|
|
456
365
|
//# sourceMappingURL=cloudflare.js.map
|