@tanstack/db 0.0.11 → 0.0.12
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/cjs/collection.cjs +9 -49
- package/dist/cjs/collection.cjs.map +1 -1
- package/dist/cjs/collection.d.cts +29 -30
- package/dist/cjs/index.cjs +0 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/query/compiled-query.cjs +17 -5
- package/dist/cjs/query/compiled-query.cjs.map +1 -1
- package/dist/cjs/types.d.cts +39 -10
- package/dist/esm/collection.d.ts +29 -30
- package/dist/esm/collection.js +10 -50
- package/dist/esm/collection.js.map +1 -1
- package/dist/esm/index.js +1 -2
- package/dist/esm/query/compiled-query.js +17 -5
- package/dist/esm/query/compiled-query.js.map +1 -1
- package/dist/esm/types.d.ts +39 -10
- package/package.json +1 -1
- package/src/collection.ts +66 -121
- package/src/query/compiled-query.ts +43 -7
- package/src/types.ts +77 -8
package/src/types.ts
CHANGED
|
@@ -3,6 +3,39 @@ import type { Collection } from "./collection"
|
|
|
3
3
|
import type { StandardSchemaV1 } from "@standard-schema/spec"
|
|
4
4
|
import type { Transaction } from "./transactions"
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Helper type to extract the output type from a standard schema
|
|
8
|
+
*
|
|
9
|
+
* @internal This is used by the type resolution system
|
|
10
|
+
*/
|
|
11
|
+
export type InferSchemaOutput<T> = T extends StandardSchemaV1
|
|
12
|
+
? StandardSchemaV1.InferOutput<T> extends object
|
|
13
|
+
? StandardSchemaV1.InferOutput<T>
|
|
14
|
+
: Record<string, unknown>
|
|
15
|
+
: Record<string, unknown>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Helper type to determine the final type based on priority:
|
|
19
|
+
* 1. Explicit generic TExplicit (if not 'unknown')
|
|
20
|
+
* 2. Schema output type (if schema provided)
|
|
21
|
+
* 3. Fallback type TFallback
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* This type is used internally to resolve the collection item type based on the provided generics and schema.
|
|
25
|
+
* Users should not need to use this type directly, but understanding the priority order helps when defining collections.
|
|
26
|
+
*/
|
|
27
|
+
export type ResolveType<
|
|
28
|
+
TExplicit,
|
|
29
|
+
TSchema extends StandardSchemaV1 = never,
|
|
30
|
+
TFallback extends object = Record<string, unknown>,
|
|
31
|
+
> = unknown extends TExplicit
|
|
32
|
+
? [TSchema] extends [never]
|
|
33
|
+
? TFallback
|
|
34
|
+
: InferSchemaOutput<TSchema>
|
|
35
|
+
: TExplicit extends object
|
|
36
|
+
? TExplicit
|
|
37
|
+
: Record<string, unknown>
|
|
38
|
+
|
|
6
39
|
export type TransactionState = `pending` | `persisting` | `completed` | `failed`
|
|
7
40
|
|
|
8
41
|
/**
|
|
@@ -19,11 +52,18 @@ export type UtilsRecord = Record<string, Fn>
|
|
|
19
52
|
* Represents a pending mutation within a transaction
|
|
20
53
|
* Contains information about the original and modified data, as well as metadata
|
|
21
54
|
*/
|
|
22
|
-
export interface PendingMutation<
|
|
55
|
+
export interface PendingMutation<
|
|
56
|
+
T extends object = Record<string, unknown>,
|
|
57
|
+
TOperation extends OperationType = OperationType,
|
|
58
|
+
> {
|
|
23
59
|
mutationId: string
|
|
24
|
-
original:
|
|
60
|
+
original: TOperation extends `insert` ? {} : T
|
|
25
61
|
modified: T
|
|
26
|
-
changes:
|
|
62
|
+
changes: TOperation extends `insert`
|
|
63
|
+
? T
|
|
64
|
+
: TOperation extends `delete`
|
|
65
|
+
? T
|
|
66
|
+
: Partial<T>
|
|
27
67
|
globalKey: string
|
|
28
68
|
key: any
|
|
29
69
|
type: OperationType
|
|
@@ -56,8 +96,9 @@ export type NonEmptyArray<T> = [T, ...Array<T>]
|
|
|
56
96
|
*/
|
|
57
97
|
export type TransactionWithMutations<
|
|
58
98
|
T extends object = Record<string, unknown>,
|
|
99
|
+
TOperation extends OperationType = OperationType,
|
|
59
100
|
> = Transaction<T> & {
|
|
60
|
-
mutations: NonEmptyArray<PendingMutation<T>>
|
|
101
|
+
mutations: NonEmptyArray<PendingMutation<T, TOperation>>
|
|
61
102
|
}
|
|
62
103
|
|
|
63
104
|
export interface TransactionConfig<T extends object = Record<string, unknown>> {
|
|
@@ -148,15 +189,43 @@ export interface InsertConfig {
|
|
|
148
189
|
metadata?: Record<string, unknown>
|
|
149
190
|
}
|
|
150
191
|
|
|
192
|
+
export type UpdateMutationFnParams<T extends object = Record<string, unknown>> =
|
|
193
|
+
{
|
|
194
|
+
transaction: TransactionWithMutations<T, `update`>
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export type InsertMutationFnParams<T extends object = Record<string, unknown>> =
|
|
198
|
+
{
|
|
199
|
+
transaction: TransactionWithMutations<T, `insert`>
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export type DeleteMutationFnParams<T extends object = Record<string, unknown>> =
|
|
203
|
+
{
|
|
204
|
+
transaction: TransactionWithMutations<T, `delete`>
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export type InsertMutationFn<T extends object = Record<string, unknown>> = (
|
|
208
|
+
params: InsertMutationFnParams<T>
|
|
209
|
+
) => Promise<any>
|
|
210
|
+
|
|
211
|
+
export type UpdateMutationFn<T extends object = Record<string, unknown>> = (
|
|
212
|
+
params: UpdateMutationFnParams<T>
|
|
213
|
+
) => Promise<any>
|
|
214
|
+
|
|
215
|
+
export type DeleteMutationFn<T extends object = Record<string, unknown>> = (
|
|
216
|
+
params: DeleteMutationFnParams<T>
|
|
217
|
+
) => Promise<any>
|
|
218
|
+
|
|
151
219
|
export interface CollectionConfig<
|
|
152
220
|
T extends object = Record<string, unknown>,
|
|
153
221
|
TKey extends string | number = string | number,
|
|
222
|
+
TSchema extends StandardSchemaV1 = StandardSchemaV1,
|
|
154
223
|
> {
|
|
155
224
|
// If an id isn't passed in, a UUID will be
|
|
156
225
|
// generated for it.
|
|
157
226
|
id?: string
|
|
158
227
|
sync: SyncConfig<T, TKey>
|
|
159
|
-
schema?:
|
|
228
|
+
schema?: TSchema
|
|
160
229
|
/**
|
|
161
230
|
* Function to extract the ID from an object
|
|
162
231
|
* This is required for update/delete operations which now only accept IDs
|
|
@@ -172,19 +241,19 @@ export interface CollectionConfig<
|
|
|
172
241
|
* @param params Object containing transaction and mutation information
|
|
173
242
|
* @returns Promise resolving to any value
|
|
174
243
|
*/
|
|
175
|
-
onInsert?:
|
|
244
|
+
onInsert?: InsertMutationFn<T>
|
|
176
245
|
/**
|
|
177
246
|
* Optional asynchronous handler function called before an update operation
|
|
178
247
|
* @param params Object containing transaction and mutation information
|
|
179
248
|
* @returns Promise resolving to any value
|
|
180
249
|
*/
|
|
181
|
-
onUpdate?:
|
|
250
|
+
onUpdate?: UpdateMutationFn<T>
|
|
182
251
|
/**
|
|
183
252
|
* Optional asynchronous handler function called before a delete operation
|
|
184
253
|
* @param params Object containing transaction and mutation information
|
|
185
254
|
* @returns Promise resolving to any value
|
|
186
255
|
*/
|
|
187
|
-
onDelete?:
|
|
256
|
+
onDelete?: DeleteMutationFn<T>
|
|
188
257
|
}
|
|
189
258
|
|
|
190
259
|
export type ChangesPayload<T extends object = Record<string, unknown>> = Array<
|