durable-db 0.1.7 → 0.1.8

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.
@@ -1,6 +1,6 @@
1
1
  import { t as Snapshot } from "../snapshot-DOerumb7.js";
2
2
  import { l as DatabaseInfo } from "../defineDatabase-Bjws1Jkd.js";
3
- import "../index-NJ7kT_a5.js";
3
+ import "../index-CXsj1VQb.js";
4
4
  import { Command } from "commander";
5
5
 
6
6
  //#region src/cli/command.d.ts
@@ -1,4 +1,4 @@
1
1
  import { t as type } from "../arktypeConfig--4OgOv4U.js";
2
2
  import { a as ActionInfo, c as DatabaseDefinition, d as InferArgs, f as ParsedDatabaseFile, i as ActionContext, l as DatabaseInfo, n as Action, o as ArgsSchema, r as ActionConfig, s as DatabaseConfig, t as defineDatabase, u as DrizzleToKysely } from "../defineDatabase-Bjws1Jkd.js";
3
- import { _ as CreateKyselyOptions, a as QueryResult, c as DrizzleDefaultsPlugin, d as dateSerializers, f as Migration, g as SqliteDurableObject, h as Migrations, i as KyselyPlugin, l as SchemaPlugin, m as MigrationSchemaConflictError, n as CompiledQuery, o as studio, p as MigrationAttemptInfo, r as Kysely, s as DateSerializePlugin, t as CamelCasePlugin, u as createDrizzlePlugins, v as SqlExecutor, y as createKyselyFromSql } from "../index-NJ7kT_a5.js";
3
+ import { _ as CreateKyselyOptions, a as QueryResult, c as DrizzleDefaultsPlugin, d as dateSerializers, f as Migration, g as SqliteDurableObject, h as Migrations, i as KyselyPlugin, l as SchemaPlugin, m as MigrationSchemaConflictError, n as CompiledQuery, o as studio, p as MigrationAttemptInfo, r as Kysely, s as DateSerializePlugin, t as CamelCasePlugin, u as createDrizzlePlugins, v as SqlExecutor, y as createKyselyFromSql } from "../index-CXsj1VQb.js";
4
4
  export { Action, ActionConfig, ActionContext, ActionInfo, ArgsSchema, CamelCasePlugin, CompiledQuery, CreateKyselyOptions, DatabaseConfig, DatabaseDefinition, DatabaseInfo, DateSerializePlugin, DrizzleDefaultsPlugin, DrizzleToKysely, InferArgs, Kysely, KyselyPlugin, Migration, MigrationAttemptInfo, MigrationSchemaConflictError, Migrations, ParsedDatabaseFile, QueryResult, SchemaPlugin, SqlExecutor, SqliteDurableObject, createDrizzlePlugins, createKyselyFromSql, dateSerializers, defineDatabase, studio, type };
package/dist/db/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { i as init_debug, n as debugMigrations } from "../debug-DmBrUmwe.js";
2
2
  import { t as type } from "../arktypeConfig-CTCJm4vA.js";
3
3
  import { n as defineDatabase } from "../defineDatabase-BD3k1L7d.js";
4
- import { a as createDrizzlePlugins, i as SchemaPlugin, n as DateSerializePlugin, o as dateSerializers, r as DrizzleDefaultsPlugin, t as createKyselyFromSql } from "../kysely-yPsCWEvR.js";
4
+ import { a as createDrizzlePlugins, i as SchemaPlugin, n as DateSerializePlugin, o as dateSerializers, r as DrizzleDefaultsPlugin, t as createKyselyFromSql } from "../kysely-BH7QZBZw.js";
5
5
  import { DurableObject } from "cloudflare:workers";
6
6
  import { CamelCasePlugin } from "kysely";
7
7
  import { studio } from "@outerbase/browsable-durable-object";
@@ -342,6 +342,7 @@ declare class DateSerializePlugin implements KyselyPlugin {
342
342
  * - DrizzleDefaultsPlugin: auto-populates defaultFn/onUpdateFn values
343
343
  * - SchemaPlugin (extends CamelCasePlugin): schema-aware camelCase ↔ snake_case
344
344
  * - DateSerializePlugin: Date ↔ SQLite text serialization
345
+ * - BooleanDeserializePlugin: boolean-mode columns read back as booleans
345
346
  *
346
347
  * When camelCase is false, SchemaPlugin is omitted.
347
348
  */
@@ -247,6 +247,63 @@ var DateSerializePlugin = class {
247
247
  }
248
248
  };
