@anfenn/dync 1.1.1 → 1.2.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.
Files changed (40) hide show
  1. package/README.md +8 -4
  2. package/dist/capacitor.d.cts +1 -1
  3. package/dist/capacitor.d.ts +1 -1
  4. package/dist/{dexie-D85rTx4g.d.ts → dexie-Cxn4kUoF.d.ts} +2 -7
  5. package/dist/{dexie-3VOQSn1s.d.cts → dexie-D8u9cGSy.d.cts} +2 -7
  6. package/dist/dexie.cjs +11 -0
  7. package/dist/dexie.cjs.map +1 -1
  8. package/dist/dexie.d.cts +2 -2
  9. package/dist/dexie.d.ts +2 -2
  10. package/dist/dexie.js +11 -0
  11. package/dist/dexie.js.map +1 -1
  12. package/dist/expoSqlite.d.cts +1 -1
  13. package/dist/expoSqlite.d.ts +1 -1
  14. package/dist/index.cjs +190 -24
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +6 -6
  17. package/dist/index.d.ts +6 -6
  18. package/dist/index.js +190 -24
  19. package/dist/index.js.map +1 -1
  20. package/dist/node.d.cts +1 -1
  21. package/dist/node.d.ts +1 -1
  22. package/dist/react/index.d.cts +3 -3
  23. package/dist/react/index.d.ts +3 -3
  24. package/dist/{types-BIJhsSOf.d.ts → types-Da3ZhO9Y.d.cts} +3 -3
  25. package/dist/{types-6-NyRQ0D.d.cts → types-DcEg2pvl.d.cts} +1 -1
  26. package/dist/{types-6-NyRQ0D.d.ts → types-DcEg2pvl.d.ts} +1 -1
  27. package/dist/{types-DhCTdAep.d.cts → types-DnHiaBZV.d.ts} +3 -3
  28. package/dist/wa-sqlite.d.cts +1 -1
  29. package/dist/wa-sqlite.d.ts +1 -1
  30. package/package.json +5 -3
  31. package/src/core/SyncAwareCollection.ts +124 -0
  32. package/src/core/SyncAwareWhereClause.ts +66 -0
  33. package/src/core/pushOperations.ts +14 -7
  34. package/src/core/tableEnhancers.ts +18 -0
  35. package/src/index.shared.ts +7 -7
  36. package/src/storage/dexie/DexieAdapter.ts +13 -2
  37. package/src/storage/sqlite/SQLiteAdapter.ts +2 -7
  38. package/src/storage/sqlite/SQLiteQueryContext.ts +1 -22
  39. package/src/storage/sqlite/types.ts +1 -1
  40. package/src/types.ts +9 -3
package/dist/index.cjs CHANGED
@@ -360,6 +360,174 @@ function cloneRecord(record) {
360
360
  return { ...record };
361
361
  }
362
362
 
