Replies: 1 comment
-
|
I would create a new collecton for the detailed todo-items and use the "on-demand"-mode, something like this: const TodoDetailSchema = z.object({
id: z.string(),
title: z.string(),
description: z.string(),
isCompleted: z.boolean(),
})
type TodoDetail = z.infer<typeof TodoDetailSchema>
const getId = (where: LoadSubsetOptions['where'] | undefined): string => {
const comparison = extractSimpleComparisons(where)
let id = ''
comparison.forEach(({ field, operator, value }) => {
if (field[0] === 'id') {
if (operator === 'eq') {
id = value
} else if (operator === 'in') {
id = value[0]
}
}
})
return id
}
export const todoDetailCollection = createCollection(
queryCollectionOptions({
queryKey: (ctx) => {
const id = getId(ctx.where)
if (!id) {
throw new Error('id is required')
}
return ['todos', 'detail', id]
},
queryFn: async (ctx) => {
const id = getId(ctx.meta?.loadSubsetOptions?.where)
if (!id) {
throw new Error('id is required')
}
const response = await fetch(`/api/todos/${id}`).then((res) => res.json())
return [response] as TodoDetail[]
},
schema: TodoDetailSchema,
queryClient,
syncMode: 'on-demand',
getKey: (item) => item.id,
})
)For the sake of the inArray-comparsion operator, it would be nice to be able to create multiple query-keys at once inside the queryKey-fn. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I have endpoint
/todoswith support of all filters that on demand mode provides this endpoint returns only preview of todo items not their full content. And also/todos/:idwhich returns exact todo item with all data.In tanstack query i would use ['todos'] and ['todos', someId] keys accordingly. Lets say todo item have a lot of data attached which isn't required to show list of todo but required to present single todo item on separate page.
How to deal with it using tanstack db? I figured out how to deal with list of items from docs:
But how I should define collection for single (expanded, full) todo item? What queryKey should I use? How to query this data?
Maybe I missing something but I didn't find anything in docs about this case
Beta Was this translation helpful? Give feedback.
All reactions