@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.
- package/README.md +37 -0
- package/dist/cjs/SortedMap.cjs +140 -0
- package/dist/cjs/SortedMap.cjs.map +1 -0
- package/dist/cjs/SortedMap.d.cts +91 -0
- package/dist/cjs/collection.cjs +597 -0
- package/dist/cjs/collection.cjs.map +1 -0
- package/dist/cjs/collection.d.cts +176 -0
- package/dist/cjs/deferred.cjs +25 -0
- package/dist/cjs/deferred.cjs.map +1 -0
- package/dist/cjs/deferred.d.cts +20 -0
- package/dist/cjs/errors.cjs +10 -0
- package/dist/cjs/errors.cjs.map +1 -0
- package/dist/cjs/errors.d.cts +3 -0
- package/dist/cjs/index.cjs +33 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +9 -0
- package/dist/cjs/proxy.cjs +654 -0
- package/dist/cjs/proxy.cjs.map +1 -0
- package/dist/cjs/proxy.d.cts +59 -0
- package/dist/cjs/query/compiled-query.cjs +162 -0
- package/dist/cjs/query/compiled-query.cjs.map +1 -0
- package/dist/cjs/query/compiled-query.d.cts +22 -0
- package/dist/cjs/query/evaluators.cjs +146 -0
- package/dist/cjs/query/evaluators.cjs.map +1 -0
- package/dist/cjs/query/evaluators.d.cts +9 -0
- package/dist/cjs/query/extractors.cjs +122 -0
- package/dist/cjs/query/extractors.cjs.map +1 -0
- package/dist/cjs/query/extractors.d.cts +22 -0
- package/dist/cjs/query/functions.cjs +152 -0
- package/dist/cjs/query/functions.cjs.map +1 -0
- package/dist/cjs/query/functions.d.cts +21 -0
- package/dist/cjs/query/group-by.cjs +91 -0
- package/dist/cjs/query/group-by.cjs.map +1 -0
- package/dist/cjs/query/group-by.d.cts +40 -0
- package/dist/cjs/query/index.d.cts +5 -0
- package/dist/cjs/query/joins.cjs +155 -0
- package/dist/cjs/query/joins.cjs.map +1 -0
- package/dist/cjs/query/joins.d.cts +14 -0
- package/dist/cjs/query/key-by.cjs +43 -0
- package/dist/cjs/query/key-by.cjs.map +1 -0
- package/dist/cjs/query/key-by.d.cts +3 -0
- package/dist/cjs/query/order-by.cjs +229 -0
- package/dist/cjs/query/order-by.cjs.map +1 -0
- package/dist/cjs/query/order-by.d.cts +3 -0
- package/dist/cjs/query/pipeline-compiler.cjs +94 -0
- package/dist/cjs/query/pipeline-compiler.cjs.map +1 -0
- package/dist/cjs/query/pipeline-compiler.d.cts +9 -0
- package/dist/cjs/query/query-builder.cjs +314 -0
- package/dist/cjs/query/query-builder.cjs.map +1 -0
- package/dist/cjs/query/query-builder.d.cts +219 -0
- package/dist/cjs/query/schema.d.cts +98 -0
- package/dist/cjs/query/select.cjs +107 -0
- package/dist/cjs/query/select.cjs.map +1 -0
- package/dist/cjs/query/select.d.cts +3 -0
- package/dist/cjs/query/types.d.cts +188 -0
- package/dist/cjs/query/utils.cjs +154 -0
- package/dist/cjs/query/utils.cjs.map +1 -0
- package/dist/cjs/query/utils.d.cts +37 -0
- package/dist/cjs/transactions.cjs +137 -0
- package/dist/cjs/transactions.cjs.map +1 -0
- package/dist/cjs/transactions.d.cts +27 -0
- package/dist/cjs/types.d.cts +94 -0
- package/dist/cjs/utils.cjs +17 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +3 -0
- package/dist/esm/SortedMap.d.ts +91 -0
- package/dist/esm/SortedMap.js +140 -0
- package/dist/esm/SortedMap.js.map +1 -0
- package/dist/esm/collection.d.ts +176 -0
- package/dist/esm/collection.js +597 -0
- package/dist/esm/collection.js.map +1 -0
- package/dist/esm/deferred.d.ts +20 -0
- package/dist/esm/deferred.js +25 -0
- package/dist/esm/deferred.js.map +1 -0
- package/dist/esm/errors.d.ts +3 -0
- package/dist/esm/errors.js +10 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +33 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/proxy.d.ts +59 -0
- package/dist/esm/proxy.js +654 -0
- package/dist/esm/proxy.js.map +1 -0
- package/dist/esm/query/compiled-query.d.ts +22 -0
- package/dist/esm/query/compiled-query.js +162 -0
- package/dist/esm/query/compiled-query.js.map +1 -0
- package/dist/esm/query/evaluators.d.ts +9 -0
- package/dist/esm/query/evaluators.js +146 -0
- package/dist/esm/query/evaluators.js.map +1 -0
- package/dist/esm/query/extractors.d.ts +22 -0
- package/dist/esm/query/extractors.js +122 -0
- package/dist/esm/query/extractors.js.map +1 -0
- package/dist/esm/query/functions.d.ts +21 -0
- package/dist/esm/query/functions.js +152 -0
- package/dist/esm/query/functions.js.map +1 -0
- package/dist/esm/query/group-by.d.ts +40 -0
- package/dist/esm/query/group-by.js +91 -0
- package/dist/esm/query/group-by.js.map +1 -0
- package/dist/esm/query/index.d.ts +5 -0
- package/dist/esm/query/joins.d.ts +14 -0
- package/dist/esm/query/joins.js +155 -0
- package/dist/esm/query/joins.js.map +1 -0
- package/dist/esm/query/key-by.d.ts +3 -0
- package/dist/esm/query/key-by.js +43 -0
- package/dist/esm/query/key-by.js.map +1 -0
- package/dist/esm/query/order-by.d.ts +3 -0
- package/dist/esm/query/order-by.js +229 -0
- package/dist/esm/query/order-by.js.map +1 -0
- package/dist/esm/query/pipeline-compiler.d.ts +9 -0
- package/dist/esm/query/pipeline-compiler.js +94 -0
- package/dist/esm/query/pipeline-compiler.js.map +1 -0
- package/dist/esm/query/query-builder.d.ts +219 -0
- package/dist/esm/query/query-builder.js +314 -0
- package/dist/esm/query/query-builder.js.map +1 -0
- package/dist/esm/query/schema.d.ts +98 -0
- package/dist/esm/query/select.d.ts +3 -0
- package/dist/esm/query/select.js +107 -0
- package/dist/esm/query/select.js.map +1 -0
- package/dist/esm/query/types.d.ts +188 -0
- package/dist/esm/query/utils.d.ts +37 -0
- package/dist/esm/query/utils.js +154 -0
- package/dist/esm/query/utils.js.map +1 -0
- package/dist/esm/transactions.d.ts +27 -0
- package/dist/esm/transactions.js +137 -0
- package/dist/esm/transactions.js.map +1 -0
- package/dist/esm/types.d.ts +94 -0
- package/dist/esm/utils.d.ts +3 -0
- package/dist/esm/utils.js +17 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +57 -0
- package/src/SortedMap.ts +163 -0
- package/src/collection.ts +919 -0
- package/src/deferred.ts +47 -0
- package/src/errors.ts +6 -0
- package/src/index.ts +12 -0
- package/src/proxy.ts +1104 -0
- package/src/query/compiled-query.ts +193 -0
- package/src/query/evaluators.ts +222 -0
- package/src/query/extractors.ts +211 -0
- package/src/query/functions.ts +297 -0
- package/src/query/group-by.ts +137 -0
- package/src/query/index.ts +5 -0
- package/src/query/joins.ts +247 -0
- package/src/query/key-by.ts +61 -0
- package/src/query/order-by.ts +312 -0
- package/src/query/pipeline-compiler.ts +152 -0
- package/src/query/query-builder.ts +898 -0
- package/src/query/schema.ts +255 -0
- package/src/query/select.ts +173 -0
- package/src/query/types.ts +417 -0
- package/src/query/utils.ts +245 -0
- package/src/transactions.ts +198 -0
- package/src/types.ts +125 -0
- 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
|
+
}
|