363
+ // src/core/SyncAwareWhereClause.ts
364
+ var SyncAwareWhereClause = class {
365
+ constructor(inner, tableRef) {
366
+ this.inner = inner;
367
+ this.tableRef = tableRef;
368
+ }
369
+ wrap(col) {
370
+ return new SyncAwareCollection(col, this.tableRef);
371
+ }
372
+ equals(value) {
373
+ return this.wrap(this.inner.equals(value));
374
+ }
375
+ above(value) {
376
+ return this.wrap(this.inner.above(value));
377
+ }
378
+ aboveOrEqual(value) {
379
+ return this.wrap(this.inner.aboveOrEqual(value));
380
+ }
381
+ below(value) {
382
+ return this.wrap(this.inner.below(value));
383
+ }
384
+ belowOrEqual(value) {
385
+ return this.wrap(this.inner.belowOrEqual(value));
386
+ }
387
+ between(lower, upper, includeLower, includeUpper) {
388
+ return this.wrap(this.inner.between(lower, upper, includeLower, includeUpper));
389
+ }
390
+ inAnyRange(ranges, options) {
391
+ return this.wrap(this.inner.inAnyRange(ranges, options));
392
+ }
393
+ startsWith(prefix) {
394
+ return this.wrap(this.inner.startsWith(prefix));
395
+ }
396
+ startsWithIgnoreCase(prefix) {
397
+ return this.wrap(this.inner.startsWithIgnoreCase(prefix));
398
+ }
399
+ startsWithAnyOf(...args) {
400
+ return this.wrap(this.inner.startsWithAnyOf(...args));
401
+ }
402
+ startsWithAnyOfIgnoreCase(...args) {
403
+ return this.wrap(this.inner.startsWithAnyOfIgnoreCase(...args));
404
+ }
405
+ equalsIgnoreCase(value) {
406
+ return this.wrap(this.inner.equalsIgnoreCase(value));
407
+ }
408
+ anyOf(...args) {
409
+ return this.wrap(this.inner.anyOf(...args));
410
+ }
411
+ anyOfIgnoreCase(...args) {
412
+ return this.wrap(this.inner.anyOfIgnoreCase(...args));
413
+ }
414
+ noneOf(...args) {
415
+ return this.wrap(this.inner.noneOf(...args));
416
+ }
417
+ notEqual(value) {
418
+ return this.wrap(this.inner.notEqual(value));
419
+ }
420
+ };
421
+
422
+ // src/core/SyncAwareCollection.ts
423
+ var SyncAwareCollection = class _SyncAwareCollection {
424
+ constructor(inner, tableRef) {
425
+ this.inner = inner;
426
+ this.tableRef = tableRef;
427
+ }
428
+ wrap(col) {
429
+ return new _SyncAwareCollection(col, this.tableRef);
430
+ }
431
+ // ---- read-only / pass-through ----
432
+ first() {
433
+ return this.inner.first();
434
+ }
435
+ last() {
436
+ return this.inner.last();
437
+ }
438
+ each(cb) {
439
+ return this.inner.each(cb);
440
+ }
441
+ eachKey(cb) {
442
+ return this.inner.eachKey(cb);
443
+ }
444
+ eachPrimaryKey(cb) {
445
+ return this.inner.eachPrimaryKey(cb);
446
+ }
447
+ eachUniqueKey(cb) {
448
+ return this.inner.eachUniqueKey(cb);
449
+ }
450
+ keys() {
451
+ return this.inner.keys();
452
+ }
453
+ primaryKeys() {
454
+ return this.inner.primaryKeys();
455
+ }
456
+ uniqueKeys() {
457
+ return this.inner.uniqueKeys();
458
+ }
459
+ count() {
460
+ return this.inner.count();
461
+ }
462
+ sortBy(key) {
463
+ return this.inner.sortBy(key);
464
+ }
465
+ toArray() {
466
+ return this.inner.toArray();
467
+ }
468
+ // ---- chaining — preserve sync-awareness ----
469
+ distinct() {
470
+ return this.wrap(this.inner.distinct());
471
+ }
472
+ clone(props) {
473
+ return this.wrap(this.inner.clone(props));
474
+ }
475
+ reverse() {
476
+ return this.wrap(this.inner.reverse());
477
+ }
478
+ offset(n) {
479
+ return this.wrap(this.inner.offset(n));
480
+ }
481
+ limit(n) {
482
+ return this.wrap(this.inner.limit(n));
483
+ }
484
+ toCollection() {
485
+ return this.wrap(this.inner.toCollection());
486
+ }
487
+ jsFilter(predicate) {
488
+ return this.wrap(this.inner.jsFilter(predicate));
489
+ }
490
+ or(index) {
491
+ return new SyncAwareWhereClause(this.inner.or(index), this.tableRef);
492
+ }
493
+ // ---- sync-aware mutations ----
494
+ async delete() {
495
+ const records = await this.inner.toArray();
496
+ if (records.length === 0) return 0;
497
+ const keys = records.map((r) => r._localId);
498
+ await this.tableRef.bulkDelete(keys);
499
+ return records.length;
500
+ }
501
+ async modify(changes) {
502
+ const records = await this.inner.toArray();
503
+ if (records.length === 0) return 0;
504
+ if (typeof changes === "function") {
505
+ const keysAndChanges = [];
506
+ for (const record of records) {
507
+ const draft = { ...record };
508
+ await changes(draft);
509
+ const delta = {};
510
+ const allKeys = /* @__PURE__ */ new Set([...Object.keys(record), ...Object.keys(draft)]);
511
+ for (const key of allKeys) {
512
+ if (draft[key] !== record[key]) {
513
+ delta[key] = draft[key];
514
+ }
515
+ }
516
+ if (Object.keys(delta).length > 0) {
517
+ keysAndChanges.push({ key: record._localId, changes: delta });
518
+ }
519
+ }
520
+ if (keysAndChanges.length > 0) {
521
+ await this.tableRef.bulkUpdate(keysAndChanges);
522
+ }
523
+ } else {
524
+ const keysAndChanges = records.map((r) => ({ key: r._localId, changes }));
525
+ await this.tableRef.bulkUpdate(keysAndChanges);
526
+ }
527
+ return records.length;
528
+ }
529
+ };
530
+
363
531
  // src/core/tableEnhancers.ts
