@framers/sql-storage-adapter 0.7.1 → 0.7.3

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 (192) hide show
  1. package/README.md +43 -0
  2. package/dist/adapters/electron/preload.cjs +289 -0
  3. package/dist/adapters/electron/renderer.d.ts +9 -0
  4. package/dist/adapters/electron/renderer.d.ts.map +1 -0
  5. package/dist/adapters/electron/renderer.js +8 -0
  6. package/dist/adapters/electron/renderer.js.map +1 -0
  7. package/dist/cjs/adapters/__tests__/indexedDbAdapter.spec.d.ts +2 -0
  8. package/dist/cjs/adapters/__tests__/indexedDbAdapter.spec.js +133 -0
  9. package/dist/cjs/adapters/baseStorageAdapter.d.ts +368 -0
  10. package/dist/cjs/adapters/baseStorageAdapter.js +744 -0
  11. package/dist/cjs/adapters/betterSqliteAdapter.d.ts +124 -0
  12. package/dist/cjs/adapters/betterSqliteAdapter.js +334 -0
  13. package/dist/cjs/adapters/capacitorSqliteAdapter.d.ts +83 -0
  14. package/dist/cjs/adapters/capacitorSqliteAdapter.js +250 -0
  15. package/dist/cjs/adapters/electron/electronMainAdapter.d.ts +251 -0
  16. package/dist/cjs/adapters/electron/electronMainAdapter.js +392 -0
  17. package/dist/cjs/adapters/electron/electronRendererAdapter.d.ts +177 -0
  18. package/dist/cjs/adapters/electron/electronRendererAdapter.js +283 -0
  19. package/dist/cjs/adapters/electron/index.d.ts +74 -0
  20. package/dist/cjs/adapters/electron/index.js +86 -0
  21. package/dist/cjs/adapters/electron/ipc/channels.d.ts +196 -0
  22. package/dist/cjs/adapters/electron/ipc/channels.js +127 -0
  23. package/dist/cjs/adapters/electron/ipc/index.d.ts +11 -0
  24. package/dist/cjs/adapters/electron/ipc/index.js +38 -0
  25. package/dist/cjs/adapters/electron/ipc/protocol.d.ts +78 -0
  26. package/dist/cjs/adapters/electron/ipc/protocol.js +341 -0
  27. package/dist/cjs/adapters/electron/ipc/types.d.ts +248 -0
  28. package/dist/cjs/adapters/electron/ipc/types.js +15 -0
  29. package/dist/cjs/adapters/electron/migration/autoMigrator.d.ts +184 -0
  30. package/dist/cjs/adapters/electron/migration/autoMigrator.js +441 -0
  31. package/dist/cjs/adapters/electron/migration/index.d.ts +9 -0
  32. package/dist/cjs/adapters/electron/migration/index.js +29 -0
  33. package/dist/cjs/adapters/electron/preload.d.ts +126 -0
  34. package/dist/cjs/adapters/electron/preload.js +233 -0
  35. package/dist/cjs/adapters/electron/recovery/corruptionDetector.d.ts +214 -0
  36. package/dist/cjs/adapters/electron/recovery/corruptionDetector.js +359 -0
  37. package/dist/cjs/adapters/electron/recovery/index.d.ts +11 -0
  38. package/dist/cjs/adapters/electron/recovery/index.js +34 -0
  39. package/dist/cjs/adapters/electron/recovery/walCheckpoint.d.ts +186 -0
  40. package/dist/cjs/adapters/electron/recovery/walCheckpoint.js +263 -0
  41. package/dist/cjs/adapters/electron/renderer.d.ts +9 -0
  42. package/dist/cjs/adapters/electron/renderer.js +33 -0
  43. package/dist/cjs/adapters/electron/window/index.d.ts +9 -0
  44. package/dist/cjs/adapters/electron/window/index.js +29 -0
  45. package/dist/cjs/adapters/electron/window/windowManager.d.ts +190 -0
  46. package/dist/cjs/adapters/electron/window/windowManager.js +321 -0
  47. package/dist/cjs/adapters/indexedDbAdapter.d.ts +490 -0
  48. package/dist/cjs/adapters/indexedDbAdapter.js +993 -0
  49. package/dist/cjs/adapters/postgresAdapter.d.ts +424 -0
  50. package/dist/cjs/adapters/postgresAdapter.js +889 -0
  51. package/dist/cjs/adapters/sqlJsAdapter.d.ts +64 -0
  52. package/dist/cjs/adapters/sqlJsAdapter.js +268 -0
  53. package/dist/cjs/adapters/supabase.d.ts +58 -0
  54. package/dist/cjs/adapters/supabase.js +401 -0
  55. package/dist/cjs/codecs/BrowserBlobCodec.d.ts +14 -0
  56. package/dist/cjs/codecs/BrowserBlobCodec.js +57 -0
  57. package/dist/cjs/codecs/NodeBlobCodec.d.ts +14 -0
  58. package/dist/cjs/codecs/NodeBlobCodec.js +59 -0
  59. package/dist/cjs/core/contracts/blobCodec.d.ts +15 -0
  60. package/dist/cjs/core/contracts/blobCodec.js +15 -0
  61. package/dist/cjs/core/contracts/context.d.ts +221 -0
  62. package/dist/cjs/core/contracts/context.js +15 -0
  63. package/dist/cjs/core/contracts/dialect.d.ts +154 -0
  64. package/dist/cjs/core/contracts/dialect.js +15 -0
  65. package/dist/cjs/core/contracts/events.d.ts +225 -0
  66. package/dist/cjs/core/contracts/events.js +15 -0
  67. package/dist/cjs/core/contracts/exporter.d.ts +13 -0
  68. package/dist/cjs/core/contracts/exporter.js +15 -0
  69. package/dist/cjs/core/contracts/extensions.d.ts +73 -0
  70. package/dist/cjs/core/contracts/extensions.js +15 -0
  71. package/dist/cjs/core/contracts/features.d.ts +25 -0
  72. package/dist/cjs/core/contracts/features.js +61 -0
  73. package/dist/cjs/core/contracts/fts.d.ts +66 -0
  74. package/dist/cjs/core/contracts/fts.js +15 -0
  75. package/dist/cjs/core/contracts/hooks.d.ts +481 -0
  76. package/dist/cjs/core/contracts/hooks.js +140 -0
  77. package/dist/cjs/core/contracts/index.d.ts +260 -0
  78. package/dist/cjs/core/contracts/index.js +41 -0
  79. package/dist/cjs/core/contracts/limitations.d.ts +46 -0
  80. package/dist/cjs/core/contracts/limitations.js +218 -0
  81. package/dist/cjs/core/contracts/performance.d.ts +308 -0
  82. package/dist/cjs/core/contracts/performance.js +144 -0
  83. package/dist/cjs/core/database.d.ts +220 -0
  84. package/dist/cjs/core/database.js +136 -0
  85. package/dist/cjs/core/portable.d.ts +23 -0
  86. package/dist/cjs/core/portable.js +66 -0
  87. package/dist/cjs/core/resolver.d.ts +77 -0
  88. package/dist/cjs/core/resolver.js +235 -0
  89. package/dist/cjs/dialects/PostgresDialect.d.ts +35 -0
  90. package/dist/cjs/dialects/PostgresDialect.js +143 -0
  91. package/dist/cjs/dialects/SqliteDialect.d.ts +35 -0
  92. package/dist/cjs/dialects/SqliteDialect.js +142 -0
  93. package/dist/cjs/dialects/shared.d.ts +17 -0
  94. package/dist/cjs/dialects/shared.js +90 -0
  95. package/dist/cjs/exporters/PostgresExporter.d.ts +14 -0
  96. package/dist/cjs/exporters/PostgresExporter.js +61 -0
  97. package/dist/cjs/exporters/SqliteFileExporter.d.ts +16 -0
  98. package/dist/cjs/exporters/SqliteFileExporter.js +43 -0
  99. package/dist/cjs/features/backup/cloudBackup.d.ts +219 -0
  100. package/dist/cjs/features/backup/cloudBackup.js +209 -0
  101. package/dist/cjs/features/kv/idb.d.ts +79 -0
  102. package/dist/cjs/features/kv/idb.js +32 -0
  103. package/dist/cjs/features/kv/index.d.ts +14 -0
  104. package/dist/cjs/features/kv/index.js +40 -0
  105. package/dist/cjs/features/kv/indexedDbKeyValueStore.d.ts +77 -0
  106. package/dist/cjs/features/kv/indexedDbKeyValueStore.js +116 -0
  107. package/dist/cjs/features/kv/keyValueStore.d.ts +48 -0
  108. package/dist/cjs/features/kv/keyValueStore.js +67 -0
  109. package/dist/cjs/features/kv/sqlKeyValueStore.d.ts +34 -0
  110. package/dist/cjs/features/kv/sqlKeyValueStore.js +81 -0
  111. package/dist/cjs/features/kv/valueCodec.d.ts +35 -0
  112. package/dist/cjs/features/kv/valueCodec.js +426 -0
  113. package/dist/cjs/features/migrations/dataExport.d.ts +77 -0
  114. package/dist/cjs/features/migrations/dataExport.js +194 -0
  115. package/dist/cjs/features/migrations/dataImport.d.ts +54 -0
  116. package/dist/cjs/features/migrations/dataImport.js +300 -0
  117. package/dist/cjs/features/migrations/migration.d.ts +89 -0
  118. package/dist/cjs/features/migrations/migration.js +187 -0
  119. package/dist/cjs/features/migrations/schemaMigrations.d.ts +93 -0
  120. package/dist/cjs/features/migrations/schemaMigrations.js +220 -0
  121. package/dist/cjs/features/sync/conflicts/conflictResolver.d.ts +222 -0
  122. package/dist/cjs/features/sync/conflicts/conflictResolver.js +366 -0
  123. package/dist/cjs/features/sync/conflicts/index.d.ts +9 -0
  124. package/dist/cjs/features/sync/conflicts/index.js +21 -0
  125. package/dist/cjs/features/sync/crossPlatformSync.d.ts +281 -0
  126. package/dist/cjs/features/sync/crossPlatformSync.js +551 -0
  127. package/dist/cjs/features/sync/devices/deviceManager.d.ts +243 -0
  128. package/dist/cjs/features/sync/devices/deviceManager.js +450 -0
  129. package/dist/cjs/features/sync/devices/index.d.ts +10 -0
  130. package/dist/cjs/features/sync/devices/index.js +21 -0
  131. package/dist/cjs/features/sync/index.d.ts +37 -0
  132. package/dist/cjs/features/sync/index.js +111 -0
  133. package/dist/cjs/features/sync/protocol/index.d.ts +11 -0
  134. package/dist/cjs/features/sync/protocol/index.js +23 -0
  135. package/dist/cjs/features/sync/protocol/messages.d.ts +348 -0
  136. package/dist/cjs/features/sync/protocol/messages.js +215 -0
  137. package/dist/cjs/features/sync/protocol/vectorClock.d.ts +164 -0
  138. package/dist/cjs/features/sync/protocol/vectorClock.js +256 -0
  139. package/dist/cjs/features/sync/syncManager.d.ts +348 -0
  140. package/dist/cjs/features/sync/syncManager.js +455 -0
  141. package/dist/cjs/features/sync/tables/index.d.ts +10 -0
  142. package/dist/cjs/features/sync/tables/index.js +21 -0
  143. package/dist/cjs/features/sync/tables/syncLogManager.d.ts +216 -0
  144. package/dist/cjs/features/sync/tables/syncLogManager.js +423 -0
  145. package/dist/cjs/features/sync/transport/httpTransport.d.ts +123 -0
  146. package/dist/cjs/features/sync/transport/httpTransport.js +346 -0
  147. package/dist/cjs/features/sync/transport/index.d.ts +12 -0
  148. package/dist/cjs/features/sync/transport/index.js +25 -0
  149. package/dist/cjs/features/sync/transport/transport.d.ts +259 -0
  150. package/dist/cjs/features/sync/transport/transport.js +163 -0
  151. package/dist/cjs/features/sync/transport/websocketTransport.d.ts +126 -0
  152. package/dist/cjs/features/sync/transport/websocketTransport.js +342 -0
  153. package/dist/cjs/fts/PostgresFts.d.ts +29 -0
  154. package/dist/cjs/fts/PostgresFts.js +77 -0
  155. package/dist/cjs/fts/SqliteFts5.d.ts +22 -0
  156. package/dist/cjs/fts/SqliteFts5.js +58 -0
  157. package/dist/cjs/index.d.ts +30 -0
  158. package/dist/cjs/index.js +101 -0
  159. package/dist/cjs/package.json +3 -0
  160. package/dist/cjs/shared/connectionGate.d.ts +94 -0
  161. package/dist/cjs/shared/connectionGate.js +77 -0
  162. package/dist/cjs/shared/errors.d.ts +23 -0
  163. package/dist/cjs/shared/errors.js +45 -0
  164. package/dist/cjs/shared/logger.d.ts +26 -0
  165. package/dist/cjs/shared/logger.js +40 -0
  166. package/dist/cjs/shared/parameterUtils.d.ts +16 -0
  167. package/dist/cjs/shared/parameterUtils.js +59 -0
  168. package/dist/cjs/shared/postgresTarget.d.ts +11 -0
  169. package/dist/cjs/shared/postgresTarget.js +30 -0
  170. package/dist/cjs/shared/segment.d.ts +43 -0
  171. package/dist/cjs/shared/segment.js +63 -0
  172. package/dist/cjs/shared/serializedTransaction.d.ts +14 -0
  173. package/dist/cjs/shared/serializedTransaction.js +74 -0
  174. package/dist/cjs/shared/splitSqlStatements.d.ts +27 -0
  175. package/dist/cjs/shared/splitSqlStatements.js +163 -0
  176. package/dist/cjs/shared/sqlPlaceholders.d.ts +48 -0
  177. package/dist/cjs/shared/sqlPlaceholders.js +263 -0
  178. package/dist/cjs/types/index.d.ts +29 -0
  179. package/dist/cjs/types/index.js +33 -0
  180. package/dist/core/portable.d.ts +23 -0
  181. package/dist/core/portable.d.ts.map +1 -0
  182. package/dist/core/portable.js +23 -0
  183. package/dist/core/portable.js.map +1 -0
  184. package/dist/features/migrations/schemaMigrations.d.ts +93 -0
  185. package/dist/features/migrations/schemaMigrations.d.ts.map +1 -0
  186. package/dist/features/migrations/schemaMigrations.js +216 -0
  187. package/dist/features/migrations/schemaMigrations.js.map +1 -0
  188. package/dist/index.d.ts +1 -0
  189. package/dist/index.d.ts.map +1 -1
  190. package/dist/index.js +1 -0
  191. package/dist/index.js.map +1 -1
  192. package/package.json +139 -24