249
249
  /**
250
+ * Collect the names of columns Drizzle declares as boolean-typed
251
+ * (`integer({ mode: 'boolean' })`). Mirrors collectDateColumnNames: covers
252
+ * the SQL column name, the JS property name, and the snake_cased property
253
+ * name, so it works on either side of the CamelCasePlugin.
254
+ */
255
+ function collectBooleanColumnNames(schema) {
256
+ const booleanColumns = /* @__PURE__ */ new Set();
257
+ for (const table of Object.values(schema)) {
258
+ const columns = getTableColumns(table);
259
+ for (const [propertyName, column] of Object.entries(columns)) if (column.dataType === "boolean") {
260
+ booleanColumns.add(column.name);
261
+ booleanColumns.add(propertyName);
262
+ booleanColumns.add(toSnakeCase(propertyName));
263
+ }
264
+ }
265
+ return booleanColumns;
266
+ }
267
+ /**
268
+ * Plugin that deserializes boolean-mode columns on read.
269
+ *
270
+ * SQLite stores booleans as INTEGER 1/0 (the write side is normalized in the
271
+ * driver — see normalizeParameter in kysely.ts), so schema-declared boolean
272
+ * columns come back as numbers and must be mapped to real booleans. Legacy
273
+ * rows written before the driver normalization existed carry TEXT
274
+ * 'true'/'false' — those are mapped too, so old data keeps reading correctly.
275
+ * Anything unrecognized round-trips verbatim rather than being guessed at.
276
+ */
277
+ var BooleanDeserializePlugin = class {
278
+ booleanColumns;
279
+ constructor(schema) {
280
+ this.booleanColumns = collectBooleanColumnNames(schema);
281
+ }
282
+ transformQuery(args) {
283
+ return args.node;
284
+ }
285
+ async transformResult(args) {
286
+ if (this.booleanColumns.size === 0) return args.result;
287
+ return {
288
+ ...args.result,
289
+ rows: args.result.rows.map((row) => this.transformRow(row))
290
+ };
291
+ }
292
+ transformRow(row) {
293
+ const result = {};
294
+ for (const [key, value] of Object.entries(row)) result[key] = this.booleanColumns.has(key) ? this.toBoolean(value) : value;
295
+ return result;
296
+ }
297
+ toBoolean(value) {
298
+ if (value === null || value === void 0 || typeof value === "boolean") return value;
299
+ if (typeof value === "number") return value !== 0;
300
+ if (typeof value === "bigint") return value !== 0n;
301
+ if (value === "true" || value === "1") return true;
302
+ if (value === "false" || value === "0") return false;
303
+ return value;
304
+ }
305
+ };
306
+ /**
250
307
  * Validate that a schema object is shaped like a record of Drizzle tables.
251
308
  *
252
309
  * Throws a descriptive error pointing at the offending key when the schema
@@ -269,6 +326,7 @@ function assertValidSchema(schema) {
269
326
  * - DrizzleDefaultsPlugin: auto-populates defaultFn/onUpdateFn values
270
327
  * - SchemaPlugin (extends CamelCasePlugin): schema-aware camelCase ↔ snake_case
271
328
  * - DateSerializePlugin: Date ↔ SQLite text serialization
329
+ * - BooleanDeserializePlugin: boolean-mode columns read back as booleans
272
330
  *
273
331
  * When camelCase is false, SchemaPlugin is omitted.
274
332
  */
@@ -278,11 +336,97 @@ function createDrizzlePlugins(schema, camelCase = true) {
278
336
  plugins.push(new DrizzleDefaultsPlugin(schema));
279
337
  if (camelCase) plugins.push(new SchemaPlugin(schema));
280
338
  plugins.push(new DateSerializePlugin(schema));
339
+ plugins.push(new BooleanDeserializePlugin(schema));
281
340
  return plugins;
282
341
  }
283
342
 
284
343
  //#endregion
285
344
  //#region src/db/kysely.ts
