defuss-db 1.1.4 → 2.0.1

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