@tanstack/db 0.0.7 → 0.0.9

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 (43) hide show
  1. package/dist/cjs/collection.cjs +441 -284
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +103 -30
  4. package/dist/cjs/proxy.cjs +2 -2
  5. package/dist/cjs/proxy.cjs.map +1 -1
  6. package/dist/cjs/query/compiled-query.cjs +23 -37
  7. package/dist/cjs/query/compiled-query.cjs.map +1 -1
  8. package/dist/cjs/query/compiled-query.d.cts +2 -2
  9. package/dist/cjs/query/order-by.cjs +41 -38
  10. package/dist/cjs/query/order-by.cjs.map +1 -1
  11. package/dist/cjs/query/query-builder.cjs.map +1 -1
  12. package/dist/cjs/query/query-builder.d.cts +2 -2
  13. package/dist/cjs/query/schema.d.cts +5 -4
  14. package/dist/cjs/transactions.cjs +7 -6
  15. package/dist/cjs/transactions.cjs.map +1 -1
  16. package/dist/cjs/transactions.d.cts +9 -9
  17. package/dist/cjs/types.d.cts +28 -22
  18. package/dist/esm/collection.d.ts +103 -30
  19. package/dist/esm/collection.js +442 -285
  20. package/dist/esm/collection.js.map +1 -1
  21. package/dist/esm/proxy.js +2 -2
  22. package/dist/esm/proxy.js.map +1 -1
  23. package/dist/esm/query/compiled-query.d.ts +2 -2
  24. package/dist/esm/query/compiled-query.js +23 -37
  25. package/dist/esm/query/compiled-query.js.map +1 -1
  26. package/dist/esm/query/order-by.js +41 -38
  27. package/dist/esm/query/order-by.js.map +1 -1
  28. package/dist/esm/query/query-builder.d.ts +2 -2
  29. package/dist/esm/query/query-builder.js.map +1 -1
  30. package/dist/esm/query/schema.d.ts +5 -4
  31. package/dist/esm/transactions.d.ts +9 -9
  32. package/dist/esm/transactions.js +7 -6
  33. package/dist/esm/transactions.js.map +1 -1
  34. package/dist/esm/types.d.ts +28 -22
  35. package/package.json +2 -2
  36. package/src/collection.ts +624 -372
  37. package/src/proxy.ts +2 -2
  38. package/src/query/compiled-query.ts +26 -37
  39. package/src/query/order-by.ts +69 -67
  40. package/src/query/query-builder.ts +4 -3
  41. package/src/query/schema.ts +13 -3
  42. package/src/transactions.ts +24 -22
  43. package/src/types.ts +44 -22
@@ -1,27 +1,28 @@
1
- import { Derived, Store } from '@tanstack/store';
1
+ import { Store } from '@tanstack/store';
2
2
  import { Transaction } from './transactions.js';
3
3
  import { SortedMap } from './SortedMap.js';
4
- import { ChangeMessage, CollectionConfig, Fn, InsertConfig, OperationConfig, OptimisticChangeMessage, Transaction as TransactionType, UtilsRecord } from './types.js';
5
- export declare const collectionsStore: Store<Map<string, CollectionImpl<any>>, (cb: Map<string, CollectionImpl<any>>) => Map<string, CollectionImpl<any>>>;
4
+ import { ChangeListener, ChangeMessage, CollectionConfig, Fn, InsertConfig, OperationConfig, Transaction as TransactionType, UtilsRecord } from './types.js';
5
+ export declare const collectionsStore: Map<string, CollectionImpl<any, any>>;
6
6
  /**
7
7
  * Enhanced Collection interface that includes both data type T and utilities TUtils
8
8
  * @template T - The type of items in the collection
9
9
  * @template TUtils - The utilities record type
10
10
  */
