@tanstack/db 0.0.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 (154) hide show
  1. package/README.md +37 -0
  2. package/dist/cjs/SortedMap.cjs +140 -0
  3. package/dist/cjs/SortedMap.cjs.map +1 -0
  4. package/dist/cjs/SortedMap.d.cts +91 -0
  5. package/dist/cjs/collection.cjs +597 -0
  6. package/dist/cjs/collection.cjs.map +1 -0
  7. package/dist/cjs/collection.d.cts +176 -0
  8. package/dist/cjs/deferred.cjs +25 -0
  9. package/dist/cjs/deferred.cjs.map +1 -0
  10. package/dist/cjs/deferred.d.cts +20 -0
  11. package/dist/cjs/errors.cjs +10 -0
  12. package/dist/cjs/errors.cjs.map +1 -0
  13. package/dist/cjs/errors.d.cts +3 -0
  14. package/dist/cjs/index.cjs +33 -0
  15. package/dist/cjs/index.cjs.map +1 -0
  16. package/dist/cjs/index.d.cts +9 -0
  17. package/dist/cjs/proxy.cjs +654 -0
  18. package/dist/cjs/proxy.cjs.map +1 -0
  19. package/dist/cjs/proxy.d.cts +59 -0
  20. package/dist/cjs/query/compiled-query.cjs +162 -0
  21. package/dist/cjs/query/compiled-query.cjs.map +1 -0
  22. package/dist/cjs/query/compiled-query.d.cts +22 -0
  23. package/dist/cjs/query/evaluators.cjs +146 -0
  24. package/dist/cjs/query/evaluators.cjs.map +1 -0
  25. package/dist/cjs/query/evaluators.d.cts +9 -0
  26. package/dist/cjs/query/extractors.cjs +122 -0
  27. package/dist/cjs/query/extractors.cjs.map +1 -0
  28. package/dist/cjs/query/extractors.d.cts +22 -0
  29. package/dist/cjs/query/functions.cjs +152 -0
  30. package/dist/cjs/query/functions.cjs.map +1 -0
  31. package/dist/cjs/query/functions.d.cts +21 -0
  32. package/dist/cjs/query/group-by.cjs +91 -0
  33. package/dist/cjs/query/group-by.cjs.map +1 -0
  34. package/dist/cjs/query/group-by.d.cts +40 -0
  35. package/dist/cjs/query/index.d.cts +5 -0
  36. package/dist/cjs/query/joins.cjs +155 -0
  37. package/dist/cjs/query/joins.cjs.map +1 -0
  38. package/dist/cjs/query/joins.d.cts +14 -0
  39. package/dist/cjs/query/key-by.cjs +43 -0
  40. package/dist/cjs/query/key-by.cjs.map +1 -0
  41. package/dist/cjs/query/key-by.d.cts +3 -0
  42. package/dist/cjs/query/order-by.cjs +229 -0
  43. package/dist/cjs/query/order-by.cjs.map +1 -0
  44. package/dist/cjs/query/order-by.d.cts +3 -0
  45. package/dist/cjs/query/pipeline-compiler.cjs +94 -0
  46. package/dist/cjs/query/pipeline-compiler.cjs.map +1 -0
  47. package/dist/cjs/query/pipeline-compiler.d.cts +9 -0
  48. package/dist/cjs/query/query-builder.cjs +314 -0
  49. package/dist/cjs/query/query-builder.cjs.map +1 -0
  50. package/dist/cjs/query/query-builder.d.cts +219 -0
  51. package/dist/cjs/query/schema.d.cts +98 -0
  52. package/dist/cjs/query/select.cjs +107 -0
  53. package/dist/cjs/query/select.cjs.map +1 -0
  54. package/dist/cjs/query/select.d.cts +3 -0
  55. package/dist/cjs/query/types.d.cts +188 -0
  56. package/dist/cjs/query/utils.cjs +154 -0
  57. package/dist/cjs/query/utils.cjs.map +1 -0
  58. package/dist/cjs/query/utils.d.cts +37 -0
  59. package/dist/cjs/transactions.cjs +137 -0
  60. package/dist/cjs/transactions.cjs.map +1 -0
  61. package/dist/cjs/transactions.d.cts +27 -0
  62. package/dist/cjs/types.d.cts +94 -0
  63. package/dist/cjs/utils.cjs +17 -0
  64. package/dist/cjs/utils.cjs.map +1 -0
  65. package/dist/cjs/utils.d.cts +3 -0
  66. package/dist/esm/SortedMap.d.ts +91 -0
  67. package/dist/esm/SortedMap.js +140 -0
  68. package/dist/esm/SortedMap.js.map +1 -0
  69. package/dist/esm/collection.d.ts +176 -0
  70. package/dist/esm/collection.js +597 -0
  71. package/dist/esm/collection.js.map +1 -0
  72. package/dist/esm/deferred.d.ts +20 -0
  73. package/dist/esm/deferred.js +25 -0
  74. package/dist/esm/deferred.js.map +1 -0
  75. package/dist/esm/errors.d.ts +3 -0
  76. package/dist/esm/errors.js +10 -0
  77. package/dist/esm/errors.js.map +1 -0
  78. package/dist/esm/index.d.ts +9 -0
  79. package/dist/esm/index.js +33 -0
  80. package/dist/esm/index.js.map +1 -0
  81. package/dist/esm/proxy.d.ts +59 -0
  82. package/dist/esm/proxy.js +654 -0
  83. package/dist/esm/proxy.js.map +1 -0
  84. package/dist/esm/query/compiled-query.d.ts +22 -0
  85. package/dist/esm/query/compiled-query.js +162 -0
  86. package/dist/esm/query/compiled-query.js.map +1 -0
  87. package/dist/esm/query/evaluators.d.ts +9 -0
  88. package/dist/esm/query/evaluators.js +146 -0
  89. package/dist/esm/query/evaluators.js.map +1 -0
  90. package/dist/esm/query/extractors.d.ts +22 -0
  91. package/dist/esm/query/extractors.js +122 -0
  92. package/dist/esm/query/extractors.js.map +1 -0
  93. package/dist/esm/query/functions.d.ts +21 -0
  94. package/dist/esm/query/functions.js +152 -0
  95. package/dist/esm/query/functions.js.map +1 -0
  96. package/dist/esm/query/group-by.d.ts +40 -0
  97. package/dist/esm/query/group-by.js +91 -0
  98. package/dist/esm/query/group-by.js.map +1 -0
  99. package/dist/esm/query/index.d.ts +5 -0
  100. package/dist/esm/query/joins.d.ts +14 -0
  101. package/dist/esm/query/joins.js +155 -0
  102. package/dist/esm/query/joins.js.map +1 -0
  103. package/dist/esm/query/key-by.d.ts +3 -0
  104. package/dist/esm/query/key-by.js +43 -0
  105. package/dist/esm/query/key-by.js.map +1 -0
  106. package/dist/esm/query/order-by.d.ts +3 -0
  107. package/dist/esm/query/order-by.js +229 -0
  108. package/dist/esm/query/order-by.js.map +1 -0
  109. package/dist/esm/query/pipeline-compiler.d.ts +9 -0
  110. package/dist/esm/query/pipeline-compiler.js +94 -0
  111. package/dist/esm/query/pipeline-compiler.js.map +1 -0
  112. package/dist/esm/query/query-builder.d.ts +219 -0
  113. package/dist/esm/query/query-builder.js +314 -0
  114. package/dist/esm/query/query-builder.js.map +1 -0
  115. package/dist/esm/query/schema.d.ts +98 -0
  116. package/dist/esm/query/select.d.ts +3 -0
  117. package/dist/esm/query/select.js +107 -0
  118. package/dist/esm/query/select.js.map +1 -0
  119. package/dist/esm/query/types.d.ts +188 -0
  120. package/dist/esm/query/utils.d.ts +37 -0
  121. package/dist/esm/query/utils.js +154 -0
  122. package/dist/esm/query/utils.js.map +1 -0
  123. package/dist/esm/transactions.d.ts +27 -0
  124. package/dist/esm/transactions.js +137 -0
  125. package/dist/esm/transactions.js.map +1 -0
  126. package/dist/esm/types.d.ts +94 -0
  127. package/dist/esm/utils.d.ts +3 -0
  128. package/dist/esm/utils.js +17 -0
  129. package/dist/esm/utils.js.map +1 -0
  130. package/package.json +57 -0
  131. package/src/SortedMap.ts +163 -0
  132. package/src/collection.ts +919 -0
  133. package/src/deferred.ts +47 -0
  134. package/src/errors.ts +6 -0
  135. package/src/index.ts +12 -0
  136. package/src/proxy.ts +1104 -0
  137. package/src/query/compiled-query.ts +193 -0
  138. package/src/query/evaluators.ts +222 -0
  139. package/src/query/extractors.ts +211 -0
  140. package/src/query/functions.ts +297 -0
  141. package/src/query/group-by.ts +137 -0
  142. package/src/query/index.ts +5 -0
  143. package/src/query/joins.ts +247 -0
  144. package/src/query/key-by.ts +61 -0
  145. package/src/query/order-by.ts +312 -0
  146. package/src/query/pipeline-compiler.ts +152 -0
  147. package/src/query/query-builder.ts +898 -0
  148. package/src/query/schema.ts +255 -0
  149. package/src/query/select.ts +173 -0
  150. package/src/query/types.ts +417 -0
  151. package/src/query/utils.ts +245 -0
  152. package/src/transactions.ts +198 -0
  153. package/src/types.ts +125 -0
  154. package/src/utils.ts +15 -0
