@tanstack/db 0.0.33 → 0.1.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 (58) hide show
  1. package/dist/cjs/collection.cjs +22 -12
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +6 -1
  4. package/dist/cjs/local-only.cjs.map +1 -1
  5. package/dist/cjs/local-only.d.cts +2 -14
  6. package/dist/cjs/local-storage.cjs.map +1 -1
  7. package/dist/cjs/local-storage.d.cts +3 -14
  8. package/dist/cjs/query/compiler/group-by.cjs +10 -10
  9. package/dist/cjs/query/compiler/group-by.cjs.map +1 -1
  10. package/dist/cjs/query/compiler/index.cjs +11 -11
  11. package/dist/cjs/query/compiler/index.cjs.map +1 -1
  12. package/dist/cjs/query/compiler/joins.cjs +8 -8
  13. package/dist/cjs/query/compiler/joins.cjs.map +1 -1
  14. package/dist/cjs/query/compiler/order-by.cjs +2 -2
  15. package/dist/cjs/query/compiler/order-by.cjs.map +1 -1
  16. package/dist/cjs/query/compiler/order-by.d.cts +1 -1
  17. package/dist/cjs/query/compiler/select.cjs +2 -2
  18. package/dist/cjs/query/compiler/select.cjs.map +1 -1
  19. package/dist/cjs/query/live-query-collection.cjs +4 -4
  20. package/dist/cjs/query/live-query-collection.cjs.map +1 -1
  21. package/dist/cjs/transactions.cjs +1 -0
  22. package/dist/cjs/transactions.cjs.map +1 -1
  23. package/dist/cjs/types.d.cts +1 -1
  24. package/dist/esm/collection.d.ts +6 -1
  25. package/dist/esm/collection.js +22 -12
  26. package/dist/esm/collection.js.map +1 -1
  27. package/dist/esm/local-only.d.ts +2 -14
  28. package/dist/esm/local-only.js.map +1 -1
  29. package/dist/esm/local-storage.d.ts +3 -14
  30. package/dist/esm/local-storage.js.map +1 -1
  31. package/dist/esm/query/compiler/group-by.js +1 -1
  32. package/dist/esm/query/compiler/group-by.js.map +1 -1
  33. package/dist/esm/query/compiler/index.js +1 -1
  34. package/dist/esm/query/compiler/index.js.map +1 -1
  35. package/dist/esm/query/compiler/joins.js +1 -1
  36. package/dist/esm/query/compiler/joins.js.map +1 -1
  37. package/dist/esm/query/compiler/order-by.d.ts +1 -1
  38. package/dist/esm/query/compiler/order-by.js +1 -1
  39. package/dist/esm/query/compiler/order-by.js.map +1 -1
  40. package/dist/esm/query/compiler/select.js +1 -1
  41. package/dist/esm/query/compiler/select.js.map +1 -1
  42. package/dist/esm/query/live-query-collection.js +1 -1
  43. package/dist/esm/query/live-query-collection.js.map +1 -1
  44. package/dist/esm/transactions.js +1 -0
  45. package/dist/esm/transactions.js.map +1 -1
  46. package/dist/esm/types.d.ts +1 -1
  47. package/package.json +3 -3
  48. package/src/collection.ts +34 -15
  49. package/src/local-only.ts +6 -1
  50. package/src/local-storage.ts +6 -1
  51. package/src/query/compiler/group-by.ts +1 -1
  52. package/src/query/compiler/index.ts +1 -1
  53. package/src/query/compiler/joins.ts +2 -2
  54. package/src/query/compiler/order-by.ts +2 -2
  55. package/src/query/compiler/select.ts +1 -1
  56. package/src/query/live-query-collection.ts +2 -2
  57. package/src/transactions.ts +1 -0
  58. package/src/types.ts +1 -1
@@ -289,6 +289,11 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
289
289
  * This method processes operations from pending transactions and applies them to the synced data
290
290
  */
291
291
  commitPendingTransactions: () => void;
292
+ /**
293
+ * Schedule cleanup of a transaction when it completes
294
+ * @private
295
+ */
296
+ private scheduleTransactionCleanup;
292
297
  private ensureStandardSchema;
293
298
  getKeyFromItem(item: T): TKey;
294
299
  generateGlobalKey(key: any, item: any): string;
@@ -343,7 +348,7 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>,
343
348
  */
344
349
  private updateIndexes;
345
350
  private deepEqual;
