@korajs/store 1.0.0-beta.1 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/adapters/better-sqlite3.cjs.map +1 -1
  2. package/dist/adapters/better-sqlite3.d.cts +1 -1
  3. package/dist/adapters/better-sqlite3.d.ts +1 -1
  4. package/dist/adapters/better-sqlite3.js +1 -1
  5. package/dist/adapters/indexeddb.cjs +523 -148
  6. package/dist/adapters/indexeddb.cjs.map +1 -1
  7. package/dist/adapters/indexeddb.d.cts +4 -2
  8. package/dist/adapters/indexeddb.d.ts +4 -2
  9. package/dist/adapters/indexeddb.js +55 -11
  10. package/dist/adapters/indexeddb.js.map +1 -1
  11. package/dist/adapters/sqlite-wasm-worker.cjs +207 -187
  12. package/dist/adapters/sqlite-wasm-worker.cjs.map +1 -1
  13. package/dist/adapters/sqlite-wasm-worker.js +208 -188
  14. package/dist/adapters/sqlite-wasm-worker.js.map +1 -1
  15. package/dist/adapters/sqlite-wasm.cjs +474 -140
  16. package/dist/adapters/sqlite-wasm.cjs.map +1 -1
  17. package/dist/adapters/sqlite-wasm.d.cts +45 -8
  18. package/dist/adapters/sqlite-wasm.d.ts +45 -8
  19. package/dist/adapters/sqlite-wasm.js +3 -3
  20. package/dist/{backup-ZV4Y424B.js → backup-5S7K2Z35.js} +3 -3
  21. package/dist/{chunk-FFYA7J5H.js → chunk-CYO3YKFL.js} +4 -3
  22. package/dist/chunk-CYO3YKFL.js.map +1 -0
  23. package/dist/{chunk-6NKLDBTY.js → chunk-GSJ7EEUC.js} +2 -1
  24. package/dist/chunk-GSJ7EEUC.js.map +1 -0
  25. package/dist/{chunk-X57MWW35.js → chunk-HS6DBMST.js} +13 -10
  26. package/dist/chunk-HS6DBMST.js.map +1 -0
  27. package/dist/chunk-RJIEKEN4.js +751 -0
  28. package/dist/chunk-RJIEKEN4.js.map +1 -0
  29. package/dist/{chunk-QL2XVZRN.js → chunk-TUR7RGEP.js} +12 -1
  30. package/dist/{chunk-QL2XVZRN.js.map → chunk-TUR7RGEP.js.map} +1 -1
  31. package/dist/{chunk-IOEFNHHP.js → chunk-VDBJQ75K.js} +2 -2
  32. package/dist/chunk-VDBJQ75K.js.map +1 -0
  33. package/dist/{chunk-ZLWAYQEQ.js → chunk-YT7MRFCP.js} +36 -32
  34. package/dist/chunk-YT7MRFCP.js.map +1 -0
  35. package/dist/{export-audit-AJ7RORNK.js → export-audit-ODGQTQIK.js} +3 -3
  36. package/dist/index.cjs +313 -144
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d.cts +51 -5
  39. package/dist/index.d.ts +51 -5
  40. package/dist/index.js +212 -59
  41. package/dist/index.js.map +1 -1
  42. package/dist/internal.cjs +46 -40
  43. package/dist/internal.cjs.map +1 -1
  44. package/dist/internal.d.cts +3 -3
  45. package/dist/internal.d.ts +3 -3
  46. package/dist/internal.js +3 -3
  47. package/dist/sqlite-wasm-channel-GGHA2BXU.js +12 -0
  48. package/dist/{sqlite-wasm-channel-DUDPclTE.d.cts → sqlite-wasm-channel-L6o3bWQK.d.cts} +1 -1
  49. package/dist/{sqlite-wasm-channel-DUDPclTE.d.ts → sqlite-wasm-channel-L6o3bWQK.d.ts} +1 -1
  50. package/dist/{types-5h4Hqcwc.d.ts → types-BOO9EPt1.d.ts} +1 -1
  51. package/dist/{types-k6AZgiH4.d.ts → types-BslDOTk0.d.cts} +26 -1
  52. package/dist/{types-k6AZgiH4.d.cts → types-BslDOTk0.d.ts} +26 -1
  53. package/dist/{types-DfPiPwKr.d.cts → types-C_GDavG-.d.cts} +1 -1
  54. package/package.json +2 -12
  55. package/dist/adapters/sqlite-wasm-shared-host.cjs +0 -68
  56. package/dist/adapters/sqlite-wasm-shared-host.cjs.map +0 -1
  57. package/dist/adapters/sqlite-wasm-shared-host.d.cts +0 -2
  58. package/dist/adapters/sqlite-wasm-shared-host.d.ts +0 -2
  59. package/dist/adapters/sqlite-wasm-shared-host.js +0 -66
  60. package/dist/adapters/sqlite-wasm-shared-host.js.map +0 -1
  61. package/dist/chunk-6NKLDBTY.js.map +0 -1
  62. package/dist/chunk-FFYA7J5H.js.map +0 -1
  63. package/dist/chunk-IOEFNHHP.js.map +0 -1
  64. package/dist/chunk-RBPVEAVM.js +0 -425
  65. package/dist/chunk-RBPVEAVM.js.map +0 -1
  66. package/dist/chunk-X57MWW35.js.map +0 -1
  67. package/dist/chunk-ZLWAYQEQ.js.map +0 -1
  68. package/dist/sqlite-wasm-channel-H43KEZLM.js +0 -12
  69. /package/dist/{backup-ZV4Y424B.js.map → backup-5S7K2Z35.js.map} +0 -0
  70. /package/dist/{export-audit-AJ7RORNK.js.map → export-audit-ODGQTQIK.js.map} +0 -0
  71. /package/dist/{sqlite-wasm-channel-H43KEZLM.js.map → sqlite-wasm-channel-GGHA2BXU.js.map} +0 -0
package/dist/internal.cjs CHANGED
@@ -83,6 +83,7 @@ function encodeRichtext(value) {
83
83
  }
84
84
  if (typeof value === "string") {
85
85
  const doc = new Y.Doc();
86
+ doc.clientID = 0;
86
87
  doc.getText(TEXT_KEY).insert(0, value);
87
88
  return Y.encodeStateAsUpdate(doc);
88
89
  }
@@ -266,6 +267,9 @@ function deserializeValue(value, descriptor) {
266
267
  }
267
268
  }
268
269
 
270
+ // src/query/sql-builder.ts
271
+ var import_core5 = require("@korajs/core");
272
+
269
273
  // src/errors.ts
270
274
  var import_core3 = require("@korajs/core");
271
275
  var QueryError = class extends import_core3.KoraError {
@@ -310,7 +314,7 @@ function lwwVersionWhereClause(remoteVersion) {
310
314
  // src/query/sql-builder.ts
311
315
  function buildSelectQuery(descriptor, fields) {
312
316
  const params = [];
313
- const parts = [`SELECT * FROM ${descriptor.collection}`];
317
+ const parts = [`SELECT * FROM ${(0, import_core5.quoteIdent)(descriptor.collection)}`];
314
318
  const whereClause = buildWhereClauseParts(descriptor.where, fields, params);
315
319
  const deletedFilter = "_deleted = 0";
316
320
  if (whereClause) {
@@ -321,7 +325,7 @@ function buildSelectQuery(descriptor, fields) {
321
325
  if (descriptor.orderBy.length > 0) {
322
326
  const orderParts = descriptor.orderBy.map((o) => {
323
327
  validateFieldName(o.field, fields);
324
- return `${o.field} ${o.direction.toUpperCase()}`;
328
+ return `${(0, import_core5.quoteIdent)(o.field)} ${o.direction.toUpperCase()}`;
325
329
  });
326
330
  parts.push(`ORDER BY ${orderParts.join(", ")}`);
327
331
  }
@@ -335,7 +339,7 @@ function buildSelectQuery(descriptor, fields) {
335
339
  }
336
340
  function buildCountQuery(descriptor, fields) {
337
341
  const params = [];
338
- const parts = [`SELECT COUNT(*) as count FROM ${descriptor.collection}`];
342
+ const parts = [`SELECT COUNT(*) as count FROM ${(0, import_core5.quoteIdent)(descriptor.collection)}`];
339
343
  const whereClause = buildWhereClauseParts(descriptor.where, fields, params);
340
344
  const deletedFilter = "_deleted = 0";
341
345
  if (whereClause) {
@@ -349,38 +353,39 @@ function buildInsertQuery(collection, record) {
349
353
  const columns = Object.keys(record);
350
354
  const placeholders = columns.map(() => "?");
351
355
  const params = Object.values(record);
352
- const sql = `INSERT INTO ${collection} (${columns.join(", ")}) VALUES (${placeholders.join(", ")})`;
356
+ const quotedColumns = columns.map((col) => (0, import_core5.quoteIdent)(col));
357
+ const sql = `INSERT INTO ${(0, import_core5.quoteIdent)(collection)} (${quotedColumns.join(", ")}) VALUES (${placeholders.join(", ")})`;
353
358
  return { sql, params };
354
359
  }
355
360
  function buildUpdateQuery(collection, id, changes) {
356
- const setClauses = Object.keys(changes).map((col) => `${col} = ?`);
361
+ const setClauses = Object.keys(changes).map((col) => `${(0, import_core5.quoteIdent)(col)} = ?`);
357
362
  const params = [...Object.values(changes), id];
358
- const sql = `UPDATE ${collection} SET ${setClauses.join(", ")} WHERE id = ?`;
363
+ const sql = `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET ${setClauses.join(", ")} WHERE id = ?`;
359
364
  return { sql, params };
360
365
  }
361
366
  function buildSoftDeleteQuery(collection, id, updatedAt, version) {
362
367
  if (version !== void 0) {
363
368
  return {
364
- sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ?`,
369
+ sql: `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ?`,
365
370
  params: [updatedAt, version, id]
366
371
  };
367
372
  }
368
373
  return {
369
- sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ? WHERE id = ?`,
374
+ sql: `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ? WHERE id = ?`,
370
375
  params: [updatedAt, id]
371
376
  };
372
377
  }
373
378
  function buildLwwUpdateQuery(collection, id, changes, remoteVersion) {
374
- const setClauses = Object.keys(changes).map((col) => `${col} = ?`);
379
+ const setClauses = Object.keys(changes).map((col) => `${(0, import_core5.quoteIdent)(col)} = ?`);
375
380
  const lww = lwwVersionWhereClause(remoteVersion);
376
- const sql = `UPDATE ${collection} SET ${setClauses.join(", ")} WHERE id = ? AND ${lww.sql}`;
381
+ const sql = `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET ${setClauses.join(", ")} WHERE id = ? AND ${lww.sql}`;
377
382
  const params = [...Object.values(changes), id, ...lww.params];
378
383
  return { sql, params };
379
384
  }
380
385
  function buildLwwSoftDeleteQuery(collection, id, updatedAt, version) {
381
386
  const lww = lwwVersionWhereClause(version);
382
387
  return {
383
- sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ? AND ${lww.sql}`,
388
+ sql: `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ? AND ${lww.sql}`,
384
389
  params: [updatedAt, version, id, ...lww.params]
385
390
  };
386
391
  }
@@ -417,31 +422,32 @@ function buildWhereClauseParts(where, fields, params) {
417
422
  }
418
423
  function buildOperatorCondition(fieldName, operator, value, descriptor, params) {
419
424
  const sqlValue = descriptor?.kind === "boolean" && typeof value === "boolean" ? value ? 1 : 0 : value;
425
+ const column = (0, import_core5.quoteIdent)(fieldName);
420
426
  switch (operator) {
421
427
  case "$eq":
422
428
  if (sqlValue === null) {
423
- return `${fieldName} IS NULL`;
429
+ return `${column} IS NULL`;
424
430
  }
425
431
  params.push(sqlValue);
426
- return `${fieldName} = ?`;
432
+ return `${column} = ?`;
427
433
  case "$ne":
428
434
  if (sqlValue === null) {
429
- return `${fieldName} IS NOT NULL`;
435
+ return `${column} IS NOT NULL`;
430
436
  }
431
437
  params.push(sqlValue);
432
- return `${fieldName} != ?`;
438
+ return `${column} != ?`;
433
439
  case "$gt":
434
440
  params.push(sqlValue);
435
- return `${fieldName} > ?`;
441
+ return `${column} > ?`;
436
442
  case "$gte":
437
443
  params.push(sqlValue);
438
- return `${fieldName} >= ?`;
444
+ return `${column} >= ?`;
439
445
  case "$lt":
440
446
  params.push(sqlValue);
441
- return `${fieldName} < ?`;
447
+ return `${column} < ?`;
442
448
  case "$lte":
443
449
  params.push(sqlValue);
444
- return `${fieldName} <= ?`;
450
+ return `${column} <= ?`;
445
451
  case "$in": {
446
452
  if (!Array.isArray(sqlValue)) {
447
453
  throw new QueryError(`$in operator requires an array value for field "${fieldName}"`, {
@@ -455,7 +461,7 @@ function buildOperatorCondition(fieldName, operator, value, descriptor, params)
455
461
  descriptor?.kind === "boolean" && typeof item === "boolean" ? item ? 1 : 0 : item
456
462
  );
457
463
  }
458
- return `${fieldName} IN (${placeholders.join(", ")})`;
464
+ return `${column} IN (${placeholders.join(", ")})`;
459
465
  }
460
466
  default:
461
467
  throw new QueryError(`Unknown operator "${operator}"`, { operator });
@@ -479,7 +485,7 @@ function validateFieldName(fieldName, fields) {
479
485
  }
480
486
 
481
487
  // src/mutations/execute-insert.ts
482
- var import_core5 = require("@korajs/core");
488
+ var import_core6 = require("@korajs/core");
483
489
 
484
490
  // src/lww/field-versions.ts
485
491
  function parseFieldVersions(raw) {
@@ -546,14 +552,14 @@ function resolveCausalDeps(ctx) {
546
552
 
547
553
  // src/mutations/execute-insert.ts
548
554
  async function executeInsert(ctx, data) {
549
- const validated = (0, import_core5.validateRecord)(ctx.collection, ctx.definition, data, "insert");
550
- const recordId = (0, import_core5.generateUUIDv7)();
555
+ const validated = (0, import_core6.validateRecord)(ctx.collection, ctx.definition, data, "insert");
556
+ const recordId = (0, import_core6.generateUUIDv7)();
551
557
  for (const [fieldName, descriptor] of Object.entries(ctx.definition.fields)) {
552
558
  if (descriptor.auto && descriptor.kind === "timestamp") {
553
559
  validated[fieldName] = Date.now();
554
560
  }
555
561
  }
556
- const writeData = await (0, import_core5.transformSecretFieldsForWrite)(
562
+ const writeData = await (0, import_core6.transformSecretFieldsForWrite)(
557
563
  validated,
558
564
  ctx.definition,
559
565
  ctx.secretKeyProvider
@@ -563,7 +569,7 @@ async function executeInsert(ctx, data) {
563
569
  let record;
564
570
  await ctx.adapter.transaction(async (tx) => {
565
571
  const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
566
- operation = await (0, import_core5.createOperation)(
572
+ operation = await (0, import_core6.createOperation)(
567
573
  {
568
574
  nodeId: ctx.nodeId,
569
575
  type: "insert",
@@ -613,11 +619,11 @@ async function executeInsert(ctx, data) {
613
619
  }
614
620
 
615
621
  // src/mutations/execute-update.ts
616
- var import_core7 = require("@korajs/core");
622
+ var import_core8 = require("@korajs/core");
617
623
 
618
624
  // src/state-machine/state-validator.ts
619
- var import_core6 = require("@korajs/core");
620
- var InvalidStateTransitionError = class extends import_core6.KoraError {
625
+ var import_core7 = require("@korajs/core");
626
+ var InvalidStateTransitionError = class extends import_core7.KoraError {
621
627
  constructor(collection, recordId, field, fromState, toState, allowedStates) {
622
628
  super(
623
629
  `Invalid state transition in collection "${collection}": cannot transition field "${field}" from "${fromState}" to "${toState}". Allowed transitions from "${fromState}": ${allowedStates.length > 0 ? allowedStates.join(", ") : "(none -- terminal state)"}`,
@@ -651,7 +657,7 @@ function validateStateTransition(collectionName, recordId, stateMachine, current
651
657
  collection: collectionName,
652
658
  transitions: stateMachine.transitions
653
659
  };
654
- const transitionResult = (0, import_core6.validateTransition)(constraint, currentState, newState);
660
+ const transitionResult = (0, import_core7.validateTransition)(constraint, currentState, newState);
655
661
  if (transitionResult.valid) {
656
662
  return { valid: true, allowedStates: transitionResult.allowedTargets };
657
663
  }
@@ -700,14 +706,14 @@ function validateUpdateStateMachine(collectionName, recordId, collectionDef, cur
700
706
  // src/mutations/execute-update.ts
701
707
  async function executeUpdate(ctx, id, data) {
702
708
  const currentRows = await ctx.adapter.query(
703
- `SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
709
+ `SELECT * FROM ${(0, import_core8.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
704
710
  [id]
705
711
  );
706
712
  const currentRow = currentRows[0];
707
713
  if (!currentRow) {
708
714
  throw new RecordNotFoundError(ctx.collection, id);
709
715
  }
710
- let validated = (0, import_core7.validateRecord)(ctx.collection, ctx.definition, data, "update");
716
+ let validated = (0, import_core8.validateRecord)(ctx.collection, ctx.definition, data, "update");
711
717
  const currentRecord = deserializeRecord(currentRow, ctx.definition.fields);
712
718
  validated = validateUpdateStateMachine(
713
719
  ctx.collection,
@@ -725,14 +731,14 @@ async function executeUpdate(ctx, id, data) {
725
731
  for (const key of Object.keys(validated)) {
726
732
  const value = validated[key];
727
733
  previousData[key] = currentRecord[key];
728
- if ((0, import_core7.isAtomicOp)(value)) {
729
- resolvedData[key] = (0, import_core7.resolveAtomicOp)(currentRecord[key], value);
730
- atomicOps[key] = (0, import_core7.toAtomicOp)(value);
734
+ if ((0, import_core8.isAtomicOp)(value)) {
735
+ resolvedData[key] = (0, import_core8.resolveAtomicOp)(currentRecord[key], value);
736
+ atomicOps[key] = (0, import_core8.toAtomicOp)(value);
731
737
  } else {
732
738
  resolvedData[key] = value;
733
739
  }
734
740
  }
735
- const writeData = await (0, import_core7.transformSecretFieldsForWrite)(
741
+ const writeData = await (0, import_core8.transformSecretFieldsForWrite)(
736
742
  resolvedData,
737
743
  ctx.definition,
738
744
  ctx.secretKeyProvider
@@ -742,7 +748,7 @@ async function executeUpdate(ctx, id, data) {
742
748
  let operation;
743
749
  await ctx.adapter.transaction(async (tx) => {
744
750
  const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
745
- operation = await (0, import_core7.createOperation)(
751
+ operation = await (0, import_core8.createOperation)(
746
752
  {
747
753
  nodeId: ctx.nodeId,
748
754
  type: "update",
@@ -782,7 +788,7 @@ async function executeUpdate(ctx, id, data) {
782
788
  });
783
789
  ctx.onMutation(ctx.collection, operation);
784
790
  const rows = await ctx.adapter.query(
785
- `SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
791
+ `SELECT * FROM ${(0, import_core8.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
786
792
  [id]
787
793
  );
788
794
  const row = rows[0];
@@ -793,10 +799,10 @@ async function executeUpdate(ctx, id, data) {
793
799
  }
794
800
 
795
801
  // src/mutations/execute-delete.ts
796
- var import_core8 = require("@korajs/core");
802
+ var import_core9 = require("@korajs/core");
797
803
  async function executeDelete(ctx, id, options) {
798
804
  const currentRows = await ctx.adapter.query(
799
- `SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
805
+ `SELECT * FROM ${(0, import_core9.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
800
806
  [id]
801
807
  );
802
808
  if (!currentRows[0]) {
@@ -808,7 +814,7 @@ async function executeDelete(ctx, id, options) {
808
814
  await ctx.adapter.transaction(async (tx) => {
809
815
  if (!operation) {
810
816
  const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
811
- operation = await (0, import_core8.createOperation)(
817
+ operation = await (0, import_core9.createOperation)(
812
818
  {
813
819
  nodeId: ctx.nodeId,
814
820
  type: "delete",