@nextlyhq/adapter-sqlite 0.0.2-alpha.7 → 0.0.2-alpha.70

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/index.mjs CHANGED
@@ -1,567 +1,892 @@
1
- import { DrizzleAdapter } from '@nextlyhq/adapter-drizzle';
2
- import { createDatabaseError, isDatabaseError } from '@nextlyhq/adapter-drizzle/types';
3
- import { checkDialectVersion } from '@nextlyhq/adapter-drizzle/version-check';
4
- import { drizzle } from 'drizzle-orm/better-sqlite3';
5
-
1
+ import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
2
+ import { DrizzleAdapter } from "@nextlyhq/adapter-drizzle";
3
+ import { createDatabaseError, isApplicationError, isDatabaseError, poolWaitTimeoutError } from "@nextlyhq/adapter-drizzle/types";
4
+ import { checkDialectVersion } from "@nextlyhq/adapter-drizzle/version-check";
5
+ import { drizzle } from "drizzle-orm/better-sqlite3";
6
+ import { getTableConfig } from "drizzle-orm/sqlite-core";
6
7
  // src/index.ts
7
- var VERSION = "0.1.0";
8
- var SQLITE_ERROR_CODES = {
9
- // Constraint violations
10
- SQLITE_CONSTRAINT: "constraint",
11
- SQLITE_CONSTRAINT_UNIQUE: "unique_violation",
12
- SQLITE_CONSTRAINT_PRIMARYKEY: "unique_violation",
13
- SQLITE_CONSTRAINT_FOREIGNKEY: "foreign_key_violation",
14
- SQLITE_CONSTRAINT_NOTNULL: "not_null_violation",
15
- SQLITE_CONSTRAINT_CHECK: "check_violation",
16
- // Busy/locked errors
17
- SQLITE_BUSY: "timeout",
18
- SQLITE_LOCKED: "timeout",
19
- // Connection errors
20
- SQLITE_CANTOPEN: "connection",
21
- SQLITE_NOTADB: "connection",
22
- SQLITE_CORRUPT: "connection",
23
- // Query errors
24
- SQLITE_ERROR: "query",
25
- SQLITE_MISUSE: "query",
26
- SQLITE_RANGE: "query"
8
+ /**
9
+ * @nextlyhq/adapter-sqlite
10
+ *
11
+ * SQLite database adapter for Nextly.
12
+ * Extends DrizzleAdapter from @nextlyhq/adapter-drizzle to provide SQLite-specific functionality.
13
+ *
14
+ * @remarks
15
+ * This adapter uses the better-sqlite3 package for database connectivity and provides:
16
+ * - Synchronous API wrapped for async interface compatibility
17
+ * - Transactions the adapter runs itself (BEGIN IMMEDIATE / COMMIT / ROLLBACK), with nested calls run as savepoints
18
+ * - CRUD operations with RETURNING clause support (SQLite 3.35+)
19
+ * - WAL mode for better concurrent read performance
20
+ * - In-memory and file-based database support
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * import { createSqliteAdapter } from '@nextlyhq/adapter-sqlite';
25
+ *
26
+ * const adapter = createSqliteAdapter({
27
+ * url: 'file:./data/app.db',
28
+ * });
29
+ *
30
+ * await adapter.connect();
31
+ *
32
+ * // Query data
33
+ * const users = await adapter.select('users', {
34
+ * where: { and: [{ column: 'status', op: '=', value: 'active' }] },
35
+ * limit: 10,
36
+ * });
37
+ *
38
+ * await adapter.disconnect();
39
+ * ```
40
+ *
41
+ * @packageDocumentation
42
+ */
43
+ /**
44
+ * Package version
45
+ */
46
+ const VERSION = "0.1.0";
47
+ /**
48
+ * SQLite error codes mapping to DatabaseErrorKind.
49
+ *
50
+ * @see https://sqlite.org/rescode.html
51
+ */
52
+ const SQLITE_ERROR_CODES = {
53
+ SQLITE_CONSTRAINT: "constraint",
54
+ SQLITE_CONSTRAINT_UNIQUE: "unique_violation",
55
+ SQLITE_CONSTRAINT_PRIMARYKEY: "unique_violation",
56
+ SQLITE_CONSTRAINT_FOREIGNKEY: "foreign_key_violation",
57
+ SQLITE_CONSTRAINT_NOTNULL: "not_null_violation",
58
+ SQLITE_CONSTRAINT_CHECK: "check_violation",
59
+ SQLITE_BUSY: "timeout",
60
+ SQLITE_LOCKED: "timeout",
61
+ SQLITE_CANTOPEN: "connection",
62
+ SQLITE_NOTADB: "connection",
63
+ SQLITE_CORRUPT: "connection",
64
+ SQLITE_ERROR: "query",
65
+ SQLITE_MISUSE: "query",
66
+ SQLITE_RANGE: "query"
27
67
  };
28
- var DEFAULT_CONFIG = {
29
- busyTimeout: 5e3,
30
- wal: true,
31
- foreignKeys: true
68
+ /**
69
+ * Default configuration values.
70
+ */
71
+ const DEFAULT_CONFIG = {
72
+ busyTimeout: 5e3,
73
+ wal: true,
74
+ foreignKeys: true,
75
+ connectionTimeoutMs: 15e3
32
76
  };
77
+ /**
78
+ * Converts a JavaScript value to a type that better-sqlite3 can bind.
79
+ * better-sqlite3 only accepts: number, string, bigint, Buffer, null.
80
+ *
81
+ * These conversions have to match what the schema declares, because the rows
82
+ * they write are read back through it. Every column here that takes a `Date`
83
+ * is `integer({ mode: "timestamp" })` — the system columns and user date
84
+ * fields in `runtime-schema-generator.ts`, and the core tables, whose date
85
+ * columns are all INTEGER. So a date is bound as unix seconds, which is what
86
+ * that mode reads.
87
+ */
33
88
  function sanitizeSqliteValue(v) {
34
- if (v === void 0) return null;
35
- if (typeof v === "boolean") return v ? 1 : 0;
36
- if (v instanceof Date) return v.toISOString();
37
- if (v !== null && typeof v === "object") return JSON.stringify(v);
38
- return v;
89
+ if (v === void 0) return null;
90
+ if (typeof v === "boolean") return v ? 1 : 0;
91
+ if (Buffer.isBuffer(v) || ArrayBuffer.isView(v)) return v;
92
+ if (v instanceof Date) return Math.floor(v.getTime() / 1e3);
93
+ if (v !== null && typeof v === "object") return JSON.stringify(v);
94
+ return v;
39
95
  }
96
+ /**
97
+ * SQLite database adapter for Nextly.
98
+ *
99
+ * Extends the base DrizzleAdapter to provide SQLite-specific functionality
100
+ * using the better-sqlite3 package.
101
+ *
102
+ * @remarks
103
+ * SQLite has some differences from PostgreSQL:
104
+ * - Synchronous API (wrapped for async compatibility)
105
+ * - No connection pooling (single file/connection)
106
+ * - RETURNING clause supported (SQLite 3.35+)
107
+ * - Savepoints supported via nested transactions
108
+ * - No native ILIKE (uses LOWER() LIKE workaround)
109
+ * - JSON support (not JSONB)
110
+ * - No array types
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * const adapter = new SqliteAdapter({
115
+ * url: 'file:./data.db',
116
+ * });
117
+ *
118
+ * await adapter.connect();
119
+ * ```
120
+ */
40
121
  var SqliteAdapter = class extends DrizzleAdapter {
41
- /**
42
- * The database dialect - always 'sqlite' for this adapter.
43
- */
44
- dialect = "sqlite";
45
- /**
46
- * Adapter configuration.
47
- */
48
- config;
49
- /**
50
- * better-sqlite3 Database instance.
51
- */
52
- db = null;
53
- /**
54
- * Connection state flag.
55
- */
56
- connected = false;
57
- /**
58
- * Serialization queue for concurrent `transaction()` calls.
59
- *
60
- * better-sqlite3 is synchronous, single-connection, and rejects nested
61
- * `BEGIN` with "cannot start a transaction within a transaction".
62
- * When two `await adapter.transaction(...)` calls overlap (e.g. a bulk
63
- * mutation calling `Promise.allSettled` with N per-row updates), the
64
- * second one tries to BEGIN while the first is still open and the
65
- * driver throws.
66
- *
67
- * Postgres/MySQL avoid this by allocating a fresh client/connection
68
- * per transaction from a pool — there is no equivalent for
69
- * better-sqlite3, so we serialize at the JS layer instead. Every
70
- * `transaction()` invocation chains onto this promise; only one
71
- * BEGIN→COMMIT/ROLLBACK section runs at any moment, preserving
72
- * write-isolation semantics that callers expect from a transactional
73
- * adapter.
74
- *
75
- * Performance impact is the natural floor: SQLite already serializes
76
- * writers at the file-lock level, so the queue removes failed-call
77
- * surface area without changing the achievable concurrency.
78
- */
79
- transactionQueue = Promise.resolve();
80
- /**
81
- * Creates a new SQLite adapter instance.
82
- *
83
- * @param config - Adapter configuration
84
- */
85
- constructor(config) {
86
- super();
87
- this.config = config;
88
- }
89
- /**
90
- * Connect to the SQLite database.
91
- *
92
- * @remarks
93
- * This method initializes the database connection. For SQLite, this
94
- * opens the database file or creates an in-memory database.
95
- * Also configures WAL mode and foreign keys based on config.
96
- *
97
- * @throws {DatabaseError} If connection fails
98
- */
99
- async connect() {
100
- if (this.connected && this.db) {
101
- return;
102
- }
103
- try {
104
- const BetterSqlite3 = await import('better-sqlite3');
105
- const Database = BetterSqlite3.default;
106
- let dbPath;
107
- if (this.config.memory) {
108
- dbPath = ":memory:";
109
- } else if (this.config.url) {
110
- dbPath = this.config.url.replace(/^file:/, "");
111
- } else {
112
- dbPath = ":memory:";
113
- }
114
- if (dbPath !== ":memory:" && !this.config.readonly) {
115
- const fs = await import('fs');
116
- const path = await import('path');
117
- const dir = path.dirname(path.resolve(dbPath));
118
- await fs.promises.mkdir(dir, { recursive: true });
119
- }
120
- this.db = new Database(dbPath, {
121
- readonly: this.config.readonly ?? false,
122
- timeout: this.config.busyTimeout ?? DEFAULT_CONFIG.busyTimeout
123
- });
124
- if ((this.config.wal ?? DEFAULT_CONFIG.wal) && dbPath !== ":memory:" && !this.config.readonly) {
125
- this.db.pragma("journal_mode = WAL");
126
- }
127
- if (this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys) {
128
- this.db.pragma("foreign_keys = ON");
129
- }
130
- await checkDialectVersion(this.db, "sqlite");
131
- this.connected = true;
132
- if (this.config.logger?.info) {
133
- this.config.logger.info("SQLite connection established", {
134
- url: dbPath === ":memory:" ? "in-memory" : dbPath,
135
- wal: this.config.wal ?? DEFAULT_CONFIG.wal,
136
- foreignKeys: this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys
137
- });
138
- }
139
- } catch (error) {
140
- if (this.db) {
141
- try {
142
- this.db.close();
143
- } catch {
144
- }
145
- this.db = null;
146
- }
147
- throw this.classifyError(error);
148
- }
149
- }
150
- /**
151
- * Disconnect from the SQLite database.
152
- *
153
- * @remarks
154
- * This method closes the database connection and releases resources.
155
- */
156
- // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.
157
- // eslint-disable-next-line @typescript-eslint/require-await
158
- async disconnect() {
159
- if (!this.db) {
160
- return;
161
- }
162
- try {
163
- this.db.close();
164
- if (this.config.logger?.info) {
165
- this.config.logger.info("SQLite connection closed");
166
- }
167
- } finally {
168
- this.db = null;
169
- this.connected = false;
170
- }
171
- }
172
- /**
173
- * Check if connected to the database.
174
- */
175
- isConnected() {
176
- return this.connected && this.db !== null;
177
- }
178
- /**
179
- * Get connection pool statistics.
180
- *
181
- * @remarks
182
- * SQLite doesn't use connection pooling, so this returns null.
183
- * The database is single-file with a single connection.
184
- */
185
- getPoolStats() {
186
- return null;
187
- }
188
- /**
189
- * Execute a raw SQL query.
190
- *
191
- * @param sql - SQL query string with $1, $2 placeholders (converted to ? for SQLite)
192
- * @param params - Query parameters
193
- * @returns Query results
194
- *
195
- * @throws {DatabaseError} If query execution fails
196
- */
197
- // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.
198
- // eslint-disable-next-line @typescript-eslint/require-await
199
- async executeQuery(sql, params = []) {
200
- const db = this.ensureDb();
201
- const startTime = Date.now();
202
- try {
203
- const convertedSql = this.convertPlaceholders(sql);
204
- const trimmedSql = convertedSql.trim().toUpperCase();
205
- const isPragmaQuery = trimmedSql.startsWith("PRAGMA") && !trimmedSql.includes("=");
206
- const isSelect = trimmedSql.startsWith("SELECT") || isPragmaQuery || trimmedSql.startsWith("WITH");
207
- const hasReturning = trimmedSql.includes("RETURNING");
208
- let result;
209
- const sanitizedParams = params.map(sanitizeSqliteValue);
210
- if (isSelect || hasReturning) {
211
- const stmt = db.prepare(convertedSql);
212
- result = stmt.all(...sanitizedParams);
213
- } else {
214
- const stmt = db.prepare(convertedSql);
215
- const runResult = stmt.run(...sanitizedParams);
216
- result = [
217
- {
218
- changes: runResult.changes,
219
- lastInsertRowid: runResult.lastInsertRowid
220
- }
221
- ];
222
- }
223
- if (this.config.logger?.query) {
224
- const durationMs = Date.now() - startTime;
225
- this.config.logger.query(convertedSql, params, durationMs);
226
- }
227
- return result;
228
- } catch (error) {
229
- throw this.classifyError(error, sql);
230
- }
231
- }
232
- /**
233
- * Execute work within a transaction.
234
- *
235
- * @param work - Function containing transactional operations
236
- * @param options - Transaction options (isolation level not fully supported in SQLite)
237
- * @returns Result of the work function
238
- *
239
- * @remarks
240
- * Uses better-sqlite3's native transaction handling which:
241
- * - Automatically commits on success
242
- * - Automatically rolls back on error
243
- * - Converts nested transactions to savepoints
244
- *
245
- * Note: SQLite uses DEFERRED, IMMEDIATE, or EXCLUSIVE transaction modes
246
- * rather than isolation levels. This adapter uses IMMEDIATE by default.
247
- */
248
- async transaction(work, _options) {
249
- const run = async () => this.runTransaction(work);
250
- const next = this.transactionQueue.then(run, run);
251
- this.transactionQueue = next.catch(() => void 0);
252
- return next;
253
- }
254
- /**
255
- * Inner transaction body. Kept private so callers always go through
256
- * the serialized `transaction()` queue above and the BEGIN/COMMIT
257
- * pair is never invoked outside of it.
258
- */
259
- async runTransaction(work) {
260
- const db = this.ensureDb();
261
- const startTime = Date.now();
262
- try {
263
- const ctx = this.createTransactionContext(db);
264
- db.exec("BEGIN IMMEDIATE");
265
- try {
266
- const result = await work(ctx);
267
- db.exec("COMMIT");
268
- if (this.config.logger?.debug) {
269
- const durationMs = Date.now() - startTime;
270
- this.config.logger.debug("Transaction committed", {
271
- durationMs
272
- });
273
- }
274
- return result;
275
- } catch (error) {
276
- try {
277
- db.exec("ROLLBACK");
278
- } catch {
279
- }
280
- throw error;
281
- }
282
- } catch (error) {
283
- throw this.classifyError(error);
284
- }
285
- }
286
- /**
287
- * Get SQLite database capabilities.
288
- *
289
- * @remarks
290
- * SQLite capabilities:
291
- * - JSON support (not JSONB)
292
- * - No arrays
293
- * - No native ILIKE
294
- * - RETURNING clause (3.35+)
295
- * - Savepoints supported
296
- * - ON CONFLICT supported
297
- */
298
- getCapabilities() {
299
- return {
300
- dialect: "sqlite",
301
- supportsJsonb: false,
302
- // SQLite uses JSON, not JSONB
303
- supportsJson: true,
304
- supportsArrays: false,
305
- // SQLite doesn't support array types
306
- supportsGeneratedColumns: true,
307
- // SQLite 3.31+
308
- supportsFts: true,
309
- // SQLite FTS5
310
- supportsIlike: false,
311
- // No native ILIKE, use LOWER() LIKE
312
- supportsReturning: true,
313
- // SQLite 3.35+
314
- supportsSavepoints: true,
315
- // SQLite supports savepoints
316
- supportsOnConflict: true,
317
- // ON CONFLICT clause
318
- maxParamsPerQuery: 999,
319
- // SQLite SQLITE_MAX_VARIABLE_NUMBER default
320
- maxIdentifierLength: 128
321
- // SQLite doesn't have a strict limit
322
- };
323
- }
324
- /**
325
- * Override insertMany for bulk insert optimization.
326
- *
327
- * @remarks
328
- * Uses a single multi-row INSERT statement for better performance.
329
- */
330
- async insertMany(table, data, options) {
331
- if (data.length === 0) {
332
- return [];
333
- }
334
- const db = this.ensureDb();
335
- if (data.length === 1) {
336
- const result = await this.insert(table, data[0], options);
337
- return [result];
338
- }
339
- const columns = Object.keys(data[0]);
340
- const params = [];
341
- const valuesClauses = [];
342
- for (const record of data) {
343
- const placeholders = [];
344
- for (const col of columns) {
345
- params.push(sanitizeSqliteValue(record[col]));
346
- placeholders.push("?");
347
- }
348
- valuesClauses.push(`(${placeholders.join(", ")})`);
349
- }
350
- const columnList = columns.map((col) => this.escapeIdentifier(col)).join(", ");
351
- let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(", ")}`;
352
- if (options?.returning) {
353
- const returning = options.returning === "*" ? "*" : options.returning.map((col) => this.escapeIdentifier(col)).join(", ");
354
- sql += ` RETURNING ${returning}`;
355
- } else {
356
- sql += " RETURNING *";
357
- }
358
- try {
359
- const stmt = db.prepare(sql);
360
- const rows = stmt.all(...params);
361
- return rows;
362
- } catch (error) {
363
- throw this.handleQueryError(error, "insertMany", table);
364
- }
365
- }
366
- // ============================================================
367
- // Protected Helper Methods
368
- // ============================================================
369
- /**
370
- * Ensures database is connected and returns it.
371
- *
372
- * @throws {DatabaseError} If not connected
373
- */
374
- ensureDb() {
375
- if (!this.db) {
376
- throw createDatabaseError({
377
- kind: "connection",
378
- message: "SqliteAdapter is not connected. Call connect() first."
379
- });
380
- }
381
- return this.db;
382
- }
383
- /**
384
- * Return the typed Drizzle instance for SQLite.
385
- * Guarded for server-only usage and requires an active connection.
386
- *
387
- * @param schema - Optional schema for relational queries (db.query.*)
388
- * @returns Drizzle ORM instance wrapping the better-sqlite3 connection
389
- * @throws {Error} If called in browser or not connected
390
- */
391
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
392
- getDrizzle(schema) {
393
- if (typeof window !== "undefined") {
394
- throw new Error("getDrizzle() is server-only");
395
- }
396
- const db = this.ensureDb();
397
- return schema ? drizzle(db, { schema }) : drizzle(db);
398
- }
399
- /**
400
- * Convert $1, $2 placeholders to ? for better-sqlite3.
401
- *
402
- * @param sql - SQL with PostgreSQL-style placeholders
403
- * @returns SQL with ? placeholders
404
- */
405
- convertPlaceholders(sql) {
406
- return sql.replace(/\$\d+/g, "?");
407
- }
408
- /**
409
- * Creates a TransactionContext for the given database connection.
410
- */
411
- createTransactionContext(db) {
412
- return {
413
- // eslint-disable-next-line @typescript-eslint/require-await
414
- execute: async (sql, params = []) => {
415
- const convertedSql = this.convertPlaceholders(sql);
416
- const trimmedSql = convertedSql.trim().toUpperCase();
417
- const isSelect = trimmedSql.startsWith("SELECT") || trimmedSql.includes("RETURNING");
418
- const sanitizedParams = params.map(sanitizeSqliteValue);
419
- if (isSelect) {
420
- const stmt = db.prepare(convertedSql);
421
- return stmt.all(...sanitizedParams);
422
- } else {
423
- const stmt = db.prepare(convertedSql);
424
- const result = stmt.run(...sanitizedParams);
425
- return [
426
- {
427
- changes: result.changes,
428
- lastInsertRowid: result.lastInsertRowid
429
- }
430
- ];
431
- }
432
- },
433
- // eslint-disable-next-line @typescript-eslint/require-await
434
- insert: async (table, data, options) => {
435
- const columns = Object.keys(data);
436
- const values = Object.values(data).map(sanitizeSqliteValue);
437
- const placeholders = values.map(() => "?").join(", ");
438
- let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map((c) => this.escapeIdentifier(c)).join(", ")}) VALUES (${placeholders})`;
439
- if (options?.returning) {
440
- const returning = options.returning === "*" ? "*" : options.returning.map((col) => this.escapeIdentifier(col)).join(", ");
441
- sql += ` RETURNING ${returning}`;
442
- } else {
443
- sql += " RETURNING *";
444
- }
445
- const stmt = db.prepare(sql);
446
- const rows = stmt.all(...values);
447
- return rows[0];
448
- },
449
- // eslint-disable-next-line @typescript-eslint/require-await
450
- insertMany: async (table, data, options) => {
451
- if (data.length === 0) return [];
452
- const columns = Object.keys(data[0]);
453
- const allValues = [];
454
- const valuesClauses = [];
455
- for (const record of data) {
456
- const placeholders = [];
457
- for (const col of columns) {
458
- allValues.push(sanitizeSqliteValue(record[col]));
459
- placeholders.push("?");
460
- }
461
- valuesClauses.push(`(${placeholders.join(", ")})`);
462
- }
463
- let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map((c) => this.escapeIdentifier(c)).join(", ")}) VALUES ${valuesClauses.join(", ")}`;
464
- if (options?.returning) {
465
- const returning = options.returning === "*" ? "*" : options.returning.map((col) => this.escapeIdentifier(col)).join(", ");
466
- sql += ` RETURNING ${returning}`;
467
- } else {
468
- sql += " RETURNING *";
469
- }
470
- const stmt = db.prepare(sql);
471
- return stmt.all(...allValues);
472
- },
473
- // TransactionContext CRUD methods delegate to the adapter's CRUD
474
- // which uses Drizzle query API via the TableResolver.
475
- select: async (table, options) => {
476
- return this.select(table, options);
477
- },
478
- selectOne: async (table, options) => {
479
- return this.selectOne(table, options);
480
- },
481
- update: async (table, data, where, options) => {
482
- return this.update(table, data, where, options);
483
- },
484
- delete: async (table, where, _options) => {
485
- return this.delete(table, where);
486
- },
487
- upsert: async (table, data, options) => {
488
- return this.upsert(table, data, options);
489
- },
490
- // eslint-disable-next-line @typescript-eslint/require-await
491
- savepoint: async (name) => {
492
- db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);
493
- },
494
- // eslint-disable-next-line @typescript-eslint/require-await
495
- rollbackToSavepoint: async (name) => {
496
- db.exec(`ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`);
497
- },
498
- // eslint-disable-next-line @typescript-eslint/require-await
499
- releaseSavepoint: async (name) => {
500
- db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);
501
- }
502
- };
503
- }
504
- /**
505
- * Classifies a SQLite error into a DatabaseError.
506
- *
507
- * @param error - Original error from better-sqlite3
508
- * @param sql - SQL statement that caused the error (optional)
509
- * @returns DatabaseError with proper classification
510
- */
511
- classifyError(error, sql) {
512
- if (isDatabaseError(error)) return error;
513
- const sqliteError = error;
514
- let kind = "unknown";
515
- if (sqliteError.code) {
516
- kind = SQLITE_ERROR_CODES[sqliteError.code] || "unknown";
517
- } else if (sqliteError.message) {
518
- const msg = sqliteError.message.toUpperCase();
519
- if (msg.includes("UNIQUE CONSTRAINT")) {
520
- kind = "unique_violation";
521
- } else if (msg.includes("FOREIGN KEY CONSTRAINT")) {
522
- kind = "foreign_key_violation";
523
- } else if (msg.includes("NOT NULL CONSTRAINT")) {
524
- kind = "not_null_violation";
525
- } else if (msg.includes("CHECK CONSTRAINT")) {
526
- kind = "check_violation";
527
- } else if (msg.includes("BUSY") || msg.includes("LOCKED")) {
528
- kind = "timeout";
529
- } else if (msg.includes("SQLITE_CANTOPEN") || msg.includes("UNABLE TO OPEN")) {
530
- kind = "connection";
531
- }
532
- }
533
- let message = sqliteError.message ?? String(error);
534
- if (sql && kind === "query") {
535
- message = `Query failed: ${message}`;
536
- }
537
- return createDatabaseError({
538
- kind,
539
- message,
540
- code: sqliteError.code,
541
- cause: error instanceof Error ? error : void 0
542
- });
543
- }
544
- /**
545
- * Override handleQueryError to use SQLite-specific classification.
546
- */
547
- handleQueryError(error, operation, table) {
548
- const dbError = this.classifyError(error);
549
- if (!dbError.message.includes(operation)) {
550
- dbError.message = `${operation} operation failed on table '${table}': ${dbError.message}`;
551
- }
552
- if (!dbError.table) {
553
- dbError.table = table;
554
- }
555
- return dbError;
556
- }
122
+ drizzleByRelations = /* @__PURE__ */ new WeakMap();
123
+ drizzleBare;
124
+ /**
125
+ * The database dialect - always 'sqlite' for this adapter.
126
+ */
127
+ dialect = "sqlite";
128
+ /**
129
+ * Adapter configuration.
130
+ */
131
+ config;
132
+ /**
133
+ * better-sqlite3 Database instance.
134
+ */
135
+ db = null;
136
+ /**
137
+ * Connection state flag.
138
+ */
139
+ connected = false;
140
+ /**
141
+ * Serialization queue for concurrent `transaction()` calls.
142
+ *
143
+ * better-sqlite3 is synchronous, single-connection, and rejects nested
144
+ * `BEGIN` with "cannot start a transaction within a transaction".
145
+ * When two `await adapter.transaction(...)` calls overlap (e.g. a bulk
146
+ * mutation calling `Promise.allSettled` with N per-row updates), the
147
+ * second one tries to BEGIN while the first is still open and the
148
+ * driver throws.
149
+ *
150
+ * Postgres/MySQL avoid this by allocating a fresh client/connection
151
+ * per transaction from a pool — there is no equivalent for
152
+ * better-sqlite3, so we serialize at the JS layer instead. Every
153
+ * `transaction()` invocation chains onto this promise; only one
154
+ * BEGIN→COMMIT/ROLLBACK section runs at any moment, preserving
155
+ * write-isolation semantics that callers expect from a transactional
156
+ * adapter.
157
+ *
158
+ * Performance impact is the natural floor: SQLite already serializes
159
+ * writers at the file-lock level, so the queue removes failed-call
160
+ * surface area without changing the achievable concurrency.
161
+ */
162
+ transactionQueue = Promise.resolve();
163
+ /**
164
+ * The transaction the current async chain is running inside, if any.
165
+ *
166
+ * A `transaction()` call made from INSIDE a transaction's work — a store
167
+ * writing on behalf of the caller, a plugin's write inside its own
168
+ * `ctx.db.transaction` — used to queue behind the very transaction that
169
+ * was waiting for it, and neither ever settled: that hung every later
170
+ * transaction on the instance too. Such a call now runs as a SAVEPOINT of
171
+ * the active transaction instead, so it commits or rolls back with it and
172
+ * its own failure undoes only its own part.
173
+ */
174
+ transactionScope = new AsyncLocalStorage();
175
+ /**
176
+ * Creates a new SQLite adapter instance.
177
+ *
178
+ * @param config - Adapter configuration
179
+ */
180
+ constructor(config) {
181
+ super();
182
+ this.config = config;
183
+ }
184
+ /**
185
+ * Connect to the SQLite database.
186
+ *
187
+ * @remarks
188
+ * This method initializes the database connection. For SQLite, this
189
+ * opens the database file or creates an in-memory database.
190
+ * Also configures WAL mode and foreign keys based on config.
191
+ *
192
+ * @throws {DatabaseError} If connection fails
193
+ */
194
+ async connect() {
195
+ if (this.connected && this.db) return;
196
+ try {
197
+ const Database = (await import("better-sqlite3")).default;
198
+ let dbPath;
199
+ if (this.config.memory) dbPath = ":memory:";
200
+ else if (this.config.url) dbPath = this.config.url.replace(/^file:/, "");
201
+ else dbPath = ":memory:";
202
+ if (dbPath !== ":memory:" && !this.config.readonly) {
203
+ const fs = await import("node:fs");
204
+ const path = await import("node:path");
205
+ const dir = path.dirname(path.resolve(dbPath));
206
+ await fs.promises.mkdir(dir, { recursive: true });
207
+ }
208
+ this.db = new Database(dbPath, {
209
+ readonly: this.config.readonly ?? false,
210
+ timeout: this.config.busyTimeout ?? DEFAULT_CONFIG.busyTimeout
211
+ });
212
+ if ((this.config.wal ?? DEFAULT_CONFIG.wal) && dbPath !== ":memory:" && !this.config.readonly) this.db.pragma("journal_mode = WAL");
213
+ if (this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys) this.db.pragma("foreign_keys = ON");
214
+ await checkDialectVersion(this.db, "sqlite");
215
+ this.connected = true;
216
+ if (this.config.logger?.info) this.config.logger.info("SQLite connection established", {
217
+ url: dbPath === ":memory:" ? "in-memory" : dbPath,
218
+ wal: this.config.wal ?? DEFAULT_CONFIG.wal,
219
+ foreignKeys: this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys
220
+ });
221
+ } catch (error) {
222
+ if (this.db) {
223
+ try {
224
+ this.db.close();
225
+ } catch {}
226
+ this.db = null;
227
+ }
228
+ throw this.classifyError(error);
229
+ }
230
+ }
231
+ /**
232
+ * Disconnect from the SQLite database.
233
+ *
234
+ * @remarks
235
+ * This method closes the database connection and releases resources.
236
+ */
237
+ async disconnect() {
238
+ this.drizzleBare = void 0;
239
+ this.drizzleByRelations = /* @__PURE__ */ new WeakMap();
240
+ if (!this.db) return;
241
+ try {
242
+ this.db.close();
243
+ if (this.config.logger?.info) this.config.logger.info("SQLite connection closed");
244
+ } finally {
245
+ this.db = null;
246
+ this.connected = false;
247
+ }
248
+ }
249
+ /**
250
+ * Check if connected to the database.
251
+ */
252
+ isConnected() {
253
+ return this.connected && this.db !== null;
254
+ }
255
+ /**
256
+ * Get connection pool statistics.
257
+ *
258
+ * @remarks
259
+ * SQLite doesn't use connection pooling, so this returns null.
260
+ * The database is single-file with a single connection.
261
+ */
262
+ getPoolStats() {
263
+ return null;
264
+ }
265
+ /**
266
+ * Execute a raw SQL query.
267
+ *
268
+ * @param sql - SQL query string with $1, $2 placeholders (converted to ? for SQLite)
269
+ * @param params - Query parameters
270
+ * @returns Query results
271
+ *
272
+ * @throws {DatabaseError} If query execution fails
273
+ */
274
+ async executeQuery(sql, params = []) {
275
+ const db = this.ensureDb();
276
+ const startTime = Date.now();
277
+ try {
278
+ const convertedSql = this.convertPlaceholders(sql);
279
+ const trimmedSql = convertedSql.trim().toUpperCase();
280
+ const isPragmaQuery = trimmedSql.startsWith("PRAGMA") && !trimmedSql.includes("=");
281
+ const isSelect = trimmedSql.startsWith("SELECT") || isPragmaQuery || trimmedSql.startsWith("WITH");
282
+ const hasReturning = trimmedSql.includes("RETURNING");
283
+ let result;
284
+ const sanitizedParams = params.map(sanitizeSqliteValue);
285
+ if (isSelect || hasReturning) result = db.prepare(convertedSql).all(...sanitizedParams);
286
+ else {
287
+ const runResult = db.prepare(convertedSql).run(...sanitizedParams);
288
+ result = [{
289
+ changes: runResult.changes,
290
+ lastInsertRowid: runResult.lastInsertRowid
291
+ }];
292
+ }
293
+ if (this.config.logger?.query) {
294
+ const durationMs = Date.now() - startTime;
295
+ this.config.logger.query(convertedSql, params, durationMs);
296
+ }
297
+ return result;
298
+ } catch (error) {
299
+ throw this.classifyError(error, sql);
300
+ }
301
+ }
302
+ /**
303
+ * Execute work within a transaction.
304
+ *
305
+ * @param work - Function containing transactional operations
306
+ * @param options - Transaction options (isolation level not fully supported in SQLite)
307
+ * @returns Result of the work function
308
+ *
309
+ * @remarks
310
+ * The adapter manages the transaction itself, because better-sqlite3's
311
+ * `db.transaction()` does not take async work: it runs `BEGIN IMMEDIATE`,
312
+ * then `COMMIT` when `work` resolves or `ROLLBACK` when it throws.
313
+ * - Top-level calls run one at a time.
314
+ * - A call made inside an open transaction's work joins it as a savepoint
315
+ * (`SAVEPOINT` / `RELEASE` / `ROLLBACK TO`).
316
+ * - Nested calls of one scope run one at a time.
317
+ * - A call made later from a savepoint that has already released joins the
318
+ * innermost scope still open.
319
+ * - Nested work started and not awaited finishes before the enclosing scope
320
+ * commits or rolls back.
321
+ *
322
+ * Note: SQLite uses DEFERRED, IMMEDIATE, or EXCLUSIVE transaction modes
323
+ * rather than isolation levels. This adapter uses IMMEDIATE.
324
+ */
325
+ /** How long a transaction waits for the connection: `pool.connectionTimeoutMs`. */
326
+ transactionWaitMs() {
327
+ return this.config.pool?.connectionTimeoutMs ?? DEFAULT_CONFIG.connectionTimeoutMs;
328
+ }
329
+ async transaction(work, _options) {
330
+ const own = this.transactionScope.getStore();
331
+ const parent = this.nestingScope();
332
+ if (own && parent !== own) own.writesAfterClosing += 1;
333
+ if (parent) return enqueueIn(parent, () => this.runSavepoint(work, parent));
334
+ const run = async () => this.runTransaction(work);
335
+ const previous = this.transactionQueue;
336
+ const next = waitForTurn(previous, this.transactionWaitMs()).then(run);
337
+ this.transactionQueue = previous.then(() => next).catch(() => void 0);
338
+ const { result, effects } = await next;
339
+ await this.runHeldEffects(effects);
340
+ return result;
341
+ }
342
+ /**
343
+ * Hold `effect` until the transaction enclosing this async context commits,
344
+ * or run it now when none does.
345
+ *
346
+ * The effect belongs to the scope this async context runs in, even one
347
+ * that has already released or rolled back: a service whose own
348
+ * transaction nested as a savepoint registers from its caller's scope once
349
+ * that savepoint has released, and the change is undoable for as long as
350
+ * any scope around it is. The outermost transaction runs the effect once
351
+ * its COMMIT succeeds, unless a scope between the effect's scope and the
352
+ * outermost transaction rolled back, and drops every effect on ROLLBACK or
353
+ * a failed COMMIT. A context whose outermost transaction has already
354
+ * finished is outside any transaction, so its effect runs now, unless what
355
+ * it announces was undone and it has run no transaction since.
356
+ */
357
+ async afterCommit(effect, onDeferredFailure) {
358
+ const scope = this.transactionScope.getStore();
359
+ if (!scope) {
360
+ await effect();
361
+ return;
362
+ }
363
+ const root = outermost(scope);
364
+ if (!root.finished) {
365
+ root.held.push({
366
+ effect: this.inRegisteringContext(effect),
367
+ onDeferredFailure,
368
+ scope,
369
+ writesBefore: scope.writesAfterClosing
370
+ });
371
+ return;
372
+ }
373
+ if (!undone(scope, scope.writesAfterClosing)) await effect();
374
+ }
375
+ /**
376
+ * `effect`, bound to the async context it is registered from.
377
+ *
378
+ * A held effect runs after the outermost COMMIT, from the code that ran the
379
+ * outermost transaction, so without this it sees that code's context rather
380
+ * than its own: whatever its caller pinned around the write is gone. That
381
+ * includes who is calling. A scope pinned for a scoped API key lost there
382
+ * leaves a hook's nested call judged by the key's owner instead of the key,
383
+ * and a person's frame lost there leaves the hook told of nobody.
384
+ *
385
+ * The adapter's own transaction scope is the one store it leaves: the
386
+ * effect runs once that transaction has finished, outside it, as an effect
387
+ * held to the outermost commit always has.
388
+ */
389
+ inRegisteringContext(effect) {
390
+ return AsyncResource.bind(() => this.transactionScope.exit(effect));
391
+ }
392
+ /**
393
+ * Run the effects a committed transaction held, in order. A failure is
394
+ * reported, never thrown: the change is durable, and reporting the
395
+ * transaction as failed would invite a retry of something already done.
396
+ */
397
+ async runHeldEffects(effects) {
398
+ for (const { effect, onDeferredFailure } of effects) try {
399
+ await effect();
400
+ } catch (error) {
401
+ this.reportHeldEffectFailure(error, onDeferredFailure);
402
+ }
403
+ }
404
+ /** Hand a held effect's failure to its reporter, or to the adapter's log. */
405
+ reportHeldEffectFailure(error, onDeferredFailure) {
406
+ try {
407
+ if (onDeferredFailure) {
408
+ onDeferredFailure(error);
409
+ return;
410
+ }
411
+ } catch {}
412
+ const failure = error instanceof Error ? error : new Error(String(error));
413
+ if (this.config.logger?.error) this.config.logger.error(failure, { phase: "after-commit" });
414
+ else console.error("An effect held until commit failed:", failure);
415
+ }
416
+ /**
417
+ * Whether a `transaction()` call made here would join an open transaction
418
+ * as a savepoint. On SQLite it would: the connection is the only one, so a
419
+ * transaction open on it encloses whatever the async chain that opened it
420
+ * starts.
421
+ */
422
+ inTransaction() {
423
+ return this.nestingScope() !== void 0;
424
+ }
425
+ /**
426
+ * Whether the calling async flow is inside a transaction that has not
427
+ * finished, judged from the transaction this flow was started inside,
428
+ * never from the connection: another request's open transaction answers
429
+ * false here, where its own `transaction()` queues behind that one.
430
+ *
431
+ * A flow started inside a transaction, even inside a savepoint that has
432
+ * since released, stays inside it until its outermost transaction has
433
+ * committed or rolled back: until then whatever it writes on the one
434
+ * connection lands in that transaction.
435
+ */
436
+ transactionOpenInFlow() {
437
+ const own = this.transactionScope.getStore();
438
+ return own !== void 0 && !outermost(own).finished;
439
+ }
440
+ /**
441
+ * The scope a `transaction()` call made here nests in, or none when it
442
+ * opens a transaction of its own.
443
+ *
444
+ * A call made later from inside a savepoint that has since released runs
445
+ * inside the innermost scope open on the connection. Queued on an enclosing
446
+ * scope, or on the instance, it would wait behind the scope running there,
447
+ * which may be the one awaiting it. Savepoints on one connection form a
448
+ * stack, so whatever opens now is inside that innermost scope anyway; when
449
+ * none is open the call queues on the instance.
450
+ *
451
+ * The innermost open scope need not be related to the call. When it is a
452
+ * sibling savepoint that later rolls back, the call's write rolls back with
453
+ * it, although the call itself already resolved. Waiting for an unrelated
454
+ * scope instead could wait on the very scope that awaits the call.
455
+ */
456
+ nestingScope() {
457
+ const own = this.transactionScope.getStore();
458
+ return own?.active ? own : own?.open.at(-1);
459
+ }
460
+ /**
461
+ * Run `work` as a savepoint of the active transaction: released when it
462
+ * resolves, rolled back to when it throws.
463
+ */
464
+ async runSavepoint(work, parent) {
465
+ const db = this.ensureDb();
466
+ const name = `nextly_sp_${parent.depth + 1}`;
467
+ try {
468
+ const ctx = this.createTransactionContext(db);
469
+ db.exec(`SAVEPOINT ${name}`);
470
+ const scope = openScope(parent);
471
+ try {
472
+ const result = await this.transactionScope.run(scope, () => work(ctx));
473
+ await settleNested(scope);
474
+ db.exec(`RELEASE ${name}`);
475
+ return result;
476
+ } catch (error) {
477
+ await settleNested(scope);
478
+ scope.rolledBack = true;
479
+ try {
480
+ db.exec(`ROLLBACK TO ${name}`);
481
+ db.exec(`RELEASE ${name}`);
482
+ } catch {}
483
+ throw error;
484
+ }
485
+ } catch (error) {
486
+ if (isApplicationError(error)) throw error;
487
+ throw this.classifyError(error);
488
+ }
489
+ }
490
+ /**
491
+ * Inner transaction body. Kept private so callers always go through
492
+ * the serialized `transaction()` queue above and the BEGIN/COMMIT
493
+ * pair is never invoked outside of it.
494
+ */
495
+ async runTransaction(work) {
496
+ const db = this.ensureDb();
497
+ const startTime = Date.now();
498
+ try {
499
+ const ctx = this.createTransactionContext(db);
500
+ db.exec("BEGIN IMMEDIATE");
501
+ const scope = openScope();
502
+ try {
503
+ const result = await this.transactionScope.run(scope, () => work(ctx));
504
+ await settleNested(scope);
505
+ db.exec("COMMIT");
506
+ scope.finished = true;
507
+ if (this.config.logger?.debug) {
508
+ const durationMs = Date.now() - startTime;
509
+ this.config.logger.debug("Transaction committed", { durationMs });
510
+ }
511
+ return {
512
+ result,
513
+ effects: scope.held.filter((held) => !undone(held.scope, held.writesBefore))
514
+ };
515
+ } catch (error) {
516
+ await settleNested(scope);
517
+ scope.rolledBack = true;
518
+ scope.finished = true;
519
+ scope.held.length = 0;
520
+ try {
521
+ db.exec("ROLLBACK");
522
+ } catch {}
523
+ throw error;
524
+ }
525
+ } catch (error) {
526
+ if (isApplicationError(error)) throw error;
527
+ throw this.classifyError(error);
528
+ }
529
+ }
530
+ /**
531
+ * Get SQLite database capabilities.
532
+ *
533
+ * @remarks
534
+ * SQLite capabilities:
535
+ * - JSON support (not JSONB)
536
+ * - No arrays
537
+ * - No native ILIKE
538
+ * - RETURNING clause (3.35+)
539
+ * - Savepoints supported
540
+ * - ON CONFLICT supported
541
+ */
542
+ getCapabilities() {
543
+ return {
544
+ dialect: "sqlite",
545
+ supportsJsonb: false,
546
+ supportsJson: true,
547
+ supportsArrays: false,
548
+ supportsGeneratedColumns: true,
549
+ supportsFts: true,
550
+ supportsIlike: false,
551
+ supportsReturning: true,
552
+ supportsSavepoints: true,
553
+ supportsOnConflict: true,
554
+ maxParamsPerQuery: 999,
555
+ maxIdentifierLength: 128
556
+ };
557
+ }
558
+ /**
559
+ * Override insertMany for bulk insert optimization.
560
+ *
561
+ * @remarks
562
+ * Uses a single multi-row INSERT statement for better performance.
563
+ */
564
+ async insertMany(table, data, options) {
565
+ if (data.length === 0) return [];
566
+ const db = this.ensureDb();
567
+ if (data.length === 1) return [await this.insert(table, data[0], options)];
568
+ const columns = Object.keys(data[0]);
569
+ const params = [];
570
+ const valuesClauses = [];
571
+ for (const record of data) {
572
+ const placeholders = [];
573
+ for (const col of columns) {
574
+ params.push(sanitizeSqliteValue(record[col]));
575
+ placeholders.push("?");
576
+ }
577
+ valuesClauses.push(`(${placeholders.join(", ")})`);
578
+ }
579
+ const columnList = columns.map((col) => this.escapeIdentifier(col)).join(", ");
580
+ let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(", ")}`;
581
+ if (options?.returning) {
582
+ const returning = options.returning === "*" ? "*" : options.returning.map((col) => this.escapeIdentifier(col)).join(", ");
583
+ sql += ` RETURNING ${returning}`;
584
+ } else sql += " RETURNING *";
585
+ try {
586
+ const rows = db.prepare(sql).all(...params);
587
+ const tableObj = this.getTableObject(table);
588
+ return rows.map((r) => this.mapRowFromRawSql(tableObj, r));
589
+ } catch (error) {
590
+ throw this.handleQueryError(error, "insertMany", table);
591
+ }
592
+ }
593
+ /**
594
+ * Ensures database is connected and returns it.
595
+ *
596
+ * @throws {DatabaseError} If not connected
597
+ */
598
+ ensureDb() {
599
+ if (!this.db) throw createDatabaseError({
600
+ kind: "connection",
601
+ message: "SqliteAdapter is not connected. Call connect() first."
602
+ });
603
+ return this.db;
604
+ }
605
+ /**
606
+ * Return the typed Drizzle instance for SQLite.
607
+ * Guarded for server-only usage and requires an active connection.
608
+ *
609
+ * @param schema - Optional schema for relational queries (db.query.*)
610
+ * @returns Drizzle ORM instance wrapping the better-sqlite3 connection
611
+ * @throws {Error} If called in browser or not connected
612
+ */
613
+ getDrizzle(relations) {
614
+ if (typeof window !== "undefined") throw new Error("getDrizzle() is server-only");
615
+ const db = this.ensureDb();
616
+ if (!relations) {
617
+ this.drizzleBare ??= drizzle({ client: db });
618
+ return this.drizzleBare;
619
+ }
620
+ let cached = this.drizzleByRelations.get(relations);
621
+ if (!cached) {
622
+ cached = drizzle({
623
+ client: db,
624
+ relations
625
+ });
626
+ this.drizzleByRelations.set(relations, cached);
627
+ }
628
+ return cached;
629
+ }
630
+ /**
631
+ * Convert $1, $2 placeholders to ? for better-sqlite3.
632
+ *
633
+ * @param sql - SQL with PostgreSQL-style placeholders
634
+ * @returns SQL with ? placeholders
635
+ */
636
+ convertPlaceholders(sql) {
637
+ return sql.replace(/\$\d+/g, "?");
638
+ }
639
+ /**
640
+ * Creates a TransactionContext for the given database connection.
641
+ */
642
+ /** A table-level `primaryKey({ columns })`, read through the SQLite table config. */
643
+ compositePrimaryKey(tableObj) {
644
+ return getTableConfig(tableObj).primaryKeys.flatMap((key) => key.columns);
645
+ }
646
+ createTransactionContext(db) {
647
+ const txHandles = this.transactionDrizzleHandles((relations) => relations ? drizzle({
648
+ client: db,
649
+ relations
650
+ }) : drizzle({ client: db }));
651
+ const txDb = txHandles.bare;
652
+ return {
653
+ drizzleWithRelations: txHandles.withRelations,
654
+ lockRow: () => Promise.resolve(),
655
+ runStatement: (statement) => {
656
+ txDb().run(statement);
657
+ return Promise.resolve();
658
+ },
659
+ queryStatement: (statement) => Promise.resolve(txDb().all(statement)),
660
+ execute: async (sql, params = []) => {
661
+ const convertedSql = this.convertPlaceholders(sql);
662
+ const trimmedSql = convertedSql.trim().toUpperCase();
663
+ const isSelect = trimmedSql.startsWith("SELECT") || trimmedSql.includes("RETURNING");
664
+ const sanitizedParams = params.map(sanitizeSqliteValue);
665
+ if (isSelect) return db.prepare(convertedSql).all(...sanitizedParams);
666
+ else {
667
+ const result = db.prepare(convertedSql).run(...sanitizedParams);
668
+ return [{
669
+ changes: result.changes,
670
+ lastInsertRowid: result.lastInsertRowid
671
+ }];
672
+ }
673
+ },
674
+ insert: async (table, data, options) => {
675
+ const mapped = this.mapKeysToSqlColumns(this.getTableObject(table), data);
676
+ const columns = Object.keys(mapped);
677
+ const values = Object.values(mapped).map(sanitizeSqliteValue);
678
+ const placeholders = values.map(() => "?").join(", ");
679
+ let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map((c) => this.escapeIdentifier(c)).join(", ")}) VALUES (${placeholders})`;
680
+ const ret = options?.returning;
681
+ const returningEmpty = Array.isArray(ret) && ret.length === 0;
682
+ if (!returningEmpty) {
683
+ const returning = !ret || ret === "*" ? "*" : this.mapColumnNamesToSql(this.getTableObject(table), ret).map((col) => this.escapeIdentifier(col)).join(", ");
684
+ sql += ` RETURNING ${returning}`;
685
+ }
686
+ const stmt = db.prepare(sql);
687
+ if (returningEmpty) {
688
+ stmt.run(...values);
689
+ return;
690
+ }
691
+ const rows = stmt.all(...values);
692
+ return this.mapRowFromRawSql(this.getTableObject(table), rows[0]);
693
+ },
694
+ insertMany: async (table, data, options) => {
695
+ if (data.length === 0) return [];
696
+ const tableObj = this.getTableObject(table);
697
+ const mappedRecords = data.map((r) => this.mapKeysToSqlColumns(tableObj, r));
698
+ const columns = Object.keys(mappedRecords[0]);
699
+ const allValues = [];
700
+ const valuesClauses = [];
701
+ for (const record of mappedRecords) {
702
+ const placeholders = [];
703
+ for (const col of columns) {
704
+ allValues.push(sanitizeSqliteValue(record[col]));
705
+ placeholders.push("?");
706
+ }
707
+ valuesClauses.push(`(${placeholders.join(", ")})`);
708
+ }
709
+ let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map((c) => this.escapeIdentifier(c)).join(", ")}) VALUES ${valuesClauses.join(", ")}`;
710
+ const ret = options?.returning;
711
+ const returningEmpty = Array.isArray(ret) && ret.length === 0;
712
+ if (!returningEmpty) {
713
+ const returning = !ret || ret === "*" ? "*" : this.mapColumnNamesToSql(this.getTableObject(table), ret).map((col) => this.escapeIdentifier(col)).join(", ");
714
+ sql += ` RETURNING ${returning}`;
715
+ }
716
+ const stmt = db.prepare(sql);
717
+ if (returningEmpty) {
718
+ stmt.run(...allValues);
719
+ return [];
720
+ }
721
+ return stmt.all(...allValues).map((r) => this.mapRowFromRawSql(tableObj, r));
722
+ },
723
+ update: this.transactionUpdate(txDb, (statement, returnsRows) => returnsRows ? txDb().all(statement) : (txDb().run(statement), void 0), sanitizeSqliteValue),
724
+ ...this.createTransactionForwarders(txDb),
725
+ savepoint: async (name) => {
726
+ db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);
727
+ },
728
+ rollbackToSavepoint: async (name) => {
729
+ db.exec(`ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`);
730
+ },
731
+ releaseSavepoint: async (name) => {
732
+ db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);
733
+ }
734
+ };
735
+ }
736
+ /**
737
+ * Classifies a SQLite error into a DatabaseError.
738
+ *
739
+ * @param error - Original error from better-sqlite3
740
+ * @param sql - SQL statement that caused the error (optional)
741
+ * @returns DatabaseError with proper classification
742
+ */
743
+ classifyError(error, sql) {
744
+ if (isDatabaseError(error)) return error;
745
+ const sqliteError = this.driverErrorIn(error, (candidate) => typeof candidate.code === "string" && candidate.code !== "");
746
+ let kind = "unknown";
747
+ if (sqliteError.code) kind = SQLITE_ERROR_CODES[sqliteError.code] || "unknown";
748
+ else if (sqliteError.message) {
749
+ const msg = sqliteError.message.toUpperCase();
750
+ if (msg.includes("UNIQUE CONSTRAINT")) kind = "unique_violation";
751
+ else if (msg.includes("FOREIGN KEY CONSTRAINT")) kind = "foreign_key_violation";
752
+ else if (msg.includes("NOT NULL CONSTRAINT")) kind = "not_null_violation";
753
+ else if (msg.includes("CHECK CONSTRAINT")) kind = "check_violation";
754
+ else if (msg.includes("BUSY") || msg.includes("LOCKED")) kind = "timeout";
755
+ else if (msg.includes("SQLITE_CANTOPEN") || msg.includes("UNABLE TO OPEN")) kind = "connection";
756
+ }
757
+ let message = sqliteError.message ?? String(error);
758
+ if (sql && kind === "query") message = `Query failed: ${message}`;
759
+ return createDatabaseError({
760
+ kind,
761
+ message,
762
+ code: sqliteError.code,
763
+ cause: error instanceof Error ? error : void 0
764
+ });
765
+ }
557
766
  };