364
532
  function wrapWithMutationEmitter(table, tableName, emitMutation) {
365
533
  const rawAdd = table.raw.add;
@@ -730,6 +898,18 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
730
898
  table.bulkUpdate = wrappedBulkUpdate;
731
899
  table.bulkDelete = wrappedBulkDelete;
732
900
  table.clear = wrappedClear;
901
+ const originalWhere = table.where.bind(table);
902
+ const originalOrderBy = table.orderBy.bind(table);
903
+ const originalReverse = table.reverse.bind(table);
904
+ const originalOffset = table.offset.bind(table);
905
+ const originalLimit = table.limit.bind(table);
906
+ const originalJsFilter = table.jsFilter.bind(table);
907
+ table.where = (index) => new SyncAwareWhereClause(originalWhere(index), table);
908
+ table.orderBy = (index) => new SyncAwareCollection(originalOrderBy(index), table);
909
+ table.reverse = () => new SyncAwareCollection(originalReverse(), table);
910
+ table.offset = (n) => new SyncAwareCollection(originalOffset(n), table);
911
+ table.limit = (n) => new SyncAwareCollection(originalLimit(n), table);
912
+ table.jsFilter = (predicate) => new SyncAwareCollection(originalJsFilter(predicate), table);
733
913
  enhancedTables.add(tableName);
734
914
  }
735
915
 
@@ -1097,7 +1277,11 @@ async function processBatchPushResult(change, result, ctx) {
1097
1277
  const { action, tableName, localId } = change;
1098
1278
  if (!result.success) {
1099
1279
  if (action === "update" /* Update */) {
1100
- await handleMissingRemoteRecord(change, ctx);
1280
+ if (!result.error) {
1281
+ await handleMissingRemoteRecord(change, ctx);
1282
+ } else {
1283
+ ctx.logger.warn(`[dync] push:batch:update-failed tableName=${tableName} localId=${localId} error=${result.error}`);
1284
+ }
1101
1285
  } else {
1102
1286
  ctx.logger.warn(`[dync] push:batch:failed tableName=${tableName} localId=${localId} error=${result.error}`);
1103
1287
  }
@@ -1105,10 +1289,10 @@ async function processBatchPushResult(change, result, ctx) {
1105
1289
  }
1106
1290
  switch (action) {
1107
1291
  case "remove" /* Remove */:
1108
- handleRemoveSuccess(change, ctx);
1292
+ await handleRemoveSuccess(change, ctx);
1109
1293
  break;
1110
1294
  case "update" /* Update */:
1111
- handleUpdateSuccess(change, ctx);
1295
+ await handleUpdateSuccess(change, ctx);
1112
1296
  break;
1113
1297
  case "create" /* Create */: {
1114
1298
  const serverResult = { id: result.id };
@@ -2556,22 +2740,9 @@ var normalizeComparableValue = (value) => {
2556
2740
 
2557
2741
  // src/storage/sqlite/SQLiteQueryContext.ts
2558
2742
  var SQLiteQueryContext = class {
2559
- constructor(driver, adapter) {
2560
- this.driver = driver;
2743
+ constructor(adapter) {
2561
2744
  this.adapter = adapter;
2562
2745
  }
2563
- table(name) {
2564
- return this.adapter.table(name);
2565
- }
2566
- transaction(mode, tableNames, callback) {
2567
- return this.adapter.transaction(mode, tableNames, callback);
2568
- }
2569
- async execute(statement) {
2570
- return this.driver.execute(statement);
2571
- }
2572
- async run(statement, values) {
2573
- return this.driver.run(statement, values);
2574
- }
2575
2746
  async queryRows(statement, values) {
2576
2747
  return this.adapter.queryRows(statement, values);
2577
2748
  }
@@ -2740,8 +2911,7 @@ var SQLiteAdapter2 = class {
2740
2911
  }
2741
2912
  async query(arg1, arg2) {
2742
2913
  if (typeof arg1 === "function") {
2743
- const driver2 = await this.getDriver();
2744
- return arg1(new SQLiteQueryContext(driver2, this));
2914
+ return arg1(new SQLiteQueryContext(this));
2745
2915
  }
2746
2916
  const statement = arg1;
2747
2917
  const values = arg2;
@@ -2821,12 +2991,8 @@ var SQLiteAdapter2 = class {
2821
2991
  if (!debug) {
2822
2992
  return;
2823
2993
  }
2824
- const hasParams = parameters && parameters.length;
2825
- if (typeof debug === "function") {
2826
- debug(statement, hasParams ? parameters : void 0);
2827
- return;
2828
- }
2829
2994
  if (debug === true) {
2995
+ const hasParams = parameters && parameters.length;
2830
2996
  if (hasParams) {
2831
2997
  console.debug("[dync][sqlite]", statement, parameters);
2832
2998
  } else {