@tanstack/db 0.0.7 → 0.0.9

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 (43) hide show
  1. package/dist/cjs/collection.cjs +441 -284
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +103 -30
  4. package/dist/cjs/proxy.cjs +2 -2
  5. package/dist/cjs/proxy.cjs.map +1 -1
  6. package/dist/cjs/query/compiled-query.cjs +23 -37
  7. package/dist/cjs/query/compiled-query.cjs.map +1 -1
  8. package/dist/cjs/query/compiled-query.d.cts +2 -2
  9. package/dist/cjs/query/order-by.cjs +41 -38
  10. package/dist/cjs/query/order-by.cjs.map +1 -1
  11. package/dist/cjs/query/query-builder.cjs.map +1 -1
  12. package/dist/cjs/query/query-builder.d.cts +2 -2
  13. package/dist/cjs/query/schema.d.cts +5 -4
  14. package/dist/cjs/transactions.cjs +7 -6
  15. package/dist/cjs/transactions.cjs.map +1 -1
  16. package/dist/cjs/transactions.d.cts +9 -9
  17. package/dist/cjs/types.d.cts +28 -22
  18. package/dist/esm/collection.d.ts +103 -30
  19. package/dist/esm/collection.js +442 -285
  20. package/dist/esm/collection.js.map +1 -1
  21. package/dist/esm/proxy.js +2 -2
  22. package/dist/esm/proxy.js.map +1 -1
  23. package/dist/esm/query/compiled-query.d.ts +2 -2
  24. package/dist/esm/query/compiled-query.js +23 -37
  25. package/dist/esm/query/compiled-query.js.map +1 -1
  26. package/dist/esm/query/order-by.js +41 -38
  27. package/dist/esm/query/order-by.js.map +1 -1
  28. package/dist/esm/query/query-builder.d.ts +2 -2
  29. package/dist/esm/query/query-builder.js.map +1 -1
  30. package/dist/esm/query/schema.d.ts +5 -4
  31. package/dist/esm/transactions.d.ts +9 -9
  32. package/dist/esm/transactions.js +7 -6
  33. package/dist/esm/transactions.js.map +1 -1
  34. package/dist/esm/types.d.ts +28 -22
  35. package/package.json +2 -2
  36. package/src/collection.ts +624 -372
  37. package/src/proxy.ts +2 -2
  38. package/src/query/compiled-query.ts +26 -37
  39. package/src/query/order-by.ts +69 -67
  40. package/src/query/query-builder.ts +4 -3
  41. package/src/query/schema.ts +13 -3
  42. package/src/transactions.ts +24 -22
  43. package/src/types.ts +44 -22
@@ -17,36 +17,41 @@ export type UtilsRecord = Record<string, Fn>;
17
17
  */
18
18
  export interface PendingMutation<T extends object = Record<string, unknown>> {
19
19
  mutationId: string;
20
- original: Record<string, unknown>;
21
- modified: Record<string, unknown>;
22
- changes: Record<string, unknown>;
20
+ original: Partial<T>;
21
+ modified: T;
22
+ changes: Partial<T>;
23
+ globalKey: string;
23
24
  key: any;
24
25
  type: OperationType;
25
26
  metadata: unknown;
26
27
  syncMetadata: Record<string, unknown>;
27
28
  createdAt: Date;
28
29
  updatedAt: Date;
29
- collection: Collection<T>;
30
+ collection: Collection<T, any>;
30
31
  }
31
32
  /**
32
33
  * Configuration options for creating a new transaction
33
34
  */
