durable-db 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/cli/bin.js +7 -7
  2. package/dist/cli/index.d.ts +3 -3
  3. package/dist/cli/index.js +7 -7
  4. package/dist/{cli-Cr9686BF.js → cli-BsxcF5xB.js} +7 -7
  5. package/dist/context/index.d.ts +1 -1
  6. package/dist/context/index.js +3 -3
  7. package/dist/{context-BuIiIQ6K.js → context-NjS9iv2J.js} +2 -2
  8. package/dist/db/index.d.ts +2 -2
  9. package/dist/db/index.js +5 -386
  10. package/dist/{defineDatabase-jBv90-Pk.d.ts → defineDatabase-B8KJ1xYJ.d.ts} +1 -1
  11. package/dist/{defineDatabase-CONf4xFC.js → defineDatabase-DXA53pG3.js} +20 -2
  12. package/dist/{generator-BUPP9scJ.js → generator-BVwDQvKA.js} +25 -3
  13. package/dist/{index-CU9edk9Q.d.ts → index-C5C4vaBs.d.ts} +20 -18
  14. package/dist/index.d.ts +4 -4
  15. package/dist/index.js +6 -6
  16. package/dist/kysely-yPsCWEvR.js +388 -0
  17. package/dist/migrations/generator.d.ts +1 -1
  18. package/dist/migrations/generator.js +3 -3
  19. package/dist/migrations/snapshot.d.ts +1 -1
  20. package/dist/migrations/snapshot.js +1 -1
  21. package/dist/out-on886o85.js +7446 -0
  22. package/dist/{parser-CbX54Flt.js → parser-V6t9KSi1.js} +1 -1
  23. package/dist/push-DJExqk8A.js +8 -0
  24. package/dist/{push-BgQ5cnfH.js → push-tAKBd8b1.js} +5 -5
  25. package/dist/registry.d.ts +3 -1
  26. package/dist/registry.js +3 -3
  27. package/dist/schema.d.ts +1 -1
  28. package/dist/schema.js +1 -1
  29. package/dist/{state-FOEjpgJ6.js → state-CMRjqCDw.js} +1 -1
  30. package/dist/{state-BCcvNN-M.js → state-DCXcGtiu.js} +2 -2
  31. package/dist/testing/index.d.ts +57 -0
  32. package/dist/testing/index.js +72 -0
  33. package/dist/transport/protocol.js +1 -1
  34. package/dist/transport/websocket.js +1 -1
  35. package/dist/vite/durableDb.js +8 -8
  36. package/dist/{websocket-Dx1fqwnF.js → websocket-eao2rnHL.js} +1 -1
  37. package/package.json +5 -1
  38. package/dist/push-BkkaslPd.js +0 -8
  39. /package/dist/{debug-BZJbKybf.js → debug-DzvNVBs5.js} +0 -0
  40. /package/dist/{index-CqVMaMYY.d.ts → index-DaDdJ_cg.d.ts} +0 -0
  41. /package/dist/{protocol-g_plhPOY.js → protocol-DEubbBbR.js} +0 -0
  42. /package/dist/{schema-BOtrGnzM.js → schema-IFq7ovQq.js} +0 -0
  43. /package/dist/{schema-CpzTOfZ6.d.ts → schema-suxWBY7-.d.ts} +0 -0
  44. /package/dist/{snapshot-D170nL7i.js → snapshot-DMW6j2Ix.js} +0 -0
  45. /package/dist/{snapshot-C162qYoA.d.ts → snapshot-DOerumb7.d.ts} +0 -0
@@ -1,4 +1,4 @@
1
- import { i as init_debug } from "./debug-BZJbKybf.js";
1
+ import { i as init_debug } from "./debug-DzvNVBs5.js";
2
2
  import * as fs from "node:fs";
3
3
  import * as path from "node:path";
4
4
  import { parse } from "@babel/parser";
