Skip to content

Commit e5cf287

Browse files
authored
Replace getPrimaryKey w/ getId (#80)
1 parent 65b57da commit e5cf287

5 files changed

Lines changed: 26 additions & 26 deletions

File tree

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<TodoList>({
5454
queryKey: ["todos"],
5555
queryFn: async () => fetch("/api/todos"),
56-
getPrimaryKey: (item) => item.id,
56+
getId: (item) => item.id,
5757
schema: todoSchema, // any standard schema
5858
})
5959
```

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ See the [Electric docs](https://electric-sql.com/docs/intro) for more informatio
9393
const todoCollection = createQueryCollection({
9494
queryKey: ['todoItems'],
9595
queryFn: async () => fetch('/api/todos'),
96-
getPrimaryKey: (item) => item.id
96+
getId: (item) => item.id
9797
})
9898
```
9999

examples/react/todo/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ const createTodoCollection = (type: CollectionType) => {
167167
updated_at: todo.updated_at ? new Date(todo.updated_at) : undefined,
168168
}))
169169
},
170-
getPrimaryKey: (item) => String(item.id),
170+
getId: (item) => String(item.id),
171171
schema: updateTodoSchema,
172172
queryClient,
173173
})
@@ -220,7 +220,7 @@ const createConfigCollection = (type: CollectionType) => {
220220
: undefined,
221221
}))
222222
},
223-
getPrimaryKey: (item) => String(item.id),
223+
getId: (item) => String(item.id),
224224
schema: updateConfigSchema,
225225
queryClient,
226226
})

packages/db-collections/src/query.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface QueryCollectionConfig<
1515
> extends Omit<CollectionConfig<TItem>, `sync`> {
1616
queryKey: TQueryKey
1717
queryFn: (context: QueryFunctionContext<TQueryKey>) => Promise<Array<TItem>>
18-
getPrimaryKey: (item: TItem) => string
18+
getId: (item: TItem) => string
1919

2020
enabled?: boolean
2121
refetchInterval?: QueryObserverOptions<
@@ -71,11 +71,11 @@ export class QueryCollection<
7171
if (!queryClient)
7272
throw new Error(`[QueryCollection] queryClient must be provided.`)
7373
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
74-
if (!baseCollectionConfig.getPrimaryKey) {
75-
throw new Error(`[QueryCollection] getPrimaryKey must be provided.`)
74+
if (!baseCollectionConfig.getId) {
75+
throw new Error(`[QueryCollection] getId must be provided.`)
7676
}
7777

78-
const getPrimaryKeyFn = baseCollectionConfig.getPrimaryKey
78+
const getIdFn = baseCollectionConfig.getId
7979

8080
const internalSync: SyncConfig<TItem>[`sync`] = (params) => {
8181
const { begin, write, commit, collection } = params
@@ -124,7 +124,7 @@ export class QueryCollection<
124124
const newItemsMap = new Map<string, TItem>()
125125
newItemsArray.forEach((item) => {
126126
try {
127-
const key = getPrimaryKeyFn(item)
127+
const key = getIdFn(item)
128128
newItemsMap.set(key, item)
129129
} catch (e) {
130130
console.error(

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface TestItem {
99
value?: number
1010
}
1111

12-
const getPrimaryKey = (item: TestItem) => item.id
12+
const getId = (item: TestItem) => item.id
1313

1414
// Helper to advance timers and allow microtasks to flush
1515
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0))
@@ -49,7 +49,7 @@ describe(`QueryCollection`, () => {
4949
queryClient,
5050
queryKey,
5151
queryFn,
52-
getPrimaryKey,
52+
getId,
5353
}
5454

5555
const collection = createQueryCollection(config)
@@ -100,7 +100,7 @@ describe(`QueryCollection`, () => {
100100
queryClient,
101101
queryKey,
102102
queryFn,
103-
getPrimaryKey,
103+
getId,
104104
}
105105

106106
const collection = createQueryCollection(config)
@@ -168,7 +168,7 @@ describe(`QueryCollection`, () => {
168168
queryClient,
169169
queryKey,
170170
queryFn,
171-
getPrimaryKey,
171+
getId,
172172
retry: 0, // Disable retries for this test case
173173
})
174174

@@ -217,7 +217,7 @@ describe(`QueryCollection`, () => {
217217
queryClient,
218218
queryKey,
219219
queryFn,
220-
getPrimaryKey,
220+
getId,
221221
})
222222

223223
// Wait for the query to execute
@@ -264,7 +264,7 @@ describe(`QueryCollection`, () => {
264264
queryClient,
265265
queryKey,
266266
queryFn,
267-
getPrimaryKey,
267+
getId,
268268
})
269269

270270
// Wait for initial data to load
@@ -320,7 +320,7 @@ describe(`QueryCollection`, () => {
320320
consoleSpy.mockRestore()
321321
})
322322

323-
it(`should use the provided getPrimaryKey function to identify items`, async () => {
323+
it(`should use the provided getId function to identify items`, async () => {
324324
const queryKey = [`customKeyTest`]
325325

326326
// Items with a non-standard ID field
@@ -331,15 +331,15 @@ describe(`QueryCollection`, () => {
331331

332332
const queryFn = vi.fn().mockResolvedValue(items)
333333

334-
// Create a spy for the getPrimaryKey function
335-
const getPrimaryKeySpy = vi.fn((item: any) => item.customId)
334+
// Create a spy for the getId function
335+
const getIdSpy = vi.fn((item: any) => item.customId)
336336

337337
const collection = createQueryCollection({
338338
id: `test`,
339339
queryClient,
340340
queryKey,
341341
queryFn,
342-
getPrimaryKey: getPrimaryKeySpy,
342+
getId: getIdSpy,
343343
})
344344

345345
// Wait for initial data to load
@@ -348,10 +348,10 @@ describe(`QueryCollection`, () => {
348348
expect(collection.state.size).toBe(items.length)
349349
})
350350

351-
// Verify getPrimaryKey was called for each item
352-
expect(getPrimaryKeySpy).toHaveBeenCalledTimes(items.length)
351+
// Verify getId was called for each item
352+
expect(getIdSpy).toHaveBeenCalledTimes(items.length)
353353
items.forEach((item) => {
354-
expect(getPrimaryKeySpy).toHaveBeenCalledWith(item)
354+
expect(getIdSpy).toHaveBeenCalledWith(item)
355355
})
356356

357357
// Verify items are stored with the custom keys
@@ -368,7 +368,7 @@ describe(`QueryCollection`, () => {
368368
]
369369

370370
// Reset the spy to track new calls
371-
getPrimaryKeySpy.mockClear()
371+
getIdSpy.mockClear()
372372
queryFn.mockResolvedValueOnce(updatedItems)
373373

374374
// Trigger a refetch
@@ -380,11 +380,11 @@ describe(`QueryCollection`, () => {
380380
expect(collection.state.size).toBe(updatedItems.length)
381381
})
382382

383-
// Verify getPrimaryKey was called at least once for each item
383+
// Verify getId was called at least once for each item
384384
// It may be called multiple times per item during the diffing process
385-
expect(getPrimaryKeySpy).toHaveBeenCalled()
385+
expect(getIdSpy).toHaveBeenCalled()
386386
updatedItems.forEach((item) => {
387-
expect(getPrimaryKeySpy).toHaveBeenCalledWith(item)
387+
expect(getIdSpy).toHaveBeenCalledWith(item)
388388
})
389389

390390
// Verify the state reflects the changes

0 commit comments

Comments
 (0)