@typicalday/firegraph 0.7.0 → 0.8.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.
@@ -0,0 +1,78 @@
1
+ import {
2
+ buildSchemaStatements,
3
+ createSqliteBackend,
4
+ validateTableName
5
+ } from "./chunk-WOAJRVHD.js";
6
+ import {
7
+ createGraphClientFromBackend
8
+ } from "./chunk-YUXOALMR.js";
9
+
10
+ // src/do-sqlite.ts
11
+ var DOSqliteExecutor = class {
12
+ constructor(storage) {
13
+ this.storage = storage;
14
+ }
15
+ async all(sql, params) {
16
+ return this.storage.sql.exec(sql, ...params).toArray();
17
+ }
18
+ async run(sql, params) {
19
+ this.storage.sql.exec(sql, ...params).toArray();
20
+ }
21
+ async batch(statements) {
22
+ if (statements.length === 0) return;
23
+ this.storage.transactionSync(() => {
24
+ for (const s of statements) {
25
+ this.storage.sql.exec(s.sql, ...s.params).toArray();
26
+ }
27
+ });
28
+ }
29
+ async transaction(fn) {
30
+ this.storage.sql.exec("BEGIN IMMEDIATE").toArray();
31
+ try {
32
+ const txExec = {
33
+ all: async (sql, params) => this.storage.sql.exec(sql, ...params).toArray(),
34
+ run: async (sql, params) => {
35
+ this.storage.sql.exec(sql, ...params).toArray();
36
+ }
37
+ };
38
+ const result = await fn(txExec);
39
+ this.storage.sql.exec("COMMIT").toArray();
40
+ return result;
41
+ } catch (err) {
42
+ this.storage.sql.exec("ROLLBACK").toArray();
43
+ throw err;
44
+ }
45
+ }
46
+ };
47
+ function ensureSchema(storage, table) {
48
+ const statements = buildSchemaStatements(table);
49
+ for (const sql of statements) {
50
+ storage.sql.exec(sql).toArray();
51
+ }
52
+ }
53
+ function createDOSqliteGraphClient(storage, options = {}) {
54
+ const table = options.table ?? "firegraph";
55
+ validateTableName(table);
56
+ if (options.autoMigrate !== false) {
57
+ ensureSchema(storage, table);
58
+ }
59
+ const executor = new DOSqliteExecutor(storage);
60
+ const backend = createSqliteBackend(executor, table);
61
+ const { table: _t, autoMigrate: _m, ...clientOptions } = options;
62
+ void _t;
63
+ void _m;
64
+ let metaBackend;
65
+ if (clientOptions.registryMode && typeof clientOptions.registryMode === "object" && clientOptions.registryMode.collection && clientOptions.registryMode.collection !== table) {
66
+ const metaTable = clientOptions.registryMode.collection;
67
+ validateTableName(metaTable);
68
+ if (options.autoMigrate !== false) {
69
+ ensureSchema(storage, metaTable);
70
+ }
71
+ metaBackend = createSqliteBackend(executor, metaTable);
72
+ }
73
+ return createGraphClientFromBackend(backend, clientOptions, metaBackend);
74
+ }
75
+ export {
76
+ createDOSqliteGraphClient
77
+ };
78
+ //# sourceMappingURL=do-sqlite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/do-sqlite.ts"],"sourcesContent":["/**\n * Cloudflare Durable Object SQLite driver for firegraph.\n *\n * DO SQLite (`ctx.storage.sql`) is a synchronous API exposed inside the\n * Durable Object's single-threaded execution context. firegraph's storage\n * interface is async, so this driver wraps every call in a resolved\n * `Promise`. Interactive transactions go through `ctx.storage.transactionSync`,\n * which fully supports read-then-conditional-write — the migration write-back\n * path inside `runTransaction()` works as it does on Firestore.\n */\n\nimport { createGraphClientFromBackend } from './client.js';\nimport { createSqliteBackend } from './internal/sqlite-backend.js';\nimport type { SqliteExecutor, SqliteTxExecutor } from './internal/sqlite-executor.js';\nimport { buildSchemaStatements, validateTableName } from './internal/sqlite-schema.js';\nimport type {\n DynamicGraphClient,\n DynamicRegistryConfig,\n GraphClient,\n GraphClientOptions,\n} from './types.js';\n\n/**\n * Subset of the Durable Object SQL storage interface that firegraph depends\n * on. Typed against `ctx.storage` from `@cloudflare/workers-types` without\n * importing it.\n */\nexport interface DOSqlStorage {\n sql: DOSqlExecutor;\n transactionSync<T>(fn: () => T): T;\n}\n\nexport interface DOSqlExecutor {\n exec<T = Record<string, unknown>>(sql: string, ...params: unknown[]): DOSqlCursor<T>;\n}\n\nexport interface DOSqlCursor<T> {\n toArray(): T[];\n}\n\nexport interface DOSqliteClientOptions extends GraphClientOptions {\n /** Table name for firegraph triples (default: `firegraph`). */\n table?: string;\n /** Run schema DDL on first use. Default: `true`. */\n autoMigrate?: boolean;\n}\n\nclass DOSqliteExecutor implements SqliteExecutor {\n constructor(private readonly storage: DOSqlStorage) {}\n\n async all(sql: string, params: unknown[]): Promise<Record<string, unknown>[]> {\n return this.storage.sql.exec<Record<string, unknown>>(sql, ...params).toArray();\n }\n\n async run(sql: string, params: unknown[]): Promise<void> {\n this.storage.sql.exec(sql, ...params).toArray();\n }\n\n async batch(statements: ReadonlyArray<{ sql: string; params: unknown[] }>): Promise<void> {\n if (statements.length === 0) return;\n this.storage.transactionSync(() => {\n for (const s of statements) {\n this.storage.sql.exec(s.sql, ...s.params).toArray();\n }\n });\n }\n\n async transaction<T>(fn: (tx: SqliteTxExecutor) => Promise<T>): Promise<T> {\n // We can't use `transactionSync` here: it requires a synchronous\n // callback, but `fn` is async and may reject only after the sync body\n // has already returned (and the transaction has already committed).\n // Manual `BEGIN IMMEDIATE`/`COMMIT`/`ROLLBACK` lets us await `fn` and\n // still roll back on rejection. `BEGIN IMMEDIATE` acquires the RESERVED\n // lock up front so a concurrent writer can't sneak in between our reads\n // and writes (avoids SQLITE_BUSY mid-transaction).\n this.storage.sql.exec('BEGIN IMMEDIATE').toArray();\n try {\n const txExec: SqliteTxExecutor = {\n all: async (sql: string, params: unknown[]) =>\n this.storage.sql.exec<Record<string, unknown>>(sql, ...params).toArray(),\n run: async (sql: string, params: unknown[]) => {\n this.storage.sql.exec(sql, ...params).toArray();\n },\n };\n const result = await fn(txExec);\n this.storage.sql.exec('COMMIT').toArray();\n return result;\n } catch (err) {\n this.storage.sql.exec('ROLLBACK').toArray();\n throw err;\n }\n }\n}\n\nfunction ensureSchema(storage: DOSqlStorage, table: string): void {\n const statements = buildSchemaStatements(table);\n for (const sql of statements) {\n storage.sql.exec(sql).toArray();\n }\n}\n\nexport function createDOSqliteGraphClient(\n storage: DOSqlStorage,\n options: DOSqliteClientOptions & { registryMode: DynamicRegistryConfig },\n): DynamicGraphClient;\nexport function createDOSqliteGraphClient(\n storage: DOSqlStorage,\n options?: DOSqliteClientOptions,\n): GraphClient;\nexport function createDOSqliteGraphClient(\n storage: DOSqlStorage,\n options: DOSqliteClientOptions = {},\n): GraphClient | DynamicGraphClient {\n const table = options.table ?? 'firegraph';\n validateTableName(table);\n if (options.autoMigrate !== false) {\n ensureSchema(storage, table);\n }\n\n const executor = new DOSqliteExecutor(storage);\n const backend = createSqliteBackend(executor, table);\n\n const { table: _t, autoMigrate: _m, ...clientOptions } = options;\n void _t;\n void _m;\n\n let metaBackend;\n if (\n clientOptions.registryMode &&\n typeof clientOptions.registryMode === 'object' &&\n clientOptions.registryMode.collection &&\n clientOptions.registryMode.collection !== table\n ) {\n const metaTable = clientOptions.registryMode.collection;\n validateTableName(metaTable);\n if (options.autoMigrate !== false) {\n ensureSchema(storage, metaTable);\n }\n metaBackend = createSqliteBackend(executor, metaTable);\n }\n\n return createGraphClientFromBackend(backend, clientOptions, metaBackend);\n}\n"],"mappings":";;;;;;;;;;AA+CA,IAAM,mBAAN,MAAiD;AAAA,EAC/C,YAA6B,SAAuB;AAAvB;AAAA,EAAwB;AAAA,EAErD,MAAM,IAAI,KAAa,QAAuD;AAC5E,WAAO,KAAK,QAAQ,IAAI,KAA8B,KAAK,GAAG,MAAM,EAAE,QAAQ;AAAA,EAChF;AAAA,EAEA,MAAM,IAAI,KAAa,QAAkC;AACvD,SAAK,QAAQ,IAAI,KAAK,KAAK,GAAG,MAAM,EAAE,QAAQ;AAAA,EAChD;AAAA,EAEA,MAAM,MAAM,YAA8E;AACxF,QAAI,WAAW,WAAW,EAAG;AAC7B,SAAK,QAAQ,gBAAgB,MAAM;AACjC,iBAAW,KAAK,YAAY;AAC1B,aAAK,QAAQ,IAAI,KAAK,EAAE,KAAK,GAAG,EAAE,MAAM,EAAE,QAAQ;AAAA,MACpD;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,YAA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