defuss-db 1.1.3 → 2.0.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.
package/dist/server.cjs CHANGED
@@ -1,300 +1,793 @@
1
1
  'use strict';
2
2
 
3
3
  var index = require('./index.cjs');
4
+ var promises = require('node:fs/promises');
5
+ var path = require('node:path');
4
6
  var client = require('@libsql/client');
5
7
  var mongodb = require('mongodb');
8
+ var runtime = require('./runtime-E8GdpV6o.cjs');
6
9
 
7
- class LibsqlProvider {
8
- db;
9
- /**
10
- * Connects to the SQLite database using libsql.
11
- * @param config - The libsql connection configuration.
12
- */
13
- async connect(config) {
14
- if (this.db && this.isConnected()) {
10
+ const normalizedDefinitionCache = /* @__PURE__ */ new WeakMap();
11
+ const INDEX_PREFIX = "__defuss_idx_";
12
+ function normalizeTableDefinition(definition) {
13
+ const cached = normalizedDefinitionCache.get(definition);
14
+ if (cached) {
15
+ return cached;
16
+ }
17
+ if (!definition?.name?.trim()) {
18
+ throw new Error("A table definition requires a non-empty name.");
19
+ }
20
+ const selectorToIndex = /* @__PURE__ */ new Map();
21
+ const indexes = [];
22
+ const register = (index) => {
23
+ if (selectorToIndex.has(index.selector)) {
24
+ throw new Error(
25
+ `Duplicate index selector '${index.selector}' on table '${definition.name}'.`
26
+ );
27
+ }
28
+ selectorToIndex.set(index.selector, index);
29
+ indexes.push(index);
30
+ };
31
+ register({
32
+ name: "id",
33
+ selector: "id",
34
+ source: "id",
35
+ storageKey: `${INDEX_PREFIX}id`,
36
+ unique: true,
37
+ implicit: true
38
+ });
39
+ for (const candidate of definition.indexes ?? []) {
40
+ const source = candidate.source ?? candidate.name;
41
+ const selector = typeof source === "string" ? source : candidate.name;
42
+ if (!selector?.trim()) {
43
+ throw new Error(
44
+ `Every index on table '${definition.name}' requires a selector name.`
45
+ );
46
+ }
47
+ if (selector === "id") {
48
+ continue;
49
+ }
50
+ register({
51
+ ...candidate,
52
+ selector,
53
+ source,
54
+ storageKey: `${INDEX_PREFIX}${sanitizeIdentifier(selector)}_${hashString(
55
+ `${definition.name}:${candidate.name}:${selector}`
56
+ )}`,
57
+ unique: candidate.unique === true
58
+ });
59
+ }
60
+ const normalized = {
61
+ name: definition.name,
62
+ indexes,
63
+ selectorToIndex
64
+ };
65
+ normalizedDefinitionCache.set(definition, normalized);
66
+ return normalized;
67
+ }
68
+ function planSelector(definition, selector = {}) {
69
+ const normalized = normalizeTableDefinition(definition);
70
+ const indexedClauses = [];
71
+ const unindexedClauses = [];
72
+ let id;
73
+ for (const [field, rawValue] of Object.entries(selector)) {
74
+ if (rawValue === void 0) {
75
+ continue;
76
+ }
77
+ if (field === "id") {
78
+ id = normalizePrimaryKey(rawValue);
79
+ continue;
80
+ }
81
+ const index = normalized.selectorToIndex.get(field);
82
+ const clause = {
83
+ field,
84
+ value: rawValue,
85
+ index
86
+ };
87
+ if (index) {
88
+ clause.serializedValue = serializeIndexValue(rawValue);
89
+ indexedClauses.push(clause);
90
+ } else {
91
+ unindexedClauses.push(clause);
92
+ }
93
+ }
94
+ return {
95
+ id,
96
+ indexedClauses,
97
+ unindexedClauses
98
+ };
99
+ }
100
+ function assertUpsertSelector(definition, selector) {
101
+ const keys = Object.keys(selector).filter((key) => selector[key] !== void 0);
102
+ if (keys.length === 0) {
103
+ throw new Error("Upsert requires a non-empty selector.");
104
+ }
105
+ if (keys.length !== 1) {
106
+ throw new Error(
107
+ "Upsert selectors must resolve to exactly one unique field or id."
108
+ );
109
+ }
110
+ if (keys[0] === "id") {
111
+ normalizePrimaryKey(selector.id);
112
+ return;
113
+ }
114
+ const normalized = normalizeTableDefinition(definition);
115
+ const index = normalized.selectorToIndex.get(keys[0]);
116
+ if (!index || !index.unique) {
117
+ throw new Error(
118
+ `Upsert selector '${keys[0]}' must target id or a declared unique index.`
119
+ );
120
+ }
121
+ }
122
+ function alignValueWithSelector(value, selector) {
123
+ const next = cloneRecord(value);
124
+ for (const [field, selectorValue] of Object.entries(selector)) {
125
+ if (selectorValue === void 0) {
126
+ continue;
127
+ }
128
+ if (field === "id") {
129
+ const normalizedId = normalizePrimaryKey(selectorValue);
130
+ if (next.id === void 0) {
131
+ next.id = normalizedId;
132
+ continue;
133
+ }
134
+ if (normalizePrimaryKey(next.id) !== normalizedId) {
135
+ throw new Error("The upsert value conflicts with the selector id.");
136
+ }
137
+ continue;
138
+ }
139
+ const currentValue = getValueAtPath(next, field);
140
+ if (currentValue === void 0) {
141
+ setValueAtPath(next, field, selectorValue);
142
+ continue;
143
+ }
144
+ if (!recordValuesEqual(currentValue, selectorValue)) {
145
+ throw new Error(
146
+ `The upsert value conflicts with selector field '${field}'.`
147
+ );
148
+ }
149
+ }
150
+ return next;
151
+ }
152
+ async function buildStoredRow(definition, value) {
153
+ const normalized = normalizeTableDefinition(definition);
154
+ const record = cloneRecord(value);
155
+ const id = ensurePrimaryKey(record.id);
156
+ record.id = id;
157
+ const payloadRecord = cloneRecord(record);
158
+ delete payloadRecord.id;
159
+ const indexValues = {};
160
+ for (const index of normalized.indexes) {
161
+ indexValues[index.storageKey] = deriveIndexValue(index, record);
162
+ }
163
+ return {
164
+ id,
165
+ payload: JSON.stringify(await encodeDefussValue(payloadRecord)),
166
+ indexValues
167
+ };
168
+ }
169
+ function hydrateStoredRow(row) {
170
+ const payload = decodeDefussValue(
171
+ JSON.parse(row.payload)
172
+ );
173
+ return {
174
+ ...payload,
175
+ id: row.id
176
+ };
177
+ }
178
+ function matchesSelector(record, selector = {}, definition) {
179
+ const normalized = definition ? normalizeTableDefinition(definition) : void 0;
180
+ for (const [field, expectedValue] of Object.entries(selector)) {
181
+ if (expectedValue === void 0) {
182
+ continue;
183
+ }
184
+ const index = normalized?.selectorToIndex.get(field);
185
+ const currentValue = field === "id" ? record.id : index ? deriveIndexRuntimeValue(index, record) : getValueAtPath(record, field);
186
+ if (!recordValuesEqual(currentValue, expectedValue)) {
187
+ return false;
188
+ }
189
+ }
190
+ return true;
191
+ }
192
+ function quoteSqlIdentifier(identifier) {
193
+ return `"${identifier.replace(/"/g, '""')}"`;
194
+ }
195
+ function buildIndexName(tableName, storageKey) {
196
+ return `idx_${sanitizeIdentifier(tableName)}_${sanitizeIdentifier(storageKey)}`;
197
+ }
198
+ function normalizePrimaryKey(value) {
199
+ if (typeof value === "string") {
200
+ const trimmed = value.trim();
201
+ if (!trimmed) {
202
+ throw new Error("The id field cannot be an empty string.");
203
+ }
204
+ return trimmed;
205
+ }
206
+ if (typeof value === "number" || typeof value === "bigint") {
207
+ return String(value);
208
+ }
209
+ throw new Error("The id field must be a string, number, or bigint.");
210
+ }
211
+ function ensurePrimaryKey(value) {
212
+ if (value === void 0) {
213
+ return createPrimaryKey();
214
+ }
215
+ return normalizePrimaryKey(value);
216
+ }
217
+ function deriveIndexValue(index, value) {
218
+ const rawValue = deriveIndexRuntimeValue(index, value);
219
+ if (rawValue === void 0) {
220
+ return void 0;
221
+ }
222
+ return serializeIndexValue(rawValue);
223
+ }
224
+ function deriveIndexRuntimeValue(index, value) {
225
+ const source = index.source;
226
+ return typeof source === "function" ? source(value) : getValueAtPath(value, source);
227
+ }
228
+ function sanitizeIdentifier(value) {
229
+ return value.replace(/[^a-zA-Z0-9_]/g, "_");
230
+ }
231
+ function hashString(value) {
232
+ let hash = 2166136261;
233
+ for (let index = 0; index < value.length; index += 1) {
234
+ hash ^= value.charCodeAt(index);
235
+ hash = Math.imul(hash, 16777619);
236
+ }
237
+ return Math.abs(hash >>> 0).toString(36);
238
+ }
239
+ function createPrimaryKey() {
240
+ if (typeof globalThis.crypto?.randomUUID === "function") {
241
+ return globalThis.crypto.randomUUID();
242
+ }
243
+ return `id_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
244
+ }
245
+ function cloneRecord(value) {
246
+ return cloneDefussValue(value);
247
+ }
248
+ function cloneDefussValue(value) {
249
+ if (value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
250
+ return value;
251
+ }
252
+ if (value instanceof Date) {
253
+ return new Date(value.getTime());
254
+ }
255
+ if (value instanceof ArrayBuffer) {
256
+ return value.slice(0);
257
+ }
258
+ if (Array.isArray(value)) {
259
+ return value.map((item) => cloneDefussValue(item));
260
+ }
261
+ if (isBlobValue(value)) {
262
+ return value.slice(0, value.size, value.type);
263
+ }
264
+ if (typeof value === "object") {
265
+ const result = {};
266
+ for (const [key, nestedValue] of Object.entries(value)) {
267
+ result[key] = cloneDefussValue(nestedValue);
268
+ }
269
+ return result;
270
+ }
271
+ return value;
272
+ }
273
+ function getValueAtPath(target, path) {
274
+ if (!path) {
275
+ return void 0;
276
+ }
277
+ let current = target;
278
+ for (const segment of path.split(".")) {
279
+ if (current === null || typeof current !== "object" || Array.isArray(current)) {
280
+ return void 0;
281
+ }
282
+ current = current[segment];
283
+ }
284
+ return current;
285
+ }
286
+ function setValueAtPath(target, path, value) {
287
+ const segments = path.split(".");
288
+ let current = target;
289
+ for (let index = 0; index < segments.length - 1; index += 1) {
290
+ const segment = segments[index];
291
+ const nextValue = current[segment];
292
+ if (nextValue && typeof nextValue === "object" && !Array.isArray(nextValue)) {
293
+ current = nextValue;
294
+ continue;
295
+ }
296
+ const created = {};
297
+ current[segment] = created;
298
+ current = created;
299
+ }
300
+ current[segments[segments.length - 1]] = value;
301
+ }
302
+ function serializeIndexValue(value) {
303
+ if (typeof value === "string") {
304
+ return `s:${value}`;
305
+ }
306
+ if (typeof value === "number") {
307
+ if (!Number.isFinite(value)) {
308
+ throw new Error("Indexed number values must be finite.");
309
+ }
310
+ return `n:${value}`;
311
+ }
312
+ if (typeof value === "bigint") {
313
+ return `i:${value.toString()}`;
314
+ }
315
+ if (typeof value === "boolean") {
316
+ return value ? "b:1" : "b:0";
317
+ }
318
+ if (value === null) {
319
+ return "z:null";
320
+ }
321
+ if (value instanceof Date) {
322
+ return `d:${value.toISOString()}`;
323
+ }
324
+ throw new Error(
325
+ "Only string, number, bigint, boolean, null, and Date values can be indexed."
326
+ );
327
+ }
328
+ function recordValuesEqual(left, right) {
329
+ if (left === right) {
330
+ return true;
331
+ }
332
+ if (left instanceof Date && right instanceof Date) {
333
+ return left.getTime() === right.getTime();
334
+ }
335
+ if (left instanceof ArrayBuffer && right instanceof ArrayBuffer) {
336
+ return compareArrayBuffers(left, right);
337
+ }
338
+ return false;
339
+ }
340
+ function compareArrayBuffers(left, right) {
341
+ if (left.byteLength !== right.byteLength) {
342
+ return false;
343
+ }
344
+ const leftBytes = new Uint8Array(left);
345
+ const rightBytes = new Uint8Array(right);
346
+ for (let index = 0; index < leftBytes.length; index += 1) {
347
+ if (leftBytes[index] !== rightBytes[index]) {
348
+ return false;
349
+ }
350
+ }
351
+ return true;
352
+ }
353
+ async function encodeDefussValue(value) {
354
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
355
+ return value;
356
+ }
357
+ if (typeof value === "bigint") {
358
+ return {
359
+ __defussType: "bigint",
360
+ value: value.toString()
361
+ };
362
+ }
363
+ if (value instanceof Date) {
364
+ return {
365
+ __defussType: "date",
366
+ value: value.toISOString()
367
+ };
368
+ }
369
+ if (value instanceof ArrayBuffer) {
370
+ return {
371
+ __defussType: "arraybuffer",
372
+ value: bytesToBase64$1(new Uint8Array(value))
373
+ };
374
+ }
375
+ if (isBlobValue(value)) {
376
+ return {
377
+ __defussType: "blob",
378
+ value: bytesToBase64$1(new Uint8Array(await value.arrayBuffer())),
379
+ mimeType: value.type
380
+ };
381
+ }
382
+ if (Array.isArray(value)) {
383
+ return Promise.all(value.map((item) => encodeDefussValue(item)));
384
+ }
385
+ const result = {};
386
+ for (const [key, nestedValue] of Object.entries(value)) {
387
+ if (nestedValue === void 0) {
388
+ continue;
389
+ }
390
+ result[key] = await encodeDefussValue(nestedValue);
391
+ }
392
+ return result;
393
+ }
394
+ function decodeDefussValue(value) {
395
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
396
+ return value;
397
+ }
398
+ if (Array.isArray(value)) {
399
+ return value.map((item) => decodeDefussValue(item));
400
+ }
401
+ if (isEncodedDefussTaggedValue(value)) {
402
+ switch (value.__defussType) {
403
+ case "bigint":
404
+ return BigInt(value.value ?? "0");
405
+ case "date":
406
+ return new Date(value.value ?? "");
407
+ case "arraybuffer":
408
+ return base64ToBytes(value.value ?? "").buffer;
409
+ case "blob":
410
+ return new Blob([base64ToBytes(value.value ?? "")], {
411
+ type: value.mimeType ?? ""
412
+ });
413
+ default:
414
+ return value;
415
+ }
416
+ }
417
+ const result = {};
418
+ for (const [key, nestedValue] of Object.entries(value)) {
419
+ result[key] = decodeDefussValue(nestedValue);
420
+ }
421
+ return result;
422
+ }
423
+ function bytesToBase64$1(bytes) {
424
+ if (typeof Buffer !== "undefined") {
425
+ return Buffer.from(bytes).toString("base64");
426
+ }
427
+ let binary = "";
428
+ for (const byte of bytes) {
429
+ binary += String.fromCharCode(byte);
430
+ }
431
+ return btoa(binary);
432
+ }
433
+ function isBlobValue(value) {
434
+ return typeof value === "object" && value !== null && typeof value.arrayBuffer === "function" && typeof value.slice === "function" && typeof value.type === "string";
435
+ }
436
+ function isEncodedDefussTaggedValue(value) {
437
+ const tag = value.__defussType;
438
+ return tag === "bigint" || tag === "date" || tag === "arraybuffer" || tag === "blob";
439
+ }
440
+ function base64ToBytes(base64) {
441
+ if (typeof Buffer !== "undefined") {
442
+ const nodeBuffer = Buffer.from(base64, "base64");
443
+ const result2 = new Uint8Array(new ArrayBuffer(nodeBuffer.byteLength));
444
+ result2.set(nodeBuffer);
445
+ return result2;
446
+ }
447
+ const binary = atob(base64);
448
+ const result = new Uint8Array(new ArrayBuffer(binary.length));
449
+ for (let index = 0; index < binary.length; index += 1) {
450
+ result[index] = binary.charCodeAt(index);
451
+ }
452
+ return result;
453
+ }
454
+
455
+ class JsonlProvider {
456
+ baseDir = "";
457
+ connected = false;
458
+ rowsByTable = /* @__PURE__ */ new Map();
459
+ definitions = /* @__PURE__ */ new Map();
460
+ async connect(options) {
461
+ if (this.connected && this.baseDir === options.baseDir) {
15
462
  return;
16
463
  }
17
- this.db = client.createClient(config);
464
+ this.baseDir = options.baseDir;
465
+ await promises.mkdir(this.baseDir, { recursive: true });
466
+ await this.loadExistingTables();
467
+ this.connected = true;
18
468
  }
19
- /**
20
- * Disconnects from the SQLite database.
21
- */
22
469
  async disconnect() {
23
- this.db.close();
470
+ this.connected = false;
24
471
  }
25
- /**
26
- * Checks if the provider is currently connected to the database.
27
- * @returns True if connected, false otherwise.
28
- */
29
472
  isConnected() {
30
- return this.db?.closed !== true;
473
+ return this.connected;
31
474
  }
32
- /**
33
- * Creates a table if it doesn't exist.
34
- * Sets up the primary key and any initial structure needed.
35
- * @param table - The name of the table.
36
- */
37
- async createTable(table) {
38
- await this.db.execute({
39
- sql: `
40
- CREATE TABLE IF NOT EXISTS ${table} (
41
- pk INTEGER PRIMARY KEY AUTOINCREMENT
42
- );
43
- `
44
- });
475
+ tableFile(tableName) {
476
+ return path.join(this.baseDir, `${tableName}.jsonl`);
45
477
  }
46
- /**
47
- * Ensures all necessary columns exist in the table
48
- * @param table - The name of the table
49
- * @param record - DefussRecord containing fields to ensure
50
- */
51
- async ensureColumnsExist(table, record) {
52
- const existingColumns = await this.getExistingColumns(table);
53
- for (const [key, value] of Object.entries(record)) {
54
- if (key === "pk") continue;
55
- if (!existingColumns.includes(key) && value !== void 0) {
56
- const dataType = this.getSqliteType(value);
57
- await this.db.execute({
58
- sql: `ALTER TABLE ${table} ADD COLUMN ${key} ${dataType}`
59
- });
60
- if (key.endsWith("_index")) {
61
- const indexName = `idx_${table}_${key}`;
62
- await this.db.execute({
63
- sql: `CREATE INDEX IF NOT EXISTS ${indexName} ON ${table} (${key})`
64
- });
478
+ async loadExistingTables() {
479
+ this.rowsByTable.clear();
480
+ const entries = await promises.readdir(this.baseDir, { withFileTypes: true });
481
+ for (const entry of entries) {
482
+ if (!entry.isFile() || !entry.name.endsWith(".jsonl")) {
483
+ continue;
484
+ }
485
+ const tableName = entry.name.slice(0, -6);
486
+ const filePath = this.tableFile(tableName);
487
+ const content = await promises.readFile(filePath, "utf8");
488
+ const tableRows = /* @__PURE__ */ new Map();
489
+ for (const line of content.split("\n")) {
490
+ if (!line.trim()) {
491
+ continue;
65
492
  }
493
+ const row = JSON.parse(line);
494
+ tableRows.set(row.id, row);
66
495
  }
496
+ this.rowsByTable.set(tableName, tableRows);
67
497
  }
68
498
  }
69
- /**
70
- * Gets the SQLite data type for a JavaScript value
71
- * @param value The JavaScript value to map to SQLite type
72
- * @returns The SQLite type string
73
- */
74
- getSqliteType(value) {
75
- if (value === null || value === void 0) {
76
- return "TEXT";
77
- }
78
- if (typeof value === "string") {
79
- return "TEXT";
80
- } else if (typeof value === "number") {
81
- return Number.isInteger(value) ? "INTEGER" : "REAL";
82
- } else if (typeof value === "boolean") {
83
- return "INTEGER";
84
- } else if (value instanceof ArrayBuffer || value instanceof Blob) {
85
- return "BLOB";
86
- } else {
87
- return "TEXT";
499
+ async rewriteTable(tableName) {
500
+ const rows = [...this.rowsByTable.get(tableName)?.values() ?? []];
501
+ const content = rows.map((row) => JSON.stringify(row)).join("\n");
502
+ await promises.writeFile(
503
+ this.tableFile(tableName),
504
+ content ? `${content}
505
+ ` : "",
506
+ "utf8"
507
+ );
508
+ }
509
+ getTableRows(tableName) {
510
+ let tableRows = this.rowsByTable.get(tableName);
511
+ if (!tableRows) {
512
+ tableRows = /* @__PURE__ */ new Map();
513
+ this.rowsByTable.set(tableName, tableRows);
88
514
  }
515
+ return tableRows;
89
516
  }
90
- /**
91
- * Retrieves existing columns in the specified table.
92
- * @param table - The name of the table.
93
- * @returns An array of column names.
94
- */
95
- async getExistingColumns(table) {
96
- const result = await this.db.execute({
97
- sql: `PRAGMA table_info(${table})`
98
- });
99
- return result.rows.map((col) => col.name);
100
- }
101
- /**
102
- * Converts a DefussRecord to SQLite values
103
- * @param record The DefussRecord to convert
104
- * @returns Object with column names and values for SQLite
105
- */
106
- toSqliteValues(record) {
107
- const result = {};
108
- for (const [key, value] of Object.entries(record)) {
109
- if (key === "pk") continue;
110
- if (key.endsWith("_index")) {
111
- result[key] = this.convertValueForSqlite(value);
112
- } else {
113
- result[key] = this.convertValueForSqlite(value);
517
+ async ensureUniqueConstraints(definition, row, ignoreId) {
518
+ const normalized = normalizeTableDefinition(definition);
519
+ const existingRows = this.getTableRows(normalized.name);
520
+ for (const candidate of existingRows.values()) {
521
+ if (ignoreId && candidate.id === ignoreId) {
522
+ continue;
523
+ }
524
+ if (candidate.id === row.id) {
525
+ throw new Error(`defuss-db: duplicate id '${row.id}' in table '${normalized.name}'.`);
526
+ }
527
+ for (const index of normalized.indexes) {
528
+ if (!index.unique || index.selector === "id") {
529
+ continue;
530
+ }
531
+ const left = candidate.indexValues[index.storageKey];
532
+ const right = row.indexValues[index.storageKey];
533
+ if (left !== void 0 && left === right) {
534
+ throw new Error(
535
+ `defuss-db: duplicate unique index '${index.selector}' in table '${normalized.name}'.`
536
+ );
537
+ }
114
538
  }
115
539
  }
116
- return result;
117
540
  }
118
- /**
119
- * Convert JavaScript value to SQLite-compatible value
120
- */
121
- convertValueForSqlite(value) {
122
- if (value === null || value === void 0) {
123
- return null;
124
- }
125
- if (typeof value === "string" || typeof value === "number") {
126
- return value;
127
- } else if (typeof value === "boolean") {
128
- return value ? 1 : 0;
129
- } else if (value instanceof ArrayBuffer) {
130
- return Buffer.from(value);
131
- } else if (value instanceof Blob) {
132
- return null;
133
- } else {
134
- try {
135
- return JSON.stringify(value);
136
- } catch (e) {
137
- return null;
541
+ rowsToRecords(tableName) {
542
+ return [...this.getTableRows(tableName).values()].map(
543
+ (row) => hydrateStoredRow({ id: row.id, payload: row.payload })
544
+ );
545
+ }
546
+ async ensureTable(definition) {
547
+ if (!this.connected) {
548
+ throw new Error("defuss-db: JsonlProvider must be connected before use.");
549
+ }
550
+ const normalized = normalizeTableDefinition(definition);
551
+ this.definitions.set(normalized.name, normalized);
552
+ this.getTableRows(normalized.name);
553
+ await promises.writeFile(this.tableFile(normalized.name), "", { flag: "a" });
554
+ }
555
+ async insert(definition, value) {
556
+ await this.ensureTable(definition);
557
+ const normalized = normalizeTableDefinition(definition);
558
+ const row = await buildStoredRow(definition, value);
559
+ await this.ensureUniqueConstraints(definition, row);
560
+ this.getTableRows(normalized.name).set(row.id, row);
561
+ await promises.appendFile(this.tableFile(normalized.name), `${JSON.stringify(row)}
562
+ `, "utf8");
563
+ return row.id;
564
+ }
565
+ async find(definition, selector = {}) {
566
+ await this.ensureTable(definition);
567
+ const normalized = normalizeTableDefinition(definition);
568
+ return this.rowsToRecords(normalized.name).filter(
569
+ (record) => matchesSelector(record, selector, definition)
570
+ );
571
+ }
572
+ async findOne(definition, selector = {}) {
573
+ const rows = await this.find(definition, selector);
574
+ return rows[0] ?? null;
575
+ }
576
+ async update(definition, selector, update) {
577
+ if (Object.keys(selector).length === 0) {
578
+ throw new Error("defuss-db: update requires a non-empty selector.");
579
+ }
580
+ await this.ensureTable(definition);
581
+ const normalized = normalizeTableDefinition(definition);
582
+ const tableRows = this.getTableRows(normalized.name);
583
+ const records = await this.find(definition, selector);
584
+ for (const record of records) {
585
+ if (update.id !== void 0 && update.id !== record.id) {
586
+ throw new Error("defuss-db: id cannot be updated.");
138
587
  }
588
+ const nextRecord = {
589
+ ...record,
590
+ ...update,
591
+ id: record.id
592
+ };
593
+ const row = await buildStoredRow(definition, nextRecord);
594
+ await this.ensureUniqueConstraints(definition, row, row.id);
595
+ tableRows.set(row.id, row);
139
596
  }
597
+ await this.rewriteTable(normalized.name);
140
598
  }
141
- /**
142
- * Convert SQLite row to DefussRecord
143
- */
144
- fromSqliteRow(row) {
145
- const result = {};
146
- for (const [key, value] of Object.entries(row)) {
147
- result[key] = value;
599
+ async delete(definition, selector) {
600
+ if (Object.keys(selector).length === 0) {
601
+ throw new Error("defuss-db: delete requires a non-empty selector.");
148
602
  }
149
- return result;
603
+ await this.ensureTable(definition);
604
+ const normalized = normalizeTableDefinition(definition);
605
+ const tableRows = this.getTableRows(normalized.name);
606
+ const records = await this.find(definition, selector);
607
+ for (const record of records) {
608
+ if (record.id) {
609
+ tableRows.delete(String(record.id));
610
+ }
611
+ }
612
+ await this.rewriteTable(normalized.name);
150
613
  }
151
- /**
152
- * Inserts a record into the table.
153
- * @param table - The name of the table.
154
- * @param record - The record to insert.
155
- * @returns The primary key of the inserted record.
156
- */
157
- async insert(table, record) {
158
- await this.ensureTableExists(table);
159
- await this.ensureColumnsExist(table, record);
160
- const values = this.toSqliteValues(record);
161
- if (record.pk !== void 0) {
162
- values.pk = record.pk;
163
- }
164
- const columns = Object.keys(values).join(", ");
165
- const placeholders = Object.keys(values).map(() => "?").join(", ");
166
- const args = Object.values(values);
614
+ async upsert(definition, selector, value) {
615
+ assertUpsertSelector(definition, selector);
616
+ await this.ensureTable(definition);
617
+ const alignedValue = alignValueWithSelector(value, selector);
618
+ const existing = await this.findOne(definition, selector);
619
+ if (existing?.id) {
620
+ await this.update(definition, { id: existing.id }, alignedValue);
621
+ return existing.id;
622
+ }
623
+ try {
624
+ return await this.insert(definition, alignedValue);
625
+ } catch (error) {
626
+ const retried = await this.findOne(definition, selector);
627
+ if (!retried?.id) {
628
+ throw error;
629
+ }
630
+ await this.update(definition, { id: retried.id }, alignedValue);
631
+ return retried.id;
632
+ }
633
+ }
634
+ }
635
+
636
+ const PAYLOAD_COLUMN = "__payload";
637
+ class LibsqlProvider {
638
+ db;
639
+ definitions = /* @__PURE__ */ new Map();
640
+ async connect(config) {
641
+ if (this.db && this.isConnected()) {
642
+ return;
643
+ }
644
+ this.db = client.createClient(config);
645
+ }
646
+ async disconnect() {
647
+ this.db.close();
648
+ }
649
+ isConnected() {
650
+ return this.db?.closed !== true;
651
+ }
652
+ async getExistingColumns(tableName) {
167
653
  const result = await this.db.execute({
168
- sql: `INSERT INTO ${table} (${columns}) VALUES (${placeholders})`,
654
+ sql: `PRAGMA table_info(${quoteSqlIdentifier(tableName)})`
655
+ });
656
+ return result.rows.map((row) => String(row.name));
657
+ }
658
+ async upsertRow(definition, row) {
659
+ const normalized = normalizeTableDefinition(definition);
660
+ const columns = ["id", PAYLOAD_COLUMN, ...normalized.indexes.filter((index) => index.selector !== "id").map((index) => index.storageKey)];
661
+ const quotedColumns = columns.map((column) => quoteSqlIdentifier(column));
662
+ const placeholders = columns.map(() => "?").join(", ");
663
+ const assignments = columns.filter((column) => column !== "id").map((column) => `${quoteSqlIdentifier(column)} = excluded.${quoteSqlIdentifier(column)}`).join(", ");
664
+ const args = columns.map((column) => {
665
+ if (column === "id") {
666
+ return row.id;
667
+ }
668
+ if (column === PAYLOAD_COLUMN) {
669
+ return row.payload;
670
+ }
671
+ return row.indexValues[column] ?? null;
672
+ });
673
+ await this.db.execute({
674
+ sql: `INSERT INTO ${quoteSqlIdentifier(definition.name)} (${quotedColumns.join(", ")}) VALUES (${placeholders}) ON CONFLICT(${quoteSqlIdentifier("id")}) DO UPDATE SET ${assignments}`,
169
675
  args
170
676
  });
171
- return result.lastInsertRowid;
172
- }
173
- /**
174
- * Finds records in the table based on query.
175
- * @param table - The name of the table.
176
- * @param query - Query criteria.
177
- * @returns An array of matching records.
178
- */
179
- async find(table, query) {
180
- await this.ensureTableExists(table);
181
- if (Object.keys(query).length > 0) {
182
- await this.ensureColumnsExist(table, query);
183
- }
184
- let sql = `SELECT * FROM ${table}`;
677
+ }
678
+ async queryRows(definition, selector) {
679
+ const plan = planSelector(definition, selector);
680
+ let sql = `SELECT ${quoteSqlIdentifier("id")}, ${quoteSqlIdentifier(PAYLOAD_COLUMN)} FROM ${quoteSqlIdentifier(definition.name)}`;
185
681
  const args = [];
186
- if (Object.keys(query).length > 0) {
187
- const conditions = Object.keys(query).map((key) => `${key} = ?`).join(" AND ");
188
- sql += ` WHERE ${conditions}`;
189
- args.push(
190
- ...Object.values(query).map((val) => this.convertValueForSqlite(val))
191
- );
682
+ if (plan.id) {
683
+ sql += ` WHERE ${quoteSqlIdentifier("id")} = ?`;
684
+ args.push(plan.id);
685
+ } else if (plan.indexedClauses.length > 0) {
686
+ const [clause] = plan.indexedClauses;
687
+ sql += ` WHERE ${quoteSqlIdentifier(clause.index.storageKey)} = ?`;
688
+ args.push(clause.serializedValue ?? null);
192
689
  }
193
690
  const result = await this.db.execute({ sql, args });
194
- return result.rows.map((row) => this.fromSqliteRow(row));
195
- }
196
- /**
197
- * Finds a single record in the table based on query.
198
- * @param table - The name of the table.
199
- * @param query - Query criteria.
200
- * @returns The first matching record or null.
201
- */
202
- async findOne(table, query) {
203
- const results = await this.find(table, query);
204
- return results.length > 0 ? results[0] : null;
205
- }
206
- /**
207
- * Updates records in the table based on query.
208
- * @param table - The name of the table.
209
- * @param query - Query criteria.
210
- * @param update - Fields to update.
211
- */
212
- async update(table, query, update) {
213
- await this.ensureTableExists(table);
214
- await this.ensureColumnsExist(table, query);
215
- await this.ensureColumnsExist(table, update);
216
- if (Object.keys(query).length === 0) {
217
- throw new Error("At least one query field must be provided for update.");
218
- }
219
- const updateValues = this.toSqliteValues(update);
220
- const setClauses = Object.keys(updateValues).map((key) => `${key} = ?`).join(", ");
221
- const setArgs = Object.values(updateValues);
222
- const whereConditions = [];
223
- const whereArgs = [];
224
- for (const [key, value] of Object.entries(query)) {
225
- whereConditions.push(`${key} = ?`);
226
- whereArgs.push(this.convertValueForSqlite(value));
227
- }
228
- const whereClause = whereConditions.join(" AND ");
691
+ return result.rows.map(
692
+ (row) => hydrateStoredRow({
693
+ id: String(row.id),
694
+ payload: String(row[PAYLOAD_COLUMN])
695
+ })
696
+ ).filter((row) => matchesSelector(row, selector, definition));
697
+ }
698
+ async ensureTable(definition) {
699
+ const normalized = normalizeTableDefinition(definition);
700
+ if (!this.definitions.has(normalized.name)) {
701
+ this.definitions.set(normalized.name, normalized);
702
+ }
229
703
  await this.db.execute({
230
- sql: `UPDATE ${table} SET ${setClauses} WHERE ${whereClause}`,
231
- args: [...setArgs, ...whereArgs]
704
+ sql: `CREATE TABLE IF NOT EXISTS ${quoteSqlIdentifier(normalized.name)} (${quoteSqlIdentifier("id")} TEXT PRIMARY KEY NOT NULL, ${quoteSqlIdentifier(PAYLOAD_COLUMN)} TEXT NOT NULL)`
232
705
  });
706
+ const existingColumns = await this.getExistingColumns(normalized.name);
707
+ for (const index of normalized.indexes) {
708
+ if (index.selector === "id") {
709
+ continue;
710
+ }
711
+ if (!existingColumns.includes(index.storageKey)) {
712
+ await this.db.execute({
713
+ sql: `ALTER TABLE ${quoteSqlIdentifier(normalized.name)} ADD COLUMN ${quoteSqlIdentifier(index.storageKey)} TEXT`
714
+ });
715
+ }
716
+ await this.db.execute({
717
+ sql: `CREATE ${index.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS ${quoteSqlIdentifier(buildIndexName(normalized.name, index.storageKey))} ON ${quoteSqlIdentifier(normalized.name)} (${quoteSqlIdentifier(index.storageKey)})`
718
+ });
719
+ }
233
720
  }
234
- /**
235
- * Deletes records from the table based on query.
236
- * @param table - The name of the table.
237
- * @param query - Query criteria.
238
- */
239
- async delete(table, query) {
240
- await this.ensureTableExists(table);
241
- if (Object.keys(query).length === 0) {
242
- throw new Error(
243
- "At least one query field must be provided for deletion."
244
- );
721
+ async insert(definition, value) {
722
+ await this.ensureTable(definition);
723
+ const row = await buildStoredRow(definition, value);
724
+ await this.upsertRow(definition, row);
725
+ return row.id;
726
+ }
727
+ async find(definition, selector = {}) {
728
+ await this.ensureTable(definition);
729
+ return this.queryRows(definition, selector);
730
+ }
731
+ async findOne(definition, selector = {}) {
732
+ const rows = await this.find(definition, selector);
733
+ return rows[0] ?? null;
734
+ }
735
+ async update(definition, selector, update) {
736
+ if (Object.keys(selector).length === 0) {
737
+ throw new Error("defuss-db: update requires a non-empty selector.");
245
738
  }
246
- await this.ensureColumnsExist(table, query);
247
- const whereConditions = [];
248
- const whereArgs = [];
249
- for (const [key, value] of Object.entries(query)) {
250
- whereConditions.push(`${key} = ?`);
251
- whereArgs.push(this.convertValueForSqlite(value));
739
+ await this.ensureTable(definition);
740
+ const rows = await this.queryRows(definition, selector);
741
+ for (const record of rows) {
742
+ if (update.id !== void 0 && update.id !== record.id) {
743
+ throw new Error("defuss-db: id cannot be updated.");
744
+ }
745
+ const nextRecord = {
746
+ ...record,
747
+ ...update,
748
+ id: record.id
749
+ };
750
+ const row = await buildStoredRow(definition, nextRecord);
751
+ await this.upsertRow(definition, row);
252
752
  }
253
- const whereClause = whereConditions.join(" AND ");
753
+ }
754
+ async delete(definition, selector) {
755
+ if (Object.keys(selector).length === 0) {
756
+ throw new Error("defuss-db: delete requires a non-empty selector.");
757
+ }
758
+ await this.ensureTable(definition);
759
+ const rows = await this.queryRows(definition, selector);
760
+ if (rows.length === 0) {
761
+ return;
762
+ }
763
+ const placeholders = rows.map(() => "?").join(", ");
254
764
  await this.db.execute({
255
- sql: `DELETE FROM ${table} WHERE ${whereClause}`,
256
- args: whereArgs
765
+ sql: `DELETE FROM ${quoteSqlIdentifier(definition.name)} WHERE ${quoteSqlIdentifier("id")} IN (${placeholders})`,
766
+ args: rows.map((row) => String(row.id))
257
767
  });
258
768
  }
259
- /**
260
- * Upserts a record into the table based on query.
261
- * @param table - The name of the table.
262
- * @param record - The record to upsert.
263
- * @param query - Query criteria.
264
- * @returns The primary key of the upserted record.
265
- */
266
- async upsert(table, record, query) {
267
- const existingRecord = await this.findOne(table, query);
268
- if (existingRecord) {
269
- if (existingRecord.pk !== void 0) {
270
- await this.update(table, { pk: existingRecord.pk }, record);
271
- return existingRecord.pk;
272
- } else {
273
- await this.update(table, query, record);
274
- const updatedRecord = await this.findOne(table, query);
275
- return updatedRecord?.pk || 0;
769
+ async upsert(definition, selector, value) {
770
+ assertUpsertSelector(definition, selector);
771
+ await this.ensureTable(definition);
772
+ const alignedValue = alignValueWithSelector(value, selector);
773
+ const existing = await this.findOne(definition, selector);
774
+ if (existing?.id) {
775
+ await this.update(definition, { id: existing.id }, alignedValue);
776
+ return existing.id;
777
+ }
778
+ try {
779
+ return await this.insert(definition, alignedValue);
780
+ } catch (error) {
781
+ const message = error instanceof Error ? error.message : String(error);
782
+ if (!message.includes("UNIQUE") && !message.includes("constraint")) {
783
+ throw error;
276
784
  }
277
- } else {
278
- const insertRecord = { ...record };
279
- for (const [key, value] of Object.entries(query)) {
280
- if (!(key in insertRecord)) {
281
- insertRecord[key] = value;
282
- }
785
+ const retried = await this.findOne(definition, selector);
786
+ if (!retried?.id) {
787
+ throw error;
283
788
  }
284
- return await this.insert(table, insertRecord);
285
- }
286
- }
287
- /**
288
- * Ensures that the specified table exists.
289
- * @param table - The name of the table.
290
- */
291
- async ensureTableExists(table) {
292
- const result = await this.db.execute({
293
- sql: `SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
294
- args: [table]
295
- });
296
- if (result.rows.length === 0) {
297
- await this.createTable(table);
789
+ await this.update(definition, { id: retried.id }, alignedValue);
790
+ return retried.id;
298
791
  }
299
792
  }
300
793
  }
@@ -315,442 +808,369 @@ const parseMongoConnectionString = (connectionString) => {
315
808
  }
316
809
  const clientKey = `${uriObj.hostname}-${databaseName}`;
317
810
  return { databaseName, clientKey };
318
- } catch (e) {
319
- console.error("Could not parse MongoDB connection string:", String(e));
811
+ } catch (error) {
812
+ console.error("Could not parse MongoDB connection string:", String(error));
320
813
  throw new Error(
321
814
  "Invalid MongoDB connection string: database name is required"
322
815
  );
323
816
  }
