@tanstack/db 0.0.10 → 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.
Files changed (59) hide show
  1. package/dist/cjs/collection.cjs +9 -49
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +29 -30
  4. package/dist/cjs/index.cjs +0 -1
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/query/compiled-query.cjs +55 -62
  7. package/dist/cjs/query/compiled-query.cjs.map +1 -1
  8. package/dist/cjs/query/compiled-query.d.cts +0 -4
  9. package/dist/cjs/query/group-by.cjs +3 -3
  10. package/dist/cjs/query/group-by.cjs.map +1 -1
  11. package/dist/cjs/query/group-by.d.cts +1 -1
  12. package/dist/cjs/query/joins.cjs +16 -16
  13. package/dist/cjs/query/joins.cjs.map +1 -1
  14. package/dist/cjs/query/joins.d.cts +1 -1
  15. package/dist/cjs/query/order-by.cjs +6 -6
  16. package/dist/cjs/query/order-by.cjs.map +1 -1
  17. package/dist/cjs/query/pipeline-compiler.cjs +5 -5
  18. package/dist/cjs/query/pipeline-compiler.cjs.map +1 -1
  19. package/dist/cjs/query/pipeline-compiler.d.cts +1 -1
  20. package/dist/cjs/query/select.cjs +2 -2
  21. package/dist/cjs/query/select.cjs.map +1 -1
  22. package/dist/cjs/transactions.cjs +5 -12
  23. package/dist/cjs/transactions.cjs.map +1 -1
  24. package/dist/cjs/transactions.d.cts +1 -1
  25. package/dist/cjs/types.d.cts +40 -11
  26. package/dist/esm/collection.d.ts +29 -30
  27. package/dist/esm/collection.js +10 -50
  28. package/dist/esm/collection.js.map +1 -1
  29. package/dist/esm/index.js +1 -2
  30. package/dist/esm/query/compiled-query.d.ts +0 -4
  31. package/dist/esm/query/compiled-query.js +55 -62
  32. package/dist/esm/query/compiled-query.js.map +1 -1
  33. package/dist/esm/query/group-by.d.ts +1 -1
  34. package/dist/esm/query/group-by.js +1 -1
  35. package/dist/esm/query/group-by.js.map +1 -1
  36. package/dist/esm/query/joins.d.ts +1 -1
  37. package/dist/esm/query/joins.js +1 -1
  38. package/dist/esm/query/joins.js.map +1 -1
  39. package/dist/esm/query/order-by.js +1 -1
  40. package/dist/esm/query/order-by.js.map +1 -1
  41. package/dist/esm/query/pipeline-compiler.d.ts +1 -1
  42. package/dist/esm/query/pipeline-compiler.js +1 -1
  43. package/dist/esm/query/pipeline-compiler.js.map +1 -1
  44. package/dist/esm/query/select.js +1 -1
  45. package/dist/esm/query/select.js.map +1 -1
  46. package/dist/esm/transactions.d.ts +1 -1
  47. package/dist/esm/transactions.js +5 -12
  48. package/dist/esm/transactions.js.map +1 -1
  49. package/dist/esm/types.d.ts +40 -11
  50. package/package.json +2 -2
  51. package/src/collection.ts +66 -121
  52. package/src/query/compiled-query.ts +85 -71
  53. package/src/query/group-by.ts +1 -1
  54. package/src/query/joins.ts +2 -2
  55. package/src/query/order-by.ts +1 -1
  56. package/src/query/pipeline-compiler.ts +2 -2
  57. package/src/query/select.ts +1 -1
  58. package/src/transactions.ts +8 -20
  59. package/src/types.ts +78 -9
package/src/types.ts CHANGED
@@ -1,8 +1,41 @@
1
- import type { IStreamBuilder } from "@electric-sql/d2ts"
1
+ import type { IStreamBuilder } from "@electric-sql/d2mini"
2
2
  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<T extends object = Record<string, unknown>> {
55
+ export interface PendingMutation<
56
+ T extends object = Record<string, unknown>,
57
+ TOperation extends OperationType = OperationType,
58
+ > {
23
59
  mutationId: string
24
- original: Partial<T>
60
+ original: TOperation extends `insert` ? {} : T
25
61
  modified: T
26
- changes: Partial<T>
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?: StandardSchema<T>
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?: MutationFn<T>
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?: MutationFn<T>
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?: MutationFn<T>
256
+ onDelete?: DeleteMutationFn<T>
188
257
  }
189
258
 
190
259
  export type ChangesPayload<T extends object = Record<string, unknown>> = Array<