@@ -0,0 +1,8 @@
1
+ import "./debug-DzvNVBs5.js";
2
+ import "./state-CMRjqCDw.js";
3
+ import "./snapshot-DMW6j2Ix.js";
4
+ import "./generator-BVwDQvKA.js";
5
+ import "./parser-V6t9KSi1.js";
6
+ import { n as pushOne, t as push } from "./push-tAKBd8b1.js";
7
+
8
+ export { push };
@@ -1,8 +1,8 @@
1
- import { i as init_debug, t as debugCli } from "./debug-BZJbKybf.js";
2
- import { d as init_state, f as loadDevMigrations, h as saveDevMigration, n as clearDevMigrations } from "./state-FOEjpgJ6.js";
3
- import { n as generateMigrationStatements, o as snapshotsEqual, r as generateSnapshotFromSchema } from "./snapshot-D170nL7i.js";
4
- import { s as loadSnapshot, t as buildAndLoadSchema } from "./generator-BUPP9scJ.js";
5
- import { a as resolveImportPath, i as readFile, n as parseDatabaseFile, r as discoverDatabaseFiles } from "./parser-CbX54Flt.js";
1
+ import { i as init_debug, t as debugCli } from "./debug-DzvNVBs5.js";
2
+ import { d as init_state, f as loadDevMigrations, h as saveDevMigration, n as clearDevMigrations } from "./state-CMRjqCDw.js";
3
+ import { n as generateMigrationStatements, o as snapshotsEqual, r as generateSnapshotFromSchema } from "./snapshot-DMW6j2Ix.js";
4
+ import { s as loadSnapshot, t as buildAndLoadSchema } from "./generator-BVwDQvKA.js";
5
+ import { a as resolveImportPath, i as readFile, n as parseDatabaseFile, r as discoverDatabaseFiles } from "./parser-V6t9KSi1.js";
6
6
  import * as path from "node:path";
7
7
  import * as crypto from "node:crypto";
8
8
 
@@ -1,3 +1,5 @@
1
+ import { type } from "arktype";
2
+
1
3
  //#region src/registry.d.ts
2
4
  interface ActionDefinition {
3
5
  validator: (args: unknown) => unknown;
@@ -25,4 +27,4 @@ declare function runWithDoContext<T>(store: DoContext, fn: () => T): T;
25
27
  */
26
28
  declare function callAction(db: unknown, targetDb: string, actionName: string, args: unknown, ctx: RpcContext): Promise<unknown>;
27
29
  //#endregion
28
- export { ActionDefinition, DoContext, RpcContext, callAction, getAction, getDoContext, registerAction, runWithDoContext };
30
+ export { ActionDefinition, DoContext, RpcContext, callAction, getAction, getDoContext, registerAction, runWithDoContext, type };
package/dist/registry.js CHANGED
@@ -1,5 +1,5 @@
1
- import { n as WebSocketTransport } from "./websocket-Dx1fqwnF.js";
2
- import { type } from "arktype";
1
+ import { t as type } from "./out-on886o85.js";
2
+ import { n as WebSocketTransport } from "./websocket-eao2rnHL.js";
3
3
  import { AsyncLocalStorage } from "node:async_hooks";
4
4
 
5
5
  //#region src/registry.ts
@@ -69,4 +69,4 @@ async function callAction(db, targetDb, actionName, args, ctx) {
69
69
  }
70
70
 
71
71
  //#endregion
72
- export { callAction, getAction, getDoContext, registerAction, runWithDoContext };
72
+ export { callAction, getAction, getDoContext, registerAction, runWithDoContext, type };
package/dist/schema.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as integer, c as real, d as uniqueIndex, i as index, l as table, n as blob, o as numeric, r as foreignKey, s as primaryKey, t as AnySQLiteColumn, u as text } from "./schema-CpzTOfZ6.js";
1
+ import { a as integer, c as real, d as uniqueIndex, i as index, l as table, n as blob, o as numeric, r as foreignKey, s as primaryKey, t as AnySQLiteColumn, u as text } from "./schema-suxWBY7-.js";
2
2
  export { AnySQLiteColumn, blob, foreignKey, index, integer, numeric, primaryKey, real, table, text, uniqueIndex };
package/dist/schema.js CHANGED
@@ -1,3 +1,3 @@
1
- import { a as numeric, c as table, i as integer, l as text, n as foreignKey, o as primaryKey, r as index, s as real, t as blob, u as uniqueIndex } from "./schema-BOtrGnzM.js";
1
+ import { a as numeric, c as table, i as integer, l as text, n as foreignKey, o as primaryKey, r as index, s as real, t as blob, u as uniqueIndex } from "./schema-IFq7ovQq.js";
2
2
 
3
3
  export { blob, foreignKey, index, integer, numeric, primaryKey, real, table, text, uniqueIndex };
@@ -1,4 +1,4 @@
1
- import { a as __esmMin, i as init_debug, o as __export, t as debugCli } from "./debug-BZJbKybf.js";
1
+ import { a as __esmMin, i as init_debug, o as __export, t as debugCli } from "./debug-DzvNVBs5.js";
2
2
  import * as fs from "node:fs";
