Open History to see the last commit
1import { createCollection } from '@tanstack/react-db'
2import { queryCollectionOptions } from '@tanstack/query-db-collection'
3import { QueryClient } from '@tanstack/query-core'
4import {
5 selectProjectSchema,
6 selectTodoSchema,
7 selectUsersSchema,
8} from '@/db/schema'
9import { trpc } from '@/lib/trpc-client'
10
11// Create a query client for query collections
12const queryClient = new QueryClient()
13
14export const usersCollection = createCollection(
15 queryCollectionOptions({
16 id: 'users',
17 queryKey: ['users'],
18 // Poll for updates every 5 seconds
19 refetchInterval: 5000,
20 queryFn: async () => {
21 const users = await trpc.users.getAll.query()
22 return users.map((user) => ({
23 ...user,
24 created_at: new Date(user.created_at),
25 updated_at: new Date(user.updated_at),
26 }))
27 },
28 queryClient,
29 schema: selectUsersSchema,
30 getKey: (item) => item.id,
31 })
32)
33export const projectCollection = createCollection(
34 queryCollectionOptions({
35 id: 'projects',
36 queryKey: ['projects'],
37 // Poll for updates every 5 seconds
38 refetchInterval: 5000,
39 queryFn: async () => {
40 const projects = await trpc.projects.getAll.query()
41 return projects.map((project) => ({
42 ...project,
43 created_at: new Date(project.created_at),
44 updated_at: new Date(project.updated_at),
45 }))
46 },
47 queryClient,
48 schema: selectProjectSchema,
49 getKey: (item) => item.id,
50 onInsert: async ({ transaction, collection }) => {
51 const { modified: newProject } = transaction.mutations[0]
52 await trpc.projects.create.mutate({
53 name: newProject.name,
54 description: newProject.description,
55 owner_id: newProject.owner_id,
56 shared_user_ids: newProject.shared_user_ids,
57 })
58 await collection.utils.refetch()
59 },
60 onUpdate: async ({ transaction, collection }) => {
61 const { modified: updatedProject } = transaction.mutations[0]
62 await trpc.projects.update.mutate({
63 id: updatedProject.id,
64 data: {
65 name: updatedProject.name,
66 description: updatedProject.description,
67 shared_user_ids: updatedProject.shared_user_ids,
68 },
69 })
70 await collection.utils.refetch()
71 },
72 onDelete: async ({ transaction, collection }) => {
73 const { original: deletedProject } = transaction.mutations[0]
74 await trpc.projects.delete.mutate({
75 id: deletedProject.id,
76 })
77 await collection.utils.refetch()
78 },
79 })
80)
81
82export const todoCollection = createCollection(
83 queryCollectionOptions({
84 id: 'todos',
85 queryKey: ['todos'],
86 // Poll for updates every 5 seconds
87 refetchInterval: 5000,
88 queryFn: async () => {
89 const todos = await trpc.todos.getAll.query()
90 return todos.map((todo) => ({
91 ...todo,
92 created_at: new Date(todo.created_at),
93 updated_at: new Date(todo.updated_at),
94 }))
95 },
96 queryClient,
97 schema: selectTodoSchema,
98 getKey: (item) => item.id,
99 onInsert: async ({ transaction, collection }) => {
100 const { modified: newTodo } = transaction.mutations[0]
101 await trpc.todos.create.mutate({
102 user_id: newTodo.user_id,
103 text: newTodo.text,
104 completed: newTodo.completed,
105 project_id: newTodo.project_id,
106 user_ids: newTodo.user_ids,
107 })
108 await collection.utils.refetch()
109 },
110 onUpdate: async ({ transaction, collection }) => {
111 const { modified: updatedTodo } = transaction.mutations[0]
112 await trpc.todos.update.mutate({
113 id: updatedTodo.id,
114 data: {
115 text: updatedTodo.text,
116 completed: updatedTodo.completed,
117 },
118 })
119 await collection.utils.refetch()
120 },
121 onDelete: async ({ transaction, collection }) => {
122 const { original: deletedTodo } = transaction.mutations[0]
123 await trpc.todos.delete.mutate({
124 id: deletedTodo.id,
125 })
126 await collection.utils.refetch()
127 },
128 })
129)