@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,401 @@
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 supabase_exports = {};
29
+ __export(supabase_exports, {
30
+ SupabaseAdapter: () => SupabaseAdapter
31
+ });
32
+ module.exports = __toCommonJS(supabase_exports);
33
+ class SupabaseAdapter {
34
+ constructor() {
35
+ this.kind = "supabase-postgres";
36
+ this.capabilities = /* @__PURE__ */ new Set([
37
+ "transactions",
38
+ "persistence",
39
+ "locks",
40
+ "streaming",
41
+ "batch"
42
+ ]);
43
+ this.metrics = {
44
+ totalQueries: 0,
45
+ totalDuration: 0,
46
+ averageDuration: 0,
47
+ slowQueries: [],
48
+ cacheHits: 0,
49
+ cacheMisses: 0
50
+ };
51
+ this.profilingEnabled = false;
52
+ this.queryCache = /* @__PURE__ */ new Map();
53
+ this.cacheEnabled = false;
54
+ this.maxCacheSize = 100;
55
+ }
56
+ async open(options) {
57
+ this.options = options;
58
+ const pg = await Promise.resolve().then(() => __toESM(require("pg")));
59
+ const { Pool } = pg;
60
+ const getEnv = (key) => {
61
+ if (typeof process !== "undefined" && process.env) {
62
+ return process.env[key];
63
+ }
64
+ return void 0;
65
+ };
66
+ const connectionString = options?.connectionString || getEnv("DATABASE_URL");
67
+ if (!connectionString) {
68
+ throw new Error("Supabase connection string required (DATABASE_URL or connectionString option)");
69
+ }
70
+ this.pool = new Pool({
71
+ connectionString,
72
+ max: options?.poolConfig?.max || 20,
73
+ min: options?.poolConfig?.min || 2,
74
+ idleTimeoutMillis: options?.poolConfig?.idleTimeoutMillis || 3e4,
75
+ connectionTimeoutMillis: options?.poolConfig?.connectionTimeoutMillis || 5e3,
76
+ ssl: options?.ssl !== false ? { rejectUnauthorized: false } : void 0
77
+ });
78
+ try {
79
+ const client = await this.pool.connect();
80
+ client.release();
81
+ } catch (error) {
82
+ throw new Error(`Failed to connect to Supabase: ${error}`);
83
+ }
84
+ }
85
+ async close() {
86
+ if (this.pool) {
87
+ await this.pool.end();
88
+ this.pool = void 0;
89
+ }
90
+ this.clearCache();
91
+ }
92
+ trackQuery(query, startTime) {
93
+ if (!this.profilingEnabled)
94
+ return;
95
+ const duration = Date.now() - startTime;
96
+ this.metrics.totalQueries++;
97
+ this.metrics.totalDuration += duration;
98
+ this.metrics.averageDuration = this.metrics.totalDuration / this.metrics.totalQueries;
99
+ if (duration > 100) {
100
+ if (!this.metrics.slowQueries) {
101
+ this.metrics.slowQueries = [];
102
+ }
103
+ this.metrics.slowQueries.push({
104
+ query: query.substring(0, 200),
105
+ duration,
106
+ timestamp: new Date(Date.now())
107
+ });
108
+ if (this.metrics.slowQueries.length > 100) {
109
+ this.metrics.slowQueries.shift();
110
+ }
111
+ }
112
+ }
113
+ getCacheKey(query, params) {
114
+ return `${query}::${JSON.stringify(params || {})}`;
115
+ }
116
+ async run(statement, parameters) {
117
+ if (!this.pool)
118
+ throw new Error("Storage adapter not opened");
119
+ const startTime = Date.now();
120
+ try {
121
+ const result = await this.pool.query(statement, Array.isArray(parameters) ? parameters : void 0);
122
+ this.trackQuery(statement, startTime);
123
+ return {
124
+ changes: result.rowCount || 0,
125
+ lastInsertRowid: result.rows[0]?.id || 0
126
+ };
127
+ } catch (error) {
128
+ throw new Error(`Query failed: ${error}`);
129
+ }
130
+ }
131
+ async get(statement, parameters) {
132
+ if (!this.pool)
133
+ throw new Error("Storage adapter not opened");
134
+ if (this.cacheEnabled) {
135
+ const cacheKey = this.getCacheKey(statement, parameters);
136
+ if (this.queryCache.has(cacheKey)) {
137
+ if (this.metrics.cacheHits !== void 0) {
138
+ this.metrics.cacheHits++;
139
+ }
140
+ const cached = this.queryCache.get(cacheKey);
141
+ return cached ?? null;
142
+ }
143
+ if (this.metrics.cacheMisses !== void 0) {
144
+ this.metrics.cacheMisses++;
145
+ }
146
+ }
147
+ const startTime = Date.now();
148
+ try {
149
+ const result = await this.pool.query(statement, Array.isArray(parameters) ? parameters : void 0);
150
+ this.trackQuery(statement, startTime);
151
+ const value = result.rows[0] || null;
152
+ if (this.cacheEnabled && value !== null) {
153
+ const cacheKey = this.getCacheKey(statement, parameters);
154
+ this.queryCache.set(cacheKey, value);
155
+ if (this.queryCache.size > this.maxCacheSize) {
156
+ const firstKey = this.queryCache.keys().next().value;
157
+ if (firstKey !== void 0) {
158
+ this.queryCache.delete(firstKey);
159
+ }
160
+ }
161
+ }
162
+ return value;
163
+ } catch (error) {
164
+ throw new Error(`Query failed: ${error}`);
165
+ }
166
+ }
167
+ async all(statement, parameters) {
168
+ if (!this.pool)
169
+ throw new Error("Storage adapter not opened");
170
+ if (this.cacheEnabled) {
171
+ const cacheKey = this.getCacheKey(statement, parameters);
172
+ if (this.queryCache.has(cacheKey)) {
173
+ if (this.metrics.cacheHits !== void 0) {
174
+ this.metrics.cacheHits++;
175
+ }
176
+ const cached = this.queryCache.get(cacheKey);
177
+ if (cached) {
178
+ return cached;
179
+ }
180
+ }
181
+ if (this.metrics.cacheMisses !== void 0) {
182
+ this.metrics.cacheMisses++;
183
+ }
184
+ }
185
+ const startTime = Date.now();
186
+ try {
187
+ const result = await this.pool.query(statement, Array.isArray(parameters) ? parameters : void 0);
188
+ this.trackQuery(statement, startTime);
189
+ const rows = result.rows;
190
+ if (this.cacheEnabled) {
191
+ const cacheKey = this.getCacheKey(statement, parameters);
192
+ this.queryCache.set(cacheKey, rows);
193
+ if (this.queryCache.size > this.maxCacheSize) {
194
+ const firstKey = this.queryCache.keys().next().value;
195
+ if (firstKey !== void 0) {
196
+ this.queryCache.delete(firstKey);
197
+ }
198
+ }
199
+ }
200
+ return rows;
201
+ } catch (error) {
202
+ throw new Error(`Query failed: ${error}`);
203
+ }
204
+ }
205
+ async exec(script) {
206
+ if (!this.pool)
207
+ throw new Error("Storage adapter not opened");
208
+ const startTime = Date.now();
209
+ try {
210
+ await this.pool.query(script);
211
+ this.trackQuery(script, startTime);
212
+ } catch (error) {
213
+ throw new Error(`Script execution failed: ${error}`);
214
+ }
215
+ }
216
+ async transaction(fn) {
217
+ if (!this.pool)
218
+ throw new Error("Storage adapter not opened");
219
+ const client = await this.pool.connect();
220
+ try {
221
+ await client.query("BEGIN");
222
+ const trxAdapter = {
223
+ kind: this.kind,
224
+ capabilities: this.capabilities,
225
+ open: async () => {
226
+ },
227
+ close: async () => {
228
+ },
229
+ run: async (stmt, params) => {
230
+ const result2 = await client.query(stmt, Array.isArray(params) ? params : void 0);
231
+ return { changes: result2.rowCount || 0, lastInsertRowid: 0 };
232
+ },
233
+ get: async (stmt, params) => {
234
+ const result2 = await client.query(stmt, Array.isArray(params) ? params : void 0);
235
+ return result2.rows[0] || null;
236
+ },
237
+ all: async (stmt, params) => {
238
+ const result2 = await client.query(stmt, Array.isArray(params) ? params : void 0);
239
+ return result2.rows;
240
+ },
241
+ exec: async (script) => {
242
+ await client.query(script);
243
+ },
244
+ transaction: async (fn2) => fn2(trxAdapter)
245
+ };
246
+ const result = await fn(trxAdapter);
247
+ await client.query("COMMIT");
248
+ return result;
249
+ } catch (error) {
250
+ await client.query("ROLLBACK");
251
+ throw error;
252
+ } finally {
253
+ client.release();
254
+ }
255
+ }
256
+ // Extension methods
257
+ async *stream(query, parameters, options) {
258
+ if (!this.pool)
259
+ throw new Error("Storage adapter not opened");
260
+ const batchSize = options?.batchSize || 100;
261
+ let offset = 0;
262
+ let hasMore = true;
263
+ while (hasMore) {
264
+ const paginatedQuery = `${query} LIMIT ${batchSize} OFFSET ${offset}`;
265
+ const rows = await this.all(paginatedQuery, parameters);
266
+ if (rows.length < batchSize) {
267
+ hasMore = false;
268
+ }
269
+ for (const row of rows) {
270
+ yield row;
271
+ }
272
+ offset += batchSize;
273
+ }
274
+ }
275
+ async batchWrite(operations) {
276
+ return this.transaction(async (trx) => {
277
+ let successful = 0;
278
+ let failed = 0;
279
+ const errors = [];
280
+ for (let index = 0; index < operations.length; index++) {
281
+ const op = operations[index];
282
+ try {
283
+ switch (op.type) {
284
+ case "insert": {
285
+ const keys = Object.keys(op.values || {});
286
+ const values = Object.values(op.values || {});
287
+ const placeholders = keys.map((_, i) => `$${i + 1}`).join(", ");
288
+ const query = `INSERT INTO ${op.table} (${keys.join(", ")}) VALUES (${placeholders})`;
289
+ await trx.run(query, values);
290
+ successful++;
291
+ break;
292
+ }
293
+ case "update": {
294
+ const sets = Object.keys(op.values || {}).map((k, i) => `${k} = $${i + 1}`).join(", ");
295
+ const whereClause = Object.keys(op.where || {}).map((k, i) => `${k} = $${i + Object.keys(op.values || {}).length + 1}`).join(" AND ");
296
+ const query = `UPDATE ${op.table} SET ${sets} WHERE ${whereClause}`;
297
+ const params = [...Object.values(op.values || {}), ...Object.values(op.where || {})];
298
+ await trx.run(query, params);
299
+ successful++;
300
+ break;
301
+ }
302
+ case "delete": {
303
+ const whereClause = Object.keys(op.where || {}).map((k, i) => `${k} = $${i + 1}`).join(" AND ");
304
+ const query = `DELETE FROM ${op.table} WHERE ${whereClause}`;
305
+ await trx.run(query, Object.values(op.where || {}));
306
+ successful++;
307
+ break;
308
+ }
309
+ }
310
+ } catch (error) {
311
+ failed++;
312
+ errors.push({
313
+ index,
314
+ error: error instanceof Error ? error : new Error(String(error))
315
+ });
316
+ }
317
+ }
318
+ return { successful, failed, errors: errors.length > 0 ? errors : void 0 };
319
+ });
320
+ }
321
+ async migrate(migrations) {
322
+ await this.exec(`
323
+ CREATE TABLE IF NOT EXISTS _migrations (
324
+ id TEXT PRIMARY KEY,
325
+ version INTEGER NOT NULL,
326
+ applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
327
+ )
328
+ `);
329
+ const applied = await this.all("SELECT id, version FROM _migrations");
330
+ const appliedSet = new Set(applied.map((m) => m.id));
331
+ for (const migration of migrations) {
332
+ if (!appliedSet.has(migration.id)) {
333
+ await this.transaction(async (trx) => {
334
+ await trx.exec(migration.up);
335
+ await trx.run("INSERT INTO _migrations (id, version) VALUES ($1, $2)", [migration.id, migration.version]);
336
+ });
337
+ }
338
+ }
339
+ }
340
+ async getMigrationStatus() {
341
+ const applied = await this.all("SELECT * FROM _migrations ORDER BY version");
342
+ return applied.map((m) => ({
343
+ id: m.id,
344
+ version: m.version,
345
+ up: "",
346
+ down: "",
347
+ timestamp: new Date(m.applied_at).getTime()
348
+ }));
349
+ }
350
+ async backup(_destination) {
351
+ throw new Error("Backup not implemented for Supabase adapter. Use Supabase dashboard or pg_dump.");
352
+ }
353
+ async restore(_source) {
354
+ throw new Error("Restore not implemented for Supabase adapter. Use Supabase dashboard or pg_restore.");
355
+ }
356
+ enableProfiling() {
357
+ this.profilingEnabled = true;
358
+ }
359
+ disableProfiling() {
360
+ this.profilingEnabled = false;
361
+ }
362
+ getMetrics() {
363
+ return { ...this.metrics };
364
+ }
365
+ clearMetrics() {
366
+ this.metrics = {
367
+ totalQueries: 0,
368
+ totalDuration: 0,
369
+ averageDuration: 0,
370
+ slowQueries: [],
371
+ cacheHits: 0,
372
+ cacheMisses: 0
373
+ };
374
+ }
375
+ enableCache(maxSize = 100) {
376
+ this.cacheEnabled = true;
377
+ this.maxCacheSize = maxSize;
378
+ }
379
+ disableCache() {
380
+ this.cacheEnabled = false;
381
+ this.clearCache();
382
+ }
383
+ clearCache() {
384
+ this.queryCache.clear();
385
+ }
386
+ getPoolStatus() {
387
+ if (!this.pool) {
388
+ return { active: 0, idle: 0, waiting: 0, max: 0 };
389
+ }
390
+ return {
391
+ active: this.pool.totalCount,
392
+ idle: this.pool.idleCount,
393
+ waiting: this.pool.waitingCount,
394
+ max: this.pool.options?.max ?? 0
395
+ };
396
+ }
397
+ }
398
+ // Annotate the CommonJS export names for ESM import in node:
399
+ 0 && (module.exports = {
400
+ SupabaseAdapter
401
+ });
@@ -0,0 +1,14 @@
1
+ import type { IBlobCodec } from '../core/contracts/blobCodec.js';
2
+ /**
3
+ * Browser-compatible BLOB codec using typed arrays.
4
+ *
5
+ * Encodes number[] vectors as Float32Array binary BLOBs and computes
6
+ * SHA-256 hashes via `crypto.subtle` (with a Node.js fallback for
7
+ * test environments where SubtleCrypto may not be available).
8
+ */
9
+ export declare class BrowserBlobCodec implements IBlobCodec {
10
+ encode(vec: number[]): Uint8Array;
11
+ decode(blob: Uint8Array | ArrayBufferLike): number[];
12
+ sha256(input: string): Promise<string>;
13
+ }
14
+ //# sourceMappingURL=BrowserBlobCodec.d.ts.map
@@ -0,0 +1,57 @@
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 BrowserBlobCodec_exports = {};
29
+ __export(BrowserBlobCodec_exports, {
30
+ BrowserBlobCodec: () => BrowserBlobCodec
31
+ });
32
+ module.exports = __toCommonJS(BrowserBlobCodec_exports);
33
+ class BrowserBlobCodec {
34
+ encode(vec) {
35
+ const f32 = new Float32Array(vec);
36
+ return new Uint8Array(f32.buffer);
37
+ }
38
+ decode(blob) {
39
+ const bytes = blob instanceof Uint8Array ? blob : new Uint8Array(blob);
40
+ const f32 = new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
41
+ return Array.from(f32);
42
+ }
43
+ async sha256(input) {
44
+ if (typeof globalThis.crypto?.subtle?.digest === "function") {
45
+ const encoded = new TextEncoder().encode(input);
46
+ const hashBuffer = await globalThis.crypto.subtle.digest("SHA-256", encoded);
47
+ const hashArray = new Uint8Array(hashBuffer);
48
+ return Array.from(hashArray).map((b) => b.toString(16).padStart(2, "0")).join("");
49
+ }
50
+ const { createHash } = await Promise.resolve().then(() => __toESM(require("node:crypto")));
51
+ return createHash("sha256").update(input, "utf8").digest("hex");
52
+ }
53
+ }
54
+ // Annotate the CommonJS export names for ESM import in node:
55
+ 0 && (module.exports = {
56
+ BrowserBlobCodec
57
+ });
@@ -0,0 +1,14 @@
1
+ import type { IBlobCodec } from '../core/contracts/blobCodec.js';
2
+ /**
3
+ * Node.js Buffer-based BLOB codec.
4
+ *
5
+ * Encodes number[] vectors as Float32 little-endian binary BLOBs using
6
+ * `Buffer.writeFloatLE` / `Buffer.readFloatLE`, and computes SHA-256
7
+ * hashes using the `node:crypto` module.
8
+ */
9
+ export declare class NodeBlobCodec implements IBlobCodec {
10
+ encode(vec: number[]): Uint8Array;
11
+ decode(blob: Uint8Array | ArrayBufferLike): number[];
12
+ sha256(input: string): Promise<string>;
13
+ }
14
+ //# sourceMappingURL=NodeBlobCodec.d.ts.map
@@ -0,0 +1,59 @@
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 NodeBlobCodec_exports = {};
29
+ __export(NodeBlobCodec_exports, {
30
+ NodeBlobCodec: () => NodeBlobCodec
31
+ });
32
+ module.exports = __toCommonJS(NodeBlobCodec_exports);
33
+ var import_node_crypto = __toESM(require("node:crypto"));
34
+ class NodeBlobCodec {
35
+ encode(vec) {
36
+ const buf = Buffer.alloc(vec.length * 4);
37
+ for (let i = 0; i < vec.length; i++) {
38
+ buf.writeFloatLE(vec[i], i * 4);
39
+ }
40
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
41
+ }
42
+ decode(blob) {
43
+ const bytes = blob instanceof Uint8Array ? blob : new Uint8Array(blob);
44
+ const buf = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
45
+ const count = buf.length / 4;
46
+ const vec = new Array(count);
47
+ for (let i = 0; i < count; i++) {
48
+ vec[i] = buf.readFloatLE(i * 4);
49
+ }
50
+ return vec;
51
+ }
52
+ async sha256(input) {
53
+ return import_node_crypto.default.createHash("sha256").update(input, "utf8").digest("hex");
54
+ }
55
+ }
56
+ // Annotate the CommonJS export names for ESM import in node:
57
+ 0 && (module.exports = {
58
+ NodeBlobCodec
59
+ });
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Binary BLOB codec for encoding/decoding vectors and computing hashes.
3
+ *
4
+ * Node.js implementations use Buffer.
5
+ * Browser implementations use Uint8Array + DataView.
6
+ */
7
+ export interface IBlobCodec {
8
+ /** Encode a number[] vector to a binary BLOB value (Float32 little-endian). */
9
+ encode(vec: number[]): Uint8Array;
10
+ /** Decode a BLOB column value back to number[]. */
11
+ decode(blob: Uint8Array | ArrayBufferLike): number[];
12
+ /** SHA-256 hex digest. Async to support crypto.subtle in browsers. */
13
+ sha256(input: string): Promise<string>;
14
+ }
15
+ //# sourceMappingURL=blobCodec.d.ts.map
@@ -0,0 +1,15 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var blobCodec_exports = {};
15
+ module.exports = __toCommonJS(blobCodec_exports);