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

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 (37) hide show
  1. package/dist/adapters/indexeddb.cjs +39 -5
  2. package/dist/adapters/indexeddb.cjs.map +1 -1
  3. package/dist/adapters/indexeddb.js +13 -6
  4. package/dist/adapters/indexeddb.js.map +1 -1
  5. package/dist/adapters/sqlite-wasm-worker.cjs +25 -3
  6. package/dist/adapters/sqlite-wasm-worker.cjs.map +1 -1
  7. package/dist/adapters/sqlite-wasm-worker.js +25 -3
  8. package/dist/adapters/sqlite-wasm-worker.js.map +1 -1
  9. package/dist/adapters/sqlite-wasm.cjs +29 -0
  10. package/dist/adapters/sqlite-wasm.cjs.map +1 -1
  11. package/dist/adapters/sqlite-wasm.d.cts +16 -1
  12. package/dist/adapters/sqlite-wasm.d.ts +16 -1
  13. package/dist/adapters/sqlite-wasm.js +1 -1
  14. package/dist/{backup-ZV4Y424B.js → backup-IQE4QC7Q.js} +2 -2
  15. package/dist/{chunk-ZLWAYQEQ.js → chunk-2VRPF25R.js} +31 -27
  16. package/dist/chunk-2VRPF25R.js.map +1 -0
  17. package/dist/{chunk-X57MWW35.js → chunk-OCOGQRHW.js} +12 -9
  18. package/dist/chunk-OCOGQRHW.js.map +1 -0
  19. package/dist/{chunk-RBPVEAVM.js → chunk-QUJGXSZ6.js} +30 -1
  20. package/dist/chunk-QUJGXSZ6.js.map +1 -0
  21. package/dist/{chunk-FFYA7J5H.js → chunk-RCTG3IJG.js} +3 -2
  22. package/dist/chunk-RCTG3IJG.js.map +1 -0
  23. package/dist/{export-audit-AJ7RORNK.js → export-audit-MT2LTMIN.js} +2 -2
  24. package/dist/index.cjs +155 -141
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +48 -40
  27. package/dist/index.js.map +1 -1
  28. package/dist/internal.cjs +45 -40
  29. package/dist/internal.cjs.map +1 -1
  30. package/dist/internal.js +1 -1
  31. package/package.json +2 -2
  32. package/dist/chunk-FFYA7J5H.js.map +0 -1
  33. package/dist/chunk-RBPVEAVM.js.map +0 -1
  34. package/dist/chunk-X57MWW35.js.map +0 -1
  35. package/dist/chunk-ZLWAYQEQ.js.map +0 -1
  36. /package/dist/{backup-ZV4Y424B.js.map → backup-IQE4QC7Q.js.map} +0 -0
  37. /package/dist/{export-audit-AJ7RORNK.js.map → export-audit-MT2LTMIN.js.map} +0 -0
package/dist/internal.cjs CHANGED
@@ -266,6 +266,9 @@ function deserializeValue(value, descriptor) {
266
266
  }
267
267
  }
268
268
 
269
+ // src/query/sql-builder.ts
270
+ var import_core5 = require("@korajs/core");
271
+
269
272
  // src/errors.ts
270
273
  var import_core3 = require("@korajs/core");