@@ -0,0 +1,64 @@
1
+ import type { SqlJsConfig } from 'sql.js';
2
+ import type { StorageAdapter, StorageCapability, StorageOpenOptions, StorageParameters, StorageRunResult } from '../core/contracts/index.js';
3
+ /** sql.js's own configuration (`locateFile` and the rest), and the adapter's segment. */
4
+ export type SqlJsAdapterOptions = SqlJsConfig & {
5
+ /**
6
+ * A database file of its own: the segment goes into the name of the file
7
+ * `open({ filePath })` names, before its extension (`app.sqlite3` becomes
8
+ * `app-agentos.sqlite3`). An in-memory database is its own already.
9
+ */
10
+ segment?: string;
11
+ };
12
+ /**
13
+ * Storage adapter backed by sql.js (WebAssembly) for environments without native SQLite bindings.
14
+ */
15
+ export declare class SqlJsAdapter implements StorageAdapter {
16
+ readonly kind = "sqljs";
17
+ readonly capabilities: ReadonlySet<StorageCapability>;
18
+ private SQL;
19
+ private db;
20
+ private filePath?;
21
+ /** Foreign-key enforcement as open() was asked for it (undefined: the engine default); sql.js forgets the PRAGMA when export() reopens the connection. */
22
+ private foreignKeys;
23
+ private readonly runSerializedTransaction;
24
+ /**
25
+ * Open transaction depth. > 0 means a SQLite-level transaction is in
26
+ * flight on this connection and {@link persistIfNeeded} MUST be
27
+ * skipped until the outermost commit / rollback returns.
28
+ *
29
+ * Why this exists: sql.js's `db.export()` (called by persistIfNeeded
30
+ * to flush state to disk) ends any open transaction as a side
31
+ * effect. Persisting mid-transaction therefore SILENTLY closes the
32
+ * tx, making subsequent COMMIT / ROLLBACK throw "no transaction is
33
+ * active" and — worse — leaking partial writes onto disk that the
34
+ * matching rollback can no longer undo. Counter is bumped on
35
+ * BEGIN/SAVEPOINT and decremented on COMMIT/RELEASE/ROLLBACK
36
+ * regardless of whether the statement is issued via {@link run}
37
+ * (caller-managed transactions) or {@link transaction} (helper-
38
+ * managed). One source of truth for the in-tx flag.
39
+ */
40
+ private transactionDepth;
41
+ private readonly sqlJsConfig;
42
+ private readonly segment;
43
+ constructor(adapterOptions?: SqlJsAdapterOptions);
44
+ open(options?: StorageOpenOptions): Promise<void>;
45
+ run(statement: string, parameters?: StorageParameters): Promise<StorageRunResult>;
46
+ get<T>(statement: string, parameters?: StorageParameters): Promise<T | null>;
47
+ all<T>(statement: string, parameters?: StorageParameters): Promise<T[]>;
48
+ exec(script: string): Promise<void>;
49
+ transaction<T>(fn: (trx: StorageAdapter) => Promise<T>): Promise<T>;
50
+ close(): Promise<void>;
51
+ private prepareInternal;
52
+ private ensureOpen;
53
+ /** Applies the connection settings open() was asked for: sql.js loses them whenever export() reopens the connection. */
54
+ private applyConnectionSettings;
55
+ private persistIfNeeded;
56
+ }
57
+ /**
58
+ * Creates a sql.js adapter. `open({ filePath })` loads the database from that
59
+ * file and saves it there; with `segment`, from a file of the segment's own.
60
+ *
61
+ * @throws {Error} When `segment` is not a name a segment can take.
62
+ */
63
+ export declare const createSqlJsAdapter: (options?: SqlJsAdapterOptions) => StorageAdapter;
64
+ //# sourceMappingURL=sqlJsAdapter.d.ts.map
@@ -0,0 +1,268 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var sqlJsAdapter_exports = {};
29
+ __export(sqlJsAdapter_exports, {
30
+ SqlJsAdapter: () => SqlJsAdapter,
31
+ createSqlJsAdapter: () => createSqlJsAdapter
32
+ });
33
+ module.exports = __toCommonJS(sqlJsAdapter_exports);
34
+ var import_sql = __toESM(require("sql.js"));
35
+ var import_fs = __toESM(require("fs"));
36
+ var import_path = __toESM(require("path"));
37
+ var import_parameterUtils = require("../shared/parameterUtils.js");
38
+ var import_sqlPlaceholders = require("../shared/sqlPlaceholders.js");
39
+ var import_serializedTransaction = require("../shared/serializedTransaction.js");
40
+ var import_segment = require("../shared/segment.js");
41
+ const normaliseRowId = (value) => {
42
+ if (typeof value === "number" || typeof value === "string") {
43
+ return value;
44
+ }
45
+ if (typeof value === "bigint") {
46
+ return value.toString();
47
+ }
48
+ return null;
49
+ };
50
+ const hasFsAccess = () => {
51
+ try {
52
+ return typeof import_fs.default.accessSync === "function";
53
+ } catch {
54
+ return false;
55
+ }
56
+ };
57
+ class SqlJsAdapter {
58
+ constructor(adapterOptions = {}) {
59
+ this.kind = "sqljs";
60
+ this.SQL = null;
61
+ this.db = null;
62
+ this.runSerializedTransaction = (0, import_serializedTransaction.createSerializedTransactionRunner)();
63
+ this.transactionDepth = 0;
64
+ const { segment, ...sqlJsConfig } = adapterOptions;
65
+ this.segment = segment === void 0 ? void 0 : (0, import_segment.validateSegment)(segment);
66
+ this.sqlJsConfig = sqlJsConfig;
67
+ const caps = ["transactions", "json", "prepared"];
68
+ if (hasFsAccess()) {
69
+ caps.push("persistence");
70
+ }
71
+ this.capabilities = new Set(caps);
72
+ }
73
+ async open(options) {
74
+ if (this.db) {
75
+ return;
76
+ }
77
+ this.SQL = await (0, import_sql.default)(this.sqlJsConfig);
78
+ const requested = options?.filePath;
79
+ this.filePath = requested === void 0 || (0, import_segment.namesInMemoryDatabase)(requested) ? void 0 : (0, import_segment.segmentFilePath)(requested, this.segment);
80
+ this.foreignKeys = options?.foreignKeys;
81
+ if (this.filePath && hasFsAccess() && import_fs.default.existsSync(this.filePath)) {
82
+ const buffer = import_fs.default.readFileSync(this.filePath);
83
+ this.db = new this.SQL.Database(buffer);
84
+ } else {
85
+ this.db = new this.SQL.Database();
86
+ }
87
+ this.applyConnectionSettings();
88
+ }
89
+ async run(statement, parameters) {
90
+ ({ statement, parameters } = (0, import_sqlPlaceholders.toSqliteStatement)(statement, parameters));
91
+ const txKind = classifyTransactionControl(statement);
92
+ if (txKind === "begin") {
93
+ this.transactionDepth += 1;
94
+ }
95
+ const stmt = this.prepareInternal(statement);
96
+ try {
97
+ const { named, positional } = (0, import_parameterUtils.normaliseParameters)(parameters);
98
+ if (named) {
99
+ stmt.bind((0, import_parameterUtils.expandNamedParameters)(named));
100
+ } else if (positional) {
101
+ stmt.bind(positional);
102
+ }
103
+ stmt.step();
104
+ const rowIdResult = this.db.exec("SELECT last_insert_rowid() AS id");
105
+ const rawRowId = rowIdResult[0]?.values?.[0]?.[0];
106
+ return {
107
+ changes: this.db.getRowsModified(),
108
+ lastInsertRowid: normaliseRowId(rawRowId)
109
+ };
110
+ } catch (err) {
111
+ if (txKind === "begin") {
112
+ this.transactionDepth = Math.max(0, this.transactionDepth - 1);
113
+ }
114
+ throw err;
115
+ } finally {
116
+ stmt.free();
117
+ if (txKind === "end" && this.transactionDepth > 0) {
118
+ this.transactionDepth -= 1;
119
+ }
120
+ if (this.transactionDepth === 0) {
121
+ await this.persistIfNeeded();
122
+ }
123
+ }
124
+ }
125
+ async get(statement, parameters) {
126
+ const rows = await this.all(statement, parameters);
127
+ return rows.length > 0 ? rows[0] : null;
128
+ }
129
+ async all(statement, parameters) {
130
+ ({ statement, parameters } = (0, import_sqlPlaceholders.toSqliteStatement)(statement, parameters));
131
+ const stmt = this.prepareInternal(statement);
132
+ try {
133
+ const { named, positional } = (0, import_parameterUtils.normaliseParameters)(parameters);
134
+ if (named) {
135
+ stmt.bind((0, import_parameterUtils.expandNamedParameters)(named));
136
+ } else if (positional) {
137
+ stmt.bind(positional);
138
+ }
139
+ const results = [];
140
+ while (stmt.step()) {
141
+ const row = {};
142
+ stmt.getColumnNames().forEach((column, index) => {
143
+ row[column] = stmt.get()[index];
144
+ });
145
+ results.push(row);
146
+ }
147
+ return results;
148
+ } finally {
149
+ stmt.free();
150
+ }
151
+ }
152
+ async exec(script) {
153
+ this.ensureOpen();
154
+ const isControl = isTransactionControlStatement(script);
155
+ const txKind = isControl ? classifyTransactionControl(script) : "none";
156
+ if (txKind === "begin") {
157
+ this.transactionDepth += 1;
158
+ }
159
+ try {
160
+ this.db.exec(script);
161
+ } catch (err) {
162
+ if (txKind === "begin") {
163
+ this.transactionDepth = Math.max(0, this.transactionDepth - 1);
164
+ }
165
+ throw err;
166
+ }
167
+ if (txKind === "end" && this.transactionDepth > 0) {
168
+ this.transactionDepth -= 1;
169
+ }
170
+ if (!isControl && this.transactionDepth === 0) {
171
+ await this.persistIfNeeded();
172
+ } else if (txKind === "end" && this.transactionDepth === 0) {
173
+ await this.persistIfNeeded();
174
+ }
175
+ }
176
+ async transaction(fn) {
177
+ this.ensureOpen();
178
+ return this.runSerializedTransaction(async () => {
179
+ this.ensureOpen();
180
+ this.db.run("BEGIN TRANSACTION;");
181
+ this.transactionDepth += 1;
182
+ const transactionAdapter = (0, import_serializedTransaction.createSharedConnectionTransactionAdapter)(this);
183
+ let committed = false;
184
+ try {
185
+ const result = await fn(transactionAdapter);
186
+ this.db.run("COMMIT;");
187
+ committed = true;
188
+ return result;
189
+ } catch (error) {
190
+ if (!committed) {
191
+ try {
192
+ this.db.run("ROLLBACK;");
193
+ } catch (rollbackError) {
194
+ const message = rollbackError instanceof Error ? rollbackError.message : String(rollbackError);
195
+ if (!message.includes("no transaction is active")) {
196
+ throw rollbackError;
197
+ }
198
+ }
199
+ }
200
+ throw error;
201
+ } finally {
202
+ this.transactionDepth = Math.max(0, this.transactionDepth - 1);
203
+ if (this.transactionDepth === 0) {
204
+ await this.persistIfNeeded();
205
+ }
206
+ }
207
+ });
208
+ }
209
+ async close() {
210
+ if (this.db) {
211
+ await this.persistIfNeeded();
212
+ this.db.close();
213
+ this.db = null;
214
+ }
215
+ }
216
+ prepareInternal(statement) {
217
+ this.ensureOpen();
218
+ return this.db.prepare(statement);
219
+ }
220
+ ensureOpen() {
221
+ if (!this.db) {
222
+ throw new Error("Storage adapter not opened. Call open() first.");
223
+ }
224
+ }
225
+ /** Applies the connection settings open() was asked for: sql.js loses them whenever export() reopens the connection. */
226
+ applyConnectionSettings() {
227
+ if (this.foreignKeys !== void 0) {
228
+ this.db.run(`PRAGMA foreign_keys = ${this.foreignKeys ? "ON" : "OFF"};`);
229
+ }
230
+ }
231
+ async persistIfNeeded() {
232
+ if (!this.filePath || !hasFsAccess()) {
233
+ return;
234
+ }
235
+ const dir = import_path.default.dirname(this.filePath);
236
+ if (!import_fs.default.existsSync(dir)) {
237
+ import_fs.default.mkdirSync(dir, { recursive: true });
238
+ }
239
+ const data = this.db.export();
240
+ this.applyConnectionSettings();
241
+ import_fs.default.writeFileSync(this.filePath, Buffer.from(data));
242
+ }
243
+ }
244
+ const createSqlJsAdapter = (options) => new SqlJsAdapter(options);
245
+ function isTransactionControlStatement(script) {
246
+ return classifyTransactionControl(script) !== "none";
247
+ }
248
+ function classifyTransactionControl(script) {
249
+ const trimmed = script.trim().replace(/;\s*$/, "").toUpperCase();
250
+ if (trimmed === "BEGIN")
251
+ return "begin";
252
+ if (trimmed === "COMMIT" || trimmed === "ROLLBACK")
253
+ return "end";
254
+ if (/^BEGIN\s+(DEFERRED|IMMEDIATE|EXCLUSIVE|TRANSACTION)$/.test(trimmed))
255
+ return "begin";
256
+ if (/^SAVEPOINT\s+\S+$/.test(trimmed))
257
+ return "begin";
258
+ if (/^RELEASE(\s+SAVEPOINT)?\s+\S+$/.test(trimmed))
259
+ return "end";
260
+ if (/^ROLLBACK(\s+TO(\s+SAVEPOINT)?)?\s+\S+$/.test(trimmed))
261
+ return "end";
262
+ return "none";
263
+ }
264
+ // Annotate the CommonJS export names for ESM import in node:
265
+ 0 && (module.exports = {
266
+ SqlJsAdapter,
267
+ createSqlJsAdapter
268
+ });
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Supabase Postgres Storage Adapter
3
+ * Provides full Postgres capabilities with Supabase's managed infrastructure
4
+ */
5
+ import type { StorageAdapter, StorageCapability, StorageOpenOptions, StorageParameters, StorageRunResult, BatchResult } from '../core/contracts/index.js';
6
+ import type { StorageAdapterExtensions, ExtendedBatchOperation, Migration, PerformanceMetrics, StreamOptions } from '../core/contracts/extensions.js';
7
+ export interface SupabaseAdapterOptions extends StorageOpenOptions {
8
+ connectionString?: string;
9
+ poolConfig?: {
10
+ max?: number;
11
+ min?: number;
12
+ idleTimeoutMillis?: number;
13
+ connectionTimeoutMillis?: number;
14
+ };
15
+ ssl?: boolean | {
16
+ rejectUnauthorized: boolean;
17
+ };
18
+ }
19
+ export declare class SupabaseAdapter implements StorageAdapter, StorageAdapterExtensions {
20
+ readonly kind = "supabase-postgres";
21
+ readonly capabilities: ReadonlySet<StorageCapability>;
22
+ private pool?;
23
+ private options?;
24
+ private metrics;
25
+ private profilingEnabled;
26
+ private queryCache;
27
+ private cacheEnabled;
28
+ private maxCacheSize;
29
+ open(options?: SupabaseAdapterOptions): Promise<void>;
30
+ close(): Promise<void>;
31
+ private trackQuery;
32
+ private getCacheKey;
33
+ run(statement: string, parameters?: StorageParameters): Promise<StorageRunResult>;
34
+ get<T = unknown>(statement: string, parameters?: StorageParameters): Promise<T | null>;
35
+ all<T = unknown>(statement: string, parameters?: StorageParameters): Promise<T[]>;
36
+ exec(script: string): Promise<void>;
37
+ transaction<T>(fn: (trx: StorageAdapter) => Promise<T>): Promise<T>;
38
+ stream<T = unknown>(query: string, parameters?: unknown, options?: StreamOptions): AsyncIterableIterator<T>;
39
+ batchWrite(operations: ExtendedBatchOperation[]): Promise<BatchResult>;
40
+ migrate(migrations: Migration[]): Promise<void>;
41
+ getMigrationStatus(): Promise<Migration[]>;
42
+ backup(_destination: string): Promise<void>;
43
+ restore(_source: string): Promise<void>;
44
+ enableProfiling(): void;
45
+ disableProfiling(): void;
46
+ getMetrics(): PerformanceMetrics;
47
+ clearMetrics(): void;
48
+ enableCache(maxSize?: number): void;
49
+ disableCache(): void;
50
+ clearCache(): void;
51
+ getPoolStatus(): {
52
+ active: number;
53
+ idle: number;
54
+ waiting: number;
55
+ max: number;
56
+ };
57
+ }
58
+ //# sourceMappingURL=supabase.d.ts.map