3
3
  import * as path from "node:path";
4
4
 
@@ -1,5 +1,5 @@
1
- import "./debug-BZJbKybf.js";
2
- import { _ as saveDevState, a as generateEpoch, c as getDevPaths, d as init_state, f as loadDevMigrations, g as saveDevSnapshot, h as saveDevMigration, i as findNodeModules, l as getInstanceKey, m as loadDevState, n as clearDevMigrations, o as getDatabaseDevState, p as loadDevSnapshot, r as createEmptyDevState, s as getDevEpoch, t as clearDatabaseDevState, u as hasProdSnapshotChanged } from "./state-FOEjpgJ6.js";
1
+ import "./debug-DzvNVBs5.js";
2
+ import { _ as saveDevState, a as generateEpoch, c as getDevPaths, d as init_state, f as loadDevMigrations, g as saveDevSnapshot, h as saveDevMigration, i as findNodeModules, l as getInstanceKey, m as loadDevState, n as clearDevMigrations, o as getDatabaseDevState, p as loadDevSnapshot, r as createEmptyDevState, s as getDevEpoch, t as clearDatabaseDevState, u as hasProdSnapshotChanged } from "./state-CMRjqCDw.js";
3
3
 
4
4
  init_state();
5
5
  export { loadDevMigrations };