324
817
  };
325
- async function connectToMongo(connectionString) {
326
- try {
327
- const { databaseName, clientKey } = parseMongoConnectionString(connectionString);
328
- if (_dbClientMap[clientKey] && _dbMap[clientKey]) {
329
- return { client: _dbClientMap[clientKey], db: _dbMap[clientKey] };
330
- }
331
- console.log(`db: Connecting to MongoDB (${databaseName})...`);
332
- const client = new mongodb.MongoClient(connectionString, {
333
- serverSelectionTimeoutMS: 2e3,
334
- // 2 seconds for faster test failures
335
- socketTimeoutMS: 3e3,
336
- // 3 seconds
337
- connectTimeoutMS: 2e3
338
- // 2 seconds
339
- });
340
- await client.connect();
341
- const db = client.db(databaseName);
342
- _dbClientMap[clientKey] = client;
343
- _dbMap[clientKey] = db;
344
- console.log(`db: Successfully connected to MongoDB (${databaseName})`);
345
- return { client, db };
346
- } catch (error) {
347
- console.error("Error connecting to MongoDB:", error);
348
- throw error;
818
+ async function connectToMongo(connectionString, options = {}) {
819
+ const { databaseName, clientKey } = parseMongoConnectionString(connectionString);
820
+ if (_dbClientMap[clientKey] && _dbMap[clientKey]) {
821
+ return { client: _dbClientMap[clientKey], db: _dbMap[clientKey] };
349
822
  }
823
+ const client = new mongodb.MongoClient(connectionString, {
824
+ serverSelectionTimeoutMS: 2e3,
825
+ socketTimeoutMS: 3e3,
826
+ connectTimeoutMS: 2e3,
827
+ ...options
828
+ });
829
+ await client.connect();
830
+ const db = client.db(databaseName);
831
+ _dbClientMap[clientKey] = client;
832
+ _dbMap[clientKey] = db;
833
+ return { client, db };
350
834
  }
351
835
  const isConnected = (connectionString) => {
352
836
  const { clientKey } = parseMongoConnectionString(connectionString);
353
- return !!_dbClientMap[clientKey] && !!_dbMap[clientKey];
837
+ return Boolean(_dbClientMap[clientKey] && _dbMap[clientKey]);
354
838
  };
355
839
  const ping = async (connectionString) => {
840
+ parseMongoConnectionString(connectionString);
841
+ const client = new mongodb.MongoClient(connectionString, {
842
+ serverSelectionTimeoutMS: 2e3,
843
+ connectTimeoutMS: 2e3
844
+ });
356
845
  try {
357
- parseMongoConnectionString(connectionString);
358
- const timeoutPromise = new Promise((_, reject) => {
359
- setTimeout(() => reject(new Error("Ping operation timed out")), 3e3);
360
- });
361
- const pingPromise = (async () => {
362
- if (connectionString.includes("localhost") || connectionString.includes("127.0.0.1")) {
363
- const { clientKey } = parseMongoConnectionString(connectionString);
364
- if (_dbClientMap[clientKey]) {
365
- return { ok: 1 };
366
- } else {
367
- if (connectionString.includes("nonexistent")) {
368
- throw new Error("Invalid database");
369
- }
370
- }
371
- }
372
- const client = new mongodb.MongoClient(connectionString, {
373
- serverSelectionTimeoutMS: 2e3,
374
- connectTimeoutMS: 2e3
375
- });
376
- try {
377
- await client.connect();
378
- const result = await client.db().admin().command({ ping: 1 });
379
- await client.close();
380
- return result;
381
- } finally {
382
- await client.close(true);
383
- }
384
- })();
385
- return await Promise.race([pingPromise, timeoutPromise]);
386
- } catch (error) {
387
- console.error("Error pinging MongoDB:", error);
388
- throw error;
846
+ await client.connect();
847
+ return await client.db().admin().command({ ping: 1 });
848
+ } finally {
849
+ await client.close(true);
389
850
  }
390
851
  };
391
852
  const getCollectionByName = async (collectionName, connectionString) => {
392
- try {
393
- const timeoutPromise = new Promise((_, reject) => {
394
- setTimeout(
395
- () => reject(new Error("Get collection operation timed out")),
396
- 3e3
397
- );
398
- });
399
- const getCollectionPromise = (async () => {
400
- const { db } = await connectToMongo(connectionString);
401
- return db.collection(collectionName);
402
- })();
403
- return await Promise.race([
404
- getCollectionPromise,
405
- timeoutPromise
406
- ]);
407
- } catch (error) {
408
- console.error(
409
- `db: Error getting collection by name: ${collectionName}.`,
410
- error
411
- );
412
- throw error;
413
- }
853
+ const { db } = await connectToMongo(connectionString);
854
+ return db.collection(collectionName);
414
855
  };
415
856
  function isValidObjectId(id) {
416
- const isHexString = (str) => /^[0-9a-fA-F]+$/.test(str);
417
- const objectIdRegExp = /^[0-9a-fA-F]{24}$/;
418
- const isValid = objectIdRegExp.test(id);
419
- if (!isValid && isHexString(id)) {
420
- console.warn(
421
- "ERROR! The passed ObjectId is a hex string BUT is an INVALID ObjectId with ",
422
- id.length,
423
- "characters!"
424
- );
425
- }
426
- return isValid;
857
+ return /^[0-9a-fA-F]{24}$/.test(id);
427
858
  }
428
859
  const toObjectId = (id) => {
860
+ return id instanceof mongodb.ObjectId ? id : new mongodb.ObjectId(id);
861
+ };
862
+ function toMongoQueryId(id) {
863
+ if (typeof id === "bigint") {
864
+ return id.toString();
865
+ }
866
+ if (typeof id === "string" && isValidObjectId(id)) {
867
+ return { $in: [id, toObjectId(id)] };
868
+ }
869
+ return id;
870
+ }
871
+ function toMongoStoredId(id) {
872
+ if (id === void 0) {
873
+ return void 0;
874
+ }
875
+ if (typeof id === "bigint") {
876
+ return id.toString();
877
+ }
878
+ if (typeof id === "string" && isValidObjectId(id)) {
879
+ return toObjectId(id);
880
+ }
881
+ return id;
882
+ }
883
+ function fromMongoStoredId(id) {
429
884
  if (id instanceof mongodb.ObjectId) {
885
+ return id.toString();
886
+ }
887
+ if (typeof id === "bigint" || typeof id === "number" || typeof id === "string") {
430
888
  return id;
431
889
  }
432
- return new mongodb.ObjectId(id);
433
- };
890
+ return String(id);
891
+ }
892
+ function bytesToBase64(bytes) {
893
+ return Buffer.from(bytes).toString("base64");
894
+ }
895
+ function base64ToArrayBuffer(base64) {
896
+ const bytes = Buffer.from(base64, "base64");
897
+ const result = new Uint8Array(new ArrayBuffer(bytes.byteLength));
898
+ result.set(bytes);
899
+ return result.buffer;
900
+ }
901
+ function isBlobLike(value) {
902
+ return typeof value === "object" && value !== null && typeof value.arrayBuffer === "function" && typeof value.slice === "function" && typeof value.type === "string";
903
+ }
904
+ function isEncodedMongoValue(value) {
905
+ const tag = value.__defussType;
906
+ return tag === "arraybuffer" || tag === "bigint" || tag === "blob";
907
+ }
908
+ async function encodeMongoValue(value) {
909
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
910
+ return value;
911
+ }
912
+ if (typeof value === "bigint") {
913
+ return {
914
+ __defussType: "bigint",
915
+ value: value.toString()
916
+ };
917
+ }
918
+ if (value instanceof Date) {
919
+ return value;
920
+ }
921
+ if (value instanceof ArrayBuffer) {
922
+ return {
923
+ __defussType: "arraybuffer",
924
+ value: bytesToBase64(new Uint8Array(value))
925
+ };
926
+ }
927
+ if (isBlobLike(value)) {
928
+ return {
929
+ __defussType: "blob",
930
+ value: bytesToBase64(new Uint8Array(await value.arrayBuffer())),
931
+ mimeType: value.type
932
+ };
933
+ }
934
+ if (Array.isArray(value)) {
935
+ return Promise.all(value.map((item) => encodeMongoValue(item)));
936
+ }
937
+ if (typeof value === "object") {
938
+ const result = {};
939
+ for (const [key, nestedValue] of Object.entries(value)) {
940
+ result[key] = await encodeMongoValue(nestedValue);
941
+ }
942
+ return result;
943
+ }
944
+ return value;
945
+ }
946
+ function decodeMongoValue(value) {
947
+ if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
948
+ return value;
949
+ }
950
+ if (value instanceof Date) {
951
+ return value;
952
+ }
953
+ if (Array.isArray(value)) {
954
+ return value.map((item) => decodeMongoValue(item));
955
+ }
956
+ if (typeof value === "object" && value !== null && isEncodedMongoValue(value)) {
957
+ switch (value.__defussType) {
958
+ case "bigint":
959
+ return BigInt(String(value.value ?? "0"));
960
+ case "arraybuffer":
961
+ return base64ToArrayBuffer(String(value.value ?? ""));
962
+ case "blob":
963
+ return new Blob([
964
+ base64ToArrayBuffer(String(value.value ?? ""))
965
+ ], {
966
+ type: String(value.mimeType ?? "")
967
+ });
968
+ default:
969
+ return value;
970
+ }
971
+ }
972
+ if (typeof value === "object" && value !== null) {
973
+ const result = {};
974
+ for (const [key, nestedValue] of Object.entries(value)) {
975
+ result[key] = decodeMongoValue(nestedValue);
976
+ }
977
+ return result;
978
+ }
979
+ return value;
980
+ }
981
+ async function toMongoDocument(storedRecord) {
982
+ const document = {};
983
+ for (const [key, value] of Object.entries(storedRecord)) {
984
+ if (key === "id") {
985
+ const mongoId = toMongoStoredId(value);
986
+ if (mongoId !== void 0) {
987
+ document._id = mongoId;
988
+ }
989
+ continue;
990
+ }
991
+ document[key] = await encodeMongoValue(value);
992
+ }
993
+ return document;
994
+ }
995
+ function fromMongoDocument(document) {
996
+ const storedRecord = {};
997
+ for (const [key, value] of Object.entries(document)) {
998
+ if (key === "_id") {
999
+ storedRecord.id = fromMongoStoredId(value);
1000
+ continue;
1001
+ }
1002
+ storedRecord[key] = decodeMongoValue(value);
1003
+ }
1004
+ return runtime.fromStoredRecord(storedRecord);
1005
+ }
434
1006
  class MongoProvider {
435
1007
  client = null;
436
1008
  db = null;
437
1009
  collections = /* @__PURE__ */ new Map();
1010
+ definitions = /* @__PURE__ */ new Map();
438
1011
  connectionString;
439
1012
  constructor(options) {
440
1013
  this.connectionString = options.connectionString;
441
1014
  }
442
- /**
443
- * Connects to MongoDB using the shared connection function.
444
- */
445
1015
  async connect(options = {}) {
446
- try {
447
- if (this.isConnected()) {
448
- return;
449
- }
450
- const connectionString = options.connectionString || this.connectionString;
451
- const { client, db } = await connectToMongo(connectionString);
452
- this.client = client;
453
- this.db = db;
454
- } catch (error) {
455
- console.error("Error in MongoProvider.connect:", error);
456
- throw error;
1016
+ if (this.isConnected()) {
1017
+ return;
457
1018
  }
1019
+ const connectionString = options.connectionString || this.connectionString;
1020
+ const { client, db } = await connectToMongo(connectionString);
1021
+ this.client = client;
1022
+ this.db = db;
458
1023
  }
459
- /**
460
- * Disconnects from MongoDB.
461
- */
462
1024
  async disconnect() {
463
- if (this.client) {
464
- try {
465
- if (process.env.NODE_ENV === "test") {
466
- const clientKey = parseMongoConnectionString(
467
- this.connectionString
468
- ).clientKey;
469
- delete _dbClientMap[clientKey];
470
- delete _dbMap[clientKey];
471
- await this.client.close(true);
472
- }
473
- this.client = null;
474
- this.db = null;
475
- this.collections.clear();
476
- console.log("db: MongoProvider disconnected (references cleared)");
477
- } catch (error) {
478
- console.error("Error disconnecting MongoDB provider:", String(error));
479
- }
1025
+ if (!this.client) {
1026
+ return;
480
1027
  }
1028
+ const { clientKey } = parseMongoConnectionString(this.connectionString);
1029
+ delete _dbClientMap[clientKey];
1030
+ delete _dbMap[clientKey];
1031
+ await this.client.close(true);
1032
+ this.client = null;
1033
+ this.db = null;
1034
+ this.collections.clear();
481
1035
  }
482
- /**
483
- * Checks if the provider is currently connected to the database.
484
- * @returns True if connected, false otherwise.
485
- */
486
1036
  isConnected() {
487
- return !!this.client && !!this.db;
1037
+ return Boolean(this.client && this.db);
488
1038
  }
489
- /**
490
- * Creates a collection if it doesn't exist.
491
- */
492
- async createTable(table) {
1039
+ async ensureCollection(name) {
493
1040
  if (!this.db) {
494
1041
  await this.connect();
495
1042
  }
496
- if (this.collections.has(table)) return;
497
- try {
498
- const collectionsList = await this.db.listCollections({
499
- name: table
500
- }).toArray();
501
- if (collectionsList.length === 0) {
502
- await this.db.createCollection(table);
503
- console.log(`db: Created collection '${table}'`);
504
- }
505
- const collection = this.db.collection(table);
506
- this.collections.set(table, collection);
507
- } catch (error) {
508
- console.error(`db: Error creating collection '${table}':`, error);
509
- throw error;
510
- }
511
- }
512
- /**
513
- * Converts DefussRecord to MongoDB document
514
- * @param record DefussRecord to convert
515
- * @returns MongoDB document
516
- */
517
- toMongoDoc(record) {
518
- const result = {};
519
- if (record.pk !== void 0) {
520
- if (typeof record.pk === "string" && isValidObjectId(record.pk)) {
521
- result._id = toObjectId(record.pk);
522
- } else {
523
- result._id = record.pk;
524
- }
1043
+ const existing = this.collections.get(name);
1044
+ if (existing) {
1045
+ return existing;
525
1046
  }
526
- for (const [key, value] of Object.entries(record)) {
527
- if (key === "pk") continue;
528
- if (key.endsWith("_index")) {
529
- const baseFieldName = key.slice(0, -6);
530
- result[baseFieldName] = value;
531
- } else {
532
- result[key] = value;
533
- }
534
- }
535
- return result;
536
- }
537
- /**
538
- * Converts MongoDB document to DefussRecord
539
- * @param doc MongoDB document to convert
540
- * @returns DefussRecord
541
- */
542
- fromMongoDoc(doc) {
543
- if (!doc) return {};
544
- const result = {};
545
- if (doc._id !== void 0) {
546
- if (doc._id instanceof mongodb.ObjectId) {
547
- result.pk = doc._id.toString();
548
- } else {
549
- result.pk = doc._id;
550
- }
551
- }
552
- for (const [key, value] of Object.entries(doc)) {
553
- if (key === "_id") continue;
554
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
555
- result[`${key}_index`] = value;
556
- }
557
- result[key] = value;
1047
+ const collections = await this.db.listCollections({ name }).toArray();
1048
+ if (collections.length === 0) {
1049
+ await this.db.createCollection(name);
558
1050
  }
559
- return result;
1051
+ const collection = this.db.collection(name);
1052
+ this.collections.set(name, collection);
1053
+ return collection;
560
1054
  }
561
- /**
562
- * Inserts a new document into the collection.
563
- * @param table - The name of the collection.
564
- * @param record - The record to insert.
565
- * @returns The primary key of the inserted document.
566
- */
567
- async insert(table, record) {
568
- await this.createTable(table);
569
- const collection = this.collections.get(table);
570
- const doc = this.toMongoDoc(record);
571
- const result = await collection.insertOne(doc);
572
- return result.insertedId.toString();
573
- }
574
- /**
575
- * Finds documents in the collection based on query.
576
- * @param table - The name of the collection.
577
- * @param query - Query criteria.
578
- * @returns An array of matching documents.
579
- */
580
- async find(table, query) {
581
- await this.createTable(table);
582
- const collection = this.collections.get(table);
583
- const mongoQuery = {};
584
- if (query.pk !== void 0) {
585
- if (typeof query.pk === "string" && isValidObjectId(query.pk)) {
586
- mongoQuery._id = toObjectId(query.pk);
587
- } else {
588
- mongoQuery._id = query.pk;
1055
+ buildMongoQuery(definition, selector) {
1056
+ const normalized = runtime.normalizeTableDefinition(definition);
1057
+ const query = {};
1058
+ for (const [key, value] of Object.entries(selector)) {
1059
+ if (value === void 0) {
1060
+ continue;
589
1061
  }
590
- }
591
- for (const [key, value] of Object.entries(query)) {
592
- if (key === "pk") continue;
593
- if (key.endsWith("_index")) {
594
- mongoQuery[key.slice(0, -6)] = value;
595
- } else {
596
- mongoQuery[key] = value;
1062
+ if (key === "id") {
1063
+ query._id = toMongoQueryId(value);
1064
+ continue;
597
1065
  }
598
- }
599
- const documents = await collection.find(mongoQuery).toArray();
600
- return documents.map((doc) => this.fromMongoDoc(doc));
601
- }
602
- /**
603
- * Finds a single document in the collection based on query.
604
- * @param table - The name of the collection.
605
- * @param query - Query criteria.
606
- * @returns The first matching document or null.
607
- */
608
- async findOne(table, query) {
609
- await this.createTable(table);
610
- const collection = this.collections.get(table);
611
- const mongoQuery = {};
612
- if (query.pk !== void 0) {
613
- if (typeof query.pk === "string" && isValidObjectId(query.pk)) {
614
- mongoQuery._id = toObjectId(query.pk);
615
- } else {
616
- mongoQuery._id = query.pk;
617
- }
618
- }
619
- for (const [key, value] of Object.entries(query)) {
620
- if (key === "pk") continue;
621
- if (key.endsWith("_index")) {
622
- mongoQuery[key.slice(0, -6)] = value;
623
- } else {
624
- mongoQuery[key] = value;
625
- }
626
- }
627
- const doc = await collection.findOne(mongoQuery);
628
- if (!doc) return null;
629
- return this.fromMongoDoc(doc);
630
- }
631
- /**
632
- * Updates documents in the collection based on query.
633
- * @param table - The name of the collection.
634
- * @param query - Query criteria.
635
- * @param update - Fields to update.
636
- */
637
- async update(table, query, update) {
638
- await this.createTable(table);
639
- const collection = this.collections.get(table);
640
- const mongoQuery = {};
641
- if (query.pk !== void 0) {
642
- if (typeof query.pk === "string" && isValidObjectId(query.pk)) {
643
- mongoQuery._id = toObjectId(query.pk);
1066
+ const index = runtime.resolveSelectorIndex(normalized, key);
1067
+ if (index) {
1068
+ query[index.storageKey] = value;
644
1069
  } else {
645
- mongoQuery._id = query.pk;
1070
+ query[key] = value;
646
1071
  }
647
1072
  }
648
- for (const [key, value] of Object.entries(query)) {
649
- if (key === "pk") continue;
650
- if (key.endsWith("_index")) {
651
- mongoQuery[key.slice(0, -6)] = value;
652
- } else {
653
- mongoQuery[key] = value;
654
- }
1073
+ return query;
1074
+ }
1075
+ async ensureTable(definition) {
1076
+ const normalized = runtime.normalizeTableDefinition(definition);
1077
+ const existing = this.definitions.get(normalized.name);
1078
+ const collection = await this.ensureCollection(normalized.name);
1079
+ if (existing) {
1080
+ return;
655
1081
  }
656
- const mongoUpdate = { $set: {} };
657
- for (const [key, value] of Object.entries(update)) {
658
- if (key === "pk") continue;
659
- if (key.endsWith("_index")) {
660
- mongoUpdate.$set[key.slice(0, -6)] = value;
661
- } else {
662
- mongoUpdate.$set[key] = value;
1082
+ this.definitions.set(normalized.name, normalized);
1083
+ for (const index of normalized.indexes) {
1084
+ if (index.selectorKey === "id") {
1085
+ continue;
663
1086
  }
1087
+ await collection.createIndex(
1088
+ { [index.storageKey]: 1 },
1089
+ {
1090
+ name: index.storageKey,
1091
+ unique: index.unique,
1092
+ sparse: true
1093
+ }
1094
+ );
664
1095
  }
665
- await collection.updateMany(mongoQuery, mongoUpdate);
666
- }
667
- /**
668
- * Deletes documents from the collection based on query.
669
- * @param table - The name of the collection.
670
- * @param query - Query criteria.
671
- */
672
- async delete(table, query) {
673
- await this.createTable(table);
674
- const collection = this.collections.get(table);
675
- const mongoQuery = {};
676
- if (query.pk !== void 0) {
677
- if (typeof query.pk === "string" && isValidObjectId(query.pk)) {
678
- mongoQuery._id = toObjectId(query.pk);
679
- } else {
680
- mongoQuery._id = query.pk;
681
- }
1096
+ }
1097
+ async insert(definition, value) {
1098
+ await this.ensureTable(definition);
1099
+ const normalized = runtime.normalizeTableDefinition(definition);
1100
+ const collection = await this.ensureCollection(normalized.name);
1101
+ const storedRecord = runtime.toStoredRecord(normalized, value);
1102
+ const document = await toMongoDocument(storedRecord);
1103
+ const result = await collection.insertOne(document);
1104
+ return fromMongoStoredId(result.insertedId);
1105
+ }
1106
+ async find(definition, selector = {}) {
1107
+ await this.ensureTable(definition);
1108
+ const normalized = runtime.normalizeTableDefinition(definition);
1109
+ const collection = await this.ensureCollection(normalized.name);
1110
+ const documents = await collection.find(this.buildMongoQuery(definition, selector)).toArray();
1111
+ return documents.map((document) => fromMongoDocument(document));
1112
+ }
1113
+ async findOne(definition, selector = {}) {
1114
+ await this.ensureTable(definition);
1115
+ const normalized = runtime.normalizeTableDefinition(definition);
1116
+ const collection = await this.ensureCollection(normalized.name);
1117
+ const document = await collection.findOne(this.buildMongoQuery(definition, selector));
1118
+ return document ? fromMongoDocument(document) : null;
1119
+ }
1120
+ async update(definition, selector, update) {
1121
+ if (Object.keys(selector).length === 0) {
1122
+ throw new Error("defuss-db: update requires a non-empty selector.");
682
1123
  }
683
- for (const [key, value] of Object.entries(query)) {
684
- if (key === "pk") continue;
685
- if (key.endsWith("_index")) {
686
- mongoQuery[key.slice(0, -6)] = value;
687
- } else {
688
- mongoQuery[key] = value;
1124
+ await this.ensureTable(definition);
1125
+ const normalized = runtime.normalizeTableDefinition(definition);
1126
+ const collection = await this.ensureCollection(normalized.name);
1127
+ const documents = await collection.find(this.buildMongoQuery(definition, selector)).toArray();
1128
+ for (const document of documents) {
1129
+ const existingRecord = fromMongoDocument(document);
1130
+ if (update.id !== void 0 && update.id !== existingRecord.id) {
1131
+ throw new Error("defuss-db: id cannot be updated.");
689
1132
  }
1133
+ const nextRecord = {
1134
+ ...existingRecord,
1135
+ ...update,
1136
+ id: existingRecord.id
1137
+ };
1138
+ const storedRecord = runtime.toStoredRecord(normalized, nextRecord);
1139
+ await collection.replaceOne({ _id: document._id }, await toMongoDocument(storedRecord));
690
1140
  }
691
- await collection.deleteMany(mongoQuery);
692
- }
693
- /**
694
- * Upserts a document into the collection based on query.
695
- * @param table - The name of the collection.
696
- * @param record - The record to upsert.
697
- * @param query - Query criteria.
698
- * @returns The primary key of the upserted document.
699
- */
700
- async upsert(table, record, query) {
701
- await this.createTable(table);
702
- const collection = this.collections.get(table);
703
- const mongoQuery = {};
704
- if (query.pk !== void 0) {
705
- if (typeof query.pk === "string" && isValidObjectId(query.pk)) {
706
- mongoQuery._id = toObjectId(query.pk);
707
- } else {
708
- mongoQuery._id = query.pk;
709
- }
1141
+ }
1142
+ async delete(definition, selector) {
1143
+ if (Object.keys(selector).length === 0) {
1144
+ throw new Error("defuss-db: delete requires a non-empty selector.");
710
1145
  }
711
- for (const [key, value] of Object.entries(query)) {
712
- if (key === "pk") continue;
713
- if (key.endsWith("_index")) {
714
- mongoQuery[key.slice(0, -6)] = value;
715
- } else {
716
- mongoQuery[key] = value;
717
- }
1146
+ await this.ensureTable(definition);
1147
+ const normalized = runtime.normalizeTableDefinition(definition);
1148
+ const collection = await this.ensureCollection(normalized.name);
1149
+ await collection.deleteMany(this.buildMongoQuery(definition, selector));
1150
+ }
1151
+ async upsert(definition, selector, value) {
1152
+ await this.ensureTable(definition);
1153
+ const normalized = runtime.normalizeTableDefinition(definition);
1154
+ runtime.resolveUniqueSelector(normalized, selector);
1155
+ const mergedValue = runtime.mergeSelectorIntoValue(normalized, selector, value);
1156
+ const existingRecord = await this.findOne(definition, selector);
1157
+ if (existingRecord?.id !== void 0) {
1158
+ await this.update(definition, { id: existingRecord.id }, mergedValue);
1159
+ return existingRecord.id;
718
1160
  }
719
- const doc = this.toMongoDoc(record);
720
- const result = await collection.updateOne(
721
- mongoQuery,
722
- { $set: doc },
723
- { upsert: true }
724
- );
725
- if (result.upsertedId) {
726
- return result.upsertedId.toString();
727
- } else {
728
- const existingDoc = await collection.findOne(mongoQuery);
729
- return existingDoc ? existingDoc._id.toString() : "";
730
- }
731
- }
732
- /**
733
- * Deserializes a MongoDB document into a JavaScript object.
734
- * @param row - The document to deserialize.
735
- * @returns The deserialized JavaScript object.
736
- */
737
- deserializeRow(row) {
738
- if (row.type === "json" && row.json) {
739
- try {
740
- return JSON.parse(row.json);
741
- } catch (e) {
742
- console.error("Failed to parse JSON data:", e);
743
- return row;
1161
+ try {
1162
+ return await this.insert(definition, mergedValue);
1163
+ } catch (error) {
1164
+ const duplicateKey = error && typeof error === "object" && "code" in error ? Number(error.code) === 11e3 : false;
1165
+ if (!duplicateKey) {
1166
+ throw error;
744
1167
  }
745
- } else if (row.type === "blob" && row.blob) {
746
- try {
747
- return row.blob;
748
- } catch (e) {
749
- console.error("Failed to process binary data:", e);
750
- return row;
1168
+ const retried = await this.findOne(definition, selector);
1169
+ if (!retried?.id) {
1170
+ throw error;
751
1171
  }
752
- } else {
753
- return row;
1172
+ await this.update(definition, { id: retried.id }, mergedValue);
1173
+ return retried.id;
754
1174
  }
755
1175
  }
756
1176
  }
@@ -758,20 +1178,10 @@ async function closeAllConnections() {
758
1178
  for (const clientKey in _dbClientMap) {
759
1179
  const client = _dbClientMap[clientKey];
760
1180
  if (client) {
761
- try {
762
- await client.close(true);
763
- console.log(`Closed MongoDB connection: ${clientKey}`);
764
- } catch (err) {
765
- console.error(`Error closing MongoDB connection ${clientKey}:`, err);
766
- }
1181
+ await client.close(true);
767
1182
  }
768
1183
  }
769
- for (const key in _dbClientMap) {
770
- delete _dbClientMap[key];
771
- }
772
- for (const key in _dbMap) {
773
- delete _dbMap[key];
774
- }
1184
+ clearMongoConnections();
775
1185
  }
776
1186
  function clearMongoConnections() {
777
1187
  for (const key in _dbClientMap) {
@@ -782,7 +1192,19 @@ function clearMongoConnections() {
782
1192
  }
783
1193
  }
784
1194
 
1195
+ exports.AggregationBuilder = index.AggregationBuilder;
785
1196
  exports.DefussTable = index.DefussTable;
1197
+ exports.aggregationReducers = index.aggregationReducers;
1198
+ exports.avgBy = index.avgBy;
1199
+ exports.countRows = index.countRows;
1200
+ exports.createAggregation = index.createAggregation;
1201
+ exports.defineTable = index.defineTable;
1202
+ exports.firstBy = index.firstBy;
1203
+ exports.lastBy = index.lastBy;
1204
+ exports.maxBy = index.maxBy;
1205
+ exports.minBy = index.minBy;
1206
+ exports.sumBy = index.sumBy;
1207
+ exports.JsonlProvider = JsonlProvider;
786
1208
  exports.LibsqlProvider = LibsqlProvider;
787
1209
  exports.MongoProvider = MongoProvider;
788
1210
  exports.clearMongoConnections = clearMongoConnections;