@@ -0,0 +1,176 @@
1
+ import { Derived, Store } from '@tanstack/store';
2
+ import { SortedMap } from './SortedMap.js';
3
+ import { ChangeMessage, CollectionConfig, InsertConfig, OperationConfig, OptimisticChangeMessage, Transaction } from './types.js';
4
+ export declare const collectionsStore: Store<Map<string, Collection<any>>, (cb: Map<string, Collection<any>>) => Map<string, Collection<any>>>;
5
+ /**
6
+ * Preloads a collection with the given configuration
7
+ * Returns a promise that resolves once the sync tool has done its first commit (initial sync is finished)
8
+ * If the collection has already loaded, it resolves immediately
9
+ *
10
+ * This function is useful in route loaders or similar pre-rendering scenarios where you want
11
+ * to ensure data is available before a route transition completes. It uses the same shared collection
12
+ * instance that will be used by useCollection, ensuring data consistency.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * // In a route loader
17
+ * async function loader({ params }) {
18
+ * await preloadCollection({
19
+ * id: `users-${params.userId}`,
20
+ * sync: { ... },
21
+ * });
22
+ *
23
+ * return null;
24
+ * }
25
+ * ```
26
+ *
27
+ * @template T - The type of items in the collection
28
+ * @param config - Configuration for the collection, including id and sync
29
+ * @returns Promise that resolves when the initial sync is finished
30
+ */
31
+ export declare function preloadCollection<T extends object = Record<string, unknown>>(config: CollectionConfig<T>): Promise<Collection<T>>;
32
+ /**
33
+ * Custom error class for schema validation errors
34
+ */
35
+ export declare class SchemaValidationError extends Error {
36
+ type: `insert` | `update`;
37
+ issues: ReadonlyArray<{
38
+ message: string;
39
+ path?: ReadonlyArray<string | number | symbol>;
40
+ }>;
41
+ constructor(type: `insert` | `update`, issues: ReadonlyArray<{
42
+ message: string;
43
+ path?: ReadonlyArray<string | number | symbol>;
44
+ }>, message?: string);
45
+ }
46
+ export declare class Collection<T extends object = Record<string, unknown>> {
47
+ transactions: Store<SortedMap<string, Transaction>>;
48
+ optimisticOperations: Derived<Array<OptimisticChangeMessage<T>>>;
49
+ derivedState: Derived<Map<string, T>>;
50
+ derivedArray: Derived<Array<T>>;
51
+ derivedChanges: Derived<Array<ChangeMessage<T>>>;
52
+ syncedData: Store<Map<string, T>, (cb: Map<string, T>) => Map<string, T>>;
53
+ syncedMetadata: Store<Map<string, unknown>, (cb: Map<string, unknown>) => Map<string, unknown>>;
54
+ private pendingSyncedTransactions;
55
+ private syncedKeys;
56
+ config: CollectionConfig<T>;
57
+ private hasReceivedFirstCommit;
58
+ objectKeyMap: WeakMap<object, string>;
59
+ private onFirstCommitCallbacks;
60
+ /**
61
+ * Register a callback to be executed on the next commit
62
+ * Useful for preloading collections
63
+ * @param callback Function to call after the next commit
64
+ */
65
+ onFirstCommit(callback: () => void): void;
66
+ id: `${string}-${string}-${string}-${string}-${string}`;
67
+ /**
68
+ * Creates a new Collection instance
69
+ *
70
+ * @param config - Configuration object for the collection
71
+ * @throws Error if sync config is missing
72
+ */
73
+ constructor(config?: CollectionConfig<T>);
74
+ /**
75
+ * Attempts to commit pending synced transactions if there are no active transactions
76
+ * This method processes operations from pending transactions and applies them to the synced data
77
+ */
78
+ commitPendingTransactions: () => void;
79
+ private ensureStandardSchema;
80
+ private validateData;
81
+ private generateKey;
82
+ /**
83
+ * Inserts one or more items into the collection
84
+ * @param items - Single item or array of items to insert
85
+ * @param config - Optional configuration including metadata and custom keys
86
+ * @returns A Transaction object representing the insert operation(s)
87
+ * @throws {SchemaValidationError} If the data fails schema validation
88
+ * @example
89
+ * // Insert a single item
90
+ * insert({ text: "Buy groceries", completed: false })
91
+ *
92
+ * // Insert multiple items
93
+ * insert([
94
+ * { text: "Buy groceries", completed: false },
95
+ * { text: "Walk dog", completed: false }
96
+ * ])
97
+ *
98
+ * // Insert with custom key
99
+ * insert({ text: "Buy groceries" }, { key: "grocery-task" })
100
+ */
101
+ insert: (data: T | Array<T>, config?: InsertConfig) => Transaction;
102
+ /**
103
+ * Updates one or more items in the collection using a callback function
104
+ * @param items - Single item/key or array of items/keys to update
105
+ * @param configOrCallback - Either update configuration or update callback
106
+ * @param maybeCallback - Update callback if config was provided
107
+ * @returns A Transaction object representing the update operation(s)
108
+ * @throws {SchemaValidationError} If the updated data fails schema validation
109
+ * @example
110
+ * // Update a single item
111
+ * update(todo, (draft) => { draft.completed = true })
112
+ *
113
+ * // Update multiple items
114
+ * update([todo1, todo2], (drafts) => {
115
+ * drafts.forEach(draft => { draft.completed = true })
116
+ * })
117
+ *
118
+ * // Update with metadata
119
+ * update(todo, { metadata: { reason: "user update" } }, (draft) => { draft.text = "Updated text" })
120
+ */
121
+ update<TItem extends object = T>(item: TItem, configOrCallback: ((draft: TItem) => void) | OperationConfig, maybeCallback?: (draft: TItem) => void): Transaction;
122
+ update<TItem extends object = T>(items: Array<TItem>, configOrCallback: ((draft: Array<TItem>) => void) | OperationConfig, maybeCallback?: (draft: Array<TItem>) => void): Transaction;
123
+ /**
124
+ * Deletes one or more items from the collection
125
+ * @param items - Single item/key or array of items/keys to delete
126
+ * @param config - Optional configuration including metadata
127
+ * @returns A Transaction object representing the delete operation(s)
128
+ * @example
129
+ * // Delete a single item
130
+ * delete(todo)
131
+ *
132
+ * // Delete multiple items
133
+ * delete([todo1, todo2])
134
+ *
135
+ * // Delete with metadata
136
+ * delete(todo, { metadata: { reason: "completed" } })
137
+ */
138
+ delete: (items: Array<T | string> | T | string, config?: OperationConfig) => Transaction;
139
+ /**
140
+ * Gets the current state of the collection as a Map
141
+ *
142
+ * @returns A Map containing all items in the collection, with keys as identifiers
143
+ */
144
+ get state(): Map<string, T>;
145
+ /**
146
+ * Gets the current state of the collection as a Map, but only resolves when data is available
147
+ * Waits for the first sync commit to complete before resolving
148
+ *
149
+ * @returns Promise that resolves to a Map containing all items in the collection
150
+ */
151
+ stateWhenReady(): Promise<Map<string, T>>;
152
+ /**
153
+ * Gets the current state of the collection as an Array
154
+ *
155
+ * @returns An Array containing all items in the collection
156
+ */
157
+ get toArray(): T[];
158
+ /**
159
+ * Gets the current state of the collection as an Array, but only resolves when data is available
160
+ * Waits for the first sync commit to complete before resolving
161
+ *
162
+ * @returns Promise that resolves to an Array containing all items in the collection
163
+ */
164
+ toArrayWhenReady(): Promise<Array<T>>;
165
+ /**
166
+ * Returns the current state of the collection as an array of changes
167
+ * @returns An array of changes
168
+ */
169
+ currentStateAsChanges(): Array<ChangeMessage<T>>;
170
+ /**
171
+ * Subscribe to changes in the collection
172
+ * @param callback - A function that will be called with the changes in the collection
173
+ * @returns A function that can be called to unsubscribe from the changes
174
+ */
175
+ subscribeChanges(callback: (changes: Array<ChangeMessage<T>>) => void): () => void;
176
+ }