767
+ /**
768
+ * Create a SQLite database adapter.
769
+ *
770
+ * @param config - SQLite adapter configuration
771
+ * @returns A new SqliteAdapter instance
772
+ *
773
+ * @example
774
+ * ```typescript
775
+ * // Simple usage with file path
776
+ * const adapter = createSqliteAdapter({
777
+ * url: 'file:./data.db',
778
+ * });
779
+ *
780
+ * // In-memory database
781
+ * const memAdapter = createSqliteAdapter({
782
+ * memory: true,
783
+ * });
784
+ *
785
+ * // Full configuration
786
+ * const adapter = createSqliteAdapter({
787
+ * url: 'file:./data.db',
788
+ * wal: true,
789
+ * foreignKeys: true,
790
+ * busyTimeout: 5000,
791
+ * logger: {
792
+ * query: (sql, params, duration) => console.log(`Query: ${sql}`),
793
+ * },
794
+ * });
795
+ *
796
+ * await adapter.connect();
797
+ * ```
798
+ */
558
799
  function createSqliteAdapter(config) {
559
- return new SqliteAdapter(config);
800
+ return new SqliteAdapter(config);
560
801
  }
802
+ /**
803
+ * Type guard to check if a value is a SqliteAdapter.
804
+ *
805
+ * @param value - Value to check
806
+ * @returns True if value is a SqliteAdapter instance
807
+ *
808
+ * @example
809
+ * ```typescript
810
+ * if (isSqliteAdapter(adapter)) {
811
+ * // TypeScript knows adapter is SqliteAdapter
812
+ * console.log('Using SQLite');
813
+ * }
814
+ * ```
815
+ */
561
816
  function isSqliteAdapter(value) {
562
- return value instanceof SqliteAdapter;
817
+ return value instanceof SqliteAdapter;
818
+ }
819
+ /**
820
+ * Open the scope of a savepoint inside `parent`, or of an outermost
821
+ * transaction when there is none, and record it among the connection's open
822
+ * scopes.
823
+ */
824
+ function openScope(parent) {
825
+ const scope = {
826
+ active: true,
827
+ depth: parent ? parent.depth + 1 : 0,
828
+ parent,
829
+ rolledBack: false,
830
+ writesAfterClosing: 0,
831
+ finished: false,
832
+ held: [],
833
+ queue: Promise.resolve(),
834
+ open: parent ? parent.open : []
835
+ };
836
+ scope.open.push(scope);
837
+ return scope;
838
+ }
839
+ /** The outermost transaction `scope` runs inside; itself for that one. */
840
+ function outermost(scope) {
841
+ let at = scope;
842
+ while (at.parent) at = at.parent;
843
+ return at;
844
+ }
845
+ /**
846
+ * Whether an effect registered from `scope` describes a write that was
847
+ * undone: `scope`, or a scope it runs inside, rolled back, and no
848
+ * transaction had run from its context since it closed when the effect was
849
+ * registered (`writesBefore`). An effect registered before such a write
850
+ * still describes the undone one.
851
+ */
852
+ function undone(scope, writesBefore) {
853
+ if (writesBefore > 0) return false;
854
+ for (let at = scope; at; at = at.parent) if (at.rolledBack) return true;
855
+ return false;
856
+ }
857
+ /**
858
+ * Settles once `turn` does, or rejects with the pool-wait timeout error once
859
+ * `boundMs` passes first. A bound of 0 waits without limit, as pg-pool reads a
860
+ * `connectionTimeoutMillis` of 0.
861
+ */
862
+ function waitForTurn(turn, boundMs) {
863
+ if (boundMs <= 0) return turn.then(() => void 0);
864
+ return new Promise((resolve, reject) => {
865
+ const timer = setTimeout(() => reject(poolWaitTimeoutError(boundMs)), boundMs);
866
+ timer.unref();
867
+ turn.then(() => {
868
+ clearTimeout(timer);
869
+ resolve();
870
+ });
871
+ });
872
+ }
873
+ /** Chain `run` onto a scope's queue, keeping the queue itself unrejected. */
874
+ function enqueueIn(scope, run) {
875
+ const next = scope.queue.then(run, run);
876
+ scope.queue = next.catch(() => void 0);
877
+ return next;
878
+ }
879
+ /**
880
+ * Close a scope to new nested calls and wait for those already queued. A call
881
+ * arriving after this joins the innermost scope still open on the connection,
882
+ * or, when none is, goes to the instance queue behind the transaction.
883
+ */
884
+ async function settleNested(scope) {
885
+ scope.active = false;
886
+ const index = scope.open.indexOf(scope);
887
+ if (index !== -1) scope.open.splice(index, 1);
888
+ await scope.queue;
563
889
  }
564
-
565
890
  export { SqliteAdapter, VERSION, createSqliteAdapter, isSqliteAdapter };
566
- //# sourceMappingURL=index.mjs.map
891
+
567
892
  //# sourceMappingURL=index.mjs.map