346
- private validateData;
351
+ validateData(data: unknown, type: `insert` | `update`, key?: TKey): T | never;
347
352
  /**
348
353
  * Inserts one or more items into the collection
349
354
  * @param items - Single item or array of items to insert
@@ -252,6 +252,7 @@ class CollectionImpl {
252
252
  if (ambientTransaction) {
253
253
  ambientTransaction.applyMutations(mutations);
254
254
  this.transactions.set(ambientTransaction.id, ambientTransaction);
255
+ this.scheduleTransactionCleanup(ambientTransaction);
255
256
  this.recomputeOptimisticState(true);
256
257
  return ambientTransaction;
257
258
  } else {
@@ -266,6 +267,7 @@ class CollectionImpl {
266
267
  directOpTransaction.applyMutations(mutations);
267
268
  directOpTransaction.commit();
268
269
  this.transactions.set(directOpTransaction.id, directOpTransaction);
270
+ this.scheduleTransactionCleanup(directOpTransaction);
269
271
  this.recomputeOptimisticState(true);
270
272
  return directOpTransaction;
271
273
  }
@@ -306,6 +308,7 @@ class CollectionImpl {
306
308
  if (ambientTransaction) {
307
309
  ambientTransaction.applyMutations(mutations);
308
310
  this.transactions.set(ambientTransaction.id, ambientTransaction);
311
+ this.scheduleTransactionCleanup(ambientTransaction);
309
312
  this.recomputeOptimisticState(true);
310
313
  return ambientTransaction;
311
314
  }
@@ -321,6 +324,7 @@ class CollectionImpl {
321
324
  directOpTransaction.applyMutations(mutations);
322
325
  directOpTransaction.commit();
323
326
  this.transactions.set(directOpTransaction.id, directOpTransaction);
327
+ this.scheduleTransactionCleanup(directOpTransaction);
324
328
  this.recomputeOptimisticState(true);
325
329
  return directOpTransaction;
326
330
  };
@@ -661,11 +665,8 @@ class CollectionImpl {
661
665
  this.optimisticUpserts.clear();
662
666
  this.optimisticDeletes.clear();
663
667
  const activeTransactions = [];
664
- const completedTransactions = [];
665
668
  for (const transaction of this.transactions.values()) {
666
- if (transaction.state === `completed`) {
667
- completedTransactions.push(transaction);
668
- } else if (![`completed`, `failed`].includes(transaction.state)) {
669
+ if (![`completed`, `failed`].includes(transaction.state)) {
669
670
  activeTransactions.push(transaction);
670
671
  }
671
672
  }
@@ -700,19 +701,11 @@ class CollectionImpl {
700
701
  });
701
702
  if (this.pendingSyncedTransactions.length > 0 && !triggeredByUserAction) {
702
703
  const pendingSyncKeys = /* @__PURE__ */ new Set();
703
- const completedTransactionMutations = /* @__PURE__ */ new Set();
704
704
  for (const transaction of this.pendingSyncedTransactions) {
705
705
  for (const operation of transaction.operations) {
706
706
  pendingSyncKeys.add(operation.key);
707
707
  }
708
708
  }
709
- for (const tx of completedTransactions) {
710
- for (const mutation of tx.mutations) {
711
- if (mutation.collection === this) {
712
- completedTransactionMutations.add(mutation.mutationId);
713
- }
714
- }
715
- }
716
709
  const filteredEvents = filteredEventsBySyncStatus.filter((event) => {
717
710
  if (event.type === `delete` && pendingSyncKeys.has(event.key)) {
718
711
  const hasActiveOptimisticMutation = activeTransactions.some(
@@ -933,6 +926,20 @@ class CollectionImpl {
933
926
  }
934
927
  return result;
935
928
  }
929
+ /**
930
+ * Schedule cleanup of a transaction when it completes
931
+ * @private
932
+ */
933
+ scheduleTransactionCleanup(transaction) {
934
+ if (transaction.state === `completed`) {
935
+ this.transactions.delete(transaction.id);
936
+ return;
937
+ }
938
+ transaction.isPersisted.promise.then(() => {
939
+ this.transactions.delete(transaction.id);
940
+ }).catch(() => {
941
+ });
942
+ }
936
943
  ensureStandardSchema(schema) {
937
944
  if (schema && `~standard` in schema) {
938
945
  return schema;
@@ -1213,11 +1220,13 @@ class CollectionImpl {
1213
1220
  }
1214
1221
  });
1215
1222
  emptyTransaction.commit();
1223
+ this.scheduleTransactionCleanup(emptyTransaction);
1216
1224
  return emptyTransaction;
1217
1225
  }
1218
1226
  if (ambientTransaction) {
1219
1227
  ambientTransaction.applyMutations(mutations);
1220
1228
  this.transactions.set(ambientTransaction.id, ambientTransaction);
1229
+ this.scheduleTransactionCleanup(ambientTransaction);
1221
1230
  this.recomputeOptimisticState(true);
1222
1231
  return ambientTransaction;
1223
1232
  }
@@ -1232,6 +1241,7 @@ class CollectionImpl {
1232
1241
  directOpTransaction.applyMutations(mutations);
1233
1242
  directOpTransaction.commit();
1234
1243
  this.transactions.set(directOpTransaction.id, directOpTransaction);
1244
+ this.scheduleTransactionCleanup(directOpTransaction);
1235
1245
  this.recomputeOptimisticState(true);
1236
1246
  return directOpTransaction;
1237
1247
  }