@tanstack/db 0.0.4 → 0.0.6

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 (102) hide show
  1. package/dist/cjs/collection.cjs +182 -113
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +43 -15
  4. package/dist/cjs/index.cjs +1 -0
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/proxy.cjs +87 -248
  7. package/dist/cjs/proxy.cjs.map +1 -1
  8. package/dist/cjs/proxy.d.cts +5 -5
  9. package/dist/cjs/query/compiled-query.cjs +23 -14
  10. package/dist/cjs/query/compiled-query.cjs.map +1 -1
  11. package/dist/cjs/query/compiled-query.d.cts +3 -1
  12. package/dist/cjs/query/evaluators.cjs +35 -20
  13. package/dist/cjs/query/evaluators.cjs.map +1 -1
  14. package/dist/cjs/query/evaluators.d.cts +8 -3
  15. package/dist/cjs/query/extractors.cjs +20 -20
  16. package/dist/cjs/query/extractors.cjs.map +1 -1
  17. package/dist/cjs/query/extractors.d.cts +3 -3
  18. package/dist/cjs/query/group-by.cjs +12 -15
  19. package/dist/cjs/query/group-by.cjs.map +1 -1
  20. package/dist/cjs/query/group-by.d.cts +7 -7
  21. package/dist/cjs/query/joins.cjs +41 -55
  22. package/dist/cjs/query/joins.cjs.map +1 -1
  23. package/dist/cjs/query/joins.d.cts +3 -3
  24. package/dist/cjs/query/order-by.cjs +37 -84
  25. package/dist/cjs/query/order-by.cjs.map +1 -1
  26. package/dist/cjs/query/order-by.d.cts +2 -2
  27. package/dist/cjs/query/pipeline-compiler.cjs +13 -18
  28. package/dist/cjs/query/pipeline-compiler.cjs.map +1 -1
  29. package/dist/cjs/query/pipeline-compiler.d.cts +2 -1
  30. package/dist/cjs/query/query-builder.cjs +22 -29
  31. package/dist/cjs/query/query-builder.cjs.map +1 -1
  32. package/dist/cjs/query/query-builder.d.cts +16 -10
  33. package/dist/cjs/query/schema.d.cts +12 -11
  34. package/dist/cjs/query/select.cjs +47 -24
  35. package/dist/cjs/query/select.cjs.map +1 -1
  36. package/dist/cjs/query/select.d.cts +2 -2
  37. package/dist/cjs/query/types.d.cts +1 -0
  38. package/dist/cjs/transactions.cjs +20 -9
  39. package/dist/cjs/transactions.cjs.map +1 -1
  40. package/dist/cjs/types.d.cts +66 -7
  41. package/dist/esm/collection.d.ts +43 -15
  42. package/dist/esm/collection.js +183 -114
  43. package/dist/esm/collection.js.map +1 -1
  44. package/dist/esm/index.js +2 -1
  45. package/dist/esm/proxy.d.ts +5 -5
  46. package/dist/esm/proxy.js +87 -248
  47. package/dist/esm/proxy.js.map +1 -1
  48. package/dist/esm/query/compiled-query.d.ts +3 -1
  49. package/dist/esm/query/compiled-query.js +23 -14
  50. package/dist/esm/query/compiled-query.js.map +1 -1
  51. package/dist/esm/query/evaluators.d.ts +8 -3
  52. package/dist/esm/query/evaluators.js +36 -21
  53. package/dist/esm/query/evaluators.js.map +1 -1
  54. package/dist/esm/query/extractors.d.ts +3 -3
  55. package/dist/esm/query/extractors.js +20 -20
  56. package/dist/esm/query/extractors.js.map +1 -1
  57. package/dist/esm/query/group-by.d.ts +7 -7
  58. package/dist/esm/query/group-by.js +14 -17
  59. package/dist/esm/query/group-by.js.map +1 -1
  60. package/dist/esm/query/joins.d.ts +3 -3
  61. package/dist/esm/query/joins.js +42 -56
  62. package/dist/esm/query/joins.js.map +1 -1
  63. package/dist/esm/query/order-by.d.ts +2 -2
  64. package/dist/esm/query/order-by.js +39 -86
  65. package/dist/esm/query/order-by.js.map +1 -1
  66. package/dist/esm/query/pipeline-compiler.d.ts +2 -1
  67. package/dist/esm/query/pipeline-compiler.js +14 -19
  68. package/dist/esm/query/pipeline-compiler.js.map +1 -1
  69. package/dist/esm/query/query-builder.d.ts +16 -10
  70. package/dist/esm/query/query-builder.js +22 -29
  71. package/dist/esm/query/query-builder.js.map +1 -1
  72. package/dist/esm/query/schema.d.ts +12 -11
  73. package/dist/esm/query/select.d.ts +2 -2
  74. package/dist/esm/query/select.js +48 -25
  75. package/dist/esm/query/select.js.map +1 -1
  76. package/dist/esm/query/types.d.ts +1 -0
  77. package/dist/esm/transactions.js +20 -9
  78. package/dist/esm/transactions.js.map +1 -1
  79. package/dist/esm/types.d.ts +66 -7
  80. package/package.json +2 -2
  81. package/src/collection.ts +286 -146
  82. package/src/proxy.ts +141 -358
  83. package/src/query/compiled-query.ts +30 -15
  84. package/src/query/evaluators.ts +49 -21
  85. package/src/query/extractors.ts +24 -21
  86. package/src/query/group-by.ts +24 -22
  87. package/src/query/joins.ts +88 -75
  88. package/src/query/order-by.ts +56 -106
  89. package/src/query/pipeline-compiler.ts +34 -37
  90. package/src/query/query-builder.ts +49 -46
  91. package/src/query/schema.ts +18 -15
  92. package/src/query/select.ts +68 -33
  93. package/src/query/types.ts +1 -0
  94. package/src/transactions.ts +30 -14
  95. package/src/types.ts +76 -7
  96. package/dist/cjs/query/key-by.cjs +0 -43
  97. package/dist/cjs/query/key-by.cjs.map +0 -1
  98. package/dist/cjs/query/key-by.d.cts +0 -3
  99. package/dist/esm/query/key-by.d.ts +0 -3
  100. package/dist/esm/query/key-by.js +0 -43
  101. package/dist/esm/query/key-by.js.map +0 -1
  102. package/src/query/key-by.ts +0 -61