11
- export interface Collection<T extends object = Record<string, unknown>, TUtils extends UtilsRecord = {}> extends CollectionImpl<T> {
11
+ export interface Collection<T extends object = Record<string, unknown>, TKey extends string | number = string | number, TUtils extends UtilsRecord = {}> extends CollectionImpl<T, TKey> {
12
12
  readonly utils: TUtils;
13
13
  }
14
14
  /**
15
15
  * Creates a new Collection instance with the given configuration
16
16
  *
17
17
  * @template T - The type of items in the collection
18
+ * @template TKey - The type of the key for the collection
18
19
  * @template TUtils - The utilities record type
19
20
  * @param options - Collection options with optional utilities
20
21
  * @returns A new Collection with utilities exposed both at top level and under .utils
21
22
  */
22
- export declare function createCollection<T extends object = Record<string, unknown>, TUtils extends UtilsRecord = {}>(options: CollectionConfig<T> & {
23
+ export declare function createCollection<T extends object = Record<string, unknown>, TKey extends string | number = string | number, TUtils extends UtilsRecord = {}>(options: CollectionConfig<T, TKey> & {
23
24
  utils?: TUtils;
24
- }): Collection<T, TUtils>;
25
+ }): Collection<T, TKey, TUtils>;
25
26
  /**
26
27
  * Preloads a collection with the given configuration
27
28
  * Returns a promise that resolves once the sync tool has done its first commit (initial sync is finished)
@@ -48,7 +49,7 @@ export declare function createCollection<T extends object = Record<string, unkno
48
49
  * @param config - Configuration for the collection, including id and sync
49
50
  * @returns Promise that resolves when the initial sync is finished
50
51
  */
51
- export declare function preloadCollection<T extends object = Record<string, unknown>>(config: CollectionConfig<T>): Promise<CollectionImpl<T>>;
52
+ export declare function preloadCollection<T extends object = Record<string, unknown>, TKey extends string | number = string | number>(config: CollectionConfig<T, TKey>): Promise<CollectionImpl<T, TKey>>;
52
53
  /**
53
54
  * Custom error class for schema validation errors
54
55
  */
@@ -63,22 +64,19 @@ export declare class SchemaValidationError extends Error {
63
64
  path?: ReadonlyArray<string | number | symbol>;
64
65
  }>, message?: string);
65
66
  }