345
+ const MAX_BOUND_PARAMETERS = 100;
346
+ /**
347
+ * workerd has no boolean binding type and silently stringifies JS booleans
348
+ * (storing TEXT 'true' in an INTEGER column); node:sqlite rejects them
349
+ * outright. Normalize to SQLite's 1/0 convention before the value reaches
350
+ * either. `undefined` → NULL for the same reason.
351
+ */
352
+ function normalizeParameter(value) {
353
+ if (typeof value === "boolean") return value ? 1 : 0;
354
+ if (value === void 0) return null;
355
+ return value;
356
+ }
357
+ function escapeLiteral(value) {
358
+ if (value === null || value === void 0) return "NULL";
359
+ if (typeof value === "boolean") return value ? "1" : "0";
360
+ if (typeof value === "number") {
361
+ if (!Number.isFinite(value)) throw new Error(`[db] Cannot inline non-finite number parameter (${value})`);
362
+ return String(value);
363
+ }
364
+ if (typeof value === "bigint") return value.toString();
365
+ if (typeof value === "string") {
366
+ if (value.includes("\0")) throw new Error("[db] Cannot inline a string parameter containing a NUL byte");
367
+ return `'${value.replaceAll("'", "''")}'`;
368
+ }
369
+ if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
370
+ const bytes = value instanceof ArrayBuffer ? new Uint8Array(value) : new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
371
+ let hex = "";
372
+ for (const b of bytes) hex += b.toString(16).padStart(2, "0");
373
+ return `X'${hex}'`;
374
+ }
375
+ throw new Error(`[db] Cannot inline SQL parameter of type ${typeof value}`);
376
+ }
377
+ /**
378
+ * Replace each `?` placeholder in `sql` with the escaped literal of its
379
+ * parameter. A real scanner, not a regex: '…' string literals ('' escapes),
380
+ * "…" quoted identifiers, `--` line comments and C-style block comments are
381
+ * skipped, so a `?` inside any of them is never touched.
382
+ *
383
+ * Exported for tests.
384
+ */
385
+ function inlineParameters(sql$1, parameters) {
386
+ let out = "";
387
+ let p = 0;
388
+ for (let i = 0; i < sql$1.length; i++) {
389
+ const ch = sql$1[i];
390
+ if (ch === "'" || ch === "\"") {
391
+ let j = i + 1;
392
+ while (j < sql$1.length) {
393
+ if (sql$1[j] === ch) {
394
+ if (sql$1[j + 1] === ch) {
395
+ j += 2;
396
+ continue;
397
+ }
398
+ break;
399
+ }
400
+ j++;
401
+ }
402
+ out += sql$1.slice(i, j + 1);
403
+ i = j;
404
+ continue;
405
+ }
406
+ if (ch === "-" && sql$1[i + 1] === "-") {
407
+ let j = sql$1.indexOf("\n", i);
408
+ if (j === -1) j = sql$1.length;
409
+ out += sql$1.slice(i, j);
410
+ i = j - 1;
411
+ continue;
412
+ }
413
+ if (ch === "/" && sql$1[i + 1] === "*") {
414
+ let j = sql$1.indexOf("*/", i + 2);
415
+ j = j === -1 ? sql$1.length : j + 2;
416
+ out += sql$1.slice(i, j);
417
+ i = j - 1;
418
+ continue;
419
+ }
420
+ if (ch === "?") {
421
+ if (p >= parameters.length) throw new Error("[db] Query has more `?` placeholders than parameters while inlining");
422
+ out += escapeLiteral(parameters[p++]);
423
+ continue;
424
+ }
425
+ out += ch;
426
+ }
427
+ if (p !== parameters.length) throw new Error(`[db] Inlined ${p} parameter(s) but the query provided ${parameters.length} — placeholder/parameter mismatch`);
428
+ return out;
429
+ }
286
430
  /**
287
431
  * A Kysely driver that uses Cloudflare DO SQLite storage
288
432
  */
@@ -331,7 +475,12 @@ var CloudflareSqliteConnection = class {
331
475
  this.sql = sql$1;
332
476
  }
333
477
  async executeQuery(compiledQuery) {
334
- const { sql: queryStr, parameters } = compiledQuery;
478
+ let queryStr = compiledQuery.sql;
479
+ let parameters = compiledQuery.parameters.map(normalizeParameter);
480
+ if (parameters.length > MAX_BOUND_PARAMETERS) {
481
+ queryStr = inlineParameters(queryStr, parameters);
482
+ parameters = [];
483
+ }
335
484
  const isSelect = /^\s*(SELECT|RETURNING)/i.test(queryStr) || /\s+RETURNING\s+/i.test(queryStr);
336
485
  try {
337
486
  if (isSelect) return {
@@ -3,7 +3,7 @@ import { t as type } from "../arktypeConfig-CTCJm4vA.js";
3
3
  import { t as ACTION_TEST_META } from "../defineDatabase-BD3k1L7d.js";
4
4
  import "../state-B4v8ICj8.js";
5
5
  import { a as setTenantIdResolver } from "../context-Cwe1IsnH.js";
6
- import { t as createKyselyFromSql } from "../kysely-yPsCWEvR.js";
6
+ import { t as createKyselyFromSql } from "../kysely-BH7QZBZw.js";
7
7
  import { n as generateMigrationStatements, r as generateSnapshotFromSchema, t as createEmptySnapshot } from "../snapshot-DMW6j2Ix.js";
8
8
  import { createRequire } from "node:module";
9
9
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "durable-db",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/eagerpatch/durable-db.git"