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 selectTodoSchema,
6 selectProjectSchema,
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 }) => {
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 },
59 onUpdate: async ({ transaction }) => {
60 const { modified: updatedProject } = transaction.mutations[0]
61 await trpc.projects.update.mutate({
62 id: updatedProject.id,
63 data: {
64 name: updatedProject.name,
65 description: updatedProject.description,
66 shared_user_ids: updatedProject.shared_user_ids,
67 },
68 })
69 },
70 onDelete: async ({ transaction }) => {
71 const { original: deletedProject } = transaction.mutations[0]
72 await trpc.projects.delete.mutate({
73 id: deletedProject.id,
74 })
75 },
76 })
77)
78
79export const todoCollection = createCollection(
80 queryCollectionOptions({
81 id: 'todos',
82 queryKey: ['todos'],
83 // Poll for updates every 5 seconds
84 refetchInterval: 5000,
85 queryFn: async () => {
86 const todos = await trpc.todos.getAll.query()
87 return todos.map((todo) => ({
88 ...todo,
89 created_at: new Date(todo.created_at),
90 updated_at: new Date(todo.updated_at),
91 }))
92 },
93 queryClient,
94 schema: selectTodoSchema,
95 getKey: (item) => item.id,
96 onInsert: async ({ transaction }) => {
97 const { modified: newTodo } = transaction.mutations[0]
98 await trpc.todos.create.mutate({
99 user_id: newTodo.user_id,
100 text: newTodo.text,
101 completed: newTodo.completed,
102 project_id: newTodo.project_id,
103 user_ids: newTodo.user_ids,
104 })
105 },
106 onUpdate: async ({ transaction }) => {
107 const { modified: updatedTodo } = transaction.mutations[0]
108 await trpc.todos.update.mutate({
109 id: updatedTodo.id,
110 data: {
111 text: updatedTodo.text,
112 completed: updatedTodo.completed,
113 },
114 })
115 },
116 onDelete: async ({ transaction }) => {
117 const { original: deletedTodo } = transaction.mutations[0]
118 await trpc.todos.delete.mutate({
119 id: deletedTodo.id,
120 })
121 },
122 })
123)