@event-driven-io/dumbo 0.13.0-beta.35 → 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.
Files changed (87) hide show
  1. package/dist/cloudflare.cjs +379 -434
  2. package/dist/cloudflare.cjs.map +1 -1
  3. package/dist/cloudflare.d.cts +36 -29
  4. package/dist/cloudflare.d.ts +36 -29
  5. package/dist/cloudflare.js +343 -434
  6. package/dist/cloudflare.js.map +1 -1
  7. package/dist/core-BeyC5tHn.js +519 -0
  8. package/dist/core-BeyC5tHn.js.map +1 -0
  9. package/dist/core-Bf2ujUFy.js +379 -0
  10. package/dist/core-Bf2ujUFy.js.map +1 -0
  11. package/dist/core-BgYtAxCm.cjs +3237 -0
  12. package/dist/core-BgYtAxCm.cjs.map +1 -0
  13. package/dist/core-DDo4m0Z_.js +2262 -0
  14. package/dist/core-DDo4m0Z_.js.map +1 -0
  15. package/dist/core-DmbB4qMn.cjs +656 -0
  16. package/dist/core-DmbB4qMn.cjs.map +1 -0
  17. package/dist/core-Go_IA78L.cjs +450 -0
  18. package/dist/core-Go_IA78L.cjs.map +1 -0
  19. package/dist/{connectionString-Dch_MIRU.d.cts → index-BDSQvDH2.d.cts} +1010 -922
  20. package/dist/index-BdWFmDhJ.d.cts +68 -0
  21. package/dist/index-D1jryNqo.d.ts +198 -0
  22. package/dist/index-DaP2fTEX.d.ts +67 -0
  23. package/dist/index-DqVvUav9.d.cts +198 -0
  24. package/dist/index-S_wp0Eaf.d.cts +67 -0
  25. package/dist/index-cxtbLiju.d.ts +68 -0
  26. package/dist/{connectionString-Dch_MIRU.d.ts → index-zkszkJqP.d.ts} +1010 -922
  27. package/dist/index.cjs +212 -359
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +3 -132
  30. package/dist/index.d.ts +3 -132
  31. package/dist/index.js +52 -361
  32. package/dist/index.js.map +1 -1
  33. package/dist/pg.cjs +366 -431
  34. package/dist/pg.cjs.map +1 -1
  35. package/dist/pg.d.cts +83 -72
  36. package/dist/pg.d.ts +83 -72
  37. package/dist/pg.js +320 -431
  38. package/dist/pg.js.map +1 -1
  39. package/dist/postgreSQLMetadata-LqX-Z_wo.cjs +118 -0
  40. package/dist/postgreSQLMetadata-LqX-Z_wo.cjs.map +1 -0
  41. package/dist/postgreSQLMetadata-zMq8yJo_.js +65 -0
  42. package/dist/postgreSQLMetadata-zMq8yJo_.js.map +1 -0
  43. package/dist/postgresql.cjs +25 -50
  44. package/dist/postgresql.d.cts +3 -61
  45. package/dist/postgresql.d.ts +3 -61
  46. package/dist/postgresql.js +4 -50
  47. package/dist/sqlite.cjs +30 -60
  48. package/dist/sqlite.d.cts +3 -190
  49. package/dist/sqlite.d.ts +3 -190
  50. package/dist/sqlite.js +4 -60
  51. package/dist/sqlite3.cjs +312 -380
  52. package/dist/sqlite3.cjs.map +1 -1
  53. package/dist/sqlite3.d.cts +79 -77
  54. package/dist/sqlite3.d.ts +79 -77
  55. package/dist/sqlite3.js +276 -378
  56. package/dist/sqlite3.js.map +1 -1
  57. package/dist/sqliteMetadata-BoQ7Sc9I.js +29 -0
  58. package/dist/sqliteMetadata-BoQ7Sc9I.js.map +1 -0
  59. package/dist/sqliteMetadata-D_2pKeCC.cjs +46 -0
  60. package/dist/sqliteMetadata-D_2pKeCC.cjs.map +1 -0
  61. package/package.json +12 -12
  62. package/dist/chunk-33P5766L.js +0 -83
  63. package/dist/chunk-33P5766L.js.map +0 -1
  64. package/dist/chunk-4MMMEDQ7.cjs +0 -2944
  65. package/dist/chunk-4MMMEDQ7.cjs.map +0 -1
  66. package/dist/chunk-6HAHSSPW.js +0 -701
  67. package/dist/chunk-6HAHSSPW.js.map +0 -1
  68. package/dist/chunk-GNH5XC6D.cjs +0 -481
  69. package/dist/chunk-GNH5XC6D.cjs.map +0 -1
  70. package/dist/chunk-H2JBS7HM.js +0 -2944
  71. package/dist/chunk-H2JBS7HM.js.map +0 -1
  72. package/dist/chunk-JIZGCEPE.cjs +0 -701
  73. package/dist/chunk-JIZGCEPE.cjs.map +0 -1
  74. package/dist/chunk-NJI6PJFZ.cjs +0 -83
  75. package/dist/chunk-NJI6PJFZ.cjs.map +0 -1
  76. package/dist/chunk-RQ3KKMTP.cjs +0 -34
  77. package/dist/chunk-RQ3KKMTP.cjs.map +0 -1
  78. package/dist/chunk-UK7MXVS2.js +0 -34
  79. package/dist/chunk-UK7MXVS2.js.map +0 -1
  80. package/dist/chunk-Y5TD53QE.js +0 -481
  81. package/dist/chunk-Y5TD53QE.js.map +0 -1
  82. package/dist/columnProcessors-Be2uMYjS.d.cts +0 -8
  83. package/dist/columnProcessors-D0ivj_SU.d.ts +0 -8
  84. package/dist/postgresql.cjs.map +0 -1
  85. package/dist/postgresql.js.map +0 -1
  86. package/dist/sqlite.cjs.map +0 -1
  87. package/dist/sqlite.js.map +0 -1
