diff --git a/.changeset/fast-trees-find.md b/.changeset/fast-trees-find.md new file mode 100644 index 0000000000..31fcb71b49 --- /dev/null +++ b/.changeset/fast-trees-find.md @@ -0,0 +1,9 @@ +--- +"@tanstack/db-collections": patch +"@tanstack/db-example-react-todo": patch +"@tanstack/react-db": patch +"@tanstack/vue-db": patch +"@tanstack/db": patch +--- + +Collections must have a getId function & use an id for update/delete operators diff --git a/examples/react/todo/src/App.tsx b/examples/react/todo/src/App.tsx index fee2ae2763..93fabb9866 100644 --- a/examples/react/todo/src/App.tsx +++ b/examples/react/todo/src/App.tsx @@ -149,7 +149,7 @@ const createTodoCollection = (type: CollectionType) => { timestamptz: (date: string) => new Date(date), }, }, - primaryKey: [`id`], + getId: (item) => item.id, schema: updateTodoSchema, }) } else { @@ -198,7 +198,7 @@ const createConfigCollection = (type: CollectionType) => { }, }, }, - primaryKey: [`id`], + getId: (item) => item.id, schema: updateConfigSchema, }) } else { @@ -220,7 +220,7 @@ const createConfigCollection = (type: CollectionType) => { : undefined, })) }, - getId: (item) => String(item.id), + getId: (item) => item.id, schema: updateConfigSchema, queryClient, }) @@ -290,10 +290,19 @@ export default function App() { const updateTodo = useOptimisticMutation({ mutationFn: async ({ transaction }) => { - const mutation = transaction.mutations[0] as PendingMutation - const { original, changes } = mutation - const response = await api.todos.update(original.id, changes) - await collectionSync(collectionType, mutation, response.txid) + const txids = await Promise.all( + transaction.mutations.map(async (mutation) => { + const { original, changes } = mutation + const response = await api.todos.update(original.id, changes) + return response.txid + }) + ) + await Promise.all( + txids.map(async (txid, i) => { + const mutation = transaction.mutations[i] + return await collectionSync(collectionType, mutation, txid) + }) + ) }, }) @@ -349,12 +358,9 @@ export default function App() { for (const config of configData) { if (config.key === key) { updateConfig.mutate(() => - configCollection.update( - Array.from(configCollection.state.values())[0]! as UpdateConfig, - (draft) => { - draft.value = value - } - ) + configCollection.update(config.id, (draft) => { + draft.value = value + }) ) return @@ -439,14 +445,9 @@ export default function App() { const toggleTodo = (todo: UpdateTodo) => { updateTodo.mutate(() => - todoCollection.update( - Array.from(todoCollection.state.values()).find( - (t) => t.id === todo.id - )!, - (draft) => { - draft.completed = !draft.completed - } - ) + todoCollection.update(todo.id, (draft) => { + draft.completed = !draft.completed + }) ) } @@ -544,9 +545,7 @@ export default function App() { todosToToggle.forEach((t) => togglingIds.add(t.id)) updateTodo.mutate(() => todoCollection.update( - Array.from(todoCollection.state.values()).filter((t) => - togglingIds.has(t.id) - ), + todosToToggle.map((todo) => todo.id), (drafts) => { drafts.forEach( (draft) => (draft.completed = !allCompleted) @@ -596,11 +595,7 @@ export default function App() {