34
- export type MutationFnParams = {
35
- transaction: Transaction;
35
+ export type MutationFnParams<T extends object = Record<string, unknown>> = {
36
+ transaction: TransactionWithMutations<T>;
36
37
  };
37
- export type MutationFn = (params: MutationFnParams) => Promise<any>;
38
+ export type MutationFn<T extends object = Record<string, unknown>> = (params: MutationFnParams<T>) => Promise<any>;
39
+ /**
40
+ * Represents a non-empty array (at least one element)
41
+ */
42
+ export type NonEmptyArray<T> = [T, ...Array<T>];
38
43
  /**
39
44
  * Utility type for a Transaction with at least one mutation
40
45
  * This is used internally by the Transaction.commit method
41
46
  */
42
- export type TransactionWithMutations<T extends object = Record<string, unknown>> = Transaction & {
43
- mutations: [PendingMutation<T>, ...Array<PendingMutation<T>>];
47
+ export type TransactionWithMutations<T extends object = Record<string, unknown>> = Transaction<T> & {
48
+ mutations: NonEmptyArray<PendingMutation<T>>;
44
49
  };
45
- export interface TransactionConfig {
50
+ export interface TransactionConfig<T extends object = Record<string, unknown>> {
46
51
  /** Unique identifier for the transaction */
47
52
  id?: string;
48
53
  autoCommit?: boolean;
49
- mutationFn: MutationFn;
54
+ mutationFn: MutationFn<T>;
50
55
  /** Custom metadata to associate with the transaction */
51
56
  metadata?: Record<string, unknown>;
52
57
  }
@@ -56,9 +61,9 @@ type Value<TExtensions = never> = string | number | boolean | bigint | null | TE
56
61
  };
57
62
  export type Row<TExtensions = never> = Record<string, Value<TExtensions>>;
58
63
  export type OperationType = `insert` | `update` | `delete`;
59
- export interface SyncConfig<T extends object = Record<string, unknown>> {
64
+ export interface SyncConfig<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
60
65
  sync: (params: {
61
- collection: Collection<T>;
66
+ collection: Collection<T, TKey>;
62
67
  begin: () => void;
63
68
  write: (message: Omit<ChangeMessage<T>, `key`>) => void;
64
69
  commit: () => void;
@@ -69,8 +74,8 @@ export interface SyncConfig<T extends object = Record<string, unknown>> {
69
74
  */
70
75
  getSyncMetadata?: () => Record<string, unknown>;
71
76
  }
72
- export interface ChangeMessage<T extends object = Record<string, unknown>> {
73
- key: any;
77
+ export interface ChangeMessage<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
78
+ key: TKey;
74
79
  value: T;
75
80
  previousValue?: T;
76
81
  type: OperationType;
@@ -101,9 +106,9 @@ export interface OperationConfig {
101
106
  export interface InsertConfig {
102
107
  metadata?: Record<string, unknown>;
103
108
  }
104
- export interface CollectionConfig<T extends object = Record<string, unknown>> {
109
+ export interface CollectionConfig<T extends object = Record<string, unknown>, TKey extends string | number = string | number> {
105
110
  id?: string;
106
- sync: SyncConfig<T>;
111
+ sync: SyncConfig<T, TKey>;
107
112
  schema?: StandardSchema<T>;
108
113
  /**
109
114
  * Function to extract the ID from an object
@@ -112,27 +117,27 @@ export interface CollectionConfig<T extends object = Record<string, unknown>> {
112
117
  * @returns The ID string for the item
113
118
  * @example
114
119
  * // For a collection with a 'uuid' field as the primary key
115
- * getId: (item) => item.uuid
120
+ * getKey: (item) => item.uuid
116
121
  */
117
- getId: (item: T) => any;
122
+ getKey: (item: T) => TKey;
118
123
  /**
119
124
  * Optional asynchronous handler function called before an insert operation
120
125
  * @param params Object containing transaction and mutation information
121
126
  * @returns Promise resolving to any value
122
127
  */
123
- onInsert?: MutationFn;
128
+ onInsert?: MutationFn<T>;
124
129
  /**
125
130
  * Optional asynchronous handler function called before an update operation
126
131
  * @param params Object containing transaction and mutation information
127
132
  * @returns Promise resolving to any value
128
133
  */
129
- onUpdate?: MutationFn;
134
+ onUpdate?: MutationFn<T>;
130
135
  /**
131
136
  * Optional asynchronous handler function called before a delete operation
132
137
  * @param params Object containing transaction and mutation information
133
138
  * @returns Promise resolving to any value
134
139
  */
135
- onDelete?: MutationFn;
140
+ onDelete?: MutationFn<T>;
136
141
  }
137
142
  export type ChangesPayload<T extends object = Record<string, unknown>> = Array<ChangeMessage<T>>;
138
143
  /**
@@ -159,3 +164,4 @@ export type KeyedNamespacedRow = [unknown, NamespacedRow];
159
164
  * a `select` clause.
160
165
  */
161
166
  export type NamespacedAndKeyedStream = IStreamBuilder<KeyedNamespacedRow>;
167
+ export type ChangeListener<T extends object = Record<string, unknown>, TKey extends string | number = string | number> = (changes: Array<ChangeMessage<T, TKey>>) => void;
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.7",
4
+ "version": "0.0.9",
5
5
  "dependencies": {
6
- "@electric-sql/d2ts": "^0.1.6",
6
+ "@electric-sql/d2ts": "^0.1.7",
7
7
  "@standard-schema/spec": "^1.0.0",
8
8
  "@tanstack/store": "^0.7.0"
9
9
  },