66
- export declare class CollectionImpl<T extends object = Record<string, unknown>> {
67
- /**
68
- * Utilities namespace
69
- * This is populated by createCollection
70
- */
67
+ export declare class CollectionImpl<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
68
+ transactions: SortedMap<string, Transaction<any>>;
69
+ syncedData: Map<TKey, T>;
70
+ syncedMetadata: Map<TKey, unknown>;
71
+ derivedUpserts: Map<TKey, T>;
72
+ derivedDeletes: Set<TKey>;
73
+ private _size;
74
+ private changeListeners;
75
+ private changeKeyListeners;
71
76
  utils: Record<string, Fn>;
72
- transactions: Store<SortedMap<string, TransactionType>>;
73
- optimisticOperations: Derived<Array<OptimisticChangeMessage<T>>>;
74
- derivedState: Derived<Map<string, T>>;
75
- derivedArray: Derived<Array<T>>;
76
- derivedChanges: Derived<Array<ChangeMessage<T>>>;
77
- syncedData: Store<Map<string, T>, (cb: Map<string, T>) => Map<string, T>>;
78
- syncedMetadata: Store<Map<string, unknown>, (cb: Map<string, unknown>) => Map<string, unknown>>;
79
77
  private pendingSyncedTransactions;
80
78
  private syncedKeys;
81
- config: CollectionConfig<T>;
79
+ config: CollectionConfig<T, TKey>;
82
80
  private hasReceivedFirstCommit;
83
81
  private onFirstCommitCallbacks;
84
82
  /**
@@ -94,15 +92,59 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
94
92
  * @param config - Configuration object for the collection
95
93
  * @throws Error if sync config is missing
96
94
  */
97
- constructor(config: CollectionConfig<T>);
95
+ constructor(config: CollectionConfig<T, TKey>);
96
+ /**
97
+ * Recompute optimistic state from active transactions
98
+ */
99
+ private recomputeOptimisticState;
100
+ /**
101
+ * Calculate the current size based on synced data and optimistic changes
102
+ */
103
+ private calculateSize;
104
+ /**
105
+ * Collect events for optimistic changes
106
+ */
107
+ private collectOptimisticChanges;
108
+ /**
109
+ * Get the previous value for a key given previous optimistic state
110
+ */
111
+ private getPreviousValue;
112
+ /**
113
+ * Emit multiple events at once to all listeners
114
+ */
115
+ private emitEvents;
116
+ /**
117
+ * Get the current value for a key (virtual derived state)
118
+ */
119
+ get(key: TKey): T | undefined;
120
+ /**
121
+ * Check if a key exists in the collection (virtual derived state)
122
+ */
123
+ has(key: TKey): boolean;
124
+ /**
125
+ * Get the current size of the collection (cached)
126
+ */
127
+ get size(): number;
128
+ /**
129
+ * Get all keys (virtual derived state)
130
+ */
131
+ keys(): IterableIterator<TKey>;
132
+ /**
133
+ * Get all values (virtual derived state)
134
+ */
135
+ values(): IterableIterator<T>;
136
+ /**
137
+ * Get all entries (virtual derived state)
138
+ */
139
+ entries(): IterableIterator<[TKey, T]>;
98
140
  /**
99
141
  * Attempts to commit pending synced transactions if there are no active transactions
100
142
  * This method processes operations from pending transactions and applies them to the synced data
101
143
  */
102
144
  commitPendingTransactions: () => void;
103
145
  private ensureStandardSchema;
104
- private getKeyFromId;
105
- generateObjectKey(id: any, item: any): string;
146
+ getKeyFromItem(item: T): TKey;
147
+ generateGlobalKey(key: any, item: any): string;
106
148
  private validateData;
107
149
  /**
108
150
  * Inserts one or more items into the collection
@@ -123,7 +165,7 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
123
165
  * // Insert with custom key
124
166
  * insert({ text: "Buy groceries" }, { key: "grocery-task" })
125
167
  */
126
- insert: (data: T | Array<T>, config?: InsertConfig) => Transaction;
168
+ insert: (data: T | Array<T>, config?: InsertConfig) => Transaction<Record<string, unknown>>;
127
169
  /**
128
170
  * Updates one or more items in the collection using a callback function
129
171
  * @param items - Single item/key or array of items/keys to update
@@ -162,8 +204,10 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
162
204
  * // Update with metadata
163
205
  * update("todo-1", { metadata: { reason: "user update" } }, (draft) => { draft.text = "Updated text" })
164
206
  */
165
- update<TItem extends object = T>(id: unknown, configOrCallback: ((draft: TItem) => void) | OperationConfig, maybeCallback?: (draft: TItem) => void): TransactionType;
166
- update<TItem extends object = T>(ids: Array<unknown>, configOrCallback: ((draft: Array<TItem>) => void) | OperationConfig, maybeCallback?: (draft: Array<TItem>) => void): TransactionType;
207
+ update<TItem extends object = T>(key: Array<TKey | unknown>, callback: (drafts: Array<TItem>) => void): TransactionType;
208
+ update<TItem extends object = T>(keys: Array<TKey | unknown>, config: OperationConfig, callback: (drafts: Array<TItem>) => void): TransactionType;
209
+ update<TItem extends object = T>(id: TKey | unknown, callback: (draft: TItem) => void): TransactionType;
210
+ update<TItem extends object = T>(id: TKey | unknown, config: OperationConfig, callback: (draft: TItem) => void): TransactionType;
167
211
  /**
168
212
  * Deletes one or more items from the collection
169
213
  * @param ids - Single ID or array of IDs to delete
@@ -179,20 +223,20 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
179
223
  * // Delete with metadata
180
224
  * delete("todo-1", { metadata: { reason: "completed" } })
181
225
  */
182
- delete: (ids: Array<string> | string, config?: OperationConfig) => TransactionType;
226
+ delete: (keys: Array<TKey> | TKey, config?: OperationConfig) => TransactionType<any>;
183
227
  /**
184
228
  * Gets the current state of the collection as a Map
185
229
  *
186
230
  * @returns A Map containing all items in the collection, with keys as identifiers
187
231
  */
188
- get state(): Map<string, T>;
232
+ get state(): Map<TKey, T>;
189
233
  /**
190
234
  * Gets the current state of the collection as a Map, but only resolves when data is available
191
235
  * Waits for the first sync commit to complete before resolving
192
236
  *
193
237
  * @returns Promise that resolves to a Map containing all items in the collection
194
238
  */
195
- stateWhenReady(): Promise<Map<string, T>>;
239
+ stateWhenReady(): Promise<Map<TKey, T>>;
196
240
  /**
197
241
  * Gets the current state of the collection as an Array
198
242
  *
@@ -216,5 +260,34 @@ export declare class CollectionImpl<T extends object = Record<string, unknown>>
216
260
  * @param callback - A function that will be called with the changes in the collection
217
261
  * @returns A function that can be called to unsubscribe from the changes
218
262
  */
219
- subscribeChanges(callback: (changes: Array<ChangeMessage<T>>) => void): () => void;
263
+ subscribeChanges(callback: (changes: Array<ChangeMessage<T>>) => void, { includeInitialState }?: {
264
+ includeInitialState?: boolean;
265
+ }): () => void;
266
+ /**
267
+ * Subscribe to changes for a specific key
268
+ */
269
+ subscribeChangesKey(key: TKey, listener: ChangeListener<T, TKey>, { includeInitialState }?: {
270
+ includeInitialState?: boolean;
271
+ }): () => void;
272
+ /**
273
+ * Trigger a recomputation when transactions change
274
+ * This method should be called by the Transaction class when state changes
275
+ */
276
+ onTransactionStateChange(): void;
277
+ private _storeMap;
278
+ /**
279
+ * Returns a Tanstack Store Map that is updated when the collection changes
280
+ * This is a temporary solution to enable the existing framework hooks to work
281
+ * with the new internals of Collection until they are rewritten.
282
+ * TODO: Remove this once the framework hooks are rewritten.
283
+ */
284
+ asStoreMap(): Store<Map<TKey, T>>;
285
+ private _storeArray;
286
+ /**
287
+ * Returns a Tanstack Store Array that is updated when the collection changes
288
+ * This is a temporary solution to enable the existing framework hooks to work
289
+ * with the new internals of Collection until they are rewritten.
290
+ * TODO: Remove this once the framework hooks are rewritten.
291
+ */
292
+ asStoreArray(): Store<Array<T>>;
220
293
  }