cry-synced-db-client 0.1.71 → 0.1.72

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/index.js CHANGED
@@ -2166,11 +2166,12 @@ class SyncEngine {
2166
2166
  const allUpdatedIds = {};
2167
2167
  for (const [collectionName, config] of configMap) {
2168
2168
  const serverData = allServerData[collectionName] || [];
2169
+ delete allServerData[collectionName];
2169
2170
  receivedCount += serverData.length;
2170
2171
  collectionStats[collectionName] = {
2171
2172
  receivedCount: serverData.length,
2172
2173
  sentCount: 0,
2173
- receivedItems: serverData
2174
+ receivedItems: []
2174
2175
  };
2175
2176
  const stats = await this.processIncomingServerData(collectionName, config, serverData);
2176
2177
  conflictsResolved += stats.conflictsResolved;
@@ -2420,60 +2421,69 @@ class SyncEngine {
2420
2421
  }
2421
2422
  return { updatedIds: result.updatedIds };
2422
2423
  }
2424
+ static SYNC_BATCH_SIZE = 200;
2423
2425
  async processIncomingServerData(collectionName, config, serverData) {
2424
2426
  if (serverData.length === 0) {
2425
2427
  return { conflictsResolved: 0, maxTs: undefined, updatedIds: [] };
2426
2428
  }
2427
2429
  let maxTs;
2428
2430
  let conflictsResolved = 0;
2429
- const serverIds = serverData.map((item) => item._id);
2430
- const localItems = await this.dexieDb.getByIds(collectionName, serverIds);
2431
- const dirtyChangesMap = await this.dexieDb.getDirtyChangesBatch(collectionName, serverIds);
2432
- const dexieBatch = [];
2433
- const inMemSaveBatch = [];
2434
- const inMemDeleteIds = [];
2435
- for (let i = 0;i < serverData.length; i++) {
2436
- const serverItem = serverData[i];
2437
- const localItem = localItems[i];
2438
- const dirtyChange = dirtyChangesMap.get(String(serverItem._id));
2439
- if (serverItem._ts) {
2440
- if (!maxTs || this.compareTimestamps(serverItem._ts, maxTs) > 0) {
2441
- maxTs = serverItem._ts;
2431
+ const allUpdatedIds = [];
2432
+ const BATCH = SyncEngine.SYNC_BATCH_SIZE;
2433
+ for (let offset = 0;offset < serverData.length; offset += BATCH) {
2434
+ const chunk = serverData.slice(offset, offset + BATCH);
2435
+ const chunkIds = chunk.map((item) => item._id);
2436
+ const localItems = await this.dexieDb.getByIds(collectionName, chunkIds);
2437
+ const dirtyChangesMap = await this.dexieDb.getDirtyChangesBatch(collectionName, chunkIds);
2438
+ const dexieBatch = [];
2439
+ const inMemSaveBatch = [];
2440
+ const inMemDeleteIds = [];
2441
+ for (let i = 0;i < chunk.length; i++) {
2442
+ const serverItem = chunk[i];
2443
+ const localItem = localItems[i];
2444
+ const dirtyChange = dirtyChangesMap.get(String(serverItem._id));
2445
+ if (serverItem._ts) {
2446
+ if (!maxTs || this.compareTimestamps(serverItem._ts, maxTs) > 0) {
2447
+ maxTs = serverItem._ts;
2448
+ }
2442
2449
  }
2443
- }
2444
- if (localItem) {
2445
- if (dirtyChange) {
2446
- conflictsResolved++;
2447
- const resolved = this.resolveCollectionConflict(collectionName, config, localItem, serverItem, "sync");
2448
- dexieBatch.push(resolved);
2449
- if (!resolved._deleted && !resolved._archived) {
2450
- inMemSaveBatch.push(resolved);
2450
+ if (localItem) {
2451
+ if (dirtyChange) {
2452
+ conflictsResolved++;
2453
+ const resolved = this.resolveCollectionConflict(collectionName, config, localItem, serverItem, "sync");
2454
+ dexieBatch.push(resolved);
2455
+ if (!resolved._deleted && !resolved._archived) {
2456
+ inMemSaveBatch.push(resolved);
2457
+ } else {
2458
+ inMemDeleteIds.push(serverItem._id);
2459
+ }
2451
2460
  } else {
2452
- inMemDeleteIds.push(serverItem._id);
2461
+ dexieBatch.push(serverItem);
2462
+ if (!serverItem._deleted && !serverItem._archived) {
2463
+ inMemSaveBatch.push(serverItem);
2464
+ } else {
2465
+ inMemDeleteIds.push(serverItem._id);
2466
+ }
2453
2467
  }
2454
2468
  } else {
2455
2469
  dexieBatch.push(serverItem);
2456
2470
  if (!serverItem._deleted && !serverItem._archived) {
2457
2471
  inMemSaveBatch.push(serverItem);
2458
- } else {
2459
- inMemDeleteIds.push(serverItem._id);
2460
2472
  }
2461
2473
  }
2462
- } else {
2463
- dexieBatch.push(serverItem);
2464
- if (!serverItem._deleted && !serverItem._archived) {
2465
- inMemSaveBatch.push(serverItem);
2466
- }
2467
2474
  }
2468
- }
2469
- if (dexieBatch.length > 0) {
2470
- await this.dexieDb.saveMany(collectionName, dexieBatch);
2471
- }
2472
- if (inMemSaveBatch.length > 0) {
2473
- this.deps.writeToInMemBatch(collectionName, inMemSaveBatch, "upsert");
2474
- }
2475
- if (inMemDeleteIds.length > 0) {
2476
- this.deps.writeToInMemBatch(collectionName, inMemDeleteIds.map((id) => ({ _id: id })), "delete");
2475
+ if (dexieBatch.length > 0) {
2476
+ await this.dexieDb.saveMany(collectionName, dexieBatch);
2477
+ }
2478
+ if (inMemSaveBatch.length > 0) {
2479
+ this.deps.writeToInMemBatch(collectionName, inMemSaveBatch, "upsert");
2480
+ }
2481
+ if (inMemDeleteIds.length > 0) {
2482
+ this.deps.writeToInMemBatch(collectionName, inMemDeleteIds.map((id) => ({ _id: id })), "delete");
2483
+ }
2484
+ for (const id of chunkIds) {
2485
+ allUpdatedIds.push(String(id));
2486
+ }
2477
2487
  }
2478
2488
  if (maxTs) {
2479
2489
  await this.dexieDb.setSyncMeta(collectionName, maxTs);
@@ -2483,8 +2493,7 @@ class SyncEngine {
2483
2493
  lastSyncTs: maxTs
2484
2494
  });
2485
2495
  }
2486
- const updatedIds = serverIds.map((id) => String(id));
2487
- return { conflictsResolved, maxTs, updatedIds };
2496
+ return { conflictsResolved, maxTs, updatedIds: allUpdatedIds };
2488
2497
  }
2489
2498
  compareTimestamps(a, b) {
2490
2499
  const aT = typeof a === "object" && "t" in a ? a.t : 0;
@@ -3251,8 +3260,15 @@ class SyncedDb {
3251
3260
  await this.pendingChanges.recoverPendingWrites();
3252
3261
  for (const [name] of this.collections) {
3253
3262
  const data = await this.dexieDb.getAll(name);
3254
- const activeData = data.filter((item) => !item._deleted && !item._archived);
3255
- this.inMemManager.initCollection(name, activeData);
3263
+ let writeIdx = 0;
3264
+ for (let i = 0;i < data.length; i++) {
3265
+ const item = data[i];
3266
+ if (!item._deleted && !item._archived) {
3267
+ data[writeIdx++] = item;
3268
+ }
3269
+ }
3270
+ data.length = writeIdx;
3271
+ this.inMemManager.initCollection(name, data);
3256
3272
  const meta = await this.dexieDb.getSyncMeta(name);
3257
3273
  if (meta) {
3258
3274
  this.syncMetaCache.set(name, meta);
@@ -38,6 +38,8 @@ export declare class SyncEngine implements I_SyncEngine {
38
38
  processCollectionServerData(collectionName: string, serverData: LocalDbEntity[]): Promise<{
39
39
  updatedIds: string[];
40
40
  }>;
41
+ /** Max items to process per batch in processIncomingServerData */
42
+ private static readonly SYNC_BATCH_SIZE;
41
43
  private processIncomingServerData;
42
44
  private compareTimestamps;
43
45
  private resolveCollectionConflict;
@@ -206,7 +206,7 @@ export interface CollectionSyncStats {
206
206
  receivedCount: number;
207
207
  /** Number of dirty items sent to server for this collection */
208
208
  sentCount: number;
209
- /** The actual items received from server (for debugging/logging) */
209
+ /** @deprecated Use receivedCount instead. Will be empty array in future streaming mode. */
210
210
  receivedItems: LocalDbEntity[];
211
211
  }
212
212
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cry-synced-db-client",
3
- "version": "0.1.71",
3
+ "version": "0.1.72",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",