Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

app-includes.js

28 lines832 Bytes

1// The typical app, plus a live query whose rows include a child query.
2import { createCollection, eq, toArray } from '@tanstack/db'
3import { queryCollectionOptions } from '@tanstack/query-db-collection'
4import { useLiveQuery } from '@tanstack/react-db'
5import { queryClient, todos } from './app.js'
6​
7export * from './app.js'
8​
9const users = createCollection(
10 queryCollectionOptions({
11 queryKey: [`users`],
12 queryFn: () => fetch(`/api/users`).then((response) => response.json()),
13 queryClient,
14 getKey: (user) => user.id,
15 }),
17​
18export function UsersWithTodos() {
19 const { data } = useLiveQuery((q) =>
20 q.from({ user: users }).select(({ user }) => ({
21 name: user.name,
22 todos: toArray(
23 q.from({ todo: todos }).where(({ todo }) => eq(todo.userId, user.id)),
24 ),
25 })),
26 )
27 return data