@@ -0,0 +1,57 @@
1
+ import { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
2
+ import { Kysely } from "kysely";
3
+ import { DatabaseSync } from "node:sqlite";
4
+
5
+ //#region src/testing/index.d.ts
6
+ type Schema = Record<string, SQLiteTableWithColumns<any>>;
7
+ interface CreateTestDatabaseOptions {
8
+ /**
9
+ * The drizzle schema (the same object passed to `defineDatabase({ schema })`).
10
+ * Tables are created from it automatically.
11
+ */
12
+ schema: Schema;
13
+ /**
14
+ * Explicit `CREATE TABLE …` (and index) statements to apply instead of
15
+ * deriving them from `schema`. Useful to pin a known DDL or test a migration.
16
+ */
17
+ statements?: string[];
18
+ /**
19
+ * Tenant id handlers see via `getTenantId()`. Defaults to `'test'`. The
20
+ * in-memory db is a single shared store regardless — this only affects code
21
+ * that reads the tenant id.
22
+ */
23
+ tenantId?: string;
24
+ }
25
+ interface TestDatabase<TSchema = any> {
26
+ /** The Kysely instance, for direct seeding and assertions. */
27
+ db: Kysely<TSchema>;
28
+ /**
29
+ * Run an `action()` the way production does: validate `args` with the
30
+ * action's arktype schema, then invoke its handler against {@link db}.
31
+ * Throws the same "Invalid args" error as production on a bad payload.
32
+ */
33
+ run<TArgs, TResult>(action: (args: TArgs) => Promise<TResult>, args: TArgs): Promise<TResult>;
34
+ /** The raw `node:sqlite` handle, for escape-hatch SQL. */
35
+ sqlite: DatabaseSync;
36
+ /** Close the database and clear the tenant resolver. */
37
+ destroy(): void;
38
+ }
39
+ /**
40
+ * Spin up an isolated in-memory database for a `defineDatabase` schema and run
41
+ * its actions against it.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import { createTestDatabase } from 'durable-db/testing';
46
+ * import { settings, shipments } from '../src/databases/schema';
47
+ * import { upsertShipment, listShipments } from '../src/databases/actions';
48
+ *
49
+ * const t = await createTestDatabase({ schema: { settings, shipments } });
50
+ * await t.run(upsertShipment, { orderId: 'gid://…/1', status: 'booked', … });
51
+ * expect(await t.run(listShipments, {})).toHaveLength(1);
52
+ * t.destroy();
53
+ * ```
54
+ */
55
+ declare function createTestDatabase<TSchema = any>(options: CreateTestDatabaseOptions): Promise<TestDatabase<TSchema>>;
56
+ //#endregion
57
+ export { CreateTestDatabaseOptions, TestDatabase, createTestDatabase };
@@ -0,0 +1,72 @@
1
+ import "../debug-DzvNVBs5.js";
2
+ import { t as type } from "../out-on886o85.js";
3
+ import { t as ACTION_TEST_META } from "../defineDatabase-DXA53pG3.js";
4
+ import "../state-CMRjqCDw.js";
5
+ import { a as setTenantIdResolver } from "../context-NjS9iv2J.js";
6
+ import { t as createKyselyFromSql } from "../kysely-yPsCWEvR.js";
7
+ import { n as generateMigrationStatements, r as generateSnapshotFromSchema, t as createEmptySnapshot } from "../snapshot-DMW6j2Ix.js";
8
+ import { createRequire } from "node:module";
9
+
10
+ //#region src/testing/index.ts
11
+ const loadSqlite = () => createRequire(import.meta.url)("node:sqlite");
12
+ /**
13
+ * A `SqlStorage`-shaped shim over a synchronous `node:sqlite` connection — the
14
+ * minimal surface `createKyselyFromSql` drives (`exec(sql, …params).toArray()`).
15
+ */
16
+ function sqlStorageShim(sqlite) {
17
+ const isReader = (sql) => /^\s*(SELECT|PRAGMA|WITH)/i.test(sql) || /\bRETURNING\b/i.test(sql);
18
+ const normalize = (p) => typeof p === "boolean" ? p ? 1 : 0 : p === void 0 ? null : p;
19
+ return { exec(query, ...bindings) {
20
+ const stmt = sqlite.prepare(query);
21
+ const params = bindings.map(normalize);
22
+ const rows = isReader(query) ? stmt.all(...params) : (stmt.run(...params), []);
23
+ return { toArray: () => rows };
24
+ } };
25
+ }
26
+ async function deriveStatements(schema) {
27
+ const snapshot = await generateSnapshotFromSchema(schema);
28
+ return generateMigrationStatements(createEmptySnapshot(), snapshot);
29
+ }
30
+ /**
31
+ * Spin up an isolated in-memory database for a `defineDatabase` schema and run
32
+ * its actions against it.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * import { createTestDatabase } from 'durable-db/testing';
37
+ * import { settings, shipments } from '../src/databases/schema';
38
+ * import { upsertShipment, listShipments } from '../src/databases/actions';
39
+ *
40
+ * const t = await createTestDatabase({ schema: { settings, shipments } });
41
+ * await t.run(upsertShipment, { orderId: 'gid://…/1', status: 'booked', … });
42
+ * expect(await t.run(listShipments, {})).toHaveLength(1);
43
+ * t.destroy();
44
+ * ```
45
+ */
46
+ async function createTestDatabase(options) {
47
+ const { schema, statements, tenantId = "test" } = options;
48
+ const { DatabaseSync } = loadSqlite();
49
+ const sqlite = new DatabaseSync(":memory:");
50
+ const db = createKyselyFromSql(sqlStorageShim(sqlite), { schema });
51
+ const ddl = statements ?? await deriveStatements(schema);
52
+ for (const stmt of ddl) sqlite.exec(stmt);
53
+ setTenantIdResolver(() => tenantId);
54
+ return {
55
+ db,
56
+ async run(action, args) {
57
+ const meta = action[ACTION_TEST_META];
58
+ if (!meta) throw new Error("createTestDatabase().run() was given a function that is not a durable-db action (no test metadata). Pass an export created by `action({ … })`.");
59
+ const validated = meta.validator(args);
60
+ if (validated instanceof type.errors) throw new Error(`Invalid args for action: ${validated.summary}`);
61
+ return await meta.handler(db, validated);
62
+ },
63
+ sqlite,
64
+ destroy() {
65
+ setTenantIdResolver(null);
66
+ sqlite.close();
67
+ }
68
+ };
69
+ }
70
+
71
+ //#endregion
72
+ export { createTestDatabase };
@@ -1,3 +1,3 @@
1
- import { i as encodeResponse, n as decodeResponse, r as encodeRequest, t as decodeRequest } from "../protocol-g_plhPOY.js";
1
+ import { i as encodeResponse, n as decodeResponse, r as encodeRequest, t as decodeRequest } from "../protocol-DEubbBbR.js";
2
2
 
3
3
  export { decodeRequest, decodeResponse, encodeRequest, encodeResponse };
@@ -1,3 +1,3 @@
1
- import { n as WebSocketTransport, t as DEFAULT_WS_REQUEST_TIMEOUT_MS } from "../websocket-Dx1fqwnF.js";
1
+ import { n as WebSocketTransport, t as DEFAULT_WS_REQUEST_TIMEOUT_MS } from "../websocket-eao2rnHL.js";
2
2
 
3
3
  export { DEFAULT_WS_REQUEST_TIMEOUT_MS, WebSocketTransport };
@@ -1,8 +1,8 @@
1
- import { i as init_debug, r as debugVite } from "../debug-BZJbKybf.js";
2
- import { _ as saveDevState, c as getDevPaths, d as init_state, m as loadDevState } from "../state-FOEjpgJ6.js";
3
- import "../snapshot-D170nL7i.js";
4
- import { a as loadMigrationFiles } from "../generator-BUPP9scJ.js";
5
- import { i as readFile, n as parseDatabaseFile, r as discoverDatabaseFiles, t as BABEL_PARSER_PLUGINS } from "../parser-CbX54Flt.js";
1
+ import { i as init_debug, r as debugVite } from "../debug-DzvNVBs5.js";
2
+ import { _ as saveDevState, c as getDevPaths, d as init_state, m as loadDevState } from "../state-CMRjqCDw.js";
3
+ import "../snapshot-DMW6j2Ix.js";
4
+ import { a as loadMigrationFiles } from "../generator-BVwDQvKA.js";
5
+ import { i as readFile, n as parseDatabaseFile, r as discoverDatabaseFiles, t as BABEL_PARSER_PLUGINS } from "../parser-V6t9KSi1.js";
6
6
  import * as fs from "node:fs";
7
7
  import * as path from "node:path";
8
8
  import { parse } from "@babel/parser";
@@ -597,13 +597,13 @@ function buildStubBodyStatements(config) {
597
597
  function applyActionTransforms(body, ctx) {
598
598
  const { dbName, database, actionsInFile, contextImport, registryImport } = ctx;
599
599
  if (actionsInFile.length === 0) return false;
600
- ensureNamedImports(body, "arktype", ["type"]);
601
600
  ensureNamedImports(body, contextImport, ["getTenantId"]);
602
601
  ensureNamedImports(body, "cloudflare:workers", ["env"]);
603
602
  ensureNamedImports(body, registryImport, [
604
603
  "registerAction",
605
604
  "getDoContext",
606
- "callAction"
605
+ "callAction",
606
+ "type"
607
607
  ]);
608
608
  ensureNamedImports(body, DEV_EPOCH_IMPORT, ["applyDevEpoch"]);
609
609
  if (database.transport === "websocket") ensureNamedImports(body, "durable-db/transport/websocket", ["WebSocketTransport"]);
@@ -837,7 +837,7 @@ var PluginState = class {
837
837
  const isDev = this.config.command === "serve";
838
838
  db.migrations = loadMigrationFiles(migrationsDir);
839
839
  if (isDev) try {
840
- const { loadDevMigrations } = await import("../state-BCcvNN-M.js");
840
+ const { loadDevMigrations } = await import("../state-DCXcGtiu.js");
841
841
  const devMigrations = loadDevMigrations(this.projectRoot, db.name);
842
842
  for (const [name, chunks] of devMigrations) db.migrations.set(`_dev_${name}`, chunks);
843
843
  if (devMigrations.size > 0) debugVite("Loaded %d dev migration(s) for %s", devMigrations.size, db.name);
@@ -1,4 +1,4 @@
1
- import { n as decodeResponse, r as encodeRequest } from "./protocol-g_plhPOY.js";
1
+ import { n as decodeResponse, r as encodeRequest } from "./protocol-DEubbBbR.js";
2
2
 
3
3
  //#region src/transport/websocket.ts
4
4
  /** Default timeout (ms) after which a pending WebSocket request rejects. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "durable-db",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/eagerpatch/durable-db.git"
@@ -54,6 +54,10 @@
54
54
  "types": "./dist/cli/index.d.ts",
55
55
  "import": "./dist/cli/index.js"
56
56
  },
57
+ "./testing": {
58
+ "types": "./dist/testing/index.d.ts",
59
+ "import": "./dist/testing/index.js"
60
+ },
57
61
  "./virtual": {
58
62
  "types": "./virtual.d.ts"
59
63
  }
@@ -1,8 +0,0 @@
1
- import "./debug-BZJbKybf.js";
2
- import "./state-FOEjpgJ6.js";
3
- import "./snapshot-D170nL7i.js";
4
- import "./generator-BUPP9scJ.js";
5
- import "./parser-CbX54Flt.js";
6
- import { n as pushOne, t as push } from "./push-BgQ5cnfH.js";
7
-
8
- export { push };
File without changes
File without changes