T
TanStack3w ago
ratty-blush

are collections meant to be singletons?

Hi friends, Just getting my feet wet here and thinking about the multi-tenant use case where we might want to have a query function that, for example, fetchs todos for a specific organization.
queryKey: ["organizations", ":orgId", "todos"]
queryFn: () => fetch("/api/:orgId/todos")
queryKey: ["organizations", ":orgId", "todos"]
queryFn: () => fetch("/api/:orgId/todos")
I think the obvious solution here is to create a factory that takes the orgId as an argument. However, from what I can see in the docs, collections seem to be something that is meant created once at application start. Is this true? or is it fine to dynamically create collections at runtime based on params ?
7 Replies
foreign-sapphire
foreign-sapphire3w ago
yeah sure! They're just fancy JS maps really so you can create them however would make sense
ratty-blush
ratty-blushOP3w ago
Oh perfect, thanks for the quick response!
optimistic-gold
optimistic-gold3w ago
I'm having a similar question. Does something like this work in react? Am I supposed to pass in the query client I get from a hook? This feels wrong so not sure.
const createSuggestionsCollection = (
queryClient: QueryClient,
queryKey: string[],
companySlug: string,
performanceReviewTaskId: string,
managerReviewId: string
) =>
createCollection<ManagerReviewSuggestion>(
queryCollectionOptions({
queryClient,
queryKey,
queryFn: async () => {
const response = await new ...
return response.suggestions;
},
getKey: (item) => {
return item.id;
},
onUpdate: async ({ transaction }) => {
const { original, modified } = transaction.mutations[0];
await new ...
},
onInsert: async ({ transaction }) => {
const { modified } = transaction.mutations[0];
await new...
},
})
);
const createSuggestionsCollection = (
queryClient: QueryClient,
queryKey: string[],
companySlug: string,
performanceReviewTaskId: string,
managerReviewId: string
) =>
createCollection<ManagerReviewSuggestion>(
queryCollectionOptions({
queryClient,
queryKey,
queryFn: async () => {
const response = await new ...
return response.suggestions;
},
getKey: (item) => {
return item.id;
},
onUpdate: async ({ transaction }) => {
const { original, modified } = transaction.mutations[0];
await new ...
},
onInsert: async ({ transaction }) => {
const { modified } = transaction.mutations[0];
await new...
},
})
);
It's working but getting this error when I try to update or insert
CollectionOperationError: The key "019936cf-b739-765a-8b9d-d422272baab9" was passed to update but an object for this key was not found in the collection
foreign-sapphire
foreign-sapphire3w ago
does that key exist? You can log console.log(collection.keys())
optimistic-gold
optimistic-gold3w ago
hmm getting <suspended> Anyone know what this means? I assume doing something wrong
foreign-sapphire
foreign-sapphire3w ago
I don't know what <suspended> means — it's not something db returns
optimistic-gold
optimistic-gold3w ago
Did you ever figure this out? I'm hitting this issue as well

Did you find this page helpful?