@@ -51,7 +51,7 @@ import type {
51
51
OperationConfig ,
52
52
OptimisticChangeMessage ,
53
53
PendingMutation ,
54
- ResolveInsertInput ,
54
+ ResolveInput ,
55
55
ResolveType ,
56
56
StandardSchema ,
57
57
SubscribeChangesOptions ,
@@ -73,15 +73,15 @@ interface PendingSyncedTransaction<T extends object = Record<string, unknown>> {
73
73
* @template TKey - The type of the key for the collection
74
74
* @template TUtils - The utilities record type
75
75
* @template TSchema - The schema type for validation and type inference
76
- * @template TInsertInput - The type for insert operations (can be different from T for schemas with defaults)
76
+ * @template TInput - The type for insert and update operations (can be different from T for schemas with defaults)
77
77
*/
78
78
export interface Collection <
79
79
T extends object = Record < string , unknown > ,
80
80
TKey extends string | number = string | number ,
81
81
TUtils extends UtilsRecord = { } ,
82
82
TSchema extends StandardSchemaV1 = StandardSchemaV1 ,
83
- TInsertInput extends object = T ,
84
- > extends CollectionImpl < T , TKey , TUtils , TSchema , TInsertInput > {
83
+ TInput extends object = T ,
84
+ > extends CollectionImpl < T , TKey , TUtils , TSchema , TInput > {
85
85
readonly utils : TUtils
86
86
}
87
87
@@ -170,7 +170,7 @@ export function createCollection<
170
170
ResolveType < unknown , TSchema , TFallback > ,
171
171
TKey ,
172
172
TSchema ,
173
- ResolveInsertInput < unknown , TSchema , TFallback >
173
+ ResolveInput < unknown , TSchema , TFallback >
174
174
> & {
175
175
schema : TSchema
176
176
utils ?: TUtils
@@ -180,7 +180,7 @@ export function createCollection<
180
180
TKey ,
181
181
TUtils ,
182
182
TSchema ,
183
- ResolveInsertInput < unknown , TSchema , TFallback >
183
+ ResolveInput < unknown , TSchema , TFallback >
184
184
>
185
185
186
186
// Overload for when explicit type is provided with schema - explicit type takes precedence
@@ -195,7 +195,7 @@ export function createCollection<
195
195
ResolveType < TExplicit , TSchema , TFallback > ,
196
196
TKey ,
197
197
TSchema ,
198
- ResolveInsertInput < TExplicit , TSchema , TFallback >
198
+ ResolveInput < TExplicit , TSchema , TFallback >
199
199
> & {
200
200
schema : TSchema
201
201
utils ?: TUtils
@@ -205,7 +205,7 @@ export function createCollection<
205
205
TKey ,
206
206
TUtils ,
207
207
TSchema ,
208
- ResolveInsertInput < TExplicit , TSchema , TFallback >
208
+ ResolveInput < TExplicit , TSchema , TFallback >
209
209
>
210
210
211
211
// Overload for when explicit type is provided or no schema
@@ -220,14 +220,14 @@ export function createCollection<
220
220
ResolveType < TExplicit , TSchema , TFallback > ,
221
221
TKey ,
222
222
TSchema ,
223
- ResolveInsertInput < TExplicit , TSchema , TFallback >
223
+ ResolveInput < TExplicit , TSchema , TFallback >
224
224
> & { utils ?: TUtils }
225
225
) : Collection <
226
226
ResolveType < TExplicit , TSchema , TFallback > ,
227
227
TKey ,
228
228
TUtils ,
229
229
TSchema ,
230
- ResolveInsertInput < TExplicit , TSchema , TFallback >
230
+ ResolveInput < TExplicit , TSchema , TFallback >
231
231
>
232
232
233
233
// Implementation
@@ -242,21 +242,21 @@ export function createCollection<
242
242
ResolveType < TExplicit , TSchema , TFallback > ,
243
243
TKey ,
244
244
TSchema ,
245
- ResolveInsertInput < TExplicit , TSchema , TFallback >
245
+ ResolveInput < TExplicit , TSchema , TFallback >
246
246
> & { utils ?: TUtils }
247
247
) : Collection <
248
248
ResolveType < TExplicit , TSchema , TFallback > ,
249
249
TKey ,
250
250
TUtils ,
251
251
TSchema ,
252
- ResolveInsertInput < TExplicit , TSchema , TFallback >
252
+ ResolveInput < TExplicit , TSchema , TFallback >
253
253
> {
254
254
const collection = new CollectionImpl <
255
255
ResolveType < TExplicit , TSchema , TFallback > ,
256
256
TKey ,
257
257
TUtils ,
258
258
TSchema ,
259
- ResolveInsertInput < TExplicit , TSchema , TFallback >
259
+ ResolveInput < TExplicit , TSchema , TFallback >
260
260
> ( options )
261
261
262
262
// Copy utils to both top level and .utils namespace
@@ -271,7 +271,7 @@ export function createCollection<
271
271
TKey ,
272
272
TUtils ,
273
273
TSchema ,
274
- ResolveInsertInput < TExplicit , TSchema , TFallback >
274
+ ResolveInput < TExplicit , TSchema , TFallback >
275
275
>
276
276
}
277
277
@@ -280,9 +280,9 @@ export class CollectionImpl<
280
280
TKey extends string | number = string | number ,
281
281
TUtils extends UtilsRecord = { } ,
282
282
TSchema extends StandardSchemaV1 = StandardSchemaV1 ,
283
- TInsertInput extends object = T ,
283
+ TInput extends object = T ,
284
284
> {
285
- public config : CollectionConfig < T , TKey , TSchema , TInsertInput >
285
+ public config : CollectionConfig < T , TKey , TSchema , TInput >
286
286
287
287
// Core state - make public for testing
288
288
public transactions : SortedMap < string , Transaction < any > >
@@ -478,7 +478,7 @@ export class CollectionImpl<
478
478
* @param config - Configuration object for the collection
479
479
* @throws Error if sync config is missing
480
480
*/
481
- constructor ( config : CollectionConfig < T , TKey , TSchema , TInsertInput > ) {
481
+ constructor ( config : CollectionConfig < T , TKey , TSchema , TInput > ) {
482
482
// eslint-disable-next-line
483
483
if ( ! config ) {
484
484
throw new CollectionRequiresConfigError ( )
@@ -1681,10 +1681,7 @@ export class CollectionImpl<
1681
1681
* console.log('Insert failed:', error)
1682
1682
* }
1683
1683
*/
1684
- insert = (
1685
- data : TInsertInput | Array < TInsertInput > ,
1686
- config ?: InsertConfig
1687
- ) => {
1684
+ insert = ( data : TInput | Array < TInput > , config ?: InsertConfig ) => {
1688
1685
this . validateCollectionUsable ( `insert` )
1689
1686
const ambientTransaction = getActiveTransaction ( )
1690
1687
@@ -1713,14 +1710,15 @@ export class CollectionImpl<
1713
1710
original : { } ,
1714
1711
modified : validatedData ,
1715
1712
// Pick the values from validatedData based on what's passed in - this is for cases
1716
- // where a schema has default values. The validated data has the extra default
1717
- // values but for changes, we just want to show the data that was actually passed in.
1713
+ // where a schema has default values or transforms. The validated data has the extra
1714
+ // default or transformed values but for changes, we just want to show the data that
1715
+ // was actually passed in.
1718
1716
changes : Object . fromEntries (
1719
1717
Object . keys ( item ) . map ( ( k ) => [
1720
1718
k ,
1721
1719
validatedData [ k as keyof typeof validatedData ] ,
1722
1720
] )
1723
- ) as TInsertInput ,
1721
+ ) as TInput ,
1724
1722
globalKey,
1725
1723
key,
1726
1724
metadata : config ?. metadata as unknown ,
@@ -1752,7 +1750,7 @@ export class CollectionImpl<
1752
1750
return await this . config . onInsert ! ( {
1753
1751
transaction :
1754
1752
params . transaction as unknown as TransactionWithMutations <
1755
- TInsertInput ,
1753
+ T ,
1756
1754
`insert`
1757
1755
> ,
1758
1756
collection : this as unknown as Collection < T , TKey , TUtils > ,
@@ -1814,35 +1812,37 @@ export class CollectionImpl<
1814
1812
*/
1815
1813
1816
1814
// Overload 1: Update multiple items with a callback
1817
- update < TItem extends object = T > (
1815
+ update < TDraft extends object = TInput > (
1818
1816
key : Array < TKey | unknown > ,
1819
- callback : ( drafts : Array < TItem > ) => void
1817
+ callback : ( drafts : Array < TDraft > ) => void
1820
1818
) : TransactionType
1821
1819
1822
1820
// Overload 2: Update multiple items with config and a callback
1823
- update < TItem extends object = T > (
1821
+ update < TDraft extends object = TInput > (
1824
1822
keys : Array < TKey | unknown > ,
1825
1823
config : OperationConfig ,
1826
- callback : ( drafts : Array < TItem > ) => void
1824
+ callback : ( drafts : Array < TDraft > ) => void
1827
1825
) : TransactionType
1828
1826
1829
1827
// Overload 3: Update a single item with a callback
1830
- update < TItem extends object = T > (
1828
+ update < TDraft extends object = TInput > (
1831
1829
id : TKey | unknown ,
1832
- callback : ( draft : TItem ) => void
1830
+ callback : ( draft : TDraft ) => void
1833
1831
) : TransactionType
1834
1832
1835
1833
// Overload 4: Update a single item with config and a callback
1836
- update < TItem extends object = T > (
1834
+ update < TDraft extends object = TInput > (
1837
1835
id : TKey | unknown ,
1838
1836
config : OperationConfig ,
1839
- callback : ( draft : TItem ) => void
1837
+ callback : ( draft : TDraft ) => void
1840
1838
) : TransactionType
1841
1839
1842
- update < TItem extends object = T > (
1840
+ update < TDraft extends object = TInput > (
1843
1841
keys : ( TKey | unknown ) | Array < TKey | unknown > ,
1844
- configOrCallback : ( ( draft : TItem | Array < TItem > ) => void ) | OperationConfig ,
1845
- maybeCallback ?: ( draft : TItem | Array < TItem > ) => void
1842
+ configOrCallback :
1843
+ | ( ( draft : TDraft | Array < TDraft > ) => void )
1844
+ | OperationConfig ,
1845
+ maybeCallback ?: ( draft : TDraft | Array < TDraft > ) => void
1846
1846
) {
1847
1847
if ( typeof keys === `undefined` ) {
1848
1848
throw new MissingUpdateArgumentError ( )
@@ -1877,19 +1877,19 @@ export class CollectionImpl<
1877
1877
}
1878
1878
1879
1879
return item
1880
- } ) as unknown as Array < TItem >
1880
+ } ) as unknown as Array < TDraft >
1881
1881
1882
1882
let changesArray
1883
1883
if ( isArray ) {
1884
1884
// Use the proxy to track changes for all objects
1885
1885
changesArray = withArrayChangeTracking (
1886
1886
currentObjects ,
1887
- callback as ( draft : Array < TItem > ) => void
1887
+ callback as ( draft : Array < TDraft > ) => void
1888
1888
)
1889
1889
} else {
1890
1890
const result = withChangeTracking (
1891
1891
currentObjects [ 0 ] ! ,
1892
- callback as ( draft : TItem ) => void
1892
+ callback as ( draft : TDraft ) => void
1893
1893
)
1894
1894
changesArray = [ result ]
1895
1895
}
@@ -1933,7 +1933,16 @@ export class CollectionImpl<
1933
1933
mutationId : crypto . randomUUID ( ) ,
1934
1934
original : originalItem ,
1935
1935
modified : modifiedItem ,
1936
- changes : validatedUpdatePayload as Partial < T > ,
1936
+ // Pick the values from modifiedItem based on what's passed in - this is for cases
1937
+ // where a schema has default values or transforms. The modified data has the extra
1938
+ // default or transformed values but for changes, we just want to show the data that
1939
+ // was actually passed in.
1940
+ changes : Object . fromEntries (
1941
+ Object . keys ( itemChanges ) . map ( ( k ) => [
1942
+ k ,
1943
+ modifiedItem [ k as keyof typeof modifiedItem ] ,
1944
+ ] )
1945
+ ) as TInput ,
1937
1946
globalKey,
1938
1947
key,
1939
1948
metadata : config . metadata as unknown ,
0 commit comments