T
TanStack3w ago
sunny-green

creating an empty collection and ready state

I'm having this issue where i'm basically creating a collection from another one, for instance
const subCollection = createCollection(
queryCollectionOptions({
queryClient: queryClient,
queryKey: ['...'],
queryFn: async () => {
return [] as PullRequestStatus[];
},
getKey: item => item.id,
}),
);


const mainCollection = createCollection(
queryCollectionOptions({
queryClient: queryClient,
queryKey: ['sync'],
queryFn: async () => {
const { data } = await http
.get(`sync`)
.json();

data.forEach(data => {
subCollection.utils.writeUpsert(data.subData);
});

return data
},
getKey: item => item.id,
})
const subCollection = createCollection(
queryCollectionOptions({
queryClient: queryClient,
queryKey: ['...'],
queryFn: async () => {
return [] as PullRequestStatus[];
},
getKey: item => item.id,
}),
);


const mainCollection = createCollection(
queryCollectionOptions({
queryClient: queryClient,
queryKey: ['sync'],
queryFn: async () => {
const { data } = await http
.get(`sync`)
.json();

data.forEach(data => {
subCollection.utils.writeUpsert(data.subData);
});

return data
},
getKey: item => item.id,
})
the objective of this is to populate this collection with the data from the other, and then via an SSE, start updating those elements as necessary. basically this collection does not do a sync of any type. however, with this i'm getting an error:
SyncNotInitializedError: Collection must be in 'ready' state for manual sync operations. Sync not initialized yet.
SyncNotInitializedError: Collection must be in 'ready' state for manual sync operations. Sync not initialized yet.
when trying to write on to it. any ideas? I think I can circumvent this whole thing but I'm wondering if this might be a bug?
9 Replies
evident-indigo
evident-indigo3w ago
The sub collections can just be local collections to simplify this. Query collections are just if you're actually fetching data generally
sunny-green
sunny-greenOP3w ago
oh, that makes sense! I'll try them out. the docs seem to be dead though: https://tanstack.com/db/latest/docs/overview/collections/local-only-collection I did something like:
export const subCollection = createCollection(
localOnlyCollectionOptions({
initialData: [],
getKey: item => item.id,
}),
);
export const subCollection = createCollection(
localOnlyCollectionOptions({
initialData: [],
getKey: item => item.id,
}),
);
which seems to fail for me as there's no utils.writeUpsert on it
evident-indigo
evident-indigo3w ago
GitHub
Fix broken TanStack documentation links by KyleAMathews · Pull Req...
Changed hash-based links (#localstoragecollection and #localonlycollection) to proper file path links (./collections/local-storage-collection.md and ./collections/local-only-collection.md) in the o...
evident-indigo
evident-indigo3w ago
LocalOnly Collection | TanStack DB Docs
LocalOnly Collection LocalOnly collections are designed for in-memory client data or UI state that doesn't need to persist across browser sessions or sync across tabs. Overview The localOnlyCollection...
sunny-green
sunny-greenOP3w ago
thank you! I see there's insert, but no upsert. I can probably sidestep that but is there anything wrong with using a queryCollection even though i'm using it as local? seems like I got it to work when I actually did a subscription to that collection
evident-indigo
evident-indigo3w ago
It might work to use query collection but you're fighting against the grain a bit
sunny-green
sunny-greenOP3w ago
I see, would be great for collections to share a common interface for utils, really missing that upsert
evident-indigo
evident-indigo3w ago
upsert is just (if collection.has(id) { collection.update() } else { collection.insert() } make your own! upsert can have different semantics so it's not going to be added to the main collection api
sunny-green
sunny-greenOP3w ago
oh I see -- I would've assumed they all shared the same 'core' db part but that was a big assumption I guess

Did you find this page helpful?