@anfenn/dync 1.0.1 → 1.0.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.
@@ -152,7 +152,7 @@ var StateManager = class {
152
152
  }
153
153
  addPendingChange(change) {
154
154
  const next = clonePersistedState(this.persistedState);
155
- const queueItem = next.pendingChanges.find((p) => p.localId === change.localId && p.stateKey === change.stateKey);
155
+ const queueItem = next.pendingChanges.find((p) => p.localId === change.localId && p.tableName === change.tableName);
156
156
  const omittedChanges = omitFields(change.changes, LOCAL_ONLY_SYNC_FIELDS);
157
157
  const omittedBefore = omitFields(change.before, LOCAL_ONLY_SYNC_FIELDS);
158
158
  const omittedAfter = omitFields(change.after, LOCAL_ONLY_SYNC_FIELDS);
@@ -173,7 +173,7 @@ var StateManager = class {
173
173
  next.pendingChanges = [...next.pendingChanges];
174
174
  next.pendingChanges.push({
175
175
  action,
176
- stateKey: change.stateKey,
176
+ tableName: change.tableName,
177
177
  localId: change.localId,
178
178
  id: change.id,
179
179
  version: 1,
@@ -185,18 +185,18 @@ var StateManager = class {
185
185
  this.persistedState = next;
186
186
  return this.persist();
187
187
  }
188
- samePendingVersion(stateKey, localId, version) {
189
- return this.persistedState.pendingChanges.find((p) => p.localId === localId && p.stateKey === stateKey)?.version === version;
188
+ samePendingVersion(tableName, localId, version) {
189
+ return this.persistedState.pendingChanges.find((p) => p.localId === localId && p.tableName === tableName)?.version === version;
190
190
  }
191
- removePendingChange(localId, stateKey) {
191
+ removePendingChange(localId, tableName) {
192
192
  const next = clonePersistedState(this.persistedState);
193
- next.pendingChanges = next.pendingChanges.filter((p) => !(p.localId === localId && p.stateKey === stateKey));
193
+ next.pendingChanges = next.pendingChanges.filter((p) => !(p.localId === localId && p.tableName === tableName));
194
194
  this.persistedState = next;
195
195
  return this.persist();
196
196
  }
197
- updatePendingChange(stateKey, localId, action, id) {
197
+ updatePendingChange(tableName, localId, action, id) {
198
198
  const next = clonePersistedState(this.persistedState);
199
- const changeItem = next.pendingChanges.find((p) => p.stateKey === stateKey && p.localId === localId);
199
+ const changeItem = next.pendingChanges.find((p) => p.tableName === tableName && p.localId === localId);
200
200
  if (changeItem) {
201
201
  changeItem.action = action;
202
202
  if (id) changeItem.id = id;
@@ -205,9 +205,9 @@ var StateManager = class {
205
205
  }
206
206
  return Promise.resolve();
207
207
  }
208
- setPendingChangeBefore(stateKey, localId, before) {
208
+ setPendingChangeBefore(tableName, localId, before) {
209
209
  const next = clonePersistedState(this.persistedState);
210
- const changeItem = next.pendingChanges.find((p) => p.stateKey === stateKey && p.localId === localId);
210
+ const changeItem = next.pendingChanges.find((p) => p.tableName === tableName && p.localId === localId);
211
211
  if (changeItem) {
212
212
  changeItem.before = { ...changeItem.before ?? {}, ...before };
213
213
  this.persistedState = next;
@@ -278,7 +278,7 @@ function cloneConflicts(conflicts) {
278
278
  const next = {};
279
279
  for (const [key, value] of Object.entries(conflicts)) {
280
280
  next[key] = {
281
- stateKey: value.stateKey,
281
+ tableName: value.tableName,
282
282
  fields: value.fields.map((field) => ({ ...field }))
283
283
  };
284
284
  }
@@ -389,7 +389,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
389
389
  result = await rawAdd(syncedItem);
390
390
  await state.addPendingChange({
391
391
  action: "create" /* Create */,
392
- stateKey: tableName,
392
+ tableName,
393
393
  localId,
394
394
  changes: syncedItem,
395
395
  before: null,
@@ -417,7 +417,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
417
417
  result = await rawPut(syncedItem);
418
418
  await state.addPendingChange({
419
419
  action: isUpdate ? "update" /* Update */ : "create" /* Create */,
420
- stateKey: tableName,
420
+ tableName,
421
421
  localId,
422
422
  id: existingRecord?.id,
423
423
  changes: syncedItem,
@@ -444,7 +444,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
444
444
  if (result > 0) {
445
445
  await state.addPendingChange({
446
446
  action: "update" /* Update */,
447
- stateKey: tableName,
447
+ tableName,
448
448
  localId: key,
449
449
  id: record.id,
450
450
  changes: updatedChanges,
@@ -468,7 +468,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
468
468
  deletedLocalId = record._localId;
469
469
  await state.addPendingChange({
470
470
  action: "remove" /* Remove */,
471
- stateKey: tableName,
471
+ tableName,
472
472
  localId: record._localId,
473
473
  id: record.id,
474
474
  changes: null,
@@ -497,7 +497,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
497
497
  for (const syncedItem of syncedItems) {
498
498
  await state.addPendingChange({
499
499
  action: "create" /* Create */,
500
- stateKey: tableName,
500
+ tableName,
501
501
  localId: syncedItem._localId,
502
502
  changes: syncedItem,
503
503
  before: null,
@@ -535,7 +535,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
535
535
  const existing = existingMap.get(syncedItem._localId);
536
536
  await state.addPendingChange({
537
537
  action: existing ? "update" /* Update */ : "create" /* Create */,
538
- stateKey: tableName,
538
+ tableName,
539
539
  localId: syncedItem._localId,
540
540
  id: existing?.id,
541
541
  changes: syncedItem,
@@ -576,7 +576,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
576
576
  updatedKeys.push(record._localId);
577
577
  await state.addPendingChange({
578
578
  action: "update" /* Update */,
579
- stateKey: tableName,
579
+ tableName,
580
580
  localId: record._localId,
581
581
  id: record.id,
582
582
  changes,
@@ -603,7 +603,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
603
603
  deletedLocalIds.push(record._localId);
604
604
  await state.addPendingChange({
605
605
  action: "remove" /* Remove */,
606
- stateKey: tableName,
606
+ tableName,
607
607
  localId: record._localId,
608
608
  id: record.id,
609
609
  changes: null,
@@ -627,7 +627,7 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
627
627
  deletedLocalIds.push(record._localId);
628
628
  await state.addPendingChange({
629
629
  action: "remove" /* Remove */,
630
- stateKey: tableName,
630
+ tableName,
631
631
  localId: record._localId,
632
632
  id: record.id,
633
633
  changes: null,
@@ -656,33 +656,33 @@ function enhanceSyncTable({ table, tableName, withTransaction, state, enhancedTa
656
656
  async function pullAll(ctx) {
657
657
  let firstSyncError;
658
658
  const changedTables = [];
659
- for (const [stateKey, api] of Object.entries(ctx.syncApis)) {
659
+ for (const [tableName, api] of Object.entries(ctx.syncApis)) {
660
660
  try {
661
- const lastPulled = ctx.state.getState().lastPulled[stateKey];
661
+ const lastPulled = ctx.state.getState().lastPulled[tableName];
662
662
  const since = lastPulled ? new Date(lastPulled) : /* @__PURE__ */ new Date(0);
663
- ctx.logger.debug(`[dync] pull:start stateKey=${stateKey} since=${since.toISOString()}`);
663
+ ctx.logger.debug(`[dync] pull:start tableName=${tableName} since=${since.toISOString()}`);
664
664
  const serverData = await api.list(since);
665
- const changed = await processPullData(stateKey, serverData, since, ctx);
666
- if (changed) changedTables.push(stateKey);
665
+ const changed = await processPullData(tableName, serverData, since, ctx);
666
+ if (changed) changedTables.push(tableName);
667
667
  } catch (err) {
668
668
  firstSyncError = firstSyncError ?? err;
669
- ctx.logger.error(`[dync] pull:error stateKey=${stateKey}`, err);
669
+ ctx.logger.error(`[dync] pull:error tableName=${tableName}`, err);
670
670
  }
671
671
  }
672
672
  return { error: firstSyncError, changedTables };
673
673
  }
674
- async function handleRemoteItemUpdate(table, stateKey, localItem, remote, ctx) {
675
- const pendingChange = ctx.state.getState().pendingChanges.find((p) => p.stateKey === stateKey && p.localId === localItem._localId);
674
+ async function handleRemoteItemUpdate(table, tableName, localItem, remote, ctx) {
675
+ const pendingChange = ctx.state.getState().pendingChanges.find((p) => p.tableName === tableName && p.localId === localItem._localId);
676
676
  const conflictStrategy = ctx.conflictResolutionStrategy;
677
677
  if (pendingChange) {
678
- ctx.logger.debug(`[dync] pull:conflict-strategy:${conflictStrategy} stateKey=${stateKey} id=${remote.id}`);
678
+ ctx.logger.debug(`[dync] pull:conflict-strategy:${conflictStrategy} tableName=${tableName} id=${remote.id}`);
679
679
  switch (conflictStrategy) {
680
680
  case "local-wins":
681
681
  break;
682
682
  case "remote-wins": {
683
683
  const merged = { ...remote, _localId: localItem._localId };
684
684
  await table.raw.update(localItem._localId, merged);
685
- await ctx.state.removePendingChange(localItem._localId, stateKey);
685
+ await ctx.state.removePendingChange(localItem._localId, tableName);
686
686
  break;
687
687
  }
688
688
  case "try-shallow-merge": {
@@ -695,7 +695,7 @@ async function handleRemoteItemUpdate(table, stateKey, localItem, remote, ctx) {
695
695
  ...syncState,
696
696
  conflicts: {
697
697
  ...syncState.conflicts || {},
698
- [localItem._localId]: { stateKey, fields }
698
+ [localItem._localId]: { tableName, fields }
699
699
  }
700
700
  }));
701
701
  } else {
@@ -718,7 +718,7 @@ async function handleRemoteItemUpdate(table, stateKey, localItem, remote, ctx) {
718
718
  } else {
719
719
  const merged = { ...localItem, ...remote };
720
720
  await table.raw.update(localItem._localId, merged);
721
- ctx.logger.debug(`[dync] pull:merge-remote stateKey=${stateKey} id=${remote.id}`);
721
+ ctx.logger.debug(`[dync] pull:merge-remote tableName=${tableName} id=${remote.id}`);
722
722
  }
723
723
  }
724
724
  async function pullAllBatch(ctx) {
@@ -732,17 +732,17 @@ async function pullAllBatch(ctx) {
732
732
  }
733
733
  ctx.logger.debug(`[dync] pull:batch:start tables=${[...ctx.batchSync.syncTables].join(",")}`, sinceMap);
734
734
  const serverDataByTable = await ctx.batchSync.pull(sinceMap);
735
- for (const [stateKey, serverData] of Object.entries(serverDataByTable)) {
736
- if (!ctx.batchSync.syncTables.includes(stateKey)) {
737
- ctx.logger.warn(`[dync] pull:batch:unknown-table stateKey=${stateKey}`);
735
+ for (const [tableName, serverData] of Object.entries(serverDataByTable)) {
736
+ if (!ctx.batchSync.syncTables.includes(tableName)) {
737
+ ctx.logger.warn(`[dync] pull:batch:unknown-table tableName=${tableName}`);
738
738
  continue;
739
739
  }
740
740
  try {
741
- const changed = await processPullData(stateKey, serverData, sinceMap[stateKey], ctx);
742
- if (changed) changedTables.push(stateKey);
741
+ const changed = await processPullData(tableName, serverData, sinceMap[tableName], ctx);
742
+ if (changed) changedTables.push(tableName);
743
743
  } catch (err) {
744
744
  firstSyncError = firstSyncError ?? err;
745
- ctx.logger.error(`[dync] pull:batch:error stateKey=${stateKey}`, err);
745
+ ctx.logger.error(`[dync] pull:batch:error tableName=${tableName}`, err);
746
746
  }
747
747
  }
748
748
  } catch (err) {
@@ -751,40 +751,40 @@ async function pullAllBatch(ctx) {
751
751
  }
752
752
  return { error: firstSyncError, changedTables };
753
753
  }
754
- async function processPullData(stateKey, serverData, since, ctx) {
754
+ async function processPullData(tableName, serverData, since, ctx) {
755
755
  if (!serverData?.length) return false;
756
- ctx.logger.debug(`[dync] pull:process stateKey=${stateKey} count=${serverData.length}`);
756
+ ctx.logger.debug(`[dync] pull:process tableName=${tableName} count=${serverData.length}`);
757
757
  let newest = since;
758
758
  let hasChanges = false;
759
- await ctx.withTransaction("rw", [stateKey, DYNC_STATE_TABLE], async (tables) => {
760
- const txTable = tables[stateKey];
759
+ await ctx.withTransaction("rw", [tableName, DYNC_STATE_TABLE], async (tables) => {
760
+ const txTable = tables[tableName];
761
761
  const pendingRemovalById = new Set(
762
- ctx.state.getState().pendingChanges.filter((p) => p.stateKey === stateKey && p.action === "remove" /* Remove */).map((p) => p.id)
762
+ ctx.state.getState().pendingChanges.filter((p) => p.tableName === tableName && p.action === "remove" /* Remove */).map((p) => p.id)
763
763
  );
764
764
  for (const remote of serverData) {
765
765
  const remoteUpdated = new Date(remote.updated_at);
766
766
  if (remoteUpdated > newest) newest = remoteUpdated;
767
767
  if (pendingRemovalById.has(remote.id)) {
768
- ctx.logger.debug(`[dync] pull:skip-pending-remove stateKey=${stateKey} id=${remote.id}`);
768
+ ctx.logger.debug(`[dync] pull:skip-pending-remove tableName=${tableName} id=${remote.id}`);
769
769
  continue;
770
770
  }
771
771
  const localItem = await txTable.where("id").equals(remote.id).first();
772
772
  if (remote.deleted) {
773
773
  if (localItem) {
774
774
  await txTable.raw.delete(localItem._localId);
775
- ctx.logger.debug(`[dync] pull:remove stateKey=${stateKey} id=${remote.id}`);
775
+ ctx.logger.debug(`[dync] pull:remove tableName=${tableName} id=${remote.id}`);
776
776
  hasChanges = true;
777
777
  }
778
778
  continue;
779
779
  }
780
780
  delete remote.deleted;
781
781
  if (localItem) {
782
- await handleRemoteItemUpdate(txTable, stateKey, localItem, remote, ctx);
782
+ await handleRemoteItemUpdate(txTable, tableName, localItem, remote, ctx);
783
783
  hasChanges = true;
784
784
  } else {
785
785
  const newLocalItem = { ...remote, _localId: createLocalId() };
786
786
  await txTable.raw.add(newLocalItem);
787
- ctx.logger.debug(`[dync] pull:add stateKey=${stateKey} id=${remote.id}`);
787
+ ctx.logger.debug(`[dync] pull:add tableName=${tableName} id=${remote.id}`);
788
788
  hasChanges = true;
789
789
  }
790
790
  }
@@ -792,7 +792,7 @@ async function processPullData(stateKey, serverData, since, ctx) {
792
792
  ...syncState,
793
793
  lastPulled: {
794
794
  ...syncState.lastPulled,
795
- [stateKey]: newest.toISOString()
795
+ [tableName]: newest.toISOString()
796
796
  }
797
797
  }));
798
798
  });
@@ -801,35 +801,35 @@ async function processPullData(stateKey, serverData, since, ctx) {
801
801
 
802
802
  // src/core/pushOperations.ts
803
803
  async function handleRemoveSuccess(change, ctx) {
804
- const { stateKey, localId, id } = change;
805
- ctx.logger.debug(`[dync] push:remove:success stateKey=${stateKey} localId=${localId} id=${id}`);
806
- await ctx.state.removePendingChange(localId, stateKey);
804
+ const { tableName, localId, id } = change;
805
+ ctx.logger.debug(`[dync] push:remove:success tableName=${tableName} localId=${localId} id=${id}`);
806
+ await ctx.state.removePendingChange(localId, tableName);
807
807
  }
808
808
  async function handleUpdateSuccess(change, ctx) {
809
- const { stateKey, localId, version, changes } = change;
810
- ctx.logger.debug(`[dync] push:update:success stateKey=${stateKey} localId=${localId} id=${change.id}`);
811
- if (ctx.state.samePendingVersion(stateKey, localId, version)) {
812
- await ctx.state.removePendingChange(localId, stateKey);
809
+ const { tableName, localId, version, changes } = change;
810
+ ctx.logger.debug(`[dync] push:update:success tableName=${tableName} localId=${localId} id=${change.id}`);
811
+ if (ctx.state.samePendingVersion(tableName, localId, version)) {
812
+ await ctx.state.removePendingChange(localId, tableName);
813
813
  } else {
814
- await ctx.state.setPendingChangeBefore(stateKey, localId, changes);
814
+ await ctx.state.setPendingChangeBefore(tableName, localId, changes);
815
815
  }
816
816
  }
817
817
  async function handleCreateSuccess(change, serverResult, ctx) {
818
- const { stateKey, localId, version, changes, id } = change;
819
- ctx.logger.debug(`[dync] push:create:success stateKey=${stateKey} localId=${localId} id=${id ?? serverResult.id}`);
820
- await ctx.withTransaction("rw", [stateKey, DYNC_STATE_TABLE], async (tables) => {
821
- const txTable = tables[stateKey];
818
+ const { tableName, localId, version, changes, id } = change;
819
+ ctx.logger.debug(`[dync] push:create:success tableName=${tableName} localId=${localId} id=${id ?? serverResult.id}`);
820
+ await ctx.withTransaction("rw", [tableName, DYNC_STATE_TABLE], async (tables) => {
821
+ const txTable = tables[tableName];
822
822
  const wasChanged = await txTable.raw.update(localId, serverResult) ?? 0;
823
- if (wasChanged && ctx.state.samePendingVersion(stateKey, localId, version)) {
824
- await ctx.state.removePendingChange(localId, stateKey);
823
+ if (wasChanged && ctx.state.samePendingVersion(tableName, localId, version)) {
824
+ await ctx.state.removePendingChange(localId, tableName);
825
825
  } else {
826
826
  const nextAction = wasChanged ? "update" /* Update */ : "remove" /* Remove */;
827
- await ctx.state.updatePendingChange(stateKey, localId, nextAction, serverResult.id);
827
+ await ctx.state.updatePendingChange(tableName, localId, nextAction, serverResult.id);
828
828
  if (nextAction === "remove" /* Remove */) return;
829
829
  }
830
830
  });
831
831
  const finalItem = { ...changes, ...serverResult, _localId: localId };
832
- ctx.syncOptions.onAfterRemoteAdd?.(stateKey, finalItem);
832
+ ctx.syncOptions.onAfterRemoteAdd?.(tableName, finalItem);
833
833
  }
834
834
  async function pushAll(ctx) {
835
835
  let firstSyncError;
@@ -845,15 +845,15 @@ async function pushAll(ctx) {
845
845
  return firstSyncError;
846
846
  }
847
847
  async function pushOne(change, ctx) {
848
- const api = ctx.syncApis[change.stateKey];
848
+ const api = ctx.syncApis[change.tableName];
849
849
  if (!api) return;
850
- ctx.logger.debug(`[dync] push:attempt action=${change.action} stateKey=${change.stateKey} localId=${change.localId}`);
851
- const { action, stateKey, localId, id, changes, after } = change;
850
+ ctx.logger.debug(`[dync] push:attempt action=${change.action} tableName=${change.tableName} localId=${change.localId}`);
851
+ const { action, tableName, localId, id, changes, after } = change;
852
852
  switch (action) {
853
853
  case "remove" /* Remove */:
854
854
  if (!id) {
855
- ctx.logger.warn(`[dync] push:remove:no-id stateKey=${stateKey} localId=${localId}`);
856
- await ctx.state.removePendingChange(localId, stateKey);
855
+ ctx.logger.warn(`[dync] push:remove:no-id tableName=${tableName} localId=${localId}`);
856
+ await ctx.state.removePendingChange(localId, tableName);
857
857
  return;
858
858
  }
859
859
  await api.remove(id);
@@ -861,7 +861,7 @@ async function pushOne(change, ctx) {
861
861
  break;
862
862
  case "update" /* Update */: {
863
863
  if (ctx.state.hasConflicts(localId)) {
864
- ctx.logger.warn(`[dync] push:update:skipping-with-conflicts stateKey=${stateKey} localId=${localId} id=${id}`);
864
+ ctx.logger.warn(`[dync] push:update:skipping-with-conflicts tableName=${tableName} localId=${localId} id=${id}`);
865
865
  return;
866
866
  }
867
867
  const exists = await api.update(id, changes, after);
@@ -877,9 +877,9 @@ async function pushOne(change, ctx) {
877
877
  if (result) {
878
878
  await handleCreateSuccess(change, result, ctx);
879
879
  } else {
880
- ctx.logger.warn(`[dync] push:create:no-result stateKey=${stateKey} localId=${localId} id=${id}`);
881
- if (ctx.state.samePendingVersion(stateKey, localId, change.version)) {
882
- await ctx.state.removePendingChange(localId, stateKey);
880
+ ctx.logger.warn(`[dync] push:create:no-result tableName=${tableName} localId=${localId} id=${id}`);
881
+ if (ctx.state.samePendingVersion(tableName, localId, change.version)) {
882
+ await ctx.state.removePendingChange(localId, tableName);
883
883
  }
884
884
  }
885
885
  break;
@@ -887,21 +887,21 @@ async function pushOne(change, ctx) {
887
887
  }
888
888
  }
889
889
  async function handleMissingRemoteRecord(change, ctx) {
890
- const { stateKey, localId } = change;
890
+ const { tableName, localId } = change;
891
891
  const strategy = ctx.syncOptions.missingRemoteRecordDuringUpdateStrategy;
892
892
  let localItem;
893
- await ctx.withTransaction("rw", [stateKey, DYNC_STATE_TABLE], async (tables) => {
894
- const txTable = tables[stateKey];
893
+ await ctx.withTransaction("rw", [tableName, DYNC_STATE_TABLE], async (tables) => {
894
+ const txTable = tables[tableName];
895
895
  localItem = await txTable.get(localId);
896
896
  if (!localItem) {
897
- ctx.logger.warn(`[dync] push:missing-remote:no-local-item stateKey=${stateKey} localId=${localId}`);
898
- await ctx.state.removePendingChange(localId, stateKey);
897
+ ctx.logger.warn(`[dync] push:missing-remote:no-local-item tableName=${tableName} localId=${localId}`);
898
+ await ctx.state.removePendingChange(localId, tableName);
899
899
  return;
900
900
  }
901
901
  switch (strategy) {
902
902
  case "delete-local-record":
903
903
  await txTable.raw.delete(localId);
904
- ctx.logger.debug(`[dync] push:missing-remote:${strategy} stateKey=${stateKey} id=${localItem.id}`);
904
+ ctx.logger.debug(`[dync] push:missing-remote:${strategy} tableName=${tableName} id=${localItem.id}`);
905
905
  break;
906
906
  case "insert-remote-record": {
907
907
  const newItem = {
@@ -913,36 +913,36 @@ async function handleMissingRemoteRecord(change, ctx) {
913
913
  await txTable.raw.delete(localId);
914
914
  await ctx.state.addPendingChange({
915
915
  action: "create" /* Create */,
916
- stateKey,
916
+ tableName,
917
917
  localId: newItem._localId,
918
918
  changes: newItem,
919
919
  before: null
920
920
  });
921
- ctx.logger.debug(`[dync] push:missing-remote:${strategy} stateKey=${stateKey} id=${newItem.id}`);
921
+ ctx.logger.debug(`[dync] push:missing-remote:${strategy} tableName=${tableName} id=${newItem.id}`);
922
922
  break;
923
923
  }
924
924
  case "ignore":
925
- ctx.logger.debug(`[dync] push:missing-remote:${strategy} stateKey=${stateKey} id=${localItem.id}`);
925
+ ctx.logger.debug(`[dync] push:missing-remote:${strategy} tableName=${tableName} id=${localItem.id}`);
926
926
  break;
927
927
  default:
928
- ctx.logger.error(`[dync] push:missing-remote:unknown-strategy stateKey=${stateKey} id=${localItem.id} strategy=${strategy}`);
928
+ ctx.logger.error(`[dync] push:missing-remote:unknown-strategy tableName=${tableName} id=${localItem.id} strategy=${strategy}`);
929
929
  break;
930
930
  }
931
- await ctx.state.removePendingChange(localId, stateKey);
931
+ await ctx.state.removePendingChange(localId, tableName);
932
932
  });
933
933
  ctx.syncOptions.onAfterMissingRemoteRecordDuringUpdate?.(strategy, localItem);
934
934
  }
935
935
  async function pushAllBatch(ctx) {
936
936
  let firstSyncError;
937
937
  try {
938
- const changesSnapshot = [...ctx.state.getState().pendingChanges].filter((change) => ctx.batchSync.syncTables.includes(change.stateKey)).sort((a, b) => orderFor(a.action) - orderFor(b.action));
938
+ const changesSnapshot = [...ctx.state.getState().pendingChanges].filter((change) => ctx.batchSync.syncTables.includes(change.tableName)).sort((a, b) => orderFor(a.action) - orderFor(b.action));
939
939
  if (changesSnapshot.length === 0) {
940
940
  ctx.logger.debug("[dync] push:batch:no-changes");
941
941
  return void 0;
942
942
  }
943
943
  const changesToPush = changesSnapshot.filter((change) => {
944
944
  if (change.action === "update" /* Update */ && ctx.state.hasConflicts(change.localId)) {
945
- ctx.logger.warn(`[dync] push:batch:skipping-with-conflicts stateKey=${change.stateKey} localId=${change.localId}`);
945
+ ctx.logger.warn(`[dync] push:batch:skipping-with-conflicts tableName=${change.tableName} localId=${change.localId}`);
946
946
  return false;
947
947
  }
948
948
  return true;
@@ -952,7 +952,7 @@ async function pushAllBatch(ctx) {
952
952
  return void 0;
953
953
  }
954
954
  const payloads = changesToPush.map((change) => ({
955
- table: change.stateKey,
955
+ table: change.tableName,
956
956
  action: change.action === "create" /* Create */ ? "add" : change.action === "update" /* Update */ ? "update" : "remove",
957
957
  localId: change.localId,
958
958
  id: change.id,
@@ -984,12 +984,12 @@ async function pushAllBatch(ctx) {
984
984
  return firstSyncError;
985
985
  }
986
986
  async function processBatchPushResult(change, result, ctx) {
987
- const { action, stateKey, localId } = change;
987
+ const { action, tableName, localId } = change;
988
988
  if (!result.success) {
989
989
  if (action === "update" /* Update */) {
990
990
  await handleMissingRemoteRecord(change, ctx);
991
991
  } else {
992
- ctx.logger.warn(`[dync] push:batch:failed stateKey=${stateKey} localId=${localId} error=${result.error}`);
992
+ ctx.logger.warn(`[dync] push:batch:failed tableName=${tableName} localId=${localId} error=${result.error}`);
993
993
  }
994
994
  return;
995
995
  }
@@ -1021,13 +1021,13 @@ async function startFirstLoad(ctx) {
1021
1021
  return;
1022
1022
  }
1023
1023
  let error;
1024
- for (const [stateKey, api] of Object.entries(ctx.syncApis)) {
1024
+ for (const [tableName, api] of Object.entries(ctx.syncApis)) {
1025
1025
  if (!api.firstLoad) {
1026
- ctx.logger.error(`[dync] firstLoad:no-api-function stateKey=${stateKey}`);
1026
+ ctx.logger.error(`[dync] firstLoad:no-api-function tableName=${tableName}`);
1027
1027
  continue;
1028
1028
  }
1029
1029
  try {
1030
- ctx.logger.info(`[dync] firstLoad:start stateKey=${stateKey}`);
1030
+ ctx.logger.info(`[dync] firstLoad:start tableName=${tableName}`);
1031
1031
  let lastId;
1032
1032
  let isEmptyTable = true;
1033
1033
  let batchCount = 0;
@@ -1037,19 +1037,19 @@ async function startFirstLoad(ctx) {
1037
1037
  const batch = await api.firstLoad(lastId);
1038
1038
  if (!batch?.length) break;
1039
1039
  batchCount++;
1040
- const { inserted, updated } = await processBatchInChunks(ctx, stateKey, batch, isEmptyTable, lastId === void 0);
1040
+ const { inserted, updated } = await processBatchInChunks(ctx, tableName, batch, isEmptyTable, lastId === void 0);
1041
1041
  totalInserted += inserted;
1042
1042
  totalUpdated += updated;
1043
1043
  if (ctx.onProgress) {
1044
1044
  ctx.onProgress({
1045
- table: stateKey,
1045
+ table: tableName,
1046
1046
  inserted: totalInserted,
1047
1047
  updated: totalUpdated,
1048
1048
  total: totalInserted + totalUpdated
1049
1049
  });
1050
1050
  }
1051
1051
  if (lastId === void 0) {
1052
- isEmptyTable = await ctx.table(stateKey).count() === batch.length;
1052
+ isEmptyTable = await ctx.table(tableName).count() === batch.length;
1053
1053
  }
1054
1054
  if (lastId !== void 0 && lastId === batch[batch.length - 1].id) {
1055
1055
  throw new Error(`Duplicate records downloaded, stopping to prevent infinite loop`);
@@ -1059,10 +1059,10 @@ async function startFirstLoad(ctx) {
1059
1059
  await yieldToEventLoop();
1060
1060
  }
1061
1061
  }
1062
- ctx.logger.info(`[dync] firstLoad:done stateKey=${stateKey} inserted=${totalInserted} updated=${totalUpdated}`);
1062
+ ctx.logger.info(`[dync] firstLoad:done tableName=${tableName} inserted=${totalInserted} updated=${totalUpdated}`);
1063
1063
  } catch (err) {
1064
1064
  error = error ?? err;
1065
- ctx.logger.error(`[dync] firstLoad:error stateKey=${stateKey}`, err);
1065
+ ctx.logger.error(`[dync] firstLoad:error tableName=${tableName}`, err);
1066
1066
  }
1067
1067
  }
1068
1068
  await ctx.state.setState((syncState) => ({
@@ -1072,10 +1072,10 @@ async function startFirstLoad(ctx) {
1072
1072
  }));
1073
1073
  ctx.logger.debug("[dync] First load completed");
1074
1074
  }
1075
- async function processBatchInChunks(ctx, stateKey, batch, isEmptyTable, isFirstBatch) {
1076
- let newest = new Date(ctx.state.getState().lastPulled[stateKey] || 0);
1077
- return ctx.withTransaction("rw", [stateKey, DYNC_STATE_TABLE], async (tables) => {
1078
- const txTable = tables[stateKey];
1075
+ async function processBatchInChunks(ctx, tableName, batch, isEmptyTable, isFirstBatch) {
1076
+ let newest = new Date(ctx.state.getState().lastPulled[tableName] || 0);
1077
+ return ctx.withTransaction("rw", [tableName, DYNC_STATE_TABLE], async (tables) => {
1078
+ const txTable = tables[tableName];
1079
1079
  let tableIsEmpty = isEmptyTable;
1080
1080
  if (isFirstBatch) {
1081
1081
  const count = await txTable.count();
@@ -1110,7 +1110,7 @@ async function processBatchInChunks(ctx, stateKey, batch, isEmptyTable, isFirstB
1110
1110
  ...syncState,
1111
1111
  lastPulled: {
1112
1112
  ...syncState.lastPulled,
1113
- [stateKey]: newest.toISOString()
1113
+ [tableName]: newest.toISOString()
1114
1114
  }
1115
1115
  }));
1116
1116
  return { inserted, updated };
@@ -1175,23 +1175,23 @@ async function startFirstLoadBatch(ctx) {
1175
1175
  break;
1176
1176
  }
1177
1177
  batchCount++;
1178
- for (const [stateKey, batch] of Object.entries(result.data)) {
1179
- if (!ctx.batchSync.syncTables.includes(stateKey)) {
1180
- ctx.logger.warn(`[dync] firstLoad:batch:unknown-table stateKey=${stateKey}`);
1178
+ for (const [tableName, batch] of Object.entries(result.data)) {
1179
+ if (!ctx.batchSync.syncTables.includes(tableName)) {
1180
+ ctx.logger.warn(`[dync] firstLoad:batch:unknown-table tableName=${tableName}`);
1181
1181
  continue;
1182
1182
  }
1183
1183
  if (!batch?.length) continue;
1184
- const isFirstBatch = progress[stateKey].inserted === 0 && progress[stateKey].updated === 0;
1185
- const isEmptyTable = isFirstBatch && await ctx.table(stateKey).count() === 0;
1186
- const { inserted, updated } = await processBatchInChunks(ctx, stateKey, batch, isEmptyTable, isFirstBatch);
1187
- progress[stateKey].inserted += inserted;
1188
- progress[stateKey].updated += updated;
1184
+ const isFirstBatch = progress[tableName].inserted === 0 && progress[tableName].updated === 0;
1185
+ const isEmptyTable = isFirstBatch && await ctx.table(tableName).count() === 0;
1186
+ const { inserted, updated } = await processBatchInChunks(ctx, tableName, batch, isEmptyTable, isFirstBatch);
1187
+ progress[tableName].inserted += inserted;
1188
+ progress[tableName].updated += updated;
1189
1189
  if (ctx.onProgress) {
1190
1190
  ctx.onProgress({
1191
- table: stateKey,
1192
- inserted: progress[stateKey].inserted,
1193
- updated: progress[stateKey].updated,
1194
- total: progress[stateKey].inserted + progress[stateKey].updated
1191
+ table: tableName,
1192
+ inserted: progress[tableName].inserted,
1193
+ updated: progress[tableName].updated,
1194
+ total: progress[tableName].inserted + progress[tableName].updated
1195
1195
  });
1196
1196
  }
1197
1197
  }
@@ -1203,8 +1203,8 @@ async function startFirstLoadBatch(ctx) {
1203
1203
  break;
1204
1204
  }
1205
1205
  }
1206
- for (const [stateKey, p] of Object.entries(progress)) {
1207
- ctx.logger.info(`[dync] firstLoad:batch:done stateKey=${stateKey} inserted=${p.inserted} updated=${p.updated}`);
1206
+ for (const [tableName, p] of Object.entries(progress)) {
1207
+ ctx.logger.info(`[dync] firstLoad:batch:done tableName=${tableName} inserted=${p.inserted} updated=${p.updated}`);
1208
1208
  }
1209
1209
  } catch (err) {
1210
1210
  error = err;
@@ -1574,8 +1574,8 @@ var DyncBase = class {
1574
1574
  this.logger.warn(`[dync] No conflict found for localId: ${localId}`);
1575
1575
  return;
1576
1576
  }
1577
- await this.withTransaction("rw", [conflict.stateKey, DYNC_STATE_TABLE], async (tables) => {
1578
- const txTable = tables[conflict.stateKey];
1577
+ await this.withTransaction("rw", [conflict.tableName, DYNC_STATE_TABLE], async (tables) => {
1578
+ const txTable = tables[conflict.tableName];
1579
1579
  if (!keepLocal) {
1580
1580
  const item = await txTable.get(localId);
1581
1581
  if (item) {
@@ -1588,7 +1588,7 @@ var DyncBase = class {
1588
1588
  }
1589
1589
  await this.state.setState((syncState) => ({
1590
1590
  ...syncState,
1591
- pendingChanges: syncState.pendingChanges.filter((p) => !(p.localId === localId && p.stateKey === conflict.stateKey))
1591
+ pendingChanges: syncState.pendingChanges.filter((p) => !(p.localId === localId && p.tableName === conflict.tableName))
1592
1592
  }));
1593
1593
  }
1594
1594
  await this.state.setState((syncState) => {
@@ -3881,4 +3881,4 @@ export {
3881
3881
  SqliteQueryContext,
3882
3882
  SQLiteAdapter2 as SQLiteAdapter
3883
3883
  };
3884
- //# sourceMappingURL=chunk-LGHOZECP.js.map
3884
+ //# sourceMappingURL=chunk-66PSQW4D.js.map