@@ -1,456 +1,365 @@
1
- import {
2
- DEFAULT_SQLITE_PRAGMA_OPTIONS,
3
- DefaultSQLiteMigratorOptions,
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
- // src/storage/sqlite/d1/connections/d1Client.ts
55
- var d1Client = (options) => {
56
- const { database, session, serializer } = options;
57
- const execute = session ?? database;
58
- return {
59
- database,
60
- session,
61
- connect: () => Promise.resolve(),
62
- close: () => Promise.resolve(),
63
- withSession: async (constraintOrBookmark) => {
64
- const newSession = constraintOrBookmark ? database.withSession(constraintOrBookmark) : database.withSession();
65
- return Promise.resolve(
66
- d1Client({
67
- database,
68
- session: newSession,
69
- serializer
70
- })
71
- );
72
- },
73
- query: async (sql, _options) => {
74
- const { query, params } = sqliteFormatter.format(sql, { serializer });
75
- const stmt = execute.prepare(query);
76
- const bound = params?.length ? stmt.bind(...params) : stmt;
77
- const { results } = await bound.all();
78
- return { rowCount: results?.length ?? 0, rows: results ?? [] };
79
- },
80
- batchQuery: async (sqls, _options) => {
81
- const statements = sqls.map((sql) => {
82
- const { query, params } = sqliteFormatter.format(sql, { serializer });
83
- const stmt = execute.prepare(query);
84
- return params?.length ? stmt.bind(...params) : stmt;
85
- });
86
- const results = await execute.batch(statements);
87
- return results.map((result) => ({
88
- rowCount: result.results?.length ?? 0,
89
- rows: result.results ?? []
90
- }));
91
- },
92
- command: async (sql, _options) => {
93
- const { query, params } = sqliteFormatter.format(sql, { serializer });
94
- const stmt = execute.prepare(query);
95
- const bound = params?.length ? stmt.bind(...params) : stmt;
96
- const result = await bound.run();
97
- return {
98
- rowCount: result.meta?.changes ?? 0,
99
- rows: result.results ?? []
100
- };
101
- },
102
- batchCommand: async (sqls, options2) => {
103
- const statements = sqls.map((sql) => {
104
- const { query, params } = sqliteFormatter.format(sql, { serializer });
105
- const stmt = execute.prepare(query);
106
- return params?.length ? stmt.bind(...params) : stmt;
107
- });
108
- const batchResults = await execute.batch(statements);
109
- return batchResults.map((result, i) => {
110
- const qr = {
111
- rowCount: result.meta?.changes ?? 0,
112
- rows: result.results ?? []
113
- };
114
- if (options2?.assertChanges && (qr.rowCount ?? 0) === 0) {
115
- throw new BatchCommandNoChangesError(i);
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
- // src/storage/sqlite/d1/errors/errorMapper.ts
124
- var getErrorMessage = (error) => error instanceof Error ? error.message : void 0;
125
- var asError = (error) => error instanceof Error ? error : void 0;
126
- var mapConstraintError = (message, innerError) => {
127
- const upperMessage = message?.toUpperCase() ?? "";
128
- if (upperMessage.includes("UNIQUE") || upperMessage.includes("PRIMARY KEY"))
129
- return new UniqueConstraintError(message, innerError);
130
- if (upperMessage.includes("FOREIGN KEY"))
131
- return new ForeignKeyViolationError(message, innerError);
132
- if (upperMessage.includes("NOT NULL"))
133
- return new NotNullViolationError(message, innerError);
134
- if (upperMessage.includes("CHECK"))
135
- return new CheckViolationError(message, innerError);
136
- return new IntegrityConstraintViolationError(message, innerError);
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
- var isConstraintMessage = (upper) => upper.includes("CONSTRAINT") || upper.includes("UNIQUE") || upper.includes("PRIMARY KEY") || upper.includes("FOREIGN KEY") || upper.includes("NOT NULL");
139
- var SQLITE_CODE_RE = /SQLITE_([A-Z]+)/;
140
- var extractEmbeddedSqliteCode = (message) => {
141
- const match = SQLITE_CODE_RE.exec(message);
142
- return match ? `SQLITE_${match[1]}` : void 0;
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
- var mapEmbeddedSqliteCode = (code, message, innerError) => {
145
- switch (code) {
146
- case "SQLITE_CONSTRAINT":
147
- return mapConstraintError(message, innerError);
148
- case "SQLITE_BUSY":
149
- return new LockNotAvailableError(message, innerError);
150
- case "SQLITE_LOCKED":
151
- return new DeadlockError(message, innerError);
152
- case "SQLITE_PROTOCOL":
153
- return new LockNotAvailableError(message, innerError);
154
- case "SQLITE_CANTOPEN":
155
- return new ConnectionError(message, innerError);
156
- case "SQLITE_NOTADB":
157
- return new ConnectionError(message, innerError);
158
- case "SQLITE_NOMEM":
159
- return new InsufficientResourcesError(message, innerError);
160
- case "SQLITE_FULL":
161
- return new InsufficientResourcesError(message, innerError);
162
- case "SQLITE_IOERR":
163
- return new SystemError(message, innerError);
164
- case "SQLITE_CORRUPT":
165
- return new SystemError(message, innerError);
166
- case "SQLITE_INTERNAL":
167
- return new SystemError(message, innerError);
168
- case "SQLITE_NOLFS":
169
- return new SystemError(message, innerError);
170
- case "SQLITE_TOOBIG":
171
- return new DataError(message, innerError);
172
- case "SQLITE_MISMATCH":
173
- return new DataError(message, innerError);
174
- case "SQLITE_RANGE":
175
- return new DataError(message, innerError);
176
- case "SQLITE_ERROR":
177
- return new InvalidOperationError(message, innerError);
178
- case "SQLITE_READONLY":
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
- var mapD1Error = (error) => {
196
- if (DumboError.isInstanceOf(error)) return error;
197
- const message = getErrorMessage(error);
198
- if (!message)
199
- return new DumboError({
200
- errorCode: 500,
201
- message: error instanceof Error ? error.message : String(error),
202
- innerError: asError(error)
203
- });
204
- const innerError = asError(error);
205
- const upper = message.toUpperCase();
206
- if (upper.startsWith("D1_TYPE_ERROR"))
207
- return new DataError(message, innerError);
208
- if (upper.startsWith("D1_COLUMN_NOTFOUND"))
209
- return new DataError(message, innerError);
210
- if (upper.startsWith("D1_DUMP_ERROR"))
211
- return new SystemError(message, innerError);
212
- if (upper.startsWith("D1_SESSION_ERROR"))
213
- return new ConnectionError(message, innerError);
214
- if (isConstraintMessage(upper)) {
215
- return mapConstraintError(message, innerError);
216
- }
217
- if (upper.includes("NETWORK CONNECTION LOST") || upper.includes("CANNOT RESOLVE D1 DB DUE TO TRANSIENT ISSUE") || upper.includes("D1 DB RESET BECAUSE"))
218
- return new ConnectionError(message, innerError);
219
- if (upper.includes("D1 DB IS OVERLOADED") || upper.includes("TOO MANY REQUESTS") || upper.includes("MEMORY LIMIT WOULD BE EXCEEDED"))
220
- return new InsufficientResourcesError(message, innerError);
221
- if (upper.startsWith("D1_ERROR") || upper.startsWith("D1_EXEC_ERROR")) {
222
- const embeddedCode2 = extractEmbeddedSqliteCode(message);
223
- if (embeddedCode2) {
224
- const mapped = mapEmbeddedSqliteCode(embeddedCode2, message, innerError);
225
- if (mapped) return mapped;
226
- }
227
- return new InvalidOperationError(message, innerError);
228
- }
229
- const embeddedCode = extractEmbeddedSqliteCode(message);
230
- if (embeddedCode) {
231
- const mapped = mapEmbeddedSqliteCode(embeddedCode, message, innerError);
232
- if (mapped) return mapped;
233
- }
234
- return new DumboError({
235
- errorCode: 500,
236
- message,
237
- innerError
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
- // src/storage/sqlite/d1/execute/d1SqlExecutor.ts
242
- var d1SQLExecutor = () => ({
243
- driverType: "SQLite:d1",
244
- formatter: sqliteFormatter,
245
- query: async (client, sql, options) => {
246
- try {
247
- return await client.query(sql, options);
248
- } catch (error) {
249
- tracer.error("db:sql:query:execute:error", { error });
250
- throw mapD1Error(error);
251
- }
252
- },
253
- batchQuery: async (client, sqls, options) => {
254
- try {
255
- return await client.batchQuery(sqls, options);
256
- } catch (error) {
257
- tracer.error("db:sql:batch_query:execute:error", { error });
258
- throw mapD1Error(error);
259
- }
260
- },
261
- command: async (client, sql, options) => {
262
- try {
263
- return await client.command(sql, options);
264
- } catch (error) {
265
- tracer.error("db:sql:command:execute:error", { error });
266
- throw mapD1Error(error);
267
- }
268
- },
269
- batchCommand: async (client, sqls, options) => {
270
- try {
271
- return await client.batchCommand(sqls, options);
272
- } catch (error) {
273
- tracer.error("db:sql:batch_command:execute:error", { error });
274
- throw mapD1Error(error);
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
- // src/storage/sqlite/d1/transactions/d1Transaction.ts
236
+ //#endregion
237
+ //#region src/storage/sqlite/d1/transactions/d1Transaction.ts
280
238
  var D1TransactionNotSupportedError = class extends Error {
281
- constructor() {
282
- super(
283
- '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.'
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
- var d1Transaction = (connection, serializer, defaultOptions) => (getClient, options) => {
289
- const transactionCounter = transactionNestingCounter();
290
- const allowNestedTransactions = options?.allowNestedTransactions ?? defaultOptions?.allowNestedTransactions;
291
- const mode = options?.mode ?? defaultOptions?.mode;
292
- let client = null;
293
- let sessionClient = null;
294
- const getDatabaseClient = async () => {
295
- if (client) return Promise.resolve(client);
296
- client = await getClient;
297
- return client;
298
- };
299
- return {
300
- connection: connection(),
301
- driverType: D1DriverType,
302
- begin: async function() {
303
- if (mode !== "session_based") {
304
- throw new D1TransactionNotSupportedError();
305
- }
306
- const client2 = await getDatabaseClient();
307
- if (allowNestedTransactions) {
308
- if (transactionCounter.level >= 1) {
309
- transactionCounter.increment();
310
- return;
311
- }
312
- transactionCounter.increment();
313
- }
314
- sessionClient = await client2.withSession(options?.d1Session);
315
- },
316
- commit: async function() {
317
- const client2 = await getDatabaseClient();
318
- try {
319
- if (allowNestedTransactions) {
320
- if (transactionCounter.level > 1) {
321
- transactionCounter.decrement();
322
- return;
323
- }
324
- transactionCounter.reset();
325
- }
326
- sessionClient = null;
327
- } finally {
328
- if (options?.close) await options?.close(client2);
329
- }
330
- },
331
- rollback: async function(error) {
332
- const client2 = await getDatabaseClient();
333
- try {
334
- if (allowNestedTransactions) {
335
- if (transactionCounter.level > 1) {
336
- transactionCounter.decrement();
337
- return;
338
- }
339
- }
340
- sessionClient = null;
341
- } finally {
342
- if (options?.close) await options?.close(client2, error);
343
- }
344
- },
345
- execute: sqlExecutor(d1SQLExecutor(), {
346
- connect: () => {
347
- if (!sessionClient) {
348
- throw new Error(
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
- // src/storage/sqlite/d1/connections/d1Connection.ts
360
- var D1DriverType = "SQLite:d1";
361
- var d1Connection = (options) => {
362
- const connection = options.connection ?? options.transaction?.connection ?? {
363
- ...sqliteAmbientClientConnection({
364
- driverType: D1DriverType,
365
- client: options.client ?? d1Client(options),
366
- initTransaction: (connection2) => d1Transaction(
367
- connection2,
368
- options.serializer,
369
- options.transactionOptions
370
- ),
371
- serializer: options.serializer,
372
- errorMapper: mapD1Error
373
- })
374
- };
375
- connection.d1Session = async (constraintOrBookmark) => {
376
- const client = await connection.open();
377
- const sessionClient = await client.withSession(constraintOrBookmark);
378
- return d1Connection({
379
- ...options,
380
- client: sessionClient
381
- });
382
- };
383
- connection.withD1Session = async (handle, options2) => {
384
- const sessionConnection = await connection.d1Session(options2);
385
- try {
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
- // src/storage/sqlite/d1/pool/d1ConnectionPool.ts
395
- var d1Pool = (options) => createSingletonConnectionPool({
396
- driverType: D1DriverType,
397
- getConnection: () => d1Connection({
398
- ...options,
399
- serializer: JSONSerializer.from(options)
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
- // src/storage/sqlite/d1/index.ts
404
- var d1DumboDriver = {
405
- driverType: D1DriverType,
406
- createPool: (options) => d1Pool(options),
407
- sqlFormatter: sqliteFormatter,
408
- defaultMigratorOptions: DefaultSQLiteMigratorOptions,
409
- canHandle: (options) => {
410
- return options.driverType === D1DriverType && "database" in options;
411
- },
412
- databaseMetadata: sqliteMetadata
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
- var useD1DumboDriver = () => {
415
- dumboDatabaseDriverRegistry.register(D1DriverType, d1DumboDriver);
358
+ const useD1DumboDriver = () => {
359
+ dumboDatabaseDriverRegistry.register(D1DriverType, d1DumboDriver);
416
360
  };
417
361
  useD1DumboDriver();
418
- export {
419
- D1DriverType,
420
- D1TransactionNotSupportedError,
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