Skip to content

Commit 6604a33

Browse files
committed
Change collections to us key rather than id, change key to globalKey for transactions, and rename getId to getKey
1 parent 4580559 commit 6604a33

File tree

18 files changed

+348
-331
lines changed

18 files changed

+348
-331
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { createQueryCollection } from "@tanstack/db-collections"
5353
const todoCollection = createQueryCollection<Todo>({
5454
queryKey: ["todos"],
5555
queryFn: async () => fetch("/api/todos"),
56-
getId: (item) => item.id,
56+
getKey: (item) => item.id,
5757
schema: todoSchema, // any standard schema
5858
})
5959
```

docs/overview.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ If provided, this should be a [Standard Schema](https://standardschema.dev) comp
173173
const todoCollection = createCollection<Todo>(queryCollectionOptions({
174174
queryKey: ['todoItems'],
175175
queryFn: async () => fetch('/api/todos'),
176-
getId: (item) => item.id,
176+
getKey: (item) => item.id,
177177
schema: todoSchema // any standard schema
178178
}))
179179
```
@@ -198,7 +198,7 @@ export const todoCollection = createCollection<Todo>(electricCollectionOptions({
198198
table: 'todos'
199199
}
200200
},
201-
getId: (item) => item.id,
201+
getKey: (item) => item.id,
202202
schema: todoSchema
203203
}).options)
204204
```
@@ -208,7 +208,7 @@ The Electric collection requires two Electric-specific options:
208208
- `shapeOptions` &mdash; the Electric [ShapeStreamOptions](https://electric-sql.com/docs/api/clients/typescript#options) that define the [Shape](https://electric-sql.com/docs/guides/shapes) to sync into the collection; this includes the
209209
- `url` to your sync engine; and
210210
- `params` to specify the `table` to sync and any optional `where` clauses, etc.
211-
- `getId` &mdash; identifies the id for the rows being synced into the collection
211+
- `getKey` &mdash; identifies the id for the rows being synced into the collection
212212

213213
When you create the collection, sync starts automatically.
214214

@@ -228,7 +228,7 @@ export const myPendingTodos = createCollection<Todo>(electricCollectionOptions({
228228
`
229229
}
230230
},
231-
getId: (item) => item.id,
231+
getKey: (item) => item.id,
232232
schema: todoSchema
233233
}).options)
234234
```
@@ -515,7 +515,7 @@ import { queryCollectionOptions } from "@tanstack/db-collections"
515515
const todoCollection = createCollection<Todo>(queryCollectionOptions({
516516
queryKey: ["todos"],
517517
queryFn: async () => fetch("/api/todos"),
518-
getId: (item) => item.id,
518+
getKey: (item) => item.id,
519519
schema: todoSchema, // any standard schema
520520
onInsert: ({ transaction }) => {
521521
const { changes: newTodo } = transaction.mutations[0]
@@ -528,7 +528,7 @@ const todoCollection = createCollection<Todo>(queryCollectionOptions({
528528
const listCollection = createCollection<TodoList>(queryCollectionOptions({
529529
queryKey: ["todo-lists"],
530530
queryFn: async () => fetch("/api/todo-lists"),
531-
getId: (item) => item.id,
531+
getKey: (item) => item.id,
532532
schema: todoListSchema
533533
onInsert: ({ transaction }) => {
534534
const { changes: newTodo } = transaction.mutations[0]
@@ -586,7 +586,7 @@ export const todoCollection = createCollection(electricCollectionOptions<Todo>({
586586
table: 'todos'
587587
}
588588
},
589-
getId: (item) => item.id,
589+
getKey: (item) => item.id,
590590
schema: todoSchema
591591
}))
592592

examples/react/todo/src/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const createTodoCollection = (type: CollectionType) => {
147147
timestamptz: (date: string) => new Date(date),
148148
},
149149
},
150-
getId: (item) => item.id,
150+
getKey: (item) => item.id,
151151
schema: updateTodoSchema,
152152
onInsert: async ({ transaction }) => {
153153
const modified = transaction.mutations[0].modified
@@ -201,7 +201,7 @@ const createTodoCollection = (type: CollectionType) => {
201201
: undefined,
202202
}))
203203
},
204-
getId: (item: UpdateTodo) => String(item.id),
204+
getKey: (item: UpdateTodo) => String(item.id),
205205
schema: updateTodoSchema,
206206
queryClient,
207207
onInsert: async ({ transaction }) => {
@@ -254,7 +254,7 @@ const createConfigCollection = (type: CollectionType) => {
254254
},
255255
},
256256
},
257-
getId: (item: UpdateConfig) => item.id,
257+
getKey: (item: UpdateConfig) => item.id,
258258
schema: updateConfigSchema,
259259
onInsert: async ({ transaction }) => {
260260
const modified = transaction.mutations[0].modified
@@ -297,7 +297,7 @@ const createConfigCollection = (type: CollectionType) => {
297297
: undefined,
298298
}))
299299
},
300-
getId: (item: UpdateConfig) => item.id,
300+
getKey: (item: UpdateConfig) => item.id,
301301
schema: updateConfigSchema,
302302
queryClient,
303303
onInsert: async ({ transaction }) => {

packages/db-collections/src/electric.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface ElectricCollectionConfig<T extends Row<unknown>> {
3030
*/
3131
id?: string
3232
schema?: CollectionConfig<T>[`schema`]
33-
getId: CollectionConfig<T>[`getId`]
33+
getKey: CollectionConfig<T>[`getKey`]
3434
sync?: CollectionConfig<T>[`sync`]
3535

3636
/**

packages/db-collections/src/query.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface QueryCollectionConfig<
5353

5454
// Standard Collection configuration properties
5555
id?: string
56-
getId: CollectionConfig<TItem>[`getId`]
56+
getKey: CollectionConfig<TItem>[`getKey`]
5757
schema?: CollectionConfig<TItem>[`schema`]
5858
sync?: CollectionConfig<TItem>[`sync`]
5959

@@ -105,7 +105,7 @@ export function queryCollectionOptions<
105105
retry,
106106
retryDelay,
107107
staleTime,
108-
getId,
108+
getKey,
109109
onInsert,
110110
onUpdate,
111111
onDelete,
@@ -126,8 +126,8 @@ export function queryCollectionOptions<
126126
throw new Error(`[QueryCollection] queryClient must be provided.`)
127127
}
128128
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
129-
if (!getId) {
130-
throw new Error(`[QueryCollection] getId must be provided.`)
129+
if (!getKey) {
130+
throw new Error(`[QueryCollection] getKey must be provided.`)
131131
}
132132

133133
const internalSync: SyncConfig<TItem>[`sync`] = (params) => {
@@ -178,7 +178,7 @@ export function queryCollectionOptions<
178178
const newItemsMap = new Map<string, TItem>()
179179
newItemsArray.forEach((item) => {
180180
try {
181-
const key = getId(item)
181+
const key = getKey(item)
182182
newItemsMap.set(key, item)
183183
} catch (e) {
184184
console.error(
@@ -318,7 +318,7 @@ export function queryCollectionOptions<
318318
return {
319319
options: {
320320
...baseCollectionConfig,
321-
getId,
321+
getKey,
322322
sync: { sync: internalSync },
323323
onInsert: wrappedOnInsert,
324324
onUpdate: wrappedOnUpdate,

packages/db-collections/tests/electric.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe(`Electric Integration`, () => {
4545
table: `test_table`,
4646
},
4747
},
48-
getId: (item: Row) => item.id,
48+
getKey: (item: Row) => item.id,
4949
}
5050

5151
const { options, awaitTxId: txIdFn } = electricCollectionOptions(config)
@@ -396,7 +396,7 @@ describe(`Electric Integration`, () => {
396396
table: `test_table`,
397397
},
398398
},
399-
getId: (item: Row) => item.id,
399+
getKey: (item: Row) => item.id,
400400
onInsert,
401401
onUpdate,
402402
onDelete,
@@ -426,7 +426,7 @@ describe(`Electric Integration`, () => {
426426
table: `test_table`,
427427
},
428428
},
429-
getId: (item: Row) => item.id,
429+
getKey: (item: Row) => item.id,
430430
onInsert,
431431
}
432432

@@ -510,7 +510,7 @@ describe(`Electric Integration`, () => {
510510
table: `test_table`,
511511
},
512512
},
513-
getId: (item: Row) => item.id,
513+
getKey: (item: Row) => item.id,
514514
onInsert,
515515
}
516516

packages/db-collections/tests/query.test.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface TestItem {
1111
value?: number
1212
}
1313

14-
const getId = (item: TestItem) => item.id
14+
const getKey = (item: TestItem) => item.id
1515

1616
// Helper to advance timers and allow microtasks to flush
1717
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0))
@@ -51,7 +51,7 @@ describe(`QueryCollection`, () => {
5151
queryClient,
5252
queryKey,
5353
queryFn,
54-
getId,
54+
getKey,
5555
}
5656

5757
const { options } = queryCollectionOptions(config)
@@ -111,7 +111,7 @@ describe(`QueryCollection`, () => {
111111
queryClient,
112112
queryKey,
113113
queryFn,
114-
getId,
114+
getKey,
115115
}
116116

117117
const { options, refetch } = queryCollectionOptions(config)
@@ -192,7 +192,7 @@ describe(`QueryCollection`, () => {
192192
queryClient,
193193
queryKey,
194194
queryFn,
195-
getId,
195+
getKey,
196196
retry: 0, // Disable retries for this test case
197197
})
198198
const collection = new Collection(options)
@@ -242,7 +242,7 @@ describe(`QueryCollection`, () => {
242242
queryClient,
243243
queryKey,
244244
queryFn,
245-
getId,
245+
getKey,
246246
})
247247
const collection = new Collection(options)
248248

@@ -290,7 +290,7 @@ describe(`QueryCollection`, () => {
290290
queryClient,
291291
queryKey,
292292
queryFn,
293-
getId,
293+
getKey,
294294
})
295295
const collection = new Collection(options)
296296

@@ -353,7 +353,7 @@ describe(`QueryCollection`, () => {
353353
consoleSpy.mockRestore()
354354
})
355355

356-
it(`should use the provided getId function to identify items`, async () => {
356+
it(`should use the provided getKey function to identify items`, async () => {
357357
const queryKey = [`customKeyTest`]
358358

359359
// Items with a non-standard ID field
@@ -364,15 +364,15 @@ describe(`QueryCollection`, () => {
364364

365365
const queryFn = vi.fn().mockResolvedValue(items)
366366

367-
// Create a spy for the getId function
368-
const getIdSpy = vi.fn((item: any) => item.customId)
367+
// Create a spy for the getKey function
368+
const getKeySpy = vi.fn((item: any) => item.customId)
369369

370370
const { options, refetch } = queryCollectionOptions({
371371
id: `test`,
372372
queryClient,
373373
queryKey,
374374
queryFn,
375-
getId: getIdSpy,
375+
getKey: getKeySpy,
376376
})
377377
const collection = new Collection(options)
378378

@@ -382,10 +382,10 @@ describe(`QueryCollection`, () => {
382382
expect(collection.state.size).toBe(items.length)
383383
})
384384

385-
// Verify getId was called for each item
386-
expect(getIdSpy).toHaveBeenCalledTimes(items.length * 2)
385+
// Verify getKey was called for each item
386+
expect(getKeySpy).toHaveBeenCalledTimes(items.length * 2)
387387
items.forEach((item) => {
388-
expect(getIdSpy).toHaveBeenCalledWith(item)
388+
expect(getKeySpy).toHaveBeenCalledWith(item)
389389
})
390390

391391
// Verify items are stored with the custom keys
@@ -406,7 +406,7 @@ describe(`QueryCollection`, () => {
406406
]
407407

408408
// Reset the spy to track new calls
409-
getIdSpy.mockClear()
409+
getKeySpy.mockClear()
410410
queryFn.mockResolvedValueOnce(updatedItems)
411411

412412
// Trigger a refetch
@@ -415,11 +415,11 @@ describe(`QueryCollection`, () => {
415415
expect(queryFn).toHaveBeenCalledTimes(2)
416416
expect(collection.state.size).toBe(updatedItems.length)
417417

418-
// Verify getId was called at least once for each item
418+
// Verify getKey was called at least once for each item
419419
// It may be called multiple times per item during the diffing process
420-
expect(getIdSpy).toHaveBeenCalled()
420+
expect(getKeySpy).toHaveBeenCalled()
421421
updatedItems.forEach((item) => {
422-
expect(getIdSpy).toHaveBeenCalledWith(item)
422+
expect(getKeySpy).toHaveBeenCalledWith(item)
423423
})
424424

425425
// Verify the state reflects the changes
@@ -450,7 +450,7 @@ describe(`QueryCollection`, () => {
450450
queryClient,
451451
queryKey,
452452
queryFn,
453-
getId,
453+
getKey,
454454
onInsert,
455455
onUpdate,
456456
onDelete,
@@ -483,7 +483,7 @@ describe(`QueryCollection`, () => {
483483
queryClient,
484484
queryKey,
485485
queryFn,
486-
getId,
486+
getKey,
487487
onInsert,
488488
onUpdate,
489489
onDelete,
@@ -520,7 +520,7 @@ describe(`QueryCollection`, () => {
520520
queryClient,
521521
queryKey: [`refetchTest`, `default`],
522522
queryFn: vi.fn().mockResolvedValue([{ id: `1`, name: `Item 1` }]),
523-
getId,
523+
getKey,
524524
onInsert: onInsertDefault,
525525
}
526526

@@ -529,7 +529,7 @@ describe(`QueryCollection`, () => {
529529
queryClient,
530530
queryKey: [`refetchTest`, `false`],
531531
queryFn: vi.fn().mockResolvedValue([{ id: `1`, name: `Item 1` }]),
532-
getId,
532+
getKey,
533533
onInsert: onInsertFalse,
534534
}
535535

0 commit comments

Comments
 (0)