271
274
  var QueryError = class extends import_core3.KoraError {
@@ -310,7 +313,7 @@ function lwwVersionWhereClause(remoteVersion) {
310
313
  // src/query/sql-builder.ts
311
314
  function buildSelectQuery(descriptor, fields) {
312
315
  const params = [];
313
- const parts = [`SELECT * FROM ${descriptor.collection}`];
316
+ const parts = [`SELECT * FROM ${(0, import_core5.quoteIdent)(descriptor.collection)}`];
314
317
  const whereClause = buildWhereClauseParts(descriptor.where, fields, params);
315
318
  const deletedFilter = "_deleted = 0";
316
319
  if (whereClause) {
@@ -321,7 +324,7 @@ function buildSelectQuery(descriptor, fields) {
321
324
  if (descriptor.orderBy.length > 0) {
322
325
  const orderParts = descriptor.orderBy.map((o) => {
323
326
  validateFieldName(o.field, fields);
324
- return `${o.field} ${o.direction.toUpperCase()}`;
327
+ return `${(0, import_core5.quoteIdent)(o.field)} ${o.direction.toUpperCase()}`;
325
328
  });
326
329
  parts.push(`ORDER BY ${orderParts.join(", ")}`);
327
330
  }
@@ -335,7 +338,7 @@ function buildSelectQuery(descriptor, fields) {
335
338
  }
336
339
  function buildCountQuery(descriptor, fields) {
337
340
  const params = [];
338
- const parts = [`SELECT COUNT(*) as count FROM ${descriptor.collection}`];
341
+ const parts = [`SELECT COUNT(*) as count FROM ${(0, import_core5.quoteIdent)(descriptor.collection)}`];
339
342
  const whereClause = buildWhereClauseParts(descriptor.where, fields, params);
340
343
  const deletedFilter = "_deleted = 0";
341
344
  if (whereClause) {
@@ -349,38 +352,39 @@ function buildInsertQuery(collection, record) {
349
352
  const columns = Object.keys(record);
350
353
  const placeholders = columns.map(() => "?");
351
354
  const params = Object.values(record);
352
- const sql = `INSERT INTO ${collection} (${columns.join(", ")}) VALUES (${placeholders.join(", ")})`;
355
+ const quotedColumns = columns.map((col) => (0, import_core5.quoteIdent)(col));
356
+ const sql = `INSERT INTO ${(0, import_core5.quoteIdent)(collection)} (${quotedColumns.join(", ")}) VALUES (${placeholders.join(", ")})`;
353
357
  return { sql, params };
354
358
  }
355
359
  function buildUpdateQuery(collection, id, changes) {
356
- const setClauses = Object.keys(changes).map((col) => `${col} = ?`);
360
+ const setClauses = Object.keys(changes).map((col) => `${(0, import_core5.quoteIdent)(col)} = ?`);
357
361
  const params = [...Object.values(changes), id];
358
- const sql = `UPDATE ${collection} SET ${setClauses.join(", ")} WHERE id = ?`;
362
+ const sql = `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET ${setClauses.join(", ")} WHERE id = ?`;
359
363
  return { sql, params };
360
364
  }
361
365
  function buildSoftDeleteQuery(collection, id, updatedAt, version) {
362
366
  if (version !== void 0) {
363
367
  return {
364
- sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ?`,
368
+ sql: `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ?`,
365
369
  params: [updatedAt, version, id]
366
370
  };
367
371
  }
368
372
  return {
369
- sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ? WHERE id = ?`,
373
+ sql: `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ? WHERE id = ?`,
370
374
  params: [updatedAt, id]
371
375
  };
372
376
  }
373
377
  function buildLwwUpdateQuery(collection, id, changes, remoteVersion) {
374
- const setClauses = Object.keys(changes).map((col) => `${col} = ?`);
378
+ const setClauses = Object.keys(changes).map((col) => `${(0, import_core5.quoteIdent)(col)} = ?`);
375
379
  const lww = lwwVersionWhereClause(remoteVersion);
376
- const sql = `UPDATE ${collection} SET ${setClauses.join(", ")} WHERE id = ? AND ${lww.sql}`;
380
+ const sql = `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET ${setClauses.join(", ")} WHERE id = ? AND ${lww.sql}`;
377
381
  const params = [...Object.values(changes), id, ...lww.params];
378
382
  return { sql, params };
379
383
  }
380
384
  function buildLwwSoftDeleteQuery(collection, id, updatedAt, version) {
381
385
  const lww = lwwVersionWhereClause(version);
382
386
  return {
383
- sql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ? AND ${lww.sql}`,
387
+ sql: `UPDATE ${(0, import_core5.quoteIdent)(collection)} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ? AND ${lww.sql}`,
384
388
  params: [updatedAt, version, id, ...lww.params]
385
389
  };
386
390
  }
@@ -417,31 +421,32 @@ function buildWhereClauseParts(where, fields, params) {
417
421
  }
418
422
  function buildOperatorCondition(fieldName, operator, value, descriptor, params) {
419
423
  const sqlValue = descriptor?.kind === "boolean" && typeof value === "boolean" ? value ? 1 : 0 : value;
424
+ const column = (0, import_core5.quoteIdent)(fieldName);
420
425
  switch (operator) {
421
426
  case "$eq":
422
427
  if (sqlValue === null) {
423
- return `${fieldName} IS NULL`;
428
+ return `${column} IS NULL`;
424
429
  }
425
430
  params.push(sqlValue);
426
- return `${fieldName} = ?`;
431
+ return `${column} = ?`;
427
432
  case "$ne":
428
433
  if (sqlValue === null) {
429
- return `${fieldName} IS NOT NULL`;
434
+ return `${column} IS NOT NULL`;
430
435
  }
431
436
  params.push(sqlValue);
432
- return `${fieldName} != ?`;
437
+ return `${column} != ?`;
433
438
  case "$gt":
434
439
  params.push(sqlValue);
435
- return `${fieldName} > ?`;
440
+ return `${column} > ?`;
436
441
  case "$gte":
437
442
  params.push(sqlValue);
438
- return `${fieldName} >= ?`;
443
+ return `${column} >= ?`;
439
444
  case "$lt":
440
445
  params.push(sqlValue);
441
- return `${fieldName} < ?`;
446
+ return `${column} < ?`;
442
447
  case "$lte":
443
448
  params.push(sqlValue);
444
- return `${fieldName} <= ?`;
449
+ return `${column} <= ?`;
445
450
  case "$in": {
446
451
  if (!Array.isArray(sqlValue)) {
447
452
  throw new QueryError(`$in operator requires an array value for field "${fieldName}"`, {
@@ -455,7 +460,7 @@ function buildOperatorCondition(fieldName, operator, value, descriptor, params)
455
460
  descriptor?.kind === "boolean" && typeof item === "boolean" ? item ? 1 : 0 : item
456
461
  );
457
462
  }
458
- return `${fieldName} IN (${placeholders.join(", ")})`;
463
+ return `${column} IN (${placeholders.join(", ")})`;
459
464
  }
460
465
  default:
461
466
  throw new QueryError(`Unknown operator "${operator}"`, { operator });
@@ -479,7 +484,7 @@ function validateFieldName(fieldName, fields) {
479
484
  }
480
485
 
481
486
  // src/mutations/execute-insert.ts
482
- var import_core5 = require("@korajs/core");
487
+ var import_core6 = require("@korajs/core");
483
488
 
484
489
  // src/lww/field-versions.ts
485
490
  function parseFieldVersions(raw) {
@@ -546,14 +551,14 @@ function resolveCausalDeps(ctx) {
546
551
 
547
552
  // src/mutations/execute-insert.ts
548
553
  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)();
554
+ const validated = (0, import_core6.validateRecord)(ctx.collection, ctx.definition, data, "insert");
555
+ const recordId = (0, import_core6.generateUUIDv7)();
551
556
  for (const [fieldName, descriptor] of Object.entries(ctx.definition.fields)) {
552
557
  if (descriptor.auto && descriptor.kind === "timestamp") {
553
558
  validated[fieldName] = Date.now();
554
559
  }
555
560
  }
556
- const writeData = await (0, import_core5.transformSecretFieldsForWrite)(
561
+ const writeData = await (0, import_core6.transformSecretFieldsForWrite)(
557
562
  validated,
558
563
  ctx.definition,
559
564
  ctx.secretKeyProvider
@@ -563,7 +568,7 @@ async function executeInsert(ctx, data) {
563
568
  let record;
564
569
  await ctx.adapter.transaction(async (tx) => {
565
570
  const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
566
- operation = await (0, import_core5.createOperation)(
571
+ operation = await (0, import_core6.createOperation)(
567
572
  {
568
573
  nodeId: ctx.nodeId,
569
574
  type: "insert",
@@ -613,11 +618,11 @@ async function executeInsert(ctx, data) {
613
618
  }
614
619
 
615
620
  // src/mutations/execute-update.ts
616
- var import_core7 = require("@korajs/core");
621
+ var import_core8 = require("@korajs/core");
617
622
 
618
623
  // src/state-machine/state-validator.ts
619
- var import_core6 = require("@korajs/core");
620
- var InvalidStateTransitionError = class extends import_core6.KoraError {
624
+ var import_core7 = require("@korajs/core");
625
+ var InvalidStateTransitionError = class extends import_core7.KoraError {
621
626
  constructor(collection, recordId, field, fromState, toState, allowedStates) {
622
627
  super(
623
628
  `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 +656,7 @@ function validateStateTransition(collectionName, recordId, stateMachine, current
651
656
  collection: collectionName,
652
657
  transitions: stateMachine.transitions
653
658
  };
654
- const transitionResult = (0, import_core6.validateTransition)(constraint, currentState, newState);
659
+ const transitionResult = (0, import_core7.validateTransition)(constraint, currentState, newState);
655
660
  if (transitionResult.valid) {
656
661
  return { valid: true, allowedStates: transitionResult.allowedTargets };
657
662
  }
@@ -700,14 +705,14 @@ function validateUpdateStateMachine(collectionName, recordId, collectionDef, cur
700
705
  // src/mutations/execute-update.ts
701
706
  async function executeUpdate(ctx, id, data) {
702
707
  const currentRows = await ctx.adapter.query(
703
- `SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
708
+ `SELECT * FROM ${(0, import_core8.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
704
709
  [id]
705
710
  );
706
711
  const currentRow = currentRows[0];
707
712
  if (!currentRow) {
708
713
  throw new RecordNotFoundError(ctx.collection, id);
709
714
  }
710
- let validated = (0, import_core7.validateRecord)(ctx.collection, ctx.definition, data, "update");
715
+ let validated = (0, import_core8.validateRecord)(ctx.collection, ctx.definition, data, "update");
711
716
  const currentRecord = deserializeRecord(currentRow, ctx.definition.fields);
712
717
  validated = validateUpdateStateMachine(
713
718
  ctx.collection,
@@ -725,14 +730,14 @@ async function executeUpdate(ctx, id, data) {
725
730
  for (const key of Object.keys(validated)) {
726
731
  const value = validated[key];
727
732
  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);
733
+ if ((0, import_core8.isAtomicOp)(value)) {
734
+ resolvedData[key] = (0, import_core8.resolveAtomicOp)(currentRecord[key], value);
735
+ atomicOps[key] = (0, import_core8.toAtomicOp)(value);
731
736
  } else {
732
737
  resolvedData[key] = value;
733
738
  }
734
739
  }
735
- const writeData = await (0, import_core7.transformSecretFieldsForWrite)(
740
+ const writeData = await (0, import_core8.transformSecretFieldsForWrite)(
736
741
  resolvedData,
737
742
  ctx.definition,
738
743
  ctx.secretKeyProvider
@@ -742,7 +747,7 @@ async function executeUpdate(ctx, id, data) {
742
747
  let operation;
743
748
  await ctx.adapter.transaction(async (tx) => {
744
749
  const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
745
- operation = await (0, import_core7.createOperation)(
750
+ operation = await (0, import_core8.createOperation)(
746
751
  {
747
752
  nodeId: ctx.nodeId,
748
753
  type: "update",
@@ -782,7 +787,7 @@ async function executeUpdate(ctx, id, data) {
782
787
  });
783
788
  ctx.onMutation(ctx.collection, operation);
784
789
  const rows = await ctx.adapter.query(
785
- `SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
790
+ `SELECT * FROM ${(0, import_core8.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
786
791
  [id]
787
792
  );
788
793
  const row = rows[0];
@@ -793,10 +798,10 @@ async function executeUpdate(ctx, id, data) {
793
798
  }
794
799
 
795
800
  // src/mutations/execute-delete.ts
796
- var import_core8 = require("@korajs/core");
801
+ var import_core9 = require("@korajs/core");
797
802
  async function executeDelete(ctx, id, options) {
798
803
  const currentRows = await ctx.adapter.query(
799
- `SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,
804
+ `SELECT * FROM ${(0, import_core9.quoteIdent)(ctx.collection)} WHERE id = ? AND _deleted = 0`,
800
805
  [id]
801
806
  );
802
807
  if (!currentRows[0]) {
@@ -808,7 +813,7 @@ async function executeDelete(ctx, id, options) {
808
813
  await ctx.adapter.transaction(async (tx) => {
809
814
  if (!operation) {
810
815
  const sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId);
811
- operation = await (0, import_core8.createOperation)(
816
+ operation = await (0, import_core9.createOperation)(
812
817
  {
813
818
  nodeId: ctx.nodeId,
814
819
  type: "delete",