@tanstack/db 0.2.4 → 0.3.0

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 (36) hide show
  1. package/dist/cjs/collection.cjs +23 -4
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +35 -41
  4. package/dist/cjs/local-only.cjs.map +1 -1
  5. package/dist/cjs/local-only.d.cts +17 -43
  6. package/dist/cjs/local-storage.cjs +3 -12
  7. package/dist/cjs/local-storage.cjs.map +1 -1
  8. package/dist/cjs/local-storage.d.cts +16 -39
  9. package/dist/cjs/query/builder/types.d.cts +3 -10
  10. package/dist/cjs/query/live-query-collection.cjs.map +1 -1
  11. package/dist/cjs/transactions.cjs +76 -5
  12. package/dist/cjs/transactions.cjs.map +1 -1
  13. package/dist/cjs/transactions.d.cts +17 -0
  14. package/dist/cjs/types.d.cts +10 -31
  15. package/dist/esm/collection.d.ts +35 -41
  16. package/dist/esm/collection.js +23 -4
  17. package/dist/esm/collection.js.map +1 -1
  18. package/dist/esm/local-only.d.ts +17 -43
  19. package/dist/esm/local-only.js.map +1 -1
  20. package/dist/esm/local-storage.d.ts +16 -39
  21. package/dist/esm/local-storage.js +3 -12
  22. package/dist/esm/local-storage.js.map +1 -1
  23. package/dist/esm/query/builder/types.d.ts +3 -10
  24. package/dist/esm/query/live-query-collection.js.map +1 -1
  25. package/dist/esm/transactions.d.ts +17 -0
  26. package/dist/esm/transactions.js +76 -5
  27. package/dist/esm/transactions.js.map +1 -1
  28. package/dist/esm/types.d.ts +10 -31
  29. package/package.json +2 -2
  30. package/src/collection.ts +148 -196
  31. package/src/local-only.ts +57 -77
  32. package/src/local-storage.ts +53 -85
  33. package/src/query/builder/types.ts +3 -12
  34. package/src/query/live-query-collection.ts +1 -1
  35. package/src/transactions.ts +121 -6
  36. package/src/types.ts +25 -55
package/src/types.ts CHANGED
@@ -28,51 +28,6 @@ export type InferSchemaInput<T> = T extends StandardSchemaV1
28
28
  : Record<string, unknown>
29
29
  : Record<string, unknown>
30
30
 
31
- /**
32
- * Helper type to determine the insert input type
33
- * This takes the raw generics (TExplicit, TSchema, TFallback) instead of the resolved T.
34
- *
35
- * Priority:
36
- * 1. Explicit generic TExplicit (if not 'unknown')
37
- * 2. Schema input type (if schema provided)
38
- * 3. Fallback type TFallback
39
- *
40
- * @internal This is used for collection insert type inference
41
- */
42
- export type ResolveInsertInput<
43
- TExplicit = unknown,
44
- TSchema extends StandardSchemaV1 = never,
45
- TFallback extends object = Record<string, unknown>,
46
- > = unknown extends TExplicit
47
- ? [TSchema] extends [never]
48
- ? TFallback
49
- : InferSchemaInput<TSchema>
50
- : TExplicit extends object
51
- ? TExplicit
52
- : Record<string, unknown>
53
-
54
- /**
55
- * Helper type to determine the final type based on priority:
56
- * 1. Explicit generic TExplicit (if not 'unknown')
57
- * 2. Schema output type (if schema provided)
58
- * 3. Fallback type TFallback
59
- *
60
- * @remarks
61
- * This type is used internally to resolve the collection item type based on the provided generics and schema.
62
- * Users should not need to use this type directly, but understanding the priority order helps when defining collections.
63
- */
64
- export type ResolveType<
65
- TExplicit,
66
- TSchema extends StandardSchemaV1 = never,
67
- TFallback extends object = Record<string, unknown>,
68
- > = unknown extends TExplicit
69
- ? [TSchema] extends [never]
70
- ? TFallback
71
- : InferSchemaOutput<TSchema>
72
- : TExplicit extends object
73
- ? TExplicit
74
- : Record<string, unknown>
75
-
76
31
  export type TransactionState = `pending` | `persisting` | `completed` | `failed`
77
32
 
78
33
  /**
@@ -304,19 +259,22 @@ export type InsertMutationFn<
304
259
  T extends object = Record<string, unknown>,
305
260
  TKey extends string | number = string | number,
306
261
  TUtils extends UtilsRecord = Record<string, Fn>,
307
- > = (params: InsertMutationFnParams<T, TKey, TUtils>) => Promise<any>
262
+ TReturn = any,
263
+ > = (params: InsertMutationFnParams<T, TKey, TUtils>) => Promise<TReturn>
308
264
 
309
265
  export type UpdateMutationFn<
310
266
  T extends object = Record<string, unknown>,
311
267
  TKey extends string | number = string | number,
312
268
  TUtils extends UtilsRecord = Record<string, Fn>,
313
- > = (params: UpdateMutationFnParams<T, TKey, TUtils>) => Promise<any>
269
+ TReturn = any,
270
+ > = (params: UpdateMutationFnParams<T, TKey, TUtils>) => Promise<TReturn>
314
271
 
315
272
  export type DeleteMutationFn<
316
273
  T extends object = Record<string, unknown>,
317
274
  TKey extends string | number = string | number,
318
275
  TUtils extends UtilsRecord = Record<string, Fn>,
319
- > = (params: DeleteMutationFnParams<T, TKey, TUtils>) => Promise<any>
276
+ TReturn = any,
277
+ > = (params: DeleteMutationFnParams<T, TKey, TUtils>) => Promise<TReturn>
320
278
 
321
279
  /**
322
280
  * Collection status values for lifecycle management
@@ -347,16 +305,20 @@ export type CollectionStatus =
347
305
  /** Collection has been cleaned up and resources freed */
348
306
  | `cleaned-up`
349
307
 
350
- export interface CollectionConfig<
308
+ export interface BaseCollectionConfig<
351
309
  T extends object = Record<string, unknown>,
352
310
  TKey extends string | number = string | number,
353
- TSchema extends StandardSchemaV1 = StandardSchemaV1,
354
- TInsertInput extends object = T,
311
+ // Let TSchema default to `never` such that if a user provides T explicitly and no schema
312
+ // then TSchema will be `never` otherwise if it would default to StandardSchemaV1
313
+ // then it would conflict with the overloads of createCollection which
314
+ // requires either T to be provided or a schema to be provided but not both!
315
+ TSchema extends StandardSchemaV1 = never,
316
+ TUtils extends UtilsRecord = Record<string, Fn>,
317
+ TReturn = any,
355
318
  > {
356
319
  // If an id isn't passed in, a UUID will be
357
320
  // generated for it.
358
321
  id?: string
359
- sync: SyncConfig<T, TKey>
360
322
  schema?: TSchema
361
323
  /**
362
324
  * Function to extract the ID from an object
@@ -439,7 +401,7 @@ export interface CollectionConfig<
439
401
  * })
440
402
  * }
441
403
  */
442
- onInsert?: InsertMutationFn<TInsertInput, TKey>
404
+ onInsert?: InsertMutationFn<T, TKey, TUtils, TReturn>
443
405
 
444
406
  /**
445
407
  * Optional asynchronous handler function called before an update operation
@@ -483,7 +445,7 @@ export interface CollectionConfig<
483
445
  * }
484
446
  * }
485
447
  */
486
- onUpdate?: UpdateMutationFn<T, TKey>
448
+ onUpdate?: UpdateMutationFn<T, TKey, TUtils, TReturn>
487
449
  /**
488
450
  * Optional asynchronous handler function called before a delete operation
489
451
  * @param params Object containing transaction and collection information
@@ -526,7 +488,15 @@ export interface CollectionConfig<
526
488
  * }
527
489
  * }
528
490
  */
529
- onDelete?: DeleteMutationFn<T, TKey>
491
+ onDelete?: DeleteMutationFn<T, TKey, TUtils, TReturn>
492
+ }
493
+
494
+ export interface CollectionConfig<
495
+ T extends object = Record<string, unknown>,
496
+ TKey extends string | number = string | number,
497
+ TSchema extends StandardSchemaV1 = never,
498
+ > extends BaseCollectionConfig<T, TKey, TSchema> {
499
+ sync: SyncConfig<T, TKey>
530
500
  }
531
501
 
532
502
  export type ChangesPayload<T extends object = Record<string, unknown>> = Array<