@nextlyhq/adapter-sqlite 0.0.2-alpha.60 → 0.0.2-alpha.64
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.cjs +13 -38
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -5
- package/dist/index.d.ts +1 -5
- package/dist/index.mjs +13 -38
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -536,24 +536,7 @@ var SqliteAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
536
536
|
(r) => this.mapRowFromRawSql(tableObj, r)
|
|
537
537
|
);
|
|
538
538
|
},
|
|
539
|
-
|
|
540
|
-
// but pass the transaction-bound executor so they run inside this
|
|
541
|
-
// transaction rather than on the pool.
|
|
542
|
-
select: async (table, options) => {
|
|
543
|
-
return this.select(table, options, txDb());
|
|
544
|
-
},
|
|
545
|
-
selectOne: async (table, options) => {
|
|
546
|
-
return this.selectOne(table, options, txDb());
|
|
547
|
-
},
|
|
548
|
-
update: async (table, data, where, options) => {
|
|
549
|
-
return this.update(table, data, where, options, txDb());
|
|
550
|
-
},
|
|
551
|
-
delete: async (table, where, options) => {
|
|
552
|
-
return this.delete(table, where, options, txDb());
|
|
553
|
-
},
|
|
554
|
-
upsert: async (table, data, options) => {
|
|
555
|
-
return this.upsert(table, data, options, txDb());
|
|
556
|
-
},
|
|
539
|
+
...this.createTransactionForwarders(txDb),
|
|
557
540
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
558
541
|
savepoint: async (name) => {
|
|
559
542
|
db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);
|
|
@@ -565,12 +548,7 @@ var SqliteAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
565
548
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
566
549
|
releaseSavepoint: async (name) => {
|
|
567
550
|
db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);
|
|
568
|
-
}
|
|
569
|
-
// Expose the transaction-bound Drizzle instance so callers can run
|
|
570
|
-
// Drizzle sql templates inside this transaction (junction-table writes
|
|
571
|
-
// need this to be atomic with the entry write). Reuses the memoized
|
|
572
|
-
// txDb() built for the delegated CRUD methods.
|
|
573
|
-
getDrizzle: () => txDb()
|
|
551
|
+
}
|
|
574
552
|
};
|
|
575
553
|
}
|
|
576
554
|
/**
|
|
@@ -580,9 +558,10 @@ var SqliteAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
580
558
|
* @param sql - SQL statement that caused the error (optional)
|
|
581
559
|
* @returns DatabaseError with proper classification
|
|
582
560
|
*/
|
|
561
|
+
// fallow-ignore-next-line complexity
|
|
583
562
|
classifyError(error, sql) {
|
|
584
563
|
if (types.isDatabaseError(error)) return error;
|
|
585
|
-
const sqliteError = error;
|
|
564
|
+
const sqliteError = findDriverError(error);
|
|
586
565
|
let kind = "unknown";
|
|
587
566
|
if (sqliteError.code) {
|
|
588
567
|
kind = SQLITE_ERROR_CODES[sqliteError.code] || "unknown";
|
|
@@ -613,20 +592,16 @@ var SqliteAdapter = class extends adapterDrizzle.DrizzleAdapter {
|
|
|
613
592
|
cause: error instanceof Error ? error : void 0
|
|
614
593
|
});
|
|
615
594
|
}
|
|
616
|
-
/**
|
|
617
|
-
* Override handleQueryError to use SQLite-specific classification.
|
|
618
|
-
*/
|
|
619
|
-
handleQueryError(error, operation, table) {
|
|
620
|
-
const dbError = this.classifyError(error);
|
|
621
|
-
if (!dbError.message.includes(operation)) {
|
|
622
|
-
dbError.message = `${operation} operation failed on table '${table}': ${dbError.message}`;
|
|
623
|
-
}
|
|
624
|
-
if (!dbError.table) {
|
|
625
|
-
dbError.table = table;
|
|
626
|
-
}
|
|
627
|
-
return dbError;
|
|
628
|
-
}
|
|
629
595
|
};
|
|
596
|
+
function findDriverError(error) {
|
|
597
|
+
let cursor = error;
|
|
598
|
+
for (let depth = 0; depth < 10 && cursor != null; depth++) {
|
|
599
|
+
if (typeof cursor.code === "string" && cursor.code.length > 0)
|
|
600
|
+
return cursor;
|
|
601
|
+
cursor = cursor.cause;
|
|
602
|
+
}
|
|
603
|
+
return error ?? {};
|
|
604
|
+
}
|
|
630
605
|
function createSqliteAdapter(config) {
|
|
631
606
|
return new SqliteAdapter(config);
|
|
632
607
|
}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["DrizzleAdapter","checkDialectVersion","isApplicationError","createDatabaseError","drizzle","isDatabaseError"],"mappings":";;;;;;;;AAkGO,IAAM,OAAA,GAAU;AAOvB,IAAM,kBAAA,GAAwD;AAAA;AAAA,EAE5D,iBAAA,EAAmB,YAAA;AAAA,EACnB,wBAAA,EAA0B,kBAAA;AAAA,EAC1B,4BAAA,EAA8B,kBAAA;AAAA,EAC9B,4BAAA,EAA8B,uBAAA;AAAA,EAC9B,yBAAA,EAA2B,oBAAA;AAAA,EAC3B,uBAAA,EAAyB,iBAAA;AAAA;AAAA,EAGzB,WAAA,EAAa,SAAA;AAAA,EACb,aAAA,EAAe,SAAA;AAAA;AAAA,EAGf,eAAA,EAAiB,YAAA;AAAA,EACjB,aAAA,EAAe,YAAA;AAAA,EACf,cAAA,EAAgB,YAAA;AAAA;AAAA,EAGhB,YAAA,EAAc,OAAA;AAAA,EACd,aAAA,EAAe,OAAA;AAAA,EACf,YAAA,EAAc;AAChB,CAAA;AAKA,IAAM,cAAA,GAAiB;AAAA,EACrB,WAAA,EAAa,GAAA;AAAA,EACb,GAAA,EAAK,IAAA;AAAA,EACL,WAAA,EAAa;AACf,CAAA;AAaA,SAAS,oBAAoB,CAAA,EAAqB;AAChD,EAAA,IAAI,CAAA,KAAM,QAAW,OAAO,IAAA;AAC5B,EAAA,IAAI,OAAO,CAAA,KAAM,SAAA,EAAW,OAAO,IAAI,CAAA,GAAI,CAAA;AAK3C,EAAA,IAAI,CAAA,YAAa,MAAM,OAAO,IAAA,CAAK,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KAAK,CAAA;AACjD,IAAA,IAAI,GAAA,IAAO,SAAS,OAAA,EAAS;AAC3B,MAAA,OAAA,GAAU,iBAAiB,OAAO,CAAA,CAAA;AAAA,IACpC;AAEA,IAAA,OAAOF,yBAAA,CAAoB;AAAA,MACzB,IAAA;AAAA,MACA,OAAA;AAAA,MACA,MAAM,WAAA,CAAY,IAAA;AAAA,MAClB,KAAA,EAAO,KAAA,YAAiB,KAAA,GAAQ,KAAA,GAAQ;AAAA,KACzC,CAAA;AAAA,EACH;AAAA;AAAA;AAAA;AAAA,EAKmB,gBAAA,CACjB,KAAA,EACA,SAAA,EACA,KAAA,EACe;AACf,IAAA,MAAM,OAAA,GAAU,IAAA,CAAK,aAAA,CAAc,KAAK,CAAA;AAGxC,IAAA,IAAI,CAAC,OAAA,CAAQ,OAAA,CAAQ,QAAA,CAAS,SAAS,CAAA,EAAG;AACxC,MAAA,OAAA,CAAQ,UAAU,CAAA,EAAG,SAAS,+BAA+B,KAAK,CAAA,GAAA,EAAM,QAAQ,OAAO,CAAA,CAAA;AAAA,IACzF;AAEA,IAAA,IAAI,CAAC,QAAQ,KAAA,EAAO;AAClB,MAAA,OAAA,CAAQ,KAAA,GAAQ,KAAA;AAAA,IAClB;AAEA,IAAA,OAAO,OAAA;AAAA,EACT;AACF;AAkCO,SAAS,oBACd,MAAA,EACe;AACf,EAAA,OAAO,IAAI,cAAc,MAAM,CAAA;AACjC;AAgBO,SAAS,gBAAgB,KAAA,EAAwC;AACtE,EAAA,OAAO,KAAA,YAAiB,aAAA;AAC1B","file":"index.cjs","sourcesContent":["/**\n * @nextlyhq/adapter-sqlite\n *\n * SQLite database adapter for Nextly.\n * Extends DrizzleAdapter from @nextlyhq/adapter-drizzle to provide SQLite-specific functionality.\n *\n * @remarks\n * This adapter uses the better-sqlite3 package for database connectivity and provides:\n * - Synchronous API wrapped for async interface compatibility\n * - Full transaction support with savepoints (via better-sqlite3's native transaction handling)\n * - CRUD operations with RETURNING clause support (SQLite 3.35+)\n * - WAL mode for better concurrent read performance\n * - In-memory and file-based database support\n *\n * @example\n * ```typescript\n * import { createSqliteAdapter } from '@nextlyhq/adapter-sqlite';\n *\n * const adapter = createSqliteAdapter({\n * url: 'file:./data/app.db',\n * });\n *\n * await adapter.connect();\n *\n * // Query data\n * const users = await adapter.select('users', {\n * where: { and: [{ column: 'status', op: '=', value: 'active' }] },\n * limit: 10,\n * });\n *\n * await adapter.disconnect();\n * ```\n *\n * @packageDocumentation\n */\n\nimport { DrizzleAdapter } from \"@nextlyhq/adapter-drizzle\";\n// F17: connect-time DB version check shared across all adapters.\nimport {\n createDatabaseError,\n isDatabaseError,\n type SqliteAdapterConfig,\n type DatabaseCapabilities,\n type PoolStats,\n type TransactionContext,\n type TransactionOptions,\n type SqlParam,\n type WhereClause,\n type WhereCondition,\n type WhereOperator,\n type SelectOptions,\n type InsertOptions,\n type UpdateOptions,\n type DeleteOptions,\n type UpsertOptions,\n type OrderBySpec,\n type JoinSpec,\n type DatabaseError,\n type DatabaseErrorKind,\n type BaseAdapterConfig,\n type AdapterLogger,\n isApplicationError,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport { checkDialectVersion } from \"@nextlyhq/adapter-drizzle/version-check\";\nimport type Database from \"better-sqlite3\";\nimport type { AnyRelations, SQL } from \"drizzle-orm\";\nimport {\n drizzle,\n type BetterSQLite3Database,\n} from \"drizzle-orm/better-sqlite3\";\n\n// Re-export types for convenience\nexport type {\n SqliteAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n WhereClause,\n WhereCondition,\n WhereOperator,\n SelectOptions,\n InsertOptions,\n UpdateOptions,\n DeleteOptions,\n UpsertOptions,\n OrderBySpec,\n JoinSpec,\n DatabaseError,\n DatabaseErrorKind,\n BaseAdapterConfig,\n AdapterLogger,\n};\n\n/**\n * Package version\n */\nexport const VERSION = \"0.1.0\";\n\n/**\n * SQLite error codes mapping to DatabaseErrorKind.\n *\n * @see https://sqlite.org/rescode.html\n */\nconst SQLITE_ERROR_CODES: Record<string, DatabaseErrorKind> = {\n // Constraint violations\n SQLITE_CONSTRAINT: \"constraint\",\n SQLITE_CONSTRAINT_UNIQUE: \"unique_violation\",\n SQLITE_CONSTRAINT_PRIMARYKEY: \"unique_violation\",\n SQLITE_CONSTRAINT_FOREIGNKEY: \"foreign_key_violation\",\n SQLITE_CONSTRAINT_NOTNULL: \"not_null_violation\",\n SQLITE_CONSTRAINT_CHECK: \"check_violation\",\n\n // Busy/locked errors\n SQLITE_BUSY: \"timeout\",\n SQLITE_LOCKED: \"timeout\",\n\n // Connection errors\n SQLITE_CANTOPEN: \"connection\",\n SQLITE_NOTADB: \"connection\",\n SQLITE_CORRUPT: \"connection\",\n\n // Query errors\n SQLITE_ERROR: \"query\",\n SQLITE_MISUSE: \"query\",\n SQLITE_RANGE: \"query\",\n};\n\n/**\n * Default configuration values.\n */\nconst DEFAULT_CONFIG = {\n busyTimeout: 5000,\n wal: true,\n foreignKeys: true,\n};\n\n/**\n * Converts a JavaScript value to a type that better-sqlite3 can bind.\n * better-sqlite3 only accepts: number, string, bigint, Buffer, null.\n *\n * These conversions have to match what the schema declares, because the rows\n * they write are read back through it. Every column here that takes a `Date`\n * is `integer({ mode: \"timestamp\" })` — the system columns and user date\n * fields in `runtime-schema-generator.ts`, and the core tables, whose date\n * columns are all INTEGER. So a date is bound as unix seconds, which is what\n * that mode reads.\n */\nfunction sanitizeSqliteValue(v: unknown): unknown {\n if (v === undefined) return null;\n if (typeof v === \"boolean\") return v ? 1 : 0;\n // Seconds, not an ISO string. SQLite stores whatever it is given regardless\n // of the declared type, so a string lands in the column without complaint\n // and only fails on the way back out, where the timestamp decoder reads it\n // as a number and yields an Invalid Date.\n if (v instanceof Date) return Math.floor(v.getTime() / 1000);\n if (v !== null && typeof v === \"object\") return JSON.stringify(v);\n return v;\n}\n\n/**\n * SQLite database adapter for Nextly.\n *\n * Extends the base DrizzleAdapter to provide SQLite-specific functionality\n * using the better-sqlite3 package.\n *\n * @remarks\n * SQLite has some differences from PostgreSQL:\n * - Synchronous API (wrapped for async compatibility)\n * - No connection pooling (single file/connection)\n * - RETURNING clause supported (SQLite 3.35+)\n * - Savepoints supported via nested transactions\n * - No native ILIKE (uses LOWER() LIKE workaround)\n * - JSON support (not JSONB)\n * - No array types\n *\n * @example\n * ```typescript\n * const adapter = new SqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport class SqliteAdapter extends DrizzleAdapter {\n // getDrizzle memoization: drizzle v1's constructor builds a relational\n // query builder per table in the relations config (~40 tables), and the\n // service layer resolves an instance on every db access — construct once\n // per relations object (identity-stable: the schema registry caches it\n // and hands out a NEW object on invalidation, which naturally misses\n // this cache and produces a fresh instance).\n private drizzleByRelations = new WeakMap<AnyRelations, unknown>();\n private drizzleBare: unknown;\n\n /**\n * The database dialect - always 'sqlite' for this adapter.\n */\n readonly dialect = \"sqlite\" as const;\n\n /**\n * Adapter configuration.\n */\n protected readonly config: SqliteAdapterConfig;\n\n /**\n * better-sqlite3 Database instance.\n */\n private db: Database.Database | null = null;\n\n /**\n * Connection state flag.\n */\n private connected = false;\n\n /**\n * Serialization queue for concurrent `transaction()` calls.\n *\n * better-sqlite3 is synchronous, single-connection, and rejects nested\n * `BEGIN` with \"cannot start a transaction within a transaction\".\n * When two `await adapter.transaction(...)` calls overlap (e.g. a bulk\n * mutation calling `Promise.allSettled` with N per-row updates), the\n * second one tries to BEGIN while the first is still open and the\n * driver throws.\n *\n * Postgres/MySQL avoid this by allocating a fresh client/connection\n * per transaction from a pool — there is no equivalent for\n * better-sqlite3, so we serialize at the JS layer instead. Every\n * `transaction()` invocation chains onto this promise; only one\n * BEGIN→COMMIT/ROLLBACK section runs at any moment, preserving\n * write-isolation semantics that callers expect from a transactional\n * adapter.\n *\n * Performance impact is the natural floor: SQLite already serializes\n * writers at the file-lock level, so the queue removes failed-call\n * surface area without changing the achievable concurrency.\n */\n private transactionQueue: Promise<unknown> = Promise.resolve();\n\n /**\n * Creates a new SQLite adapter instance.\n *\n * @param config - Adapter configuration\n */\n constructor(config: SqliteAdapterConfig) {\n super();\n this.config = config;\n }\n\n /**\n * Connect to the SQLite database.\n *\n * @remarks\n * This method initializes the database connection. For SQLite, this\n * opens the database file or creates an in-memory database.\n * Also configures WAL mode and foreign keys based on config.\n *\n * @throws {DatabaseError} If connection fails\n */\n async connect(): Promise<void> {\n if (this.connected && this.db) {\n return;\n }\n\n try {\n // Dynamic import of better-sqlite3 to avoid bundling issues\n const BetterSqlite3 = await import(\"better-sqlite3\");\n const Database = BetterSqlite3.default;\n\n // Determine database path\n let dbPath: string;\n if (this.config.memory) {\n dbPath = \":memory:\";\n } else if (this.config.url) {\n // Strip file: prefix if present\n dbPath = this.config.url.replace(/^file:/, \"\");\n } else {\n dbPath = \":memory:\";\n }\n\n // Ensure the parent directory exists for file-backed databases.\n // better-sqlite3 fails with a confusing \"Cannot open database\n // because the directory does not exist\" error if it doesn't, and\n // a fresh clone with `DATABASE_URL=file:./data/app.db` reliably\n // hits this on first run. Create the dir lazily so DATABASE_URL\n // can point anywhere without a separate setup step.\n if (dbPath !== \":memory:\" && !this.config.readonly) {\n const fs = await import(\"node:fs\");\n const path = await import(\"node:path\");\n const dir = path.dirname(path.resolve(dbPath));\n await fs.promises.mkdir(dir, { recursive: true });\n }\n\n // Create database instance\n this.db = new Database(dbPath, {\n readonly: this.config.readonly ?? false,\n timeout: this.config.busyTimeout ?? DEFAULT_CONFIG.busyTimeout,\n });\n\n // Configure WAL mode for better concurrency (unless in-memory or readonly)\n if (\n (this.config.wal ?? DEFAULT_CONFIG.wal) &&\n dbPath !== \":memory:\" &&\n !this.config.readonly\n ) {\n this.db.pragma(\"journal_mode = WAL\");\n }\n\n // Enable foreign keys\n if (this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys) {\n this.db.pragma(\"foreign_keys = ON\");\n }\n\n // F17: check SQLite version. Hard-fails on SQLite <3.38. SQLite has\n // no recognized cloud variants we warn on, so onWarning is omitted.\n // Note: better-sqlite3's bundled SQLite is set by the package\n // version; users on this package's pinned better-sqlite3 (3.45+)\n // will always pass.\n await checkDialectVersion(this.db, \"sqlite\");\n\n this.connected = true;\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection established\", {\n url: dbPath === \":memory:\" ? \"in-memory\" : dbPath,\n wal: this.config.wal ?? DEFAULT_CONFIG.wal,\n foreignKeys: this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys,\n });\n }\n } catch (error) {\n // Clean up on failure\n if (this.db) {\n try {\n this.db.close();\n } catch {\n // Ignore close errors during error handling\n }\n this.db = null;\n }\n throw this.classifyError(error);\n }\n }\n\n /**\n * Disconnect from the SQLite database.\n *\n * @remarks\n * This method closes the database connection and releases resources.\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async disconnect(): Promise<void> {\n // Drop memoized drizzle instances — they wrap the connection being\n // closed and must not survive a reconnect.\n this.drizzleBare = undefined;\n this.drizzleByRelations = new WeakMap();\n if (!this.db) {\n return;\n }\n\n try {\n this.db.close();\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection closed\");\n }\n } finally {\n this.db = null;\n this.connected = false;\n }\n }\n\n /**\n * Check if connected to the database.\n */\n isConnected(): boolean {\n return this.connected && this.db !== null;\n }\n\n /**\n * Get connection pool statistics.\n *\n * @remarks\n * SQLite doesn't use connection pooling, so this returns null.\n * The database is single-file with a single connection.\n */\n getPoolStats(): PoolStats | null {\n // SQLite doesn't have connection pooling\n return null;\n }\n\n /**\n * Execute a raw SQL query.\n *\n * @param sql - SQL query string with $1, $2 placeholders (converted to ? for SQLite)\n * @param params - Query parameters\n * @returns Query results\n *\n * @throws {DatabaseError} If query execution fails\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async executeQuery<T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Convert $1, $2 placeholders to ? for better-sqlite3\n const convertedSql = this.convertPlaceholders(sql);\n\n // Determine if this is a SELECT query or a modifying query\n const trimmedSql = convertedSql.trim().toUpperCase();\n\n // Check for PRAGMA query statements (e.g., \"PRAGMA foreign_keys\").\n // Setting PRAGMAs (e.g., \"PRAGMA foreign_keys = OFF\") don't return data.\n const isPragmaQuery =\n trimmedSql.startsWith(\"PRAGMA\") && !trimmedSql.includes(\"=\");\n\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") ||\n isPragmaQuery ||\n trimmedSql.startsWith(\"WITH\");\n const hasReturning = trimmedSql.includes(\"RETURNING\");\n\n let result: T[];\n\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect || hasReturning) {\n // Use .all() for SELECT queries or queries with RETURNING\n const stmt = db.prepare(convertedSql);\n result = stmt.all(...sanitizedParams) as T[];\n } else {\n // Use .run() for INSERT/UPDATE/DELETE/PRAGMA settings without RETURNING\n const stmt = db.prepare(convertedSql);\n const runResult = stmt.run(...sanitizedParams);\n // Return info about the operation\n result = [\n {\n changes: runResult.changes,\n lastInsertRowid: runResult.lastInsertRowid,\n } as unknown as T,\n ];\n }\n\n // Log query if logger configured\n if (this.config.logger?.query) {\n const durationMs = Date.now() - startTime;\n this.config.logger.query(convertedSql, params, durationMs);\n }\n\n return result;\n } catch (error) {\n throw this.classifyError(error, sql);\n }\n }\n\n /**\n * Execute work within a transaction.\n *\n * @param work - Function containing transactional operations\n * @param options - Transaction options (isolation level not fully supported in SQLite)\n * @returns Result of the work function\n *\n * @remarks\n * Uses better-sqlite3's native transaction handling which:\n * - Automatically commits on success\n * - Automatically rolls back on error\n * - Converts nested transactions to savepoints\n *\n * Note: SQLite uses DEFERRED, IMMEDIATE, or EXCLUSIVE transaction modes\n * rather than isolation levels. This adapter uses IMMEDIATE by default.\n */\n async transaction<T>(\n work: (tx: TransactionContext) => Promise<T>,\n _options?: TransactionOptions\n ): Promise<T> {\n // Why: serialize concurrent transaction() invocations to dodge\n // better-sqlite3's \"cannot start a transaction within a transaction\"\n // error when two awaits overlap on the same connection. See the\n // `transactionQueue` field comment for the full rationale. The queue\n // chains every call onto a tail promise so only one BEGIN → COMMIT\n // section runs at a time; the next call's BEGIN waits for the\n // previous COMMIT/ROLLBACK to finish.\n //\n // Failure isolation: the queue's tail promise must not reject (a\n // rejection would poison every subsequent transaction with the same\n // rejected value). We attach a no-op `.catch` so the chain stays\n // resolved while the actual error propagates back to the original\n // caller through the inner promise.\n const run = async (): Promise<T> => this.runTransaction(work);\n const next = this.transactionQueue.then(run, run);\n this.transactionQueue = next.catch(() => undefined);\n return next;\n }\n\n /**\n * Inner transaction body. Kept private so callers always go through\n * the serialized `transaction()` queue above and the BEGIN/COMMIT\n * pair is never invoked outside of it.\n */\n private async runTransaction<T>(\n work: (tx: TransactionContext) => Promise<T>\n ): Promise<T> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Create transaction context\n const ctx = this.createTransactionContext(db);\n\n // Since better-sqlite3's db.transaction() doesn't support async functions,\n // we manually manage the transaction with BEGIN/COMMIT/ROLLBACK\n db.exec(\"BEGIN IMMEDIATE\");\n\n try {\n const result = await work(ctx);\n db.exec(\"COMMIT\");\n\n // Log success\n if (this.config.logger?.debug) {\n const durationMs = Date.now() - startTime;\n this.config.logger.debug(\"Transaction committed\", {\n durationMs,\n });\n }\n\n return result;\n } catch (error) {\n // Rollback on error\n try {\n db.exec(\"ROLLBACK\");\n } catch {\n // Ignore rollback errors - transaction may already be aborted\n }\n throw error;\n }\n } catch (error) {\n // Work inside a transaction may throw to roll the write back — a refused\n // value, a denied permission — and that is the application's verdict, not\n // the driver's failure. Classifying it would replace its code and payload\n // with a generic database error, so a caller that asked for a refusal is\n // handed an unexplained failure and the per-field detail never arrives.\n if (isApplicationError(error)) throw error;\n throw this.classifyError(error);\n }\n }\n\n /**\n * Get SQLite database capabilities.\n *\n * @remarks\n * SQLite capabilities:\n * - JSON support (not JSONB)\n * - No arrays\n * - No native ILIKE\n * - RETURNING clause (3.35+)\n * - Savepoints supported\n * - ON CONFLICT supported\n */\n getCapabilities(): DatabaseCapabilities {\n return {\n dialect: \"sqlite\",\n supportsJsonb: false, // SQLite uses JSON, not JSONB\n supportsJson: true,\n supportsArrays: false, // SQLite doesn't support array types\n supportsGeneratedColumns: true, // SQLite 3.31+\n supportsFts: true, // SQLite FTS5\n supportsIlike: false, // No native ILIKE, use LOWER() LIKE\n supportsReturning: true, // SQLite 3.35+\n supportsSavepoints: true, // SQLite supports savepoints\n supportsOnConflict: true, // ON CONFLICT clause\n maxParamsPerQuery: 999, // SQLite SQLITE_MAX_VARIABLE_NUMBER default\n maxIdentifierLength: 128, // SQLite doesn't have a strict limit\n };\n }\n\n /**\n * Override insertMany for bulk insert optimization.\n *\n * @remarks\n * Uses a single multi-row INSERT statement for better performance.\n */\n override async insertMany<T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> {\n if (data.length === 0) {\n return [];\n }\n\n const db = this.ensureDb();\n\n // For single record, use parent implementation\n if (data.length === 1) {\n const result = await this.insert<T>(table, data[0], options);\n return [result];\n }\n\n // Build multi-row INSERT\n const columns = Object.keys(data[0]);\n const params: SqlParam[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of data) {\n const placeholders: string[] = [];\n for (const col of columns) {\n params.push(sanitizeSqliteValue(record[col]) as SqlParam);\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n const columnList = columns\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(\", \")}`;\n\n // Add RETURNING clause\n if (options?.returning) {\n const returning =\n options.returning === \"*\"\n ? \"*\"\n : options.returning.map(col => this.escapeIdentifier(col)).join(\", \");\n sql += ` RETURNING ${returning}`;\n } else {\n sql += \" RETURNING *\";\n }\n\n try {\n const stmt = db.prepare(sql);\n const rows = stmt.all(...(params as unknown[])) as T[];\n // SQLite stores a timestamp as an integer and hands it back as one. The\n // Drizzle read paths decode it through the column definition, so without\n // this a bulk insert answers with numbers where a read answers `Date`.\n const tableObj = this.getTableObject(table);\n return rows.map(r => this.mapRowFromRawSql(tableObj, r));\n } catch (error) {\n throw this.handleQueryError(error, \"insertMany\", table);\n }\n }\n\n // ============================================================\n // Protected Helper Methods\n // ============================================================\n\n /**\n * Ensures database is connected and returns it.\n *\n * @throws {DatabaseError} If not connected\n */\n private ensureDb(): Database.Database {\n if (!this.db) {\n throw createDatabaseError({\n kind: \"connection\",\n message: \"SqliteAdapter is not connected. Call connect() first.\",\n });\n }\n return this.db;\n }\n\n /**\n * Return the typed Drizzle instance for SQLite.\n * Guarded for server-only usage and requires an active connection.\n *\n * @param schema - Optional schema for relational queries (db.query.*)\n * @returns Drizzle ORM instance wrapping the better-sqlite3 connection\n * @throws {Error} If called in browser or not connected\n */\n getDrizzle<T = BetterSQLite3Database<AnyRelations>>(\n relations?: AnyRelations\n ): T {\n if (typeof window !== \"undefined\") {\n throw new Error(\"getDrizzle() is server-only\");\n }\n const db = this.ensureDb();\n // drizzle v1 removed the positional (client, config) form — positional\n // calls silently open a NEW :memory: database. Object form only.\n if (!relations) {\n this.drizzleBare ??= drizzle({ client: db });\n return this.drizzleBare as T;\n }\n let cached = this.drizzleByRelations.get(relations);\n if (!cached) {\n cached = drizzle({ client: db, relations });\n this.drizzleByRelations.set(relations, cached);\n }\n return cached as T;\n }\n\n /**\n * Convert $1, $2 placeholders to ? for better-sqlite3.\n *\n * @param sql - SQL with PostgreSQL-style placeholders\n * @returns SQL with ? placeholders\n */\n private convertPlaceholders(sql: string): string {\n // Replace $1, $2, etc. with ?\n return sql.replace(/\\$\\d+/g, \"?\");\n }\n\n /**\n * Creates a TransactionContext for the given database connection.\n */\n private createTransactionContext(db: Database.Database): TransactionContext {\n // better-sqlite3 is synchronous; the methods below are async to satisfy\n // the TransactionContext contract shared with truly-async dialect adapters.\n // Bind a Drizzle instance to the transaction's connection so the delegated\n // CRUD methods run inside it. better-sqlite3 is single-connection, so this\n // is the same connection getDrizzle() would use, but binding explicitly\n // keeps the three adapters consistent and correct if that ever changes.\n // Built lazily and memoized: transactions that use only raw execute/insert\n // never construct it.\n const buildTxExecutor = () => drizzle({ client: db });\n let txExecutor: ReturnType<typeof buildTxExecutor> | undefined;\n const txDb = () => (txExecutor ??= buildTxExecutor());\n return {\n // SQLite has no row-level locking and needs none here: `withTransaction`\n // opens SQLite transactions with BEGIN IMMEDIATE, which takes the write\n // lock up front and serializes writers for the whole transaction.\n lockRow: (): Promise<void> => Promise.resolve(),\n\n // `run`, not `all`: better-sqlite3 throws on a statement that returns no\n // rows, which is every statement this method exists to carry. The Drizzle\n // instance is the transaction-bound one, so the statement runs inside this\n // transaction.\n //\n // better-sqlite3 is synchronous, so this resolves an already-settled\n // promise rather than being declared `async` over a body that never\n // awaits.\n runStatement: (statement: SQL): Promise<void> => {\n txDb().run(statement);\n return Promise.resolve();\n },\n\n // `all`, not `run`: this is the reading half, and better-sqlite3 returns\n // rows only from `all`. Synchronous, so the promise is already settled.\n queryStatement: <T = Record<string, unknown>>(\n statement: SQL\n ): Promise<T[]> => Promise.resolve(txDb().all(statement)),\n\n // eslint-disable-next-line @typescript-eslint/require-await\n execute: async <T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> => {\n const convertedSql = this.convertPlaceholders(sql);\n const trimmedSql = convertedSql.trim().toUpperCase();\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") || trimmedSql.includes(\"RETURNING\");\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect) {\n const stmt = db.prepare(convertedSql);\n return stmt.all(...sanitizedParams) as T[];\n } else {\n const stmt = db.prepare(convertedSql);\n const result = stmt.run(...sanitizedParams);\n return [\n {\n changes: result.changes,\n lastInsertRowid: result.lastInsertRowid,\n } as unknown as T,\n ];\n }\n },\n\n // Keys only, unlike the PostgreSQL and MySQL raw paths, which also encode\n // their date values. `sanitizeSqliteValue` below binds a `Date` as unix\n // seconds, and an instant carries no zone -- so what SQLite stores does\n // not depend on the server's timezone and there is nothing to correct.\n // eslint-disable-next-line @typescript-eslint/require-await\n insert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options?: InsertOptions\n ): Promise<T> => {\n const mapped = this.mapKeysToSqlColumns(\n this.getTableObject(table),\n data\n );\n const columns = Object.keys(mapped);\n const values = Object.values(mapped).map(sanitizeSqliteValue);\n const placeholders = values.map(() => \"?\").join(\", \");\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES (${placeholders})`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n // No RETURNING clause: better-sqlite3's .all() rejects a statement\n // that returns no columns, so run it and return nothing.\n stmt.run(...values);\n return undefined as T;\n }\n const rows = stmt.all(...values) as T[];\n // Return JS property-named keys AND Drizzle-decoded date values, so a\n // write inside a transaction answers with the same representation a\n // read of the same row gives.\n return this.mapRowFromRawSql(this.getTableObject(table), rows[0]);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n insertMany: async <T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> => {\n if (data.length === 0) return [];\n\n const tableObj = this.getTableObject(table);\n const mappedRecords = data.map(r =>\n this.mapKeysToSqlColumns(tableObj, r)\n );\n const columns = Object.keys(mappedRecords[0]);\n const allValues: unknown[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of mappedRecords) {\n const placeholders: string[] = [];\n for (const col of columns) {\n allValues.push(sanitizeSqliteValue(record[col]));\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES ${valuesClauses.join(\", \")}`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n stmt.run(...allValues);\n return [];\n }\n return (stmt.all(...allValues) as T[]).map(r =>\n this.mapRowFromRawSql(tableObj, r)\n );\n },\n\n // TransactionContext CRUD methods delegate to the adapter's Drizzle CRUD\n // but pass the transaction-bound executor so they run inside this\n // transaction rather than on the pool.\n select: async <T = unknown>(\n table: string,\n options?: SelectOptions\n ): Promise<T[]> => {\n return this.select<T>(table, options, txDb());\n },\n\n selectOne: async <T = unknown>(\n table: string,\n options?: SelectOptions\n ): Promise<T | null> => {\n return this.selectOne<T>(table, options, txDb());\n },\n\n update: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n where: WhereClause,\n options?: UpdateOptions\n ): Promise<T[]> => {\n return this.update<T>(table, data, where, options, txDb());\n },\n\n delete: async (\n table: string,\n where: WhereClause,\n options?: DeleteOptions\n ): Promise<number> => {\n return this.delete(table, where, options, txDb());\n },\n\n upsert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options: UpsertOptions\n ): Promise<T> => {\n return this.upsert<T>(table, data, options, txDb());\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n savepoint: async (name: string): Promise<void> => {\n db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n rollbackToSavepoint: async (name: string): Promise<void> => {\n db.exec(`ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n releaseSavepoint: async (name: string): Promise<void> => {\n db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // Expose the transaction-bound Drizzle instance so callers can run\n // Drizzle sql templates inside this transaction (junction-table writes\n // need this to be atomic with the entry write). Reuses the memoized\n // txDb() built for the delegated CRUD methods.\n getDrizzle: <T = unknown>(): T => txDb() as T,\n };\n }\n\n /**\n * Classifies a SQLite error into a DatabaseError.\n *\n * @param error - Original error from better-sqlite3\n * @param sql - SQL statement that caused the error (optional)\n * @returns DatabaseError with proper classification\n */\n private classifyError(error: unknown, sql?: string): DatabaseError {\n // Why short-circuit on existing DatabaseError: F17's\n // UnsupportedDialectVersionError is already a typed DatabaseError with\n // kind: \"unsupported_version\" plus detectedVersion/requiredVersion\n // fields. Re-wrapping it here would erase those fields.\n if (isDatabaseError(error)) return error;\n\n const sqliteError = error as {\n code?: string;\n message?: string;\n name?: string;\n };\n\n // Determine error kind from SQLite error code\n let kind: DatabaseErrorKind = \"unknown\";\n\n if (sqliteError.code) {\n kind = SQLITE_ERROR_CODES[sqliteError.code] || \"unknown\";\n } else if (sqliteError.message) {\n // Try to extract error type from message\n const msg = sqliteError.message.toUpperCase();\n if (msg.includes(\"UNIQUE CONSTRAINT\")) {\n kind = \"unique_violation\";\n } else if (msg.includes(\"FOREIGN KEY CONSTRAINT\")) {\n kind = \"foreign_key_violation\";\n } else if (msg.includes(\"NOT NULL CONSTRAINT\")) {\n kind = \"not_null_violation\";\n } else if (msg.includes(\"CHECK CONSTRAINT\")) {\n kind = \"check_violation\";\n } else if (msg.includes(\"BUSY\") || msg.includes(\"LOCKED\")) {\n kind = \"timeout\";\n } else if (\n msg.includes(\"SQLITE_CANTOPEN\") ||\n msg.includes(\"UNABLE TO OPEN\")\n ) {\n kind = \"connection\";\n }\n }\n\n // Build error message\n let message = sqliteError.message ?? String(error);\n if (sql && kind === \"query\") {\n message = `Query failed: ${message}`;\n }\n\n return createDatabaseError({\n kind,\n message,\n code: sqliteError.code,\n cause: error instanceof Error ? error : undefined,\n });\n }\n\n /**\n * Override handleQueryError to use SQLite-specific classification.\n */\n protected override handleQueryError(\n error: unknown,\n operation: string,\n table: string\n ): DatabaseError {\n const dbError = this.classifyError(error);\n\n // Add operation context if not already present\n if (!dbError.message.includes(operation)) {\n dbError.message = `${operation} operation failed on table '${table}': ${dbError.message}`;\n }\n\n if (!dbError.table) {\n dbError.table = table;\n }\n\n return dbError;\n }\n}\n\n/**\n * Create a SQLite database adapter.\n *\n * @param config - SQLite adapter configuration\n * @returns A new SqliteAdapter instance\n *\n * @example\n * ```typescript\n * // Simple usage with file path\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * // In-memory database\n * const memAdapter = createSqliteAdapter({\n * memory: true,\n * });\n *\n * // Full configuration\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * wal: true,\n * foreignKeys: true,\n * busyTimeout: 5000,\n * logger: {\n * query: (sql, params, duration) => console.log(`Query: ${sql}`),\n * },\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport function createSqliteAdapter(\n config: SqliteAdapterConfig\n): SqliteAdapter {\n return new SqliteAdapter(config);\n}\n\n/**\n * Type guard to check if a value is a SqliteAdapter.\n *\n * @param value - Value to check\n * @returns True if value is a SqliteAdapter instance\n *\n * @example\n * ```typescript\n * if (isSqliteAdapter(adapter)) {\n * // TypeScript knows adapter is SqliteAdapter\n * console.log('Using SQLite');\n * }\n * ```\n */\nexport function isSqliteAdapter(value: unknown): value is SqliteAdapter {\n return value instanceof SqliteAdapter;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["DrizzleAdapter","checkDialectVersion","isApplicationError","createDatabaseError","drizzle","isDatabaseError"],"mappings":";;;;;;;;AAkGO,IAAM,O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CAAY,OAAA,IAAW,MAAA,CAAO,KAAK,CAAA;AACjD,IAAA,IAAI,GAAA,IAAO,SAAS,OAAA,EAAS;AAC3B,MAAA,OAAA,GAAU,iBAAiB,OAAO,CAAA,CAAA;AAAA,IACpC;AAEA,IAAA,OAAOF,yBAAA,CAAoB;AAAA,MACzB,IAAA;AAAA,MACA,OAAA;AAAA,MACA,MAAM,WAAA,CAAY,IAAA;AAAA,MAClB,KAAA,EAAO,KAAA,YAAiB,KAAA,GAAQ,KAAA,GAAQ;AAAA,KACzC,CAAA;AAAA,EACH;AACF;AAwBA,SAAS,gBAAgB,KAAA,EAAkC;AACzD,EAAA,IAAI,MAAA,GAAS,KAAA;AACb,EAAA,KAAA,IAAS,QAAQ,CAAA,EAAG,KAAA,GAAQ,EAAA,IAAM,MAAA,IAAU,MAAM,KAAA,EAAA,EAAS;AACzD,IAAA,IAAI,OAAO,MAAA,CAAO,IAAA,KAAS,QAAA,IAAY,MAAA,CAAO,KAAK,MAAA,GAAS,CAAA;AAC1D,MAAA,OAAO,MAAA;AACT,IAAA,MAAA,GAAS,MAAA,CAAO,KAAA;AAAA,EAClB;AACA,EAAA,OAAO,SAAS,EAAC;AACnB;AAkCO,SAAS,oBACd,MAAA,EACe;AACf,EAAA,OAAO,IAAI,cAAc,MAAM,CAAA;AACjC;AAgBO,SAAS,gBAAgB,KAAA,EAAwC;AACtE,EAAA,OAAO,KAAA,YAAiB,aAAA;AAC1B","file":"index.cjs","sourcesContent":["/**\n * @nextlyhq/adapter-sqlite\n *\n * SQLite database adapter for Nextly.\n * Extends DrizzleAdapter from @nextlyhq/adapter-drizzle to provide SQLite-specific functionality.\n *\n * @remarks\n * This adapter uses the better-sqlite3 package for database connectivity and provides:\n * - Synchronous API wrapped for async interface compatibility\n * - Full transaction support with savepoints (via better-sqlite3's native transaction handling)\n * - CRUD operations with RETURNING clause support (SQLite 3.35+)\n * - WAL mode for better concurrent read performance\n * - In-memory and file-based database support\n *\n * @example\n * ```typescript\n * import { createSqliteAdapter } from '@nextlyhq/adapter-sqlite';\n *\n * const adapter = createSqliteAdapter({\n * url: 'file:./data/app.db',\n * });\n *\n * await adapter.connect();\n *\n * // Query data\n * const users = await adapter.select('users', {\n * where: { and: [{ column: 'status', op: '=', value: 'active' }] },\n * limit: 10,\n * });\n *\n * await adapter.disconnect();\n * ```\n *\n * @packageDocumentation\n */\n\nimport { DrizzleAdapter } from \"@nextlyhq/adapter-drizzle\";\n// F17: connect-time DB version check shared across all adapters.\nimport {\n createDatabaseError,\n isDatabaseError,\n type SqliteAdapterConfig,\n type DatabaseCapabilities,\n type PoolStats,\n type TransactionContext,\n type TransactionOptions,\n type SqlParam,\n type WhereClause,\n type WhereCondition,\n type WhereOperator,\n type SelectOptions,\n type InsertOptions,\n type UpdateOptions,\n type DeleteOptions,\n type UpsertOptions,\n type OrderBySpec,\n type JoinSpec,\n type DatabaseError,\n type DatabaseErrorKind,\n type BaseAdapterConfig,\n type AdapterLogger,\n isApplicationError,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport { checkDialectVersion } from \"@nextlyhq/adapter-drizzle/version-check\";\nimport type Database from \"better-sqlite3\";\nimport type { AnyRelations, SQL } from \"drizzle-orm\";\nimport {\n drizzle,\n type BetterSQLite3Database,\n} from \"drizzle-orm/better-sqlite3\";\n\n// Re-export types for convenience\nexport type {\n SqliteAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n WhereClause,\n WhereCondition,\n WhereOperator,\n SelectOptions,\n InsertOptions,\n UpdateOptions,\n DeleteOptions,\n UpsertOptions,\n OrderBySpec,\n JoinSpec,\n DatabaseError,\n DatabaseErrorKind,\n BaseAdapterConfig,\n AdapterLogger,\n};\n\n/**\n * Package version\n */\nexport const VERSION = \"0.1.0\";\n\n/**\n * SQLite error codes mapping to DatabaseErrorKind.\n *\n * @see https://sqlite.org/rescode.html\n */\nconst SQLITE_ERROR_CODES: Record<string, DatabaseErrorKind> = {\n // Constraint violations\n SQLITE_CONSTRAINT: \"constraint\",\n SQLITE_CONSTRAINT_UNIQUE: \"unique_violation\",\n SQLITE_CONSTRAINT_PRIMARYKEY: \"unique_violation\",\n SQLITE_CONSTRAINT_FOREIGNKEY: \"foreign_key_violation\",\n SQLITE_CONSTRAINT_NOTNULL: \"not_null_violation\",\n SQLITE_CONSTRAINT_CHECK: \"check_violation\",\n\n // Busy/locked errors\n SQLITE_BUSY: \"timeout\",\n SQLITE_LOCKED: \"timeout\",\n\n // Connection errors\n SQLITE_CANTOPEN: \"connection\",\n SQLITE_NOTADB: \"connection\",\n SQLITE_CORRUPT: \"connection\",\n\n // Query errors\n SQLITE_ERROR: \"query\",\n SQLITE_MISUSE: \"query\",\n SQLITE_RANGE: \"query\",\n};\n\n/**\n * Default configuration values.\n */\nconst DEFAULT_CONFIG = {\n busyTimeout: 5000,\n wal: true,\n foreignKeys: true,\n};\n\n/**\n * Converts a JavaScript value to a type that better-sqlite3 can bind.\n * better-sqlite3 only accepts: number, string, bigint, Buffer, null.\n *\n * These conversions have to match what the schema declares, because the rows\n * they write are read back through it. Every column here that takes a `Date`\n * is `integer({ mode: \"timestamp\" })` — the system columns and user date\n * fields in `runtime-schema-generator.ts`, and the core tables, whose date\n * columns are all INTEGER. So a date is bound as unix seconds, which is what\n * that mode reads.\n */\nfunction sanitizeSqliteValue(v: unknown): unknown {\n if (v === undefined) return null;\n if (typeof v === \"boolean\") return v ? 1 : 0;\n // Seconds, not an ISO string. SQLite stores whatever it is given regardless\n // of the declared type, so a string lands in the column without complaint\n // and only fails on the way back out, where the timestamp decoder reads it\n // as a number and yields an Invalid Date.\n if (v instanceof Date) return Math.floor(v.getTime() / 1000);\n if (v !== null && typeof v === \"object\") return JSON.stringify(v);\n return v;\n}\n\n/**\n * SQLite database adapter for Nextly.\n *\n * Extends the base DrizzleAdapter to provide SQLite-specific functionality\n * using the better-sqlite3 package.\n *\n * @remarks\n * SQLite has some differences from PostgreSQL:\n * - Synchronous API (wrapped for async compatibility)\n * - No connection pooling (single file/connection)\n * - RETURNING clause supported (SQLite 3.35+)\n * - Savepoints supported via nested transactions\n * - No native ILIKE (uses LOWER() LIKE workaround)\n * - JSON support (not JSONB)\n * - No array types\n *\n * @example\n * ```typescript\n * const adapter = new SqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport class SqliteAdapter extends DrizzleAdapter {\n // getDrizzle memoization: drizzle v1's constructor builds a relational\n // query builder per table in the relations config (~40 tables), and the\n // service layer resolves an instance on every db access — construct once\n // per relations object (identity-stable: the schema registry caches it\n // and hands out a NEW object on invalidation, which naturally misses\n // this cache and produces a fresh instance).\n private drizzleByRelations = new WeakMap<AnyRelations, unknown>();\n private drizzleBare: unknown;\n\n /**\n * The database dialect - always 'sqlite' for this adapter.\n */\n readonly dialect = \"sqlite\" as const;\n\n /**\n * Adapter configuration.\n */\n protected readonly config: SqliteAdapterConfig;\n\n /**\n * better-sqlite3 Database instance.\n */\n private db: Database.Database | null = null;\n\n /**\n * Connection state flag.\n */\n private connected = false;\n\n /**\n * Serialization queue for concurrent `transaction()` calls.\n *\n * better-sqlite3 is synchronous, single-connection, and rejects nested\n * `BEGIN` with \"cannot start a transaction within a transaction\".\n * When two `await adapter.transaction(...)` calls overlap (e.g. a bulk\n * mutation calling `Promise.allSettled` with N per-row updates), the\n * second one tries to BEGIN while the first is still open and the\n * driver throws.\n *\n * Postgres/MySQL avoid this by allocating a fresh client/connection\n * per transaction from a pool — there is no equivalent for\n * better-sqlite3, so we serialize at the JS layer instead. Every\n * `transaction()` invocation chains onto this promise; only one\n * BEGIN→COMMIT/ROLLBACK section runs at any moment, preserving\n * write-isolation semantics that callers expect from a transactional\n * adapter.\n *\n * Performance impact is the natural floor: SQLite already serializes\n * writers at the file-lock level, so the queue removes failed-call\n * surface area without changing the achievable concurrency.\n */\n private transactionQueue: Promise<unknown> = Promise.resolve();\n\n /**\n * Creates a new SQLite adapter instance.\n *\n * @param config - Adapter configuration\n */\n constructor(config: SqliteAdapterConfig) {\n super();\n this.config = config;\n }\n\n /**\n * Connect to the SQLite database.\n *\n * @remarks\n * This method initializes the database connection. For SQLite, this\n * opens the database file or creates an in-memory database.\n * Also configures WAL mode and foreign keys based on config.\n *\n * @throws {DatabaseError} If connection fails\n */\n async connect(): Promise<void> {\n if (this.connected && this.db) {\n return;\n }\n\n try {\n // Dynamic import of better-sqlite3 to avoid bundling issues\n const BetterSqlite3 = await import(\"better-sqlite3\");\n const Database = BetterSqlite3.default;\n\n // Determine database path\n let dbPath: string;\n if (this.config.memory) {\n dbPath = \":memory:\";\n } else if (this.config.url) {\n // Strip file: prefix if present\n dbPath = this.config.url.replace(/^file:/, \"\");\n } else {\n dbPath = \":memory:\";\n }\n\n // Ensure the parent directory exists for file-backed databases.\n // better-sqlite3 fails with a confusing \"Cannot open database\n // because the directory does not exist\" error if it doesn't, and\n // a fresh clone with `DATABASE_URL=file:./data/app.db` reliably\n // hits this on first run. Create the dir lazily so DATABASE_URL\n // can point anywhere without a separate setup step.\n if (dbPath !== \":memory:\" && !this.config.readonly) {\n const fs = await import(\"node:fs\");\n const path = await import(\"node:path\");\n const dir = path.dirname(path.resolve(dbPath));\n await fs.promises.mkdir(dir, { recursive: true });\n }\n\n // Create database instance\n this.db = new Database(dbPath, {\n readonly: this.config.readonly ?? false,\n timeout: this.config.busyTimeout ?? DEFAULT_CONFIG.busyTimeout,\n });\n\n // Configure WAL mode for better concurrency (unless in-memory or readonly)\n if (\n (this.config.wal ?? DEFAULT_CONFIG.wal) &&\n dbPath !== \":memory:\" &&\n !this.config.readonly\n ) {\n this.db.pragma(\"journal_mode = WAL\");\n }\n\n // Enable foreign keys\n if (this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys) {\n this.db.pragma(\"foreign_keys = ON\");\n }\n\n // F17: check SQLite version. Hard-fails on SQLite <3.38. SQLite has\n // no recognized cloud variants we warn on, so onWarning is omitted.\n // Note: better-sqlite3's bundled SQLite is set by the package\n // version; users on this package's pinned better-sqlite3 (3.45+)\n // will always pass.\n await checkDialectVersion(this.db, \"sqlite\");\n\n this.connected = true;\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection established\", {\n url: dbPath === \":memory:\" ? \"in-memory\" : dbPath,\n wal: this.config.wal ?? DEFAULT_CONFIG.wal,\n foreignKeys: this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys,\n });\n }\n } catch (error) {\n // Clean up on failure\n if (this.db) {\n try {\n this.db.close();\n } catch {\n // Ignore close errors during error handling\n }\n this.db = null;\n }\n throw this.classifyError(error);\n }\n }\n\n /**\n * Disconnect from the SQLite database.\n *\n * @remarks\n * This method closes the database connection and releases resources.\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async disconnect(): Promise<void> {\n // Drop memoized drizzle instances — they wrap the connection being\n // closed and must not survive a reconnect.\n this.drizzleBare = undefined;\n this.drizzleByRelations = new WeakMap();\n if (!this.db) {\n return;\n }\n\n try {\n this.db.close();\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection closed\");\n }\n } finally {\n this.db = null;\n this.connected = false;\n }\n }\n\n /**\n * Check if connected to the database.\n */\n isConnected(): boolean {\n return this.connected && this.db !== null;\n }\n\n /**\n * Get connection pool statistics.\n *\n * @remarks\n * SQLite doesn't use connection pooling, so this returns null.\n * The database is single-file with a single connection.\n */\n getPoolStats(): PoolStats | null {\n // SQLite doesn't have connection pooling\n return null;\n }\n\n /**\n * Execute a raw SQL query.\n *\n * @param sql - SQL query string with $1, $2 placeholders (converted to ? for SQLite)\n * @param params - Query parameters\n * @returns Query results\n *\n * @throws {DatabaseError} If query execution fails\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async executeQuery<T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Convert $1, $2 placeholders to ? for better-sqlite3\n const convertedSql = this.convertPlaceholders(sql);\n\n // Determine if this is a SELECT query or a modifying query\n const trimmedSql = convertedSql.trim().toUpperCase();\n\n // Check for PRAGMA query statements (e.g., \"PRAGMA foreign_keys\").\n // Setting PRAGMAs (e.g., \"PRAGMA foreign_keys = OFF\") don't return data.\n const isPragmaQuery =\n trimmedSql.startsWith(\"PRAGMA\") && !trimmedSql.includes(\"=\");\n\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") ||\n isPragmaQuery ||\n trimmedSql.startsWith(\"WITH\");\n const hasReturning = trimmedSql.includes(\"RETURNING\");\n\n let result: T[];\n\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect || hasReturning) {\n // Use .all() for SELECT queries or queries with RETURNING\n const stmt = db.prepare(convertedSql);\n result = stmt.all(...sanitizedParams) as T[];\n } else {\n // Use .run() for INSERT/UPDATE/DELETE/PRAGMA settings without RETURNING\n const stmt = db.prepare(convertedSql);\n const runResult = stmt.run(...sanitizedParams);\n // Return info about the operation\n result = [\n {\n changes: runResult.changes,\n lastInsertRowid: runResult.lastInsertRowid,\n } as unknown as T,\n ];\n }\n\n // Log query if logger configured\n if (this.config.logger?.query) {\n const durationMs = Date.now() - startTime;\n this.config.logger.query(convertedSql, params, durationMs);\n }\n\n return result;\n } catch (error) {\n throw this.classifyError(error, sql);\n }\n }\n\n /**\n * Execute work within a transaction.\n *\n * @param work - Function containing transactional operations\n * @param options - Transaction options (isolation level not fully supported in SQLite)\n * @returns Result of the work function\n *\n * @remarks\n * Uses better-sqlite3's native transaction handling which:\n * - Automatically commits on success\n * - Automatically rolls back on error\n * - Converts nested transactions to savepoints\n *\n * Note: SQLite uses DEFERRED, IMMEDIATE, or EXCLUSIVE transaction modes\n * rather than isolation levels. This adapter uses IMMEDIATE by default.\n */\n async transaction<T>(\n work: (tx: TransactionContext) => Promise<T>,\n _options?: TransactionOptions\n ): Promise<T> {\n // Why: serialize concurrent transaction() invocations to dodge\n // better-sqlite3's \"cannot start a transaction within a transaction\"\n // error when two awaits overlap on the same connection. See the\n // `transactionQueue` field comment for the full rationale. The queue\n // chains every call onto a tail promise so only one BEGIN → COMMIT\n // section runs at a time; the next call's BEGIN waits for the\n // previous COMMIT/ROLLBACK to finish.\n //\n // Failure isolation: the queue's tail promise must not reject (a\n // rejection would poison every subsequent transaction with the same\n // rejected value). We attach a no-op `.catch` so the chain stays\n // resolved while the actual error propagates back to the original\n // caller through the inner promise.\n const run = async (): Promise<T> => this.runTransaction(work);\n const next = this.transactionQueue.then(run, run);\n this.transactionQueue = next.catch(() => undefined);\n return next;\n }\n\n /**\n * Inner transaction body. Kept private so callers always go through\n * the serialized `transaction()` queue above and the BEGIN/COMMIT\n * pair is never invoked outside of it.\n */\n private async runTransaction<T>(\n work: (tx: TransactionContext) => Promise<T>\n ): Promise<T> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Create transaction context\n const ctx = this.createTransactionContext(db);\n\n // Since better-sqlite3's db.transaction() doesn't support async functions,\n // we manually manage the transaction with BEGIN/COMMIT/ROLLBACK\n db.exec(\"BEGIN IMMEDIATE\");\n\n try {\n const result = await work(ctx);\n db.exec(\"COMMIT\");\n\n // Log success\n if (this.config.logger?.debug) {\n const durationMs = Date.now() - startTime;\n this.config.logger.debug(\"Transaction committed\", {\n durationMs,\n });\n }\n\n return result;\n } catch (error) {\n // Rollback on error\n try {\n db.exec(\"ROLLBACK\");\n } catch {\n // Ignore rollback errors - transaction may already be aborted\n }\n throw error;\n }\n } catch (error) {\n // Work inside a transaction may throw to roll the write back — a refused\n // value, a denied permission — and that is the application's verdict, not\n // the driver's failure. Classifying it would replace its code and payload\n // with a generic database error, so a caller that asked for a refusal is\n // handed an unexplained failure and the per-field detail never arrives.\n if (isApplicationError(error)) throw error;\n throw this.classifyError(error);\n }\n }\n\n /**\n * Get SQLite database capabilities.\n *\n * @remarks\n * SQLite capabilities:\n * - JSON support (not JSONB)\n * - No arrays\n * - No native ILIKE\n * - RETURNING clause (3.35+)\n * - Savepoints supported\n * - ON CONFLICT supported\n */\n getCapabilities(): DatabaseCapabilities {\n return {\n dialect: \"sqlite\",\n supportsJsonb: false, // SQLite uses JSON, not JSONB\n supportsJson: true,\n supportsArrays: false, // SQLite doesn't support array types\n supportsGeneratedColumns: true, // SQLite 3.31+\n supportsFts: true, // SQLite FTS5\n supportsIlike: false, // No native ILIKE, use LOWER() LIKE\n supportsReturning: true, // SQLite 3.35+\n supportsSavepoints: true, // SQLite supports savepoints\n supportsOnConflict: true, // ON CONFLICT clause\n maxParamsPerQuery: 999, // SQLite SQLITE_MAX_VARIABLE_NUMBER default\n maxIdentifierLength: 128, // SQLite doesn't have a strict limit\n };\n }\n\n /**\n * Override insertMany for bulk insert optimization.\n *\n * @remarks\n * Uses a single multi-row INSERT statement for better performance.\n */\n override async insertMany<T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> {\n if (data.length === 0) {\n return [];\n }\n\n const db = this.ensureDb();\n\n // For single record, use parent implementation\n if (data.length === 1) {\n const result = await this.insert<T>(table, data[0], options);\n return [result];\n }\n\n // Build multi-row INSERT\n const columns = Object.keys(data[0]);\n const params: SqlParam[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of data) {\n const placeholders: string[] = [];\n for (const col of columns) {\n params.push(sanitizeSqliteValue(record[col]) as SqlParam);\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n const columnList = columns\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(\", \")}`;\n\n // Add RETURNING clause\n if (options?.returning) {\n const returning =\n options.returning === \"*\"\n ? \"*\"\n : options.returning.map(col => this.escapeIdentifier(col)).join(\", \");\n sql += ` RETURNING ${returning}`;\n } else {\n sql += \" RETURNING *\";\n }\n\n try {\n const stmt = db.prepare(sql);\n const rows = stmt.all(...(params as unknown[])) as T[];\n // SQLite stores a timestamp as an integer and hands it back as one. The\n // Drizzle read paths decode it through the column definition, so without\n // this a bulk insert answers with numbers where a read answers `Date`.\n const tableObj = this.getTableObject(table);\n return rows.map(r => this.mapRowFromRawSql(tableObj, r));\n } catch (error) {\n throw this.handleQueryError(error, \"insertMany\", table);\n }\n }\n\n // ============================================================\n // Protected Helper Methods\n // ============================================================\n\n /**\n * Ensures database is connected and returns it.\n *\n * @throws {DatabaseError} If not connected\n */\n private ensureDb(): Database.Database {\n if (!this.db) {\n throw createDatabaseError({\n kind: \"connection\",\n message: \"SqliteAdapter is not connected. Call connect() first.\",\n });\n }\n return this.db;\n }\n\n /**\n * Return the typed Drizzle instance for SQLite.\n * Guarded for server-only usage and requires an active connection.\n *\n * @param schema - Optional schema for relational queries (db.query.*)\n * @returns Drizzle ORM instance wrapping the better-sqlite3 connection\n * @throws {Error} If called in browser or not connected\n */\n getDrizzle<T = BetterSQLite3Database<AnyRelations>>(\n relations?: AnyRelations\n ): T {\n if (typeof window !== \"undefined\") {\n throw new Error(\"getDrizzle() is server-only\");\n }\n const db = this.ensureDb();\n // drizzle v1 removed the positional (client, config) form — positional\n // calls silently open a NEW :memory: database. Object form only.\n if (!relations) {\n this.drizzleBare ??= drizzle({ client: db });\n return this.drizzleBare as T;\n }\n let cached = this.drizzleByRelations.get(relations);\n if (!cached) {\n cached = drizzle({ client: db, relations });\n this.drizzleByRelations.set(relations, cached);\n }\n return cached as T;\n }\n\n /**\n * Convert $1, $2 placeholders to ? for better-sqlite3.\n *\n * @param sql - SQL with PostgreSQL-style placeholders\n * @returns SQL with ? placeholders\n */\n private convertPlaceholders(sql: string): string {\n // Replace $1, $2, etc. with ?\n return sql.replace(/\\$\\d+/g, \"?\");\n }\n\n /**\n * Creates a TransactionContext for the given database connection.\n */\n private createTransactionContext(db: Database.Database): TransactionContext {\n // better-sqlite3 is synchronous; the methods below are async to satisfy\n // the TransactionContext contract shared with truly-async dialect adapters.\n // Bind a Drizzle instance to the transaction's connection so the delegated\n // CRUD methods run inside it. better-sqlite3 is single-connection, so this\n // is the same connection getDrizzle() would use, but binding explicitly\n // keeps the three adapters consistent and correct if that ever changes.\n // Built lazily and memoized: transactions that use only raw execute/insert\n // never construct it.\n const buildTxExecutor = () => drizzle({ client: db });\n let txExecutor: ReturnType<typeof buildTxExecutor> | undefined;\n const txDb = () => (txExecutor ??= buildTxExecutor());\n return {\n // SQLite has no row-level locking and needs none here: `withTransaction`\n // opens SQLite transactions with BEGIN IMMEDIATE, which takes the write\n // lock up front and serializes writers for the whole transaction.\n lockRow: (): Promise<void> => Promise.resolve(),\n\n // `run`, not `all`: better-sqlite3 throws on a statement that returns no\n // rows, which is every statement this method exists to carry. The Drizzle\n // instance is the transaction-bound one, so the statement runs inside this\n // transaction.\n //\n // better-sqlite3 is synchronous, so this resolves an already-settled\n // promise rather than being declared `async` over a body that never\n // awaits.\n runStatement: (statement: SQL): Promise<void> => {\n txDb().run(statement);\n return Promise.resolve();\n },\n\n // `all`, not `run`: this is the reading half, and better-sqlite3 returns\n // rows only from `all`. Synchronous, so the promise is already settled.\n queryStatement: <T = Record<string, unknown>>(\n statement: SQL\n ): Promise<T[]> => Promise.resolve(txDb().all(statement)),\n\n // eslint-disable-next-line @typescript-eslint/require-await\n execute: async <T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> => {\n const convertedSql = this.convertPlaceholders(sql);\n const trimmedSql = convertedSql.trim().toUpperCase();\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") || trimmedSql.includes(\"RETURNING\");\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect) {\n const stmt = db.prepare(convertedSql);\n return stmt.all(...sanitizedParams) as T[];\n } else {\n const stmt = db.prepare(convertedSql);\n const result = stmt.run(...sanitizedParams);\n return [\n {\n changes: result.changes,\n lastInsertRowid: result.lastInsertRowid,\n } as unknown as T,\n ];\n }\n },\n\n // Keys only, unlike the PostgreSQL and MySQL raw paths, which also encode\n // their date values. `sanitizeSqliteValue` below binds a `Date` as unix\n // seconds, and an instant carries no zone -- so what SQLite stores does\n // not depend on the server's timezone and there is nothing to correct.\n // eslint-disable-next-line @typescript-eslint/require-await\n insert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options?: InsertOptions\n ): Promise<T> => {\n const mapped = this.mapKeysToSqlColumns(\n this.getTableObject(table),\n data\n );\n const columns = Object.keys(mapped);\n const values = Object.values(mapped).map(sanitizeSqliteValue);\n const placeholders = values.map(() => \"?\").join(\", \");\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES (${placeholders})`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n // No RETURNING clause: better-sqlite3's .all() rejects a statement\n // that returns no columns, so run it and return nothing.\n stmt.run(...values);\n return undefined as T;\n }\n const rows = stmt.all(...values) as T[];\n // Return JS property-named keys AND Drizzle-decoded date values, so a\n // write inside a transaction answers with the same representation a\n // read of the same row gives.\n return this.mapRowFromRawSql(this.getTableObject(table), rows[0]);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n insertMany: async <T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> => {\n if (data.length === 0) return [];\n\n const tableObj = this.getTableObject(table);\n const mappedRecords = data.map(r =>\n this.mapKeysToSqlColumns(tableObj, r)\n );\n const columns = Object.keys(mappedRecords[0]);\n const allValues: unknown[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of mappedRecords) {\n const placeholders: string[] = [];\n for (const col of columns) {\n allValues.push(sanitizeSqliteValue(record[col]));\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES ${valuesClauses.join(\", \")}`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n stmt.run(...allValues);\n return [];\n }\n return (stmt.all(...allValues) as T[]).map(r =>\n this.mapRowFromRawSql(tableObj, r)\n );\n },\n\n ...this.createTransactionForwarders(txDb),\n\n // eslint-disable-next-line @typescript-eslint/require-await\n savepoint: async (name: string): Promise<void> => {\n db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n rollbackToSavepoint: async (name: string): Promise<void> => {\n db.exec(`ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n releaseSavepoint: async (name: string): Promise<void> => {\n db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n };\n }\n\n /**\n * Classifies a SQLite error into a DatabaseError.\n *\n * @param error - Original error from better-sqlite3\n * @param sql - SQL statement that caused the error (optional)\n * @returns DatabaseError with proper classification\n */\n // fallow-ignore-next-line complexity\n protected override classifyError(\n error: unknown,\n sql?: string\n ): DatabaseError {\n // Why short-circuit on existing DatabaseError: F17's\n // UnsupportedDialectVersionError is already a typed DatabaseError with\n // kind: \"unsupported_version\" plus detectedVersion/requiredVersion\n // fields. Re-wrapping it here would erase those fields.\n if (isDatabaseError(error)) return error;\n\n // Classify from the DRIVER's error, which is not necessarily the one handed\n // in. A query failure arrives wrapped: DrizzleQueryError carries the SQL\n // statement as its message and no code, and better-sqlite3's own error —\n // the only object that knows what actually went wrong — sits below it in\n // the `cause` chain. Classifying the wrapper means classifying the QUERY\n // TEXT, which reported a unique violation on a table with a `locked_by`\n // column as a \"timeout\", because the statement contains the word LOCKED.\n const sqliteError = findDriverError(error);\n\n // Determine error kind from SQLite error code\n let kind: DatabaseErrorKind = \"unknown\";\n\n if (sqliteError.code) {\n kind = SQLITE_ERROR_CODES[sqliteError.code] || \"unknown\";\n } else if (sqliteError.message) {\n // Try to extract error type from message. Safe to match loosely now:\n // `sqliteError` is the driver's error, so its message is the driver's\n // description of the failure and never a SQL statement.\n const msg = sqliteError.message.toUpperCase();\n if (msg.includes(\"UNIQUE CONSTRAINT\")) {\n kind = \"unique_violation\";\n } else if (msg.includes(\"FOREIGN KEY CONSTRAINT\")) {\n kind = \"foreign_key_violation\";\n } else if (msg.includes(\"NOT NULL CONSTRAINT\")) {\n kind = \"not_null_violation\";\n } else if (msg.includes(\"CHECK CONSTRAINT\")) {\n kind = \"check_violation\";\n } else if (msg.includes(\"BUSY\") || msg.includes(\"LOCKED\")) {\n kind = \"timeout\";\n } else if (\n msg.includes(\"SQLITE_CANTOPEN\") ||\n msg.includes(\"UNABLE TO OPEN\")\n ) {\n kind = \"connection\";\n }\n }\n\n // Build error message from the driver's description, for the same reason\n // the classification uses it: the wrapper's message is the SQL statement.\n let message = sqliteError.message ?? String(error);\n if (sql && kind === \"query\") {\n message = `Query failed: ${message}`;\n }\n\n return createDatabaseError({\n kind,\n message,\n code: sqliteError.code,\n cause: error instanceof Error ? error : undefined,\n });\n }\n}\n\n/** The fields this classifier reads off an error, at any depth in the chain. */\ninterface DriverErrorShape {\n code?: string;\n message?: string;\n name?: string;\n cause?: unknown;\n}\n\n/**\n * The deepest error in the `cause` chain that carries a SQLite error code, or\n * the outermost error when none does.\n *\n * better-sqlite3 sets `code` (`SQLITE_CONSTRAINT_UNIQUE`, `SQLITE_BUSY`, ...);\n * the wrappers around it do not. Walking to the code is therefore walking to\n * the only object that knows what failed. The walk is depth-bounded rather than\n * unbounded because an error chain can be made cyclic, and a classifier that\n * can hang is worse than one that occasionally gives up.\n *\n * Falls back to the outermost error so a driver error that genuinely carries no\n * code — one constructed from a message alone — still classifies exactly as it\n * did before.\n */\nfunction findDriverError(error: unknown): DriverErrorShape {\n let cursor = error as DriverErrorShape | null | undefined;\n for (let depth = 0; depth < 10 && cursor != null; depth++) {\n if (typeof cursor.code === \"string\" && cursor.code.length > 0)\n return cursor;\n cursor = cursor.cause as DriverErrorShape | null | undefined;\n }\n return error ?? {};\n}\n\n/**\n * Create a SQLite database adapter.\n *\n * @param config - SQLite adapter configuration\n * @returns A new SqliteAdapter instance\n *\n * @example\n * ```typescript\n * // Simple usage with file path\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * // In-memory database\n * const memAdapter = createSqliteAdapter({\n * memory: true,\n * });\n *\n * // Full configuration\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * wal: true,\n * foreignKeys: true,\n * busyTimeout: 5000,\n * logger: {\n * query: (sql, params, duration) => console.log(`Query: ${sql}`),\n * },\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport function createSqliteAdapter(\n config: SqliteAdapterConfig\n): SqliteAdapter {\n return new SqliteAdapter(config);\n}\n\n/**\n * Type guard to check if a value is a SqliteAdapter.\n *\n * @param value - Value to check\n * @returns True if value is a SqliteAdapter instance\n *\n * @example\n * ```typescript\n * if (isSqliteAdapter(adapter)) {\n * // TypeScript knows adapter is SqliteAdapter\n * console.log('Using SQLite');\n * }\n * ```\n */\nexport function isSqliteAdapter(value: unknown): value is SqliteAdapter {\n return value instanceof SqliteAdapter;\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -233,11 +233,7 @@ declare class SqliteAdapter extends DrizzleAdapter {
|
|
|
233
233
|
* @param sql - SQL statement that caused the error (optional)
|
|
234
234
|
* @returns DatabaseError with proper classification
|
|
235
235
|
*/
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Override handleQueryError to use SQLite-specific classification.
|
|
239
|
-
*/
|
|
240
|
-
protected handleQueryError(error: unknown, operation: string, table: string): DatabaseError;
|
|
236
|
+
protected classifyError(error: unknown, sql?: string): DatabaseError;
|
|
241
237
|
}
|
|
242
238
|
/**
|
|
243
239
|
* Create a SQLite database adapter.
|
package/dist/index.d.ts
CHANGED
|
@@ -233,11 +233,7 @@ declare class SqliteAdapter extends DrizzleAdapter {
|
|
|
233
233
|
* @param sql - SQL statement that caused the error (optional)
|
|
234
234
|
* @returns DatabaseError with proper classification
|
|
235
235
|
*/
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Override handleQueryError to use SQLite-specific classification.
|
|
239
|
-
*/
|
|
240
|
-
protected handleQueryError(error: unknown, operation: string, table: string): DatabaseError;
|
|
236
|
+
protected classifyError(error: unknown, sql?: string): DatabaseError;
|
|
241
237
|
}
|
|
242
238
|
/**
|
|
243
239
|
* Create a SQLite database adapter.
|
package/dist/index.mjs
CHANGED
|
@@ -534,24 +534,7 @@ var SqliteAdapter = class extends DrizzleAdapter {
|
|
|
534
534
|
(r) => this.mapRowFromRawSql(tableObj, r)
|
|
535
535
|
);
|
|
536
536
|
},
|
|
537
|
-
|
|
538
|
-
// but pass the transaction-bound executor so they run inside this
|
|
539
|
-
// transaction rather than on the pool.
|
|
540
|
-
select: async (table, options) => {
|
|
541
|
-
return this.select(table, options, txDb());
|
|
542
|
-
},
|
|
543
|
-
selectOne: async (table, options) => {
|
|
544
|
-
return this.selectOne(table, options, txDb());
|
|
545
|
-
},
|
|
546
|
-
update: async (table, data, where, options) => {
|
|
547
|
-
return this.update(table, data, where, options, txDb());
|
|
548
|
-
},
|
|
549
|
-
delete: async (table, where, options) => {
|
|
550
|
-
return this.delete(table, where, options, txDb());
|
|
551
|
-
},
|
|
552
|
-
upsert: async (table, data, options) => {
|
|
553
|
-
return this.upsert(table, data, options, txDb());
|
|
554
|
-
},
|
|
537
|
+
...this.createTransactionForwarders(txDb),
|
|
555
538
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
556
539
|
savepoint: async (name) => {
|
|
557
540
|
db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);
|
|
@@ -563,12 +546,7 @@ var SqliteAdapter = class extends DrizzleAdapter {
|
|
|
563
546
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
564
547
|
releaseSavepoint: async (name) => {
|
|
565
548
|
db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);
|
|
566
|
-
}
|
|
567
|
-
// Expose the transaction-bound Drizzle instance so callers can run
|
|
568
|
-
// Drizzle sql templates inside this transaction (junction-table writes
|
|
569
|
-
// need this to be atomic with the entry write). Reuses the memoized
|
|
570
|
-
// txDb() built for the delegated CRUD methods.
|
|
571
|
-
getDrizzle: () => txDb()
|
|
549
|
+
}
|
|
572
550
|
};
|
|
573
551
|
}
|
|
574
552
|
/**
|
|
@@ -578,9 +556,10 @@ var SqliteAdapter = class extends DrizzleAdapter {
|
|
|
578
556
|
* @param sql - SQL statement that caused the error (optional)
|
|
579
557
|
* @returns DatabaseError with proper classification
|
|
580
558
|
*/
|
|
559
|
+
// fallow-ignore-next-line complexity
|
|
581
560
|
classifyError(error, sql) {
|
|
582
561
|
if (isDatabaseError(error)) return error;
|
|
583
|
-
const sqliteError = error;
|
|
562
|
+
const sqliteError = findDriverError(error);
|
|
584
563
|
let kind = "unknown";
|
|
585
564
|
if (sqliteError.code) {
|
|
586
565
|
kind = SQLITE_ERROR_CODES[sqliteError.code] || "unknown";
|
|
@@ -611,20 +590,16 @@ var SqliteAdapter = class extends DrizzleAdapter {
|
|
|
611
590
|
cause: error instanceof Error ? error : void 0
|
|
612
591
|
});
|
|
613
592
|
}
|
|
614
|
-
/**
|
|
615
|
-
* Override handleQueryError to use SQLite-specific classification.
|
|
616
|
-
*/
|
|
617
|
-
handleQueryError(error, operation, table) {
|
|
618
|
-
const dbError = this.classifyError(error);
|
|
619
|
-
if (!dbError.message.includes(operation)) {
|
|
620
|
-
dbError.message = `${operation} operation failed on table '${table}': ${dbError.message}`;
|
|
621
|
-
}
|
|
622
|
-
if (!dbError.table) {
|
|
623
|
-
dbError.table = table;
|
|
624
|
-
}
|
|
625
|
-
return dbError;
|
|
626
|
-
}
|
|
627
593
|
};
|
|
594
|
+
function findDriverError(error) {
|
|
595
|
+
let cursor = error;
|
|
596
|
+
for (let depth = 0; depth < 10 && cursor != null; depth++) {
|
|
597
|
+
if (typeof cursor.code === "string" && cursor.code.length > 0)
|
|
598
|
+
return cursor;
|
|
599
|
+
cursor = cursor.cause;
|
|
600
|
+
}
|
|
601
|
+
return error ?? {};
|
|
602
|
+
}
|
|
628
603
|
function createSqliteAdapter(config) {
|
|
629
604
|
return new SqliteAdapter(config);
|
|
630
605
|
}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAkGO,IAAM,OAAA,GAAU;AAOvB,IAAM,kBAAA,GAAwD;AAAA;AAAA,EAE5D,iBAAA,EAAmB,YAAA;AAAA,EACnB,wBAAA,EAA0B,kBAAA;AAAA,EAC1B,4BAAA,EAA8B,kBAAA;AAAA,EAC9B,4BAAA,EAA8B,uBAAA;AAAA,EAC9B,yBAAA,EAA2B,oBAAA;AAAA,EAC3B,uBAAA,EAAyB,iBAAA;AAAA;AAAA,EAGzB,WAAA,EAAa,SAAA;AAAA,EACb,aAAA,EAAe,SAAA;AAAA;AAAA,EAGf,eAAA,EAAiB,YAAA;AAAA,EACjB,aAAA,EAAe,YAAA;AAAA,EACf,cAAA,EAAgB,YAAA;AAAA;AAAA,EAGhB,YAAA,EAAc,OAAA;AAAA,EACd,aAAA,EAAe,OAAA;AAAA,EACf,YAAA,EAAc;AAChB,CAAA;AAKA,IAAM,cAAA,GAAiB;AAAA,EACrB,WAAA,EAAa,GAAA;AAAA,EACb,GAAA,EAAK,IAA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adapter-sqlite\n *\n * SQLite database adapter for Nextly.\n * Extends DrizzleAdapter from @nextlyhq/adapter-drizzle to provide SQLite-specific functionality.\n *\n * @remarks\n * This adapter uses the better-sqlite3 package for database connectivity and provides:\n * - Synchronous API wrapped for async interface compatibility\n * - Full transaction support with savepoints (via better-sqlite3's native transaction handling)\n * - CRUD operations with RETURNING clause support (SQLite 3.35+)\n * - WAL mode for better concurrent read performance\n * - In-memory and file-based database support\n *\n * @example\n * ```typescript\n * import { createSqliteAdapter } from '@nextlyhq/adapter-sqlite';\n *\n * const adapter = createSqliteAdapter({\n * url: 'file:./data/app.db',\n * });\n *\n * await adapter.connect();\n *\n * // Query data\n * const users = await adapter.select('users', {\n * where: { and: [{ column: 'status', op: '=', value: 'active' }] },\n * limit: 10,\n * });\n *\n * await adapter.disconnect();\n * ```\n *\n * @packageDocumentation\n */\n\nimport { DrizzleAdapter } from \"@nextlyhq/adapter-drizzle\";\n// F17: connect-time DB version check shared across all adapters.\nimport {\n createDatabaseError,\n isDatabaseError,\n type SqliteAdapterConfig,\n type DatabaseCapabilities,\n type PoolStats,\n type TransactionContext,\n type TransactionOptions,\n type SqlParam,\n type WhereClause,\n type WhereCondition,\n type WhereOperator,\n type SelectOptions,\n type InsertOptions,\n type UpdateOptions,\n type DeleteOptions,\n type UpsertOptions,\n type OrderBySpec,\n type JoinSpec,\n type DatabaseError,\n type DatabaseErrorKind,\n type BaseAdapterConfig,\n type AdapterLogger,\n isApplicationError,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport { checkDialectVersion } from \"@nextlyhq/adapter-drizzle/version-check\";\nimport type Database from \"better-sqlite3\";\nimport type { AnyRelations, SQL } from \"drizzle-orm\";\nimport {\n drizzle,\n type BetterSQLite3Database,\n} from \"drizzle-orm/better-sqlite3\";\n\n// Re-export types for convenience\nexport type {\n SqliteAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n WhereClause,\n WhereCondition,\n WhereOperator,\n SelectOptions,\n InsertOptions,\n UpdateOptions,\n DeleteOptions,\n UpsertOptions,\n OrderBySpec,\n JoinSpec,\n DatabaseError,\n DatabaseErrorKind,\n BaseAdapterConfig,\n AdapterLogger,\n};\n\n/**\n * Package version\n */\nexport const VERSION = \"0.1.0\";\n\n/**\n * SQLite error codes mapping to DatabaseErrorKind.\n *\n * @see https://sqlite.org/rescode.html\n */\nconst SQLITE_ERROR_CODES: Record<string, DatabaseErrorKind> = {\n // Constraint violations\n SQLITE_CONSTRAINT: \"constraint\",\n SQLITE_CONSTRAINT_UNIQUE: \"unique_violation\",\n SQLITE_CONSTRAINT_PRIMARYKEY: \"unique_violation\",\n SQLITE_CONSTRAINT_FOREIGNKEY: \"foreign_key_violation\",\n SQLITE_CONSTRAINT_NOTNULL: \"not_null_violation\",\n SQLITE_CONSTRAINT_CHECK: \"check_violation\",\n\n // Busy/locked errors\n SQLITE_BUSY: \"timeout\",\n SQLITE_LOCKED: \"timeout\",\n\n // Connection errors\n SQLITE_CANTOPEN: \"connection\",\n SQLITE_NOTADB: \"connection\",\n SQLITE_CORRUPT: \"connection\",\n\n // Query errors\n SQLITE_ERROR: \"query\",\n SQLITE_MISUSE: \"query\",\n SQLITE_RANGE: \"query\",\n};\n\n/**\n * Default configuration values.\n */\nconst DEFAULT_CONFIG = {\n busyTimeout: 5000,\n wal: true,\n foreignKeys: true,\n};\n\n/**\n * Converts a JavaScript value to a type that better-sqlite3 can bind.\n * better-sqlite3 only accepts: number, string, bigint, Buffer, null.\n *\n * These conversions have to match what the schema declares, because the rows\n * they write are read back through it. Every column here that takes a `Date`\n * is `integer({ mode: \"timestamp\" })` — the system columns and user date\n * fields in `runtime-schema-generator.ts`, and the core tables, whose date\n * columns are all INTEGER. So a date is bound as unix seconds, which is what\n * that mode reads.\n */\nfunction sanitizeSqliteValue(v: unknown): unknown {\n if (v === undefined) return null;\n if (typeof v === \"boolean\") return v ? 1 : 0;\n // Seconds, not an ISO string. SQLite stores whatever it is given regardless\n // of the declared type, so a string lands in the column without complaint\n // and only fails on the way back out, where the timestamp decoder reads it\n // as a number and yields an Invalid Date.\n if (v instanceof Date) return Math.floor(v.getTime() / 1000);\n if (v !== null && typeof v === \"object\") return JSON.stringify(v);\n return v;\n}\n\n/**\n * SQLite database adapter for Nextly.\n *\n * Extends the base DrizzleAdapter to provide SQLite-specific functionality\n * using the better-sqlite3 package.\n *\n * @remarks\n * SQLite has some differences from PostgreSQL:\n * - Synchronous API (wrapped for async compatibility)\n * - No connection pooling (single file/connection)\n * - RETURNING clause supported (SQLite 3.35+)\n * - Savepoints supported via nested transactions\n * - No native ILIKE (uses LOWER() LIKE workaround)\n * - JSON support (not JSONB)\n * - No array types\n *\n * @example\n * ```typescript\n * const adapter = new SqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport class SqliteAdapter extends DrizzleAdapter {\n // getDrizzle memoization: drizzle v1's constructor builds a relational\n // query builder per table in the relations config (~40 tables), and the\n // service layer resolves an instance on every db access — construct once\n // per relations object (identity-stable: the schema registry caches it\n // and hands out a NEW object on invalidation, which naturally misses\n // this cache and produces a fresh instance).\n private drizzleByRelations = new WeakMap<AnyRelations, unknown>();\n private drizzleBare: unknown;\n\n /**\n * The database dialect - always 'sqlite' for this adapter.\n */\n readonly dialect = \"sqlite\" as const;\n\n /**\n * Adapter configuration.\n */\n protected readonly config: SqliteAdapterConfig;\n\n /**\n * better-sqlite3 Database instance.\n */\n private db: Database.Database | null = null;\n\n /**\n * Connection state flag.\n */\n private connected = false;\n\n /**\n * Serialization queue for concurrent `transaction()` calls.\n *\n * better-sqlite3 is synchronous, single-connection, and rejects nested\n * `BEGIN` with \"cannot start a transaction within a transaction\".\n * When two `await adapter.transaction(...)` calls overlap (e.g. a bulk\n * mutation calling `Promise.allSettled` with N per-row updates), the\n * second one tries to BEGIN while the first is still open and the\n * driver throws.\n *\n * Postgres/MySQL avoid this by allocating a fresh client/connection\n * per transaction from a pool — there is no equivalent for\n * better-sqlite3, so we serialize at the JS layer instead. Every\n * `transaction()` invocation chains onto this promise; only one\n * BEGIN→COMMIT/ROLLBACK section runs at any moment, preserving\n * write-isolation semantics that callers expect from a transactional\n * adapter.\n *\n * Performance impact is the natural floor: SQLite already serializes\n * writers at the file-lock level, so the queue removes failed-call\n * surface area without changing the achievable concurrency.\n */\n private transactionQueue: Promise<unknown> = Promise.resolve();\n\n /**\n * Creates a new SQLite adapter instance.\n *\n * @param config - Adapter configuration\n */\n constructor(config: SqliteAdapterConfig) {\n super();\n this.config = config;\n }\n\n /**\n * Connect to the SQLite database.\n *\n * @remarks\n * This method initializes the database connection. For SQLite, this\n * opens the database file or creates an in-memory database.\n * Also configures WAL mode and foreign keys based on config.\n *\n * @throws {DatabaseError} If connection fails\n */\n async connect(): Promise<void> {\n if (this.connected && this.db) {\n return;\n }\n\n try {\n // Dynamic import of better-sqlite3 to avoid bundling issues\n const BetterSqlite3 = await import(\"better-sqlite3\");\n const Database = BetterSqlite3.default;\n\n // Determine database path\n let dbPath: string;\n if (this.config.memory) {\n dbPath = \":memory:\";\n } else if (this.config.url) {\n // Strip file: prefix if present\n dbPath = this.config.url.replace(/^file:/, \"\");\n } else {\n dbPath = \":memory:\";\n }\n\n // Ensure the parent directory exists for file-backed databases.\n // better-sqlite3 fails with a confusing \"Cannot open database\n // because the directory does not exist\" error if it doesn't, and\n // a fresh clone with `DATABASE_URL=file:./data/app.db` reliably\n // hits this on first run. Create the dir lazily so DATABASE_URL\n // can point anywhere without a separate setup step.\n if (dbPath !== \":memory:\" && !this.config.readonly) {\n const fs = await import(\"node:fs\");\n const path = await import(\"node:path\");\n const dir = path.dirname(path.resolve(dbPath));\n await fs.promises.mkdir(dir, { recursive: true });\n }\n\n // Create database instance\n this.db = new Database(dbPath, {\n readonly: this.config.readonly ?? false,\n timeout: this.config.busyTimeout ?? DEFAULT_CONFIG.busyTimeout,\n });\n\n // Configure WAL mode for better concurrency (unless in-memory or readonly)\n if (\n (this.config.wal ?? DEFAULT_CONFIG.wal) &&\n dbPath !== \":memory:\" &&\n !this.config.readonly\n ) {\n this.db.pragma(\"journal_mode = WAL\");\n }\n\n // Enable foreign keys\n if (this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys) {\n this.db.pragma(\"foreign_keys = ON\");\n }\n\n // F17: check SQLite version. Hard-fails on SQLite <3.38. SQLite has\n // no recognized cloud variants we warn on, so onWarning is omitted.\n // Note: better-sqlite3's bundled SQLite is set by the package\n // version; users on this package's pinned better-sqlite3 (3.45+)\n // will always pass.\n await checkDialectVersion(this.db, \"sqlite\");\n\n this.connected = true;\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection established\", {\n url: dbPath === \":memory:\" ? \"in-memory\" : dbPath,\n wal: this.config.wal ?? DEFAULT_CONFIG.wal,\n foreignKeys: this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys,\n });\n }\n } catch (error) {\n // Clean up on failure\n if (this.db) {\n try {\n this.db.close();\n } catch {\n // Ignore close errors during error handling\n }\n this.db = null;\n }\n throw this.classifyError(error);\n }\n }\n\n /**\n * Disconnect from the SQLite database.\n *\n * @remarks\n * This method closes the database connection and releases resources.\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async disconnect(): Promise<void> {\n // Drop memoized drizzle instances — they wrap the connection being\n // closed and must not survive a reconnect.\n this.drizzleBare = undefined;\n this.drizzleByRelations = new WeakMap();\n if (!this.db) {\n return;\n }\n\n try {\n this.db.close();\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection closed\");\n }\n } finally {\n this.db = null;\n this.connected = false;\n }\n }\n\n /**\n * Check if connected to the database.\n */\n isConnected(): boolean {\n return this.connected && this.db !== null;\n }\n\n /**\n * Get connection pool statistics.\n *\n * @remarks\n * SQLite doesn't use connection pooling, so this returns null.\n * The database is single-file with a single connection.\n */\n getPoolStats(): PoolStats | null {\n // SQLite doesn't have connection pooling\n return null;\n }\n\n /**\n * Execute a raw SQL query.\n *\n * @param sql - SQL query string with $1, $2 placeholders (converted to ? for SQLite)\n * @param params - Query parameters\n * @returns Query results\n *\n * @throws {DatabaseError} If query execution fails\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async executeQuery<T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Convert $1, $2 placeholders to ? for better-sqlite3\n const convertedSql = this.convertPlaceholders(sql);\n\n // Determine if this is a SELECT query or a modifying query\n const trimmedSql = convertedSql.trim().toUpperCase();\n\n // Check for PRAGMA query statements (e.g., \"PRAGMA foreign_keys\").\n // Setting PRAGMAs (e.g., \"PRAGMA foreign_keys = OFF\") don't return data.\n const isPragmaQuery =\n trimmedSql.startsWith(\"PRAGMA\") && !trimmedSql.includes(\"=\");\n\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") ||\n isPragmaQuery ||\n trimmedSql.startsWith(\"WITH\");\n const hasReturning = trimmedSql.includes(\"RETURNING\");\n\n let result: T[];\n\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect || hasReturning) {\n // Use .all() for SELECT queries or queries with RETURNING\n const stmt = db.prepare(convertedSql);\n result = stmt.all(...sanitizedParams) as T[];\n } else {\n // Use .run() for INSERT/UPDATE/DELETE/PRAGMA settings without RETURNING\n const stmt = db.prepare(convertedSql);\n const runResult = stmt.run(...sanitizedParams);\n // Return info about the operation\n result = [\n {\n changes: runResult.changes,\n lastInsertRowid: runResult.lastInsertRowid,\n } as unknown as T,\n ];\n }\n\n // Log query if logger configured\n if (this.config.logger?.query) {\n const durationMs = Date.now() - startTime;\n this.config.logger.query(convertedSql, params, durationMs);\n }\n\n return result;\n } catch (error) {\n throw this.classifyError(error, sql);\n }\n }\n\n /**\n * Execute work within a transaction.\n *\n * @param work - Function containing transactional operations\n * @param options - Transaction options (isolation level not fully supported in SQLite)\n * @returns Result of the work function\n *\n * @remarks\n * Uses better-sqlite3's native transaction handling which:\n * - Automatically commits on success\n * - Automatically rolls back on error\n * - Converts nested transactions to savepoints\n *\n * Note: SQLite uses DEFERRED, IMMEDIATE, or EXCLUSIVE transaction modes\n * rather than isolation levels. This adapter uses IMMEDIATE by default.\n */\n async transaction<T>(\n work: (tx: TransactionContext) => Promise<T>,\n _options?: TransactionOptions\n ): Promise<T> {\n // Why: serialize concurrent transaction() invocations to dodge\n // better-sqlite3's \"cannot start a transaction within a transaction\"\n // error when two awaits overlap on the same connection. See the\n // `transactionQueue` field comment for the full rationale. The queue\n // chains every call onto a tail promise so only one BEGIN → COMMIT\n // section runs at a time; the next call's BEGIN waits for the\n // previous COMMIT/ROLLBACK to finish.\n //\n // Failure isolation: the queue's tail promise must not reject (a\n // rejection would poison every subsequent transaction with the same\n // rejected value). We attach a no-op `.catch` so the chain stays\n // resolved while the actual error propagates back to the original\n // caller through the inner promise.\n const run = async (): Promise<T> => this.runTransaction(work);\n const next = this.transactionQueue.then(run, run);\n this.transactionQueue = next.catch(() => undefined);\n return next;\n }\n\n /**\n * Inner transaction body. Kept private so callers always go through\n * the serialized `transaction()` queue above and the BEGIN/COMMIT\n * pair is never invoked outside of it.\n */\n private async runTransaction<T>(\n work: (tx: TransactionContext) => Promise<T>\n ): Promise<T> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Create transaction context\n const ctx = this.createTransactionContext(db);\n\n // Since better-sqlite3's db.transaction() doesn't support async functions,\n // we manually manage the transaction with BEGIN/COMMIT/ROLLBACK\n db.exec(\"BEGIN IMMEDIATE\");\n\n try {\n const result = await work(ctx);\n db.exec(\"COMMIT\");\n\n // Log success\n if (this.config.logger?.debug) {\n const durationMs = Date.now() - startTime;\n this.config.logger.debug(\"Transaction committed\", {\n durationMs,\n });\n }\n\n return result;\n } catch (error) {\n // Rollback on error\n try {\n db.exec(\"ROLLBACK\");\n } catch {\n // Ignore rollback errors - transaction may already be aborted\n }\n throw error;\n }\n } catch (error) {\n // Work inside a transaction may throw to roll the write back — a refused\n // value, a denied permission — and that is the application's verdict, not\n // the driver's failure. Classifying it would replace its code and payload\n // with a generic database error, so a caller that asked for a refusal is\n // handed an unexplained failure and the per-field detail never arrives.\n if (isApplicationError(error)) throw error;\n throw this.classifyError(error);\n }\n }\n\n /**\n * Get SQLite database capabilities.\n *\n * @remarks\n * SQLite capabilities:\n * - JSON support (not JSONB)\n * - No arrays\n * - No native ILIKE\n * - RETURNING clause (3.35+)\n * - Savepoints supported\n * - ON CONFLICT supported\n */\n getCapabilities(): DatabaseCapabilities {\n return {\n dialect: \"sqlite\",\n supportsJsonb: false, // SQLite uses JSON, not JSONB\n supportsJson: true,\n supportsArrays: false, // SQLite doesn't support array types\n supportsGeneratedColumns: true, // SQLite 3.31+\n supportsFts: true, // SQLite FTS5\n supportsIlike: false, // No native ILIKE, use LOWER() LIKE\n supportsReturning: true, // SQLite 3.35+\n supportsSavepoints: true, // SQLite supports savepoints\n supportsOnConflict: true, // ON CONFLICT clause\n maxParamsPerQuery: 999, // SQLite SQLITE_MAX_VARIABLE_NUMBER default\n maxIdentifierLength: 128, // SQLite doesn't have a strict limit\n };\n }\n\n /**\n * Override insertMany for bulk insert optimization.\n *\n * @remarks\n * Uses a single multi-row INSERT statement for better performance.\n */\n override async insertMany<T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> {\n if (data.length === 0) {\n return [];\n }\n\n const db = this.ensureDb();\n\n // For single record, use parent implementation\n if (data.length === 1) {\n const result = await this.insert<T>(table, data[0], options);\n return [result];\n }\n\n // Build multi-row INSERT\n const columns = Object.keys(data[0]);\n const params: SqlParam[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of data) {\n const placeholders: string[] = [];\n for (const col of columns) {\n params.push(sanitizeSqliteValue(record[col]) as SqlParam);\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n const columnList = columns\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(\", \")}`;\n\n // Add RETURNING clause\n if (options?.returning) {\n const returning =\n options.returning === \"*\"\n ? \"*\"\n : options.returning.map(col => this.escapeIdentifier(col)).join(\", \");\n sql += ` RETURNING ${returning}`;\n } else {\n sql += \" RETURNING *\";\n }\n\n try {\n const stmt = db.prepare(sql);\n const rows = stmt.all(...(params as unknown[])) as T[];\n // SQLite stores a timestamp as an integer and hands it back as one. The\n // Drizzle read paths decode it through the column definition, so without\n // this a bulk insert answers with numbers where a read answers `Date`.\n const tableObj = this.getTableObject(table);\n return rows.map(r => this.mapRowFromRawSql(tableObj, r));\n } catch (error) {\n throw this.handleQueryError(error, \"insertMany\", table);\n }\n }\n\n // ============================================================\n // Protected Helper Methods\n // ============================================================\n\n /**\n * Ensures database is connected and returns it.\n *\n * @throws {DatabaseError} If not connected\n */\n private ensureDb(): Database.Database {\n if (!this.db) {\n throw createDatabaseError({\n kind: \"connection\",\n message: \"SqliteAdapter is not connected. Call connect() first.\",\n });\n }\n return this.db;\n }\n\n /**\n * Return the typed Drizzle instance for SQLite.\n * Guarded for server-only usage and requires an active connection.\n *\n * @param schema - Optional schema for relational queries (db.query.*)\n * @returns Drizzle ORM instance wrapping the better-sqlite3 connection\n * @throws {Error} If called in browser or not connected\n */\n getDrizzle<T = BetterSQLite3Database<AnyRelations>>(\n relations?: AnyRelations\n ): T {\n if (typeof window !== \"undefined\") {\n throw new Error(\"getDrizzle() is server-only\");\n }\n const db = this.ensureDb();\n // drizzle v1 removed the positional (client, config) form — positional\n // calls silently open a NEW :memory: database. Object form only.\n if (!relations) {\n this.drizzleBare ??= drizzle({ client: db });\n return this.drizzleBare as T;\n }\n let cached = this.drizzleByRelations.get(relations);\n if (!cached) {\n cached = drizzle({ client: db, relations });\n this.drizzleByRelations.set(relations, cached);\n }\n return cached as T;\n }\n\n /**\n * Convert $1, $2 placeholders to ? for better-sqlite3.\n *\n * @param sql - SQL with PostgreSQL-style placeholders\n * @returns SQL with ? placeholders\n */\n private convertPlaceholders(sql: string): string {\n // Replace $1, $2, etc. with ?\n return sql.replace(/\\$\\d+/g, \"?\");\n }\n\n /**\n * Creates a TransactionContext for the given database connection.\n */\n private createTransactionContext(db: Database.Database): TransactionContext {\n // better-sqlite3 is synchronous; the methods below are async to satisfy\n // the TransactionContext contract shared with truly-async dialect adapters.\n // Bind a Drizzle instance to the transaction's connection so the delegated\n // CRUD methods run inside it. better-sqlite3 is single-connection, so this\n // is the same connection getDrizzle() would use, but binding explicitly\n // keeps the three adapters consistent and correct if that ever changes.\n // Built lazily and memoized: transactions that use only raw execute/insert\n // never construct it.\n const buildTxExecutor = () => drizzle({ client: db });\n let txExecutor: ReturnType<typeof buildTxExecutor> | undefined;\n const txDb = () => (txExecutor ??= buildTxExecutor());\n return {\n // SQLite has no row-level locking and needs none here: `withTransaction`\n // opens SQLite transactions with BEGIN IMMEDIATE, which takes the write\n // lock up front and serializes writers for the whole transaction.\n lockRow: (): Promise<void> => Promise.resolve(),\n\n // `run`, not `all`: better-sqlite3 throws on a statement that returns no\n // rows, which is every statement this method exists to carry. The Drizzle\n // instance is the transaction-bound one, so the statement runs inside this\n // transaction.\n //\n // better-sqlite3 is synchronous, so this resolves an already-settled\n // promise rather than being declared `async` over a body that never\n // awaits.\n runStatement: (statement: SQL): Promise<void> => {\n txDb().run(statement);\n return Promise.resolve();\n },\n\n // `all`, not `run`: this is the reading half, and better-sqlite3 returns\n // rows only from `all`. Synchronous, so the promise is already settled.\n queryStatement: <T = Record<string, unknown>>(\n statement: SQL\n ): Promise<T[]> => Promise.resolve(txDb().all(statement)),\n\n // eslint-disable-next-line @typescript-eslint/require-await\n execute: async <T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> => {\n const convertedSql = this.convertPlaceholders(sql);\n const trimmedSql = convertedSql.trim().toUpperCase();\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") || trimmedSql.includes(\"RETURNING\");\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect) {\n const stmt = db.prepare(convertedSql);\n return stmt.all(...sanitizedParams) as T[];\n } else {\n const stmt = db.prepare(convertedSql);\n const result = stmt.run(...sanitizedParams);\n return [\n {\n changes: result.changes,\n lastInsertRowid: result.lastInsertRowid,\n } as unknown as T,\n ];\n }\n },\n\n // Keys only, unlike the PostgreSQL and MySQL raw paths, which also encode\n // their date values. `sanitizeSqliteValue` below binds a `Date` as unix\n // seconds, and an instant carries no zone -- so what SQLite stores does\n // not depend on the server's timezone and there is nothing to correct.\n // eslint-disable-next-line @typescript-eslint/require-await\n insert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options?: InsertOptions\n ): Promise<T> => {\n const mapped = this.mapKeysToSqlColumns(\n this.getTableObject(table),\n data\n );\n const columns = Object.keys(mapped);\n const values = Object.values(mapped).map(sanitizeSqliteValue);\n const placeholders = values.map(() => \"?\").join(\", \");\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES (${placeholders})`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n // No RETURNING clause: better-sqlite3's .all() rejects a statement\n // that returns no columns, so run it and return nothing.\n stmt.run(...values);\n return undefined as T;\n }\n const rows = stmt.all(...values) as T[];\n // Return JS property-named keys AND Drizzle-decoded date values, so a\n // write inside a transaction answers with the same representation a\n // read of the same row gives.\n return this.mapRowFromRawSql(this.getTableObject(table), rows[0]);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n insertMany: async <T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> => {\n if (data.length === 0) return [];\n\n const tableObj = this.getTableObject(table);\n const mappedRecords = data.map(r =>\n this.mapKeysToSqlColumns(tableObj, r)\n );\n const columns = Object.keys(mappedRecords[0]);\n const allValues: unknown[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of mappedRecords) {\n const placeholders: string[] = [];\n for (const col of columns) {\n allValues.push(sanitizeSqliteValue(record[col]));\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES ${valuesClauses.join(\", \")}`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n stmt.run(...allValues);\n return [];\n }\n return (stmt.all(...allValues) as T[]).map(r =>\n this.mapRowFromRawSql(tableObj, r)\n );\n },\n\n // TransactionContext CRUD methods delegate to the adapter's Drizzle CRUD\n // but pass the transaction-bound executor so they run inside this\n // transaction rather than on the pool.\n select: async <T = unknown>(\n table: string,\n options?: SelectOptions\n ): Promise<T[]> => {\n return this.select<T>(table, options, txDb());\n },\n\n selectOne: async <T = unknown>(\n table: string,\n options?: SelectOptions\n ): Promise<T | null> => {\n return this.selectOne<T>(table, options, txDb());\n },\n\n update: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n where: WhereClause,\n options?: UpdateOptions\n ): Promise<T[]> => {\n return this.update<T>(table, data, where, options, txDb());\n },\n\n delete: async (\n table: string,\n where: WhereClause,\n options?: DeleteOptions\n ): Promise<number> => {\n return this.delete(table, where, options, txDb());\n },\n\n upsert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options: UpsertOptions\n ): Promise<T> => {\n return this.upsert<T>(table, data, options, txDb());\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n savepoint: async (name: string): Promise<void> => {\n db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n rollbackToSavepoint: async (name: string): Promise<void> => {\n db.exec(`ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n releaseSavepoint: async (name: string): Promise<void> => {\n db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // Expose the transaction-bound Drizzle instance so callers can run\n // Drizzle sql templates inside this transaction (junction-table writes\n // need this to be atomic with the entry write). Reuses the memoized\n // txDb() built for the delegated CRUD methods.\n getDrizzle: <T = unknown>(): T => txDb() as T,\n };\n }\n\n /**\n * Classifies a SQLite error into a DatabaseError.\n *\n * @param error - Original error from better-sqlite3\n * @param sql - SQL statement that caused the error (optional)\n * @returns DatabaseError with proper classification\n */\n private classifyError(error: unknown, sql?: string): DatabaseError {\n // Why short-circuit on existing DatabaseError: F17's\n // UnsupportedDialectVersionError is already a typed DatabaseError with\n // kind: \"unsupported_version\" plus detectedVersion/requiredVersion\n // fields. Re-wrapping it here would erase those fields.\n if (isDatabaseError(error)) return error;\n\n const sqliteError = error as {\n code?: string;\n message?: string;\n name?: string;\n };\n\n // Determine error kind from SQLite error code\n let kind: DatabaseErrorKind = \"unknown\";\n\n if (sqliteError.code) {\n kind = SQLITE_ERROR_CODES[sqliteError.code] || \"unknown\";\n } else if (sqliteError.message) {\n // Try to extract error type from message\n const msg = sqliteError.message.toUpperCase();\n if (msg.includes(\"UNIQUE CONSTRAINT\")) {\n kind = \"unique_violation\";\n } else if (msg.includes(\"FOREIGN KEY CONSTRAINT\")) {\n kind = \"foreign_key_violation\";\n } else if (msg.includes(\"NOT NULL CONSTRAINT\")) {\n kind = \"not_null_violation\";\n } else if (msg.includes(\"CHECK CONSTRAINT\")) {\n kind = \"check_violation\";\n } else if (msg.includes(\"BUSY\") || msg.includes(\"LOCKED\")) {\n kind = \"timeout\";\n } else if (\n msg.includes(\"SQLITE_CANTOPEN\") ||\n msg.includes(\"UNABLE TO OPEN\")\n ) {\n kind = \"connection\";\n }\n }\n\n // Build error message\n let message = sqliteError.message ?? String(error);\n if (sql && kind === \"query\") {\n message = `Query failed: ${message}`;\n }\n\n return createDatabaseError({\n kind,\n message,\n code: sqliteError.code,\n cause: error instanceof Error ? error : undefined,\n });\n }\n\n /**\n * Override handleQueryError to use SQLite-specific classification.\n */\n protected override handleQueryError(\n error: unknown,\n operation: string,\n table: string\n ): DatabaseError {\n const dbError = this.classifyError(error);\n\n // Add operation context if not already present\n if (!dbError.message.includes(operation)) {\n dbError.message = `${operation} operation failed on table '${table}': ${dbError.message}`;\n }\n\n if (!dbError.table) {\n dbError.table = table;\n }\n\n return dbError;\n }\n}\n\n/**\n * Create a SQLite database adapter.\n *\n * @param config - SQLite adapter configuration\n * @returns A new SqliteAdapter instance\n *\n * @example\n * ```typescript\n * // Simple usage with file path\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * // In-memory database\n * const memAdapter = createSqliteAdapter({\n * memory: true,\n * });\n *\n * // Full configuration\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * wal: true,\n * foreignKeys: true,\n * busyTimeout: 5000,\n * logger: {\n * query: (sql, params, duration) => console.log(`Query: ${sql}`),\n * },\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport function createSqliteAdapter(\n config: SqliteAdapterConfig\n): SqliteAdapter {\n return new SqliteAdapter(config);\n}\n\n/**\n * Type guard to check if a value is a SqliteAdapter.\n *\n * @param value - Value to check\n * @returns True if value is a SqliteAdapter instance\n *\n * @example\n * ```typescript\n * if (isSqliteAdapter(adapter)) {\n * // TypeScript knows adapter is SqliteAdapter\n * console.log('Using SQLite');\n * }\n * ```\n */\nexport function isSqliteAdapter(value: unknown): value is SqliteAdapter {\n return value instanceof SqliteAdapter;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAkGO,IAAM,OAAA,GAAU;AAOvB,IAAM,kBAAA,GAAwD;AAAA;AAAA,EAE5D,iBAAA,EAAmB,YAAA;AAAA,EACnB,wBAAA,EAA0B,kBAAA;AAAA,EAC1B,4BAAA,EAA8B,kBAAA;AAAA,EAC9B,4BAAA,EAA8B,uBAAA;AAAA,EAC9B,yBAAA,EAA2B,oBAAA;AAAA,EAC3B,uBAAA,EAAyB,iBAAA;AAAA;AAAA,EAGzB,WAAA,EAAa,SAAA;AAAA,EACb,aAAA,EAAe,SAAA;AAAA;AAAA,EAGf,eAAA,EAAiB,YAAA;AAAA,EACjB,aAAA,EAAe,YAAA;AAAA,EACf,cAAA,EAAgB,YAAA;AAAA;AAAA,EAGhB,YAAA,EAAc,OAAA;AAAA,EACd,aAAA,EAAe,OAAA;AAAA,EACf,YAAA,EAAc;AAChB,CAAA;AAKA,IAAM,cAAA,GAAiB;AAAA,EACrB,WAAA,EAAa,GAAA;AAAA,EACb,GAAA,EAAK,IAAA;AAAA,EACL,WAAA,EAAa;AACf,CAAA;AAaA,SAAS,oBAAoB,CAAA,EAAqB;AAChD,EAAA,IAAI,CAAA,KAAM,QAAW,OAAO,IAAA;AAC5B,EAAA,IAAI,OAAO,CAAA,KAAM,SAAA,EAAW,OAAO,IAAI,CAAA,GAAI,CAAA;AAK3C,EAAA,IAAI,CAAA,YAAa,MAAM,OAAO,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,KAAY,GAAI,CAAA;AAC3D,EAAA,IAAI,CAAA,KAAM,QAAQ,OAAO,CAAA,KAAM,UAAU,OAAO,IAAA,CAAK,UAAU,CAAC,CAAA;AAChE,EAAA,OAAO,CAAA;AACT;AA2BO,IAAM,aAAA,GAAN,cAA4B,c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AI,QAAA,CAAS,iBAAiB,KAC9B,GAAA,CAAI,QAAA,CAAS,gBAAgB,CAAA,EAC7B;AACA,QAAA,IAAA,GAAO,YAAA;AAAA,MACT;AAAA,IACF;AAIA,IAAA,IAAI,OAAA,GAAU,WAAA,CAAY,OAAA,IAAW,MAAA,CAAO,KAAK,CAAA;AACjD,IAAA,IAAI,GAAA,IAAO,SAAS,OAAA,EAAS;AAC3B,MAAA,OAAA,GAAU,iBAAiB,OAAO,CAAA,CAAA;AAAA,IACpC;AAEA,IAAA,OAAO,mBAAA,CAAoB;AAAA,MACzB,IAAA;AAAA,MACA,OAAA;AAAA,MACA,MAAM,WAAA,CAAY,IAAA;AAAA,MAClB,KAAA,EAAO,KAAA,YAAiB,KAAA,GAAQ,KAAA,GAAQ;AAAA,KACzC,CAAA;AAAA,EACH;AACF;AAwBA,SAAS,gBAAgB,KAAA,EAAkC;AACzD,EAAA,IAAI,MAAA,GAAS,KAAA;AACb,EAAA,KAAA,IAAS,QAAQ,CAAA,EAAG,KAAA,GAAQ,EAAA,IAAM,MAAA,IAAU,MAAM,KAAA,EAAA,EAAS;AACzD,IAAA,IAAI,OAAO,MAAA,CAAO,IAAA,KAAS,QAAA,IAAY,MAAA,CAAO,KAAK,MAAA,GAAS,CAAA;AAC1D,MAAA,OAAO,MAAA;AACT,IAAA,MAAA,GAAS,MAAA,CAAO,KAAA;AAAA,EAClB;AACA,EAAA,OAAO,SAAS,EAAC;AACnB;AAkCO,SAAS,oBACd,MAAA,EACe;AACf,EAAA,OAAO,IAAI,cAAc,MAAM,CAAA;AACjC;AAgBO,SAAS,gBAAgB,KAAA,EAAwC;AACtE,EAAA,OAAO,KAAA,YAAiB,aAAA;AAC1B","file":"index.mjs","sourcesContent":["/**\n * @nextlyhq/adapter-sqlite\n *\n * SQLite database adapter for Nextly.\n * Extends DrizzleAdapter from @nextlyhq/adapter-drizzle to provide SQLite-specific functionality.\n *\n * @remarks\n * This adapter uses the better-sqlite3 package for database connectivity and provides:\n * - Synchronous API wrapped for async interface compatibility\n * - Full transaction support with savepoints (via better-sqlite3's native transaction handling)\n * - CRUD operations with RETURNING clause support (SQLite 3.35+)\n * - WAL mode for better concurrent read performance\n * - In-memory and file-based database support\n *\n * @example\n * ```typescript\n * import { createSqliteAdapter } from '@nextlyhq/adapter-sqlite';\n *\n * const adapter = createSqliteAdapter({\n * url: 'file:./data/app.db',\n * });\n *\n * await adapter.connect();\n *\n * // Query data\n * const users = await adapter.select('users', {\n * where: { and: [{ column: 'status', op: '=', value: 'active' }] },\n * limit: 10,\n * });\n *\n * await adapter.disconnect();\n * ```\n *\n * @packageDocumentation\n */\n\nimport { DrizzleAdapter } from \"@nextlyhq/adapter-drizzle\";\n// F17: connect-time DB version check shared across all adapters.\nimport {\n createDatabaseError,\n isDatabaseError,\n type SqliteAdapterConfig,\n type DatabaseCapabilities,\n type PoolStats,\n type TransactionContext,\n type TransactionOptions,\n type SqlParam,\n type WhereClause,\n type WhereCondition,\n type WhereOperator,\n type SelectOptions,\n type InsertOptions,\n type UpdateOptions,\n type DeleteOptions,\n type UpsertOptions,\n type OrderBySpec,\n type JoinSpec,\n type DatabaseError,\n type DatabaseErrorKind,\n type BaseAdapterConfig,\n type AdapterLogger,\n isApplicationError,\n} from \"@nextlyhq/adapter-drizzle/types\";\nimport { checkDialectVersion } from \"@nextlyhq/adapter-drizzle/version-check\";\nimport type Database from \"better-sqlite3\";\nimport type { AnyRelations, SQL } from \"drizzle-orm\";\nimport {\n drizzle,\n type BetterSQLite3Database,\n} from \"drizzle-orm/better-sqlite3\";\n\n// Re-export types for convenience\nexport type {\n SqliteAdapterConfig,\n DatabaseCapabilities,\n PoolStats,\n TransactionContext,\n TransactionOptions,\n SqlParam,\n WhereClause,\n WhereCondition,\n WhereOperator,\n SelectOptions,\n InsertOptions,\n UpdateOptions,\n DeleteOptions,\n UpsertOptions,\n OrderBySpec,\n JoinSpec,\n DatabaseError,\n DatabaseErrorKind,\n BaseAdapterConfig,\n AdapterLogger,\n};\n\n/**\n * Package version\n */\nexport const VERSION = \"0.1.0\";\n\n/**\n * SQLite error codes mapping to DatabaseErrorKind.\n *\n * @see https://sqlite.org/rescode.html\n */\nconst SQLITE_ERROR_CODES: Record<string, DatabaseErrorKind> = {\n // Constraint violations\n SQLITE_CONSTRAINT: \"constraint\",\n SQLITE_CONSTRAINT_UNIQUE: \"unique_violation\",\n SQLITE_CONSTRAINT_PRIMARYKEY: \"unique_violation\",\n SQLITE_CONSTRAINT_FOREIGNKEY: \"foreign_key_violation\",\n SQLITE_CONSTRAINT_NOTNULL: \"not_null_violation\",\n SQLITE_CONSTRAINT_CHECK: \"check_violation\",\n\n // Busy/locked errors\n SQLITE_BUSY: \"timeout\",\n SQLITE_LOCKED: \"timeout\",\n\n // Connection errors\n SQLITE_CANTOPEN: \"connection\",\n SQLITE_NOTADB: \"connection\",\n SQLITE_CORRUPT: \"connection\",\n\n // Query errors\n SQLITE_ERROR: \"query\",\n SQLITE_MISUSE: \"query\",\n SQLITE_RANGE: \"query\",\n};\n\n/**\n * Default configuration values.\n */\nconst DEFAULT_CONFIG = {\n busyTimeout: 5000,\n wal: true,\n foreignKeys: true,\n};\n\n/**\n * Converts a JavaScript value to a type that better-sqlite3 can bind.\n * better-sqlite3 only accepts: number, string, bigint, Buffer, null.\n *\n * These conversions have to match what the schema declares, because the rows\n * they write are read back through it. Every column here that takes a `Date`\n * is `integer({ mode: \"timestamp\" })` — the system columns and user date\n * fields in `runtime-schema-generator.ts`, and the core tables, whose date\n * columns are all INTEGER. So a date is bound as unix seconds, which is what\n * that mode reads.\n */\nfunction sanitizeSqliteValue(v: unknown): unknown {\n if (v === undefined) return null;\n if (typeof v === \"boolean\") return v ? 1 : 0;\n // Seconds, not an ISO string. SQLite stores whatever it is given regardless\n // of the declared type, so a string lands in the column without complaint\n // and only fails on the way back out, where the timestamp decoder reads it\n // as a number and yields an Invalid Date.\n if (v instanceof Date) return Math.floor(v.getTime() / 1000);\n if (v !== null && typeof v === \"object\") return JSON.stringify(v);\n return v;\n}\n\n/**\n * SQLite database adapter for Nextly.\n *\n * Extends the base DrizzleAdapter to provide SQLite-specific functionality\n * using the better-sqlite3 package.\n *\n * @remarks\n * SQLite has some differences from PostgreSQL:\n * - Synchronous API (wrapped for async compatibility)\n * - No connection pooling (single file/connection)\n * - RETURNING clause supported (SQLite 3.35+)\n * - Savepoints supported via nested transactions\n * - No native ILIKE (uses LOWER() LIKE workaround)\n * - JSON support (not JSONB)\n * - No array types\n *\n * @example\n * ```typescript\n * const adapter = new SqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport class SqliteAdapter extends DrizzleAdapter {\n // getDrizzle memoization: drizzle v1's constructor builds a relational\n // query builder per table in the relations config (~40 tables), and the\n // service layer resolves an instance on every db access — construct once\n // per relations object (identity-stable: the schema registry caches it\n // and hands out a NEW object on invalidation, which naturally misses\n // this cache and produces a fresh instance).\n private drizzleByRelations = new WeakMap<AnyRelations, unknown>();\n private drizzleBare: unknown;\n\n /**\n * The database dialect - always 'sqlite' for this adapter.\n */\n readonly dialect = \"sqlite\" as const;\n\n /**\n * Adapter configuration.\n */\n protected readonly config: SqliteAdapterConfig;\n\n /**\n * better-sqlite3 Database instance.\n */\n private db: Database.Database | null = null;\n\n /**\n * Connection state flag.\n */\n private connected = false;\n\n /**\n * Serialization queue for concurrent `transaction()` calls.\n *\n * better-sqlite3 is synchronous, single-connection, and rejects nested\n * `BEGIN` with \"cannot start a transaction within a transaction\".\n * When two `await adapter.transaction(...)` calls overlap (e.g. a bulk\n * mutation calling `Promise.allSettled` with N per-row updates), the\n * second one tries to BEGIN while the first is still open and the\n * driver throws.\n *\n * Postgres/MySQL avoid this by allocating a fresh client/connection\n * per transaction from a pool — there is no equivalent for\n * better-sqlite3, so we serialize at the JS layer instead. Every\n * `transaction()` invocation chains onto this promise; only one\n * BEGIN→COMMIT/ROLLBACK section runs at any moment, preserving\n * write-isolation semantics that callers expect from a transactional\n * adapter.\n *\n * Performance impact is the natural floor: SQLite already serializes\n * writers at the file-lock level, so the queue removes failed-call\n * surface area without changing the achievable concurrency.\n */\n private transactionQueue: Promise<unknown> = Promise.resolve();\n\n /**\n * Creates a new SQLite adapter instance.\n *\n * @param config - Adapter configuration\n */\n constructor(config: SqliteAdapterConfig) {\n super();\n this.config = config;\n }\n\n /**\n * Connect to the SQLite database.\n *\n * @remarks\n * This method initializes the database connection. For SQLite, this\n * opens the database file or creates an in-memory database.\n * Also configures WAL mode and foreign keys based on config.\n *\n * @throws {DatabaseError} If connection fails\n */\n async connect(): Promise<void> {\n if (this.connected && this.db) {\n return;\n }\n\n try {\n // Dynamic import of better-sqlite3 to avoid bundling issues\n const BetterSqlite3 = await import(\"better-sqlite3\");\n const Database = BetterSqlite3.default;\n\n // Determine database path\n let dbPath: string;\n if (this.config.memory) {\n dbPath = \":memory:\";\n } else if (this.config.url) {\n // Strip file: prefix if present\n dbPath = this.config.url.replace(/^file:/, \"\");\n } else {\n dbPath = \":memory:\";\n }\n\n // Ensure the parent directory exists for file-backed databases.\n // better-sqlite3 fails with a confusing \"Cannot open database\n // because the directory does not exist\" error if it doesn't, and\n // a fresh clone with `DATABASE_URL=file:./data/app.db` reliably\n // hits this on first run. Create the dir lazily so DATABASE_URL\n // can point anywhere without a separate setup step.\n if (dbPath !== \":memory:\" && !this.config.readonly) {\n const fs = await import(\"node:fs\");\n const path = await import(\"node:path\");\n const dir = path.dirname(path.resolve(dbPath));\n await fs.promises.mkdir(dir, { recursive: true });\n }\n\n // Create database instance\n this.db = new Database(dbPath, {\n readonly: this.config.readonly ?? false,\n timeout: this.config.busyTimeout ?? DEFAULT_CONFIG.busyTimeout,\n });\n\n // Configure WAL mode for better concurrency (unless in-memory or readonly)\n if (\n (this.config.wal ?? DEFAULT_CONFIG.wal) &&\n dbPath !== \":memory:\" &&\n !this.config.readonly\n ) {\n this.db.pragma(\"journal_mode = WAL\");\n }\n\n // Enable foreign keys\n if (this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys) {\n this.db.pragma(\"foreign_keys = ON\");\n }\n\n // F17: check SQLite version. Hard-fails on SQLite <3.38. SQLite has\n // no recognized cloud variants we warn on, so onWarning is omitted.\n // Note: better-sqlite3's bundled SQLite is set by the package\n // version; users on this package's pinned better-sqlite3 (3.45+)\n // will always pass.\n await checkDialectVersion(this.db, \"sqlite\");\n\n this.connected = true;\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection established\", {\n url: dbPath === \":memory:\" ? \"in-memory\" : dbPath,\n wal: this.config.wal ?? DEFAULT_CONFIG.wal,\n foreignKeys: this.config.foreignKeys ?? DEFAULT_CONFIG.foreignKeys,\n });\n }\n } catch (error) {\n // Clean up on failure\n if (this.db) {\n try {\n this.db.close();\n } catch {\n // Ignore close errors during error handling\n }\n this.db = null;\n }\n throw this.classifyError(error);\n }\n }\n\n /**\n * Disconnect from the SQLite database.\n *\n * @remarks\n * This method closes the database connection and releases resources.\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async disconnect(): Promise<void> {\n // Drop memoized drizzle instances — they wrap the connection being\n // closed and must not survive a reconnect.\n this.drizzleBare = undefined;\n this.drizzleByRelations = new WeakMap();\n if (!this.db) {\n return;\n }\n\n try {\n this.db.close();\n\n if (this.config.logger?.info) {\n this.config.logger.info(\"SQLite connection closed\");\n }\n } finally {\n this.db = null;\n this.connected = false;\n }\n }\n\n /**\n * Check if connected to the database.\n */\n isConnected(): boolean {\n return this.connected && this.db !== null;\n }\n\n /**\n * Get connection pool statistics.\n *\n * @remarks\n * SQLite doesn't use connection pooling, so this returns null.\n * The database is single-file with a single connection.\n */\n getPoolStats(): PoolStats | null {\n // SQLite doesn't have connection pooling\n return null;\n }\n\n /**\n * Execute a raw SQL query.\n *\n * @param sql - SQL query string with $1, $2 placeholders (converted to ? for SQLite)\n * @param params - Query parameters\n * @returns Query results\n *\n * @throws {DatabaseError} If query execution fails\n */\n // better-sqlite3 is synchronous; method is async to satisfy the DatabaseAdapter contract.\n // eslint-disable-next-line @typescript-eslint/require-await\n async executeQuery<T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Convert $1, $2 placeholders to ? for better-sqlite3\n const convertedSql = this.convertPlaceholders(sql);\n\n // Determine if this is a SELECT query or a modifying query\n const trimmedSql = convertedSql.trim().toUpperCase();\n\n // Check for PRAGMA query statements (e.g., \"PRAGMA foreign_keys\").\n // Setting PRAGMAs (e.g., \"PRAGMA foreign_keys = OFF\") don't return data.\n const isPragmaQuery =\n trimmedSql.startsWith(\"PRAGMA\") && !trimmedSql.includes(\"=\");\n\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") ||\n isPragmaQuery ||\n trimmedSql.startsWith(\"WITH\");\n const hasReturning = trimmedSql.includes(\"RETURNING\");\n\n let result: T[];\n\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect || hasReturning) {\n // Use .all() for SELECT queries or queries with RETURNING\n const stmt = db.prepare(convertedSql);\n result = stmt.all(...sanitizedParams) as T[];\n } else {\n // Use .run() for INSERT/UPDATE/DELETE/PRAGMA settings without RETURNING\n const stmt = db.prepare(convertedSql);\n const runResult = stmt.run(...sanitizedParams);\n // Return info about the operation\n result = [\n {\n changes: runResult.changes,\n lastInsertRowid: runResult.lastInsertRowid,\n } as unknown as T,\n ];\n }\n\n // Log query if logger configured\n if (this.config.logger?.query) {\n const durationMs = Date.now() - startTime;\n this.config.logger.query(convertedSql, params, durationMs);\n }\n\n return result;\n } catch (error) {\n throw this.classifyError(error, sql);\n }\n }\n\n /**\n * Execute work within a transaction.\n *\n * @param work - Function containing transactional operations\n * @param options - Transaction options (isolation level not fully supported in SQLite)\n * @returns Result of the work function\n *\n * @remarks\n * Uses better-sqlite3's native transaction handling which:\n * - Automatically commits on success\n * - Automatically rolls back on error\n * - Converts nested transactions to savepoints\n *\n * Note: SQLite uses DEFERRED, IMMEDIATE, or EXCLUSIVE transaction modes\n * rather than isolation levels. This adapter uses IMMEDIATE by default.\n */\n async transaction<T>(\n work: (tx: TransactionContext) => Promise<T>,\n _options?: TransactionOptions\n ): Promise<T> {\n // Why: serialize concurrent transaction() invocations to dodge\n // better-sqlite3's \"cannot start a transaction within a transaction\"\n // error when two awaits overlap on the same connection. See the\n // `transactionQueue` field comment for the full rationale. The queue\n // chains every call onto a tail promise so only one BEGIN → COMMIT\n // section runs at a time; the next call's BEGIN waits for the\n // previous COMMIT/ROLLBACK to finish.\n //\n // Failure isolation: the queue's tail promise must not reject (a\n // rejection would poison every subsequent transaction with the same\n // rejected value). We attach a no-op `.catch` so the chain stays\n // resolved while the actual error propagates back to the original\n // caller through the inner promise.\n const run = async (): Promise<T> => this.runTransaction(work);\n const next = this.transactionQueue.then(run, run);\n this.transactionQueue = next.catch(() => undefined);\n return next;\n }\n\n /**\n * Inner transaction body. Kept private so callers always go through\n * the serialized `transaction()` queue above and the BEGIN/COMMIT\n * pair is never invoked outside of it.\n */\n private async runTransaction<T>(\n work: (tx: TransactionContext) => Promise<T>\n ): Promise<T> {\n const db = this.ensureDb();\n const startTime = Date.now();\n\n try {\n // Create transaction context\n const ctx = this.createTransactionContext(db);\n\n // Since better-sqlite3's db.transaction() doesn't support async functions,\n // we manually manage the transaction with BEGIN/COMMIT/ROLLBACK\n db.exec(\"BEGIN IMMEDIATE\");\n\n try {\n const result = await work(ctx);\n db.exec(\"COMMIT\");\n\n // Log success\n if (this.config.logger?.debug) {\n const durationMs = Date.now() - startTime;\n this.config.logger.debug(\"Transaction committed\", {\n durationMs,\n });\n }\n\n return result;\n } catch (error) {\n // Rollback on error\n try {\n db.exec(\"ROLLBACK\");\n } catch {\n // Ignore rollback errors - transaction may already be aborted\n }\n throw error;\n }\n } catch (error) {\n // Work inside a transaction may throw to roll the write back — a refused\n // value, a denied permission — and that is the application's verdict, not\n // the driver's failure. Classifying it would replace its code and payload\n // with a generic database error, so a caller that asked for a refusal is\n // handed an unexplained failure and the per-field detail never arrives.\n if (isApplicationError(error)) throw error;\n throw this.classifyError(error);\n }\n }\n\n /**\n * Get SQLite database capabilities.\n *\n * @remarks\n * SQLite capabilities:\n * - JSON support (not JSONB)\n * - No arrays\n * - No native ILIKE\n * - RETURNING clause (3.35+)\n * - Savepoints supported\n * - ON CONFLICT supported\n */\n getCapabilities(): DatabaseCapabilities {\n return {\n dialect: \"sqlite\",\n supportsJsonb: false, // SQLite uses JSON, not JSONB\n supportsJson: true,\n supportsArrays: false, // SQLite doesn't support array types\n supportsGeneratedColumns: true, // SQLite 3.31+\n supportsFts: true, // SQLite FTS5\n supportsIlike: false, // No native ILIKE, use LOWER() LIKE\n supportsReturning: true, // SQLite 3.35+\n supportsSavepoints: true, // SQLite supports savepoints\n supportsOnConflict: true, // ON CONFLICT clause\n maxParamsPerQuery: 999, // SQLite SQLITE_MAX_VARIABLE_NUMBER default\n maxIdentifierLength: 128, // SQLite doesn't have a strict limit\n };\n }\n\n /**\n * Override insertMany for bulk insert optimization.\n *\n * @remarks\n * Uses a single multi-row INSERT statement for better performance.\n */\n override async insertMany<T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> {\n if (data.length === 0) {\n return [];\n }\n\n const db = this.ensureDb();\n\n // For single record, use parent implementation\n if (data.length === 1) {\n const result = await this.insert<T>(table, data[0], options);\n return [result];\n }\n\n // Build multi-row INSERT\n const columns = Object.keys(data[0]);\n const params: SqlParam[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of data) {\n const placeholders: string[] = [];\n for (const col of columns) {\n params.push(sanitizeSqliteValue(record[col]) as SqlParam);\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n const columnList = columns\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columnList}) VALUES ${valuesClauses.join(\", \")}`;\n\n // Add RETURNING clause\n if (options?.returning) {\n const returning =\n options.returning === \"*\"\n ? \"*\"\n : options.returning.map(col => this.escapeIdentifier(col)).join(\", \");\n sql += ` RETURNING ${returning}`;\n } else {\n sql += \" RETURNING *\";\n }\n\n try {\n const stmt = db.prepare(sql);\n const rows = stmt.all(...(params as unknown[])) as T[];\n // SQLite stores a timestamp as an integer and hands it back as one. The\n // Drizzle read paths decode it through the column definition, so without\n // this a bulk insert answers with numbers where a read answers `Date`.\n const tableObj = this.getTableObject(table);\n return rows.map(r => this.mapRowFromRawSql(tableObj, r));\n } catch (error) {\n throw this.handleQueryError(error, \"insertMany\", table);\n }\n }\n\n // ============================================================\n // Protected Helper Methods\n // ============================================================\n\n /**\n * Ensures database is connected and returns it.\n *\n * @throws {DatabaseError} If not connected\n */\n private ensureDb(): Database.Database {\n if (!this.db) {\n throw createDatabaseError({\n kind: \"connection\",\n message: \"SqliteAdapter is not connected. Call connect() first.\",\n });\n }\n return this.db;\n }\n\n /**\n * Return the typed Drizzle instance for SQLite.\n * Guarded for server-only usage and requires an active connection.\n *\n * @param schema - Optional schema for relational queries (db.query.*)\n * @returns Drizzle ORM instance wrapping the better-sqlite3 connection\n * @throws {Error} If called in browser or not connected\n */\n getDrizzle<T = BetterSQLite3Database<AnyRelations>>(\n relations?: AnyRelations\n ): T {\n if (typeof window !== \"undefined\") {\n throw new Error(\"getDrizzle() is server-only\");\n }\n const db = this.ensureDb();\n // drizzle v1 removed the positional (client, config) form — positional\n // calls silently open a NEW :memory: database. Object form only.\n if (!relations) {\n this.drizzleBare ??= drizzle({ client: db });\n return this.drizzleBare as T;\n }\n let cached = this.drizzleByRelations.get(relations);\n if (!cached) {\n cached = drizzle({ client: db, relations });\n this.drizzleByRelations.set(relations, cached);\n }\n return cached as T;\n }\n\n /**\n * Convert $1, $2 placeholders to ? for better-sqlite3.\n *\n * @param sql - SQL with PostgreSQL-style placeholders\n * @returns SQL with ? placeholders\n */\n private convertPlaceholders(sql: string): string {\n // Replace $1, $2, etc. with ?\n return sql.replace(/\\$\\d+/g, \"?\");\n }\n\n /**\n * Creates a TransactionContext for the given database connection.\n */\n private createTransactionContext(db: Database.Database): TransactionContext {\n // better-sqlite3 is synchronous; the methods below are async to satisfy\n // the TransactionContext contract shared with truly-async dialect adapters.\n // Bind a Drizzle instance to the transaction's connection so the delegated\n // CRUD methods run inside it. better-sqlite3 is single-connection, so this\n // is the same connection getDrizzle() would use, but binding explicitly\n // keeps the three adapters consistent and correct if that ever changes.\n // Built lazily and memoized: transactions that use only raw execute/insert\n // never construct it.\n const buildTxExecutor = () => drizzle({ client: db });\n let txExecutor: ReturnType<typeof buildTxExecutor> | undefined;\n const txDb = () => (txExecutor ??= buildTxExecutor());\n return {\n // SQLite has no row-level locking and needs none here: `withTransaction`\n // opens SQLite transactions with BEGIN IMMEDIATE, which takes the write\n // lock up front and serializes writers for the whole transaction.\n lockRow: (): Promise<void> => Promise.resolve(),\n\n // `run`, not `all`: better-sqlite3 throws on a statement that returns no\n // rows, which is every statement this method exists to carry. The Drizzle\n // instance is the transaction-bound one, so the statement runs inside this\n // transaction.\n //\n // better-sqlite3 is synchronous, so this resolves an already-settled\n // promise rather than being declared `async` over a body that never\n // awaits.\n runStatement: (statement: SQL): Promise<void> => {\n txDb().run(statement);\n return Promise.resolve();\n },\n\n // `all`, not `run`: this is the reading half, and better-sqlite3 returns\n // rows only from `all`. Synchronous, so the promise is already settled.\n queryStatement: <T = Record<string, unknown>>(\n statement: SQL\n ): Promise<T[]> => Promise.resolve(txDb().all(statement)),\n\n // eslint-disable-next-line @typescript-eslint/require-await\n execute: async <T = unknown>(\n sql: string,\n params: SqlParam[] = []\n ): Promise<T[]> => {\n const convertedSql = this.convertPlaceholders(sql);\n const trimmedSql = convertedSql.trim().toUpperCase();\n const isSelect =\n trimmedSql.startsWith(\"SELECT\") || trimmedSql.includes(\"RETURNING\");\n const sanitizedParams = params.map(sanitizeSqliteValue);\n\n if (isSelect) {\n const stmt = db.prepare(convertedSql);\n return stmt.all(...sanitizedParams) as T[];\n } else {\n const stmt = db.prepare(convertedSql);\n const result = stmt.run(...sanitizedParams);\n return [\n {\n changes: result.changes,\n lastInsertRowid: result.lastInsertRowid,\n } as unknown as T,\n ];\n }\n },\n\n // Keys only, unlike the PostgreSQL and MySQL raw paths, which also encode\n // their date values. `sanitizeSqliteValue` below binds a `Date` as unix\n // seconds, and an instant carries no zone -- so what SQLite stores does\n // not depend on the server's timezone and there is nothing to correct.\n // eslint-disable-next-line @typescript-eslint/require-await\n insert: async <T = unknown>(\n table: string,\n data: Record<string, unknown>,\n options?: InsertOptions\n ): Promise<T> => {\n const mapped = this.mapKeysToSqlColumns(\n this.getTableObject(table),\n data\n );\n const columns = Object.keys(mapped);\n const values = Object.values(mapped).map(sanitizeSqliteValue);\n const placeholders = values.map(() => \"?\").join(\", \");\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES (${placeholders})`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n // No RETURNING clause: better-sqlite3's .all() rejects a statement\n // that returns no columns, so run it and return nothing.\n stmt.run(...values);\n return undefined as T;\n }\n const rows = stmt.all(...values) as T[];\n // Return JS property-named keys AND Drizzle-decoded date values, so a\n // write inside a transaction answers with the same representation a\n // read of the same row gives.\n return this.mapRowFromRawSql(this.getTableObject(table), rows[0]);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n insertMany: async <T = unknown>(\n table: string,\n data: Record<string, unknown>[],\n options?: InsertOptions\n ): Promise<T[]> => {\n if (data.length === 0) return [];\n\n const tableObj = this.getTableObject(table);\n const mappedRecords = data.map(r =>\n this.mapKeysToSqlColumns(tableObj, r)\n );\n const columns = Object.keys(mappedRecords[0]);\n const allValues: unknown[] = [];\n const valuesClauses: string[] = [];\n\n for (const record of mappedRecords) {\n const placeholders: string[] = [];\n for (const col of columns) {\n allValues.push(sanitizeSqliteValue(record[col]));\n placeholders.push(\"?\");\n }\n valuesClauses.push(`(${placeholders.join(\", \")})`);\n }\n\n let sql = `INSERT INTO ${this.escapeIdentifier(table)} (${columns.map(c => this.escapeIdentifier(c)).join(\", \")}) VALUES ${valuesClauses.join(\", \")}`;\n\n const ret = options?.returning;\n const returningEmpty = Array.isArray(ret) && ret.length === 0;\n if (!returningEmpty) {\n const returning =\n !ret || ret === \"*\"\n ? \"*\"\n : this.mapColumnNamesToSql(this.getTableObject(table), ret)\n .map(col => this.escapeIdentifier(col))\n .join(\", \");\n sql += ` RETURNING ${returning}`;\n }\n\n const stmt = db.prepare(sql);\n if (returningEmpty) {\n stmt.run(...allValues);\n return [];\n }\n return (stmt.all(...allValues) as T[]).map(r =>\n this.mapRowFromRawSql(tableObj, r)\n );\n },\n\n ...this.createTransactionForwarders(txDb),\n\n // eslint-disable-next-line @typescript-eslint/require-await\n savepoint: async (name: string): Promise<void> => {\n db.exec(`SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n rollbackToSavepoint: async (name: string): Promise<void> => {\n db.exec(`ROLLBACK TO SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n\n // eslint-disable-next-line @typescript-eslint/require-await\n releaseSavepoint: async (name: string): Promise<void> => {\n db.exec(`RELEASE SAVEPOINT ${this.escapeIdentifier(name)}`);\n },\n };\n }\n\n /**\n * Classifies a SQLite error into a DatabaseError.\n *\n * @param error - Original error from better-sqlite3\n * @param sql - SQL statement that caused the error (optional)\n * @returns DatabaseError with proper classification\n */\n // fallow-ignore-next-line complexity\n protected override classifyError(\n error: unknown,\n sql?: string\n ): DatabaseError {\n // Why short-circuit on existing DatabaseError: F17's\n // UnsupportedDialectVersionError is already a typed DatabaseError with\n // kind: \"unsupported_version\" plus detectedVersion/requiredVersion\n // fields. Re-wrapping it here would erase those fields.\n if (isDatabaseError(error)) return error;\n\n // Classify from the DRIVER's error, which is not necessarily the one handed\n // in. A query failure arrives wrapped: DrizzleQueryError carries the SQL\n // statement as its message and no code, and better-sqlite3's own error —\n // the only object that knows what actually went wrong — sits below it in\n // the `cause` chain. Classifying the wrapper means classifying the QUERY\n // TEXT, which reported a unique violation on a table with a `locked_by`\n // column as a \"timeout\", because the statement contains the word LOCKED.\n const sqliteError = findDriverError(error);\n\n // Determine error kind from SQLite error code\n let kind: DatabaseErrorKind = \"unknown\";\n\n if (sqliteError.code) {\n kind = SQLITE_ERROR_CODES[sqliteError.code] || \"unknown\";\n } else if (sqliteError.message) {\n // Try to extract error type from message. Safe to match loosely now:\n // `sqliteError` is the driver's error, so its message is the driver's\n // description of the failure and never a SQL statement.\n const msg = sqliteError.message.toUpperCase();\n if (msg.includes(\"UNIQUE CONSTRAINT\")) {\n kind = \"unique_violation\";\n } else if (msg.includes(\"FOREIGN KEY CONSTRAINT\")) {\n kind = \"foreign_key_violation\";\n } else if (msg.includes(\"NOT NULL CONSTRAINT\")) {\n kind = \"not_null_violation\";\n } else if (msg.includes(\"CHECK CONSTRAINT\")) {\n kind = \"check_violation\";\n } else if (msg.includes(\"BUSY\") || msg.includes(\"LOCKED\")) {\n kind = \"timeout\";\n } else if (\n msg.includes(\"SQLITE_CANTOPEN\") ||\n msg.includes(\"UNABLE TO OPEN\")\n ) {\n kind = \"connection\";\n }\n }\n\n // Build error message from the driver's description, for the same reason\n // the classification uses it: the wrapper's message is the SQL statement.\n let message = sqliteError.message ?? String(error);\n if (sql && kind === \"query\") {\n message = `Query failed: ${message}`;\n }\n\n return createDatabaseError({\n kind,\n message,\n code: sqliteError.code,\n cause: error instanceof Error ? error : undefined,\n });\n }\n}\n\n/** The fields this classifier reads off an error, at any depth in the chain. */\ninterface DriverErrorShape {\n code?: string;\n message?: string;\n name?: string;\n cause?: unknown;\n}\n\n/**\n * The deepest error in the `cause` chain that carries a SQLite error code, or\n * the outermost error when none does.\n *\n * better-sqlite3 sets `code` (`SQLITE_CONSTRAINT_UNIQUE`, `SQLITE_BUSY`, ...);\n * the wrappers around it do not. Walking to the code is therefore walking to\n * the only object that knows what failed. The walk is depth-bounded rather than\n * unbounded because an error chain can be made cyclic, and a classifier that\n * can hang is worse than one that occasionally gives up.\n *\n * Falls back to the outermost error so a driver error that genuinely carries no\n * code — one constructed from a message alone — still classifies exactly as it\n * did before.\n */\nfunction findDriverError(error: unknown): DriverErrorShape {\n let cursor = error as DriverErrorShape | null | undefined;\n for (let depth = 0; depth < 10 && cursor != null; depth++) {\n if (typeof cursor.code === \"string\" && cursor.code.length > 0)\n return cursor;\n cursor = cursor.cause as DriverErrorShape | null | undefined;\n }\n return error ?? {};\n}\n\n/**\n * Create a SQLite database adapter.\n *\n * @param config - SQLite adapter configuration\n * @returns A new SqliteAdapter instance\n *\n * @example\n * ```typescript\n * // Simple usage with file path\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * });\n *\n * // In-memory database\n * const memAdapter = createSqliteAdapter({\n * memory: true,\n * });\n *\n * // Full configuration\n * const adapter = createSqliteAdapter({\n * url: 'file:./data.db',\n * wal: true,\n * foreignKeys: true,\n * busyTimeout: 5000,\n * logger: {\n * query: (sql, params, duration) => console.log(`Query: ${sql}`),\n * },\n * });\n *\n * await adapter.connect();\n * ```\n */\nexport function createSqliteAdapter(\n config: SqliteAdapterConfig\n): SqliteAdapter {\n return new SqliteAdapter(config);\n}\n\n/**\n * Type guard to check if a value is a SqliteAdapter.\n *\n * @param value - Value to check\n * @returns True if value is a SqliteAdapter instance\n *\n * @example\n * ```typescript\n * if (isSqliteAdapter(adapter)) {\n * // TypeScript knows adapter is SqliteAdapter\n * console.log('Using SQLite');\n * }\n * ```\n */\nexport function isSqliteAdapter(value: unknown): value is SqliteAdapter {\n return value instanceof SqliteAdapter;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextlyhq/adapter-sqlite",
|
|
3
|
-
"version": "0.0.2-alpha.
|
|
3
|
+
"version": "0.0.2-alpha.64",
|
|
4
4
|
"description": "SQLite database adapter for Nextly - extends @nextlyhq/adapter-drizzle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,22 +21,22 @@
|
|
|
21
21
|
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"better-sqlite3": "^12.
|
|
24
|
+
"better-sqlite3": "^12.10.0",
|
|
25
25
|
"drizzle-orm": "1.0.0-rc.4",
|
|
26
|
-
"@nextlyhq/adapter-drizzle": "0.0.2-alpha.
|
|
26
|
+
"@nextlyhq/adapter-drizzle": "0.0.2-alpha.64"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/better-sqlite3": "^7.6.13",
|
|
30
30
|
"@vitest/coverage-v8": "^4.1.0",
|
|
31
31
|
"@vitest/ui": "^4.1.0",
|
|
32
|
-
"better-sqlite3": "^12.
|
|
32
|
+
"better-sqlite3": "^12.10.0",
|
|
33
33
|
"eslint": "^9.39.1",
|
|
34
34
|
"tsup": "^8.5.0",
|
|
35
35
|
"typescript": "^5.9.3",
|
|
36
36
|
"vite-tsconfig-paths": "^5.1.4",
|
|
37
37
|
"vitest": "^4.1.0",
|
|
38
|
-
"@nextlyhq/eslint-config": "0.0.2-alpha.
|
|
39
|
-
"@nextlyhq/tsconfig": "0.0.2-alpha.
|
|
38
|
+
"@nextlyhq/eslint-config": "0.0.2-alpha.64",
|
|
39
|
+
"@nextlyhq/tsconfig": "0.0.2-alpha.64"
|
|
40
40
|
},
|
|
41
41
|
"keywords": [
|
|
42
42
|
"nextly",
|