T
TanStack3mo ago
xenial-black

is a live query collection just an alias for a regular collection?

maybe im overthinking this. say i have
const usersCollection = createCollection(
queryCollectionOptions({
id: "users",
const usersCollection = createCollection(
queryCollectionOptions({
id: "users",
and want to imperatively, outside of react, get a user.
// Assuming this is correct
usersCollection.get(id)
// Assuming this is correct
usersCollection.get(id)
and i want to alias a view of this query. in react-query i would use the select callback. the docs (https://tanstack.com/db/latest/docs/guides/live-queries) say to use live query to alias—
const activeUsers = createCollection(liveQueryCollectionOptions({
query: (q) =>
q
.from({ user: usersCollection })
.where(({ user }) => eq(user.active, true))
.select(({ user }) => ({
id: user.id,
name: user.name,
email: user.email,
}))
}))
const activeUsers = createCollection(liveQueryCollectionOptions({
query: (q) =>
q
.from({ user: usersCollection })
.where(({ user }) => eq(user.active, true))
.select(({ user }) => ({
id: user.id,
name: user.name,
email: user.email,
}))
}))
but it doesn't say what usersCollection is this scenario. so i'm assuming it is a regular createCollection(queryCollectionOptions({. I guess I'm not understanding the difference in these two types and their compatibility.
Live Queries | TanStack DB Docs
TanStack DB Live Queries TanStack DB provides a powerful, type-safe query system that allows you to fetch, filter, transform, and aggregate data from collections using a SQL-like fluent API. All queri...
3 Replies
xenial-black
xenial-blackOP3mo ago
ill just give it a shot. but maybe my confusion is worth highlighting for docs purposes.
yappiest-sapphire
yappiest-sapphire3mo ago
yeah you use createCollection(liveQueryCollectionOptions({})) to create a standalone query queries are collections too which is why it has createCollection there you can query any other collection (including query collections!) so yeah, passing in your userCollection is what you do
xenial-black
xenial-blackOP3mo ago
lovely thank you kyle 🙂

Did you find this page helpful?