-
Couldn't load subscription status.
- Fork 108
Open
Description
After reading Tanstack DB documentation, I was not able to find an example that fetches data from server with parameters. All examples show how to fetch all the data. I need to use project ID to fetch that project's data, not everything else.
Fow example, this creates a collection. queryFn: async () has no parameter.
const todoCollection = createCollection(
queryCollectionOptions({
queryKey: ['todos'],
queryFn: async () => {
const response = await fetch('/api/todos')
return response.json()
},
getKey: (item) => item.id,
// Handle all CRUD operations
onInsert: async ({ transaction }) => {
const { modified: newTodo } = transaction.mutations[0]
await fetch('/api/todos', {
method: 'POST',
body: JSON.stringify(newTodo),
})
},
onUpdate: async ({ transaction }) => {
const { original, modified } = transaction.mutations[0]
await fetch(`/api/todos/${original.id}`, {
method: 'PUT',
body: JSON.stringify(modified),
})
},
onDelete: async ({ transaction }) => {
const { original } = transaction.mutations[0]
await fetch(`/api/todos/${original.id}`, { method: 'DELETE' })
},
})
)
Since, todoCollection is being used in a component like below, I can not put it in a useEffect to either.
function TodoList() {
// Basic filtering and sorting
const { data: incompleteTodos } = useLiveQuery((q) =>
q.from({ todo: todoCollection })
.where(({ todo }) => eq(todo.completed, false))
.orderBy(({ todo }) => todo.createdAt, 'desc')
)
// Transform the data
const { data: todoSummary } = useLiveQuery((q) =>
q.from({ todo: todoCollection })
.select(({ todo }) => ({
id: todo.id,
summary: `${todo.text} (${todo.completed ? 'done' : 'pending'})`,
priority: todo.priority || 'normal'
}))
)
return <div>{/* Render todos */}</div>
}
What I want is something like below:
const todoCollection = createCollection(
queryCollectionOptions({
queryKey: ['todos'],
queryFn: async (id: string) => {
const response = await fetch('/api/todos/' + id);
return response.json();
},
getKey: item => item.id,
})
);
Is there a way to do this?
ammubhave and giladv
Metadata
Metadata
Assignees
Labels
No labels