@@ -1,3 +1,4 @@
1
+ import { IStreamBuilder } from '@electric-sql/d2ts';
1
2
  import { Collection } from './collection.js';
2
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
4
  import { Transaction } from './transactions.js';
@@ -11,7 +12,7 @@ export interface PendingMutation<T extends object = Record<string, unknown>> {
11
12
  original: Record<string, unknown>;
12
13
  modified: Record<string, unknown>;
13
14
  changes: Record<string, unknown>;
14
- key: string;
15
+ key: any;
15
16
  type: OperationType;
16
17
  metadata: unknown;
17
18
  syncMetadata: Record<string, unknown>;
@@ -26,6 +27,13 @@ export type MutationFnParams = {
26
27
  transaction: Transaction;
27
28
  };
28
29
  export type MutationFn = (params: MutationFnParams) => Promise<any>;
30
+ /**
31
+ * Utility type for a Transaction with at least one mutation
32
+ * This is used internally by the Transaction.commit method
33
+ */
34
+ export type TransactionWithMutations<T extends object = Record<string, unknown>> = Transaction & {
35
+ mutations: [PendingMutation<T>, ...Array<PendingMutation<T>>];
36
+ };
29
37
  export interface TransactionConfig {
30
38
  /** Unique identifier for the transaction */
31
39
  id?: string;
@@ -36,7 +44,7 @@ export interface TransactionConfig {
36
44
  }
37
45
  export type { Transaction };
38
46
  type Value<TExtensions = never> = string | number | boolean | bigint | null | TExtensions | Array<Value<TExtensions>> | {
39
- [key: string]: Value<TExtensions>;
47
+ [key: string | number | symbol]: Value<TExtensions>;
40
48
  };
41
49
  export type Row<TExtensions = never> = Record<string, Value<TExtensions>>;
42
50
  export type OperationType = `insert` | `update` | `delete`;
@@ -44,17 +52,17 @@ export interface SyncConfig<T extends object = Record<string, unknown>> {
44
52
  sync: (params: {
45
53
  collection: Collection<T>;
46
54
  begin: () => void;
47
- write: (message: ChangeMessage<T>) => void;
55
+ write: (message: Omit<ChangeMessage<T>, `key`>) => void;
48
56
  commit: () => void;
49
57
  }) => void;
50
58
  /**
51
59
  * Get the sync metadata for insert operations
52
- * @returns Record containing primaryKey and relation information
60
+ * @returns Record containing relation information
53
61
  */
54
62
  getSyncMetadata?: () => Record<string, unknown>;
55
63
  }
56
64
  export interface ChangeMessage<T extends object = Record<string, unknown>> {
57
- key: string;
65
+ key: any;
58
66
  value: T;
59
67
  previousValue?: T;
60
68
  type: OperationType;
@@ -83,12 +91,63 @@ export interface OperationConfig {
83
91
  metadata?: Record<string, unknown>;
84
92
  }
85
93
  export interface InsertConfig {
86
- key?: string | Array<string | undefined>;
87
94
  metadata?: Record<string, unknown>;
88
95
  }
89
96
  export interface CollectionConfig<T extends object = Record<string, unknown>> {
90
- id: string;
97
+ id?: string;
91
98
  sync: SyncConfig<T>;
92
99
  schema?: StandardSchema<T>;
100
+ /**
101
+ * Function to extract the ID from an object
102
+ * This is required for update/delete operations which now only accept IDs
103
+ * @param item The item to extract the ID from
104
+ * @returns The ID string for the item
105
+ * @example
106
+ * // For a collection with a 'uuid' field as the primary key
107
+ * getId: (item) => item.uuid
108
+ */
109
+ getId: (item: T) => any;
110
+ /**
111
+ * Optional asynchronous handler function called before an insert operation
112
+ * @param params Object containing transaction and mutation information
113
+ * @returns Promise resolving to any value
114
+ */
115
+ onInsert?: MutationFn;
116
+ /**
117
+ * Optional asynchronous handler function called before an update operation
118
+ * @param params Object containing transaction and mutation information
119
+ * @returns Promise resolving to any value
120
+ */
121
+ onUpdate?: MutationFn;
122
+ /**
123
+ * Optional asynchronous handler function called before a delete operation
124
+ * @param params Object containing transaction and mutation information
125
+ * @returns Promise resolving to any value
126
+ */
127
+ onDelete?: MutationFn;
93
128
  }
94
129
  export type ChangesPayload<T extends object = Record<string, unknown>> = Array<ChangeMessage<T>>;
130
+ /**
131
+ * An input row from a collection
132
+ */
133
+ export type InputRow = [unknown, Record<string, unknown>];
134
+ /**
135
+ * A keyed stream is a stream of rows
136
+ * This is used as the inputs from a collection to a query
137
+ */
138
+ export type KeyedStream = IStreamBuilder<InputRow>;
139
+ /**
140
+ * A namespaced row is a row withing a pipeline that had each table wrapped in its alias
141
+ */
142
+ export type NamespacedRow = Record<string, Record<string, unknown>>;
143
+ /**
144
+ * A keyed namespaced row is a row with a key and a namespaced row
145
+ * This is the main representation of a row in a query pipeline
146
+ */
147
+ export type KeyedNamespacedRow = [unknown, NamespacedRow];
148
+ /**
149
+ * A namespaced and keyed stream is a stream of rows
150
+ * This is used throughout a query pipeline and as the output from a query without
151
+ * a `select` clause.
152
+ */
153
+ export type NamespacedAndKeyedStream = IStreamBuilder<KeyedNamespacedRow>;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@tanstack/db",
3
3
  "description": "A reactive client store for building super fast apps on sync",
4
- "version": "0.0.4",
4
+ "version": "0.0.6",
5
5
  "dependencies": {
6
- "@electric-sql/d2ts": "^0.1.5",
6
+ "@electric-sql/d2ts": "^0.1.6",
7
7
  "@standard-schema/spec": "^1.0.0",
8
8
  "@tanstack/store": "^0.7.0"
9
9
  },