From 032f38b711f3f91089c4ebc5abc0da782591343f Mon Sep 17 00:00:00 2001 From: Max Koon <22125083+k2on@users.noreply.github.com> Date: Mon, 13 Oct 2025 23:45:20 -0400 Subject: [PATCH] feat: add mutations --- api/src/zero.ts | 64 +++++++++++------------------------------- app/_layout.tsx | 6 ++-- app/index.tsx | 12 +++++++- shared/src/index.ts | 2 ++ shared/src/mutators.ts | 24 ++++++++++++++++ 5 files changed, 56 insertions(+), 52 deletions(-) create mode 100644 shared/src/mutators.ts diff --git a/api/src/zero.ts b/api/src/zero.ts index d9c22ab..8055fc5 100644 --- a/api/src/zero.ts +++ b/api/src/zero.ts @@ -1,6 +1,5 @@ import { type ReadonlyJSONValue, - type ServerTransaction, withValidation, } from "@rocicorp/zero"; import { @@ -8,64 +7,33 @@ import { PushProcessor, ZQLDatabase, } from "@rocicorp/zero/server"; +import { PostgresJSConnection } from '@rocicorp/zero/pg'; +import postgres from 'postgres'; import { - // createMutators as createMutatorsShared, - // isLoggedIn, - // type Mutators, + createMutators as createMutatorsShared, queries, schema, - type Schema, } from "@money/shared"; import type { AuthData } from "@money/shared/auth"; -// import { auditLogs } from "@zslack/shared/db"; -// import { -// NodePgConnection, -// type NodePgZeroTransaction, -// } from "drizzle-zero/node-postgres"; -import crypto from "node:crypto"; -// import { db } from "./db"; import { getHono } from "./hono"; -// type ServerTx = ServerTransaction>; -// const processor = new PushProcessor( -// new ZQLDatabase(new NodePgConnection(db), schema), -// ); -// -// const createMutators = (authData: AuthData | null) => { -// const mutators = createMutatorsShared(authData); -// -// return { -// ...mutators, -// message: { -// ...mutators.message, -// async sendMessage(tx: ServerTx, params) { -// isLoggedIn(authData); -// -// await mutators.message.sendMessage(tx, params); -// -// // we can use the db tx to insert server-only data, like audit logs -// await tx.dbTransaction.wrappedTransaction.insert(auditLogs).values({ -// id: crypto.randomUUID(), -// userId: authData.user.id, -// action: "sendMessage", -// }); -// }, -// }, -// } as const satisfies Mutators; -// }; +const processor = new PushProcessor( + new ZQLDatabase( + new PostgresJSConnection(postgres(process.env.ZERO_UPSTREAM_DB! as string)), + schema, + ), +); const zero = getHono() - // .post("/mutate", async (c) => { - // // get the auth data from betterauth - // const authData = c.get("auth"); - // - // const result = await processor.process(createMutators(authData), c.req.raw); - // - // return c.json(result); - // }) + .post("/mutate", async (c) => { + const authData = c.get("auth"); + + const result = await processor.process(createMutatorsShared(authData), c.req.raw); + + return c.json(result); + }) .post("/get-queries", async (c) => { - // get the auth data from betterauth const authData = c.get("auth"); const result = await handleGetQueriesRequest( diff --git a/app/_layout.tsx b/app/_layout.tsx index 62016ea..e927dc5 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -7,7 +7,7 @@ import { useMemo } from 'react'; import { authDataSchema } from '@/shared/src/auth'; import { Platform } from 'react-native'; import type { ZeroOptions } from '@rocicorp/zero'; -import { schema, type Schema } from '@/shared/src'; +import { schema, type Schema, createMutators, type Mutators } from '@/shared/src'; import { expoSQLiteStoreProvider } from "@rocicorp/zero/react-native"; export const unstable_settings = { @@ -35,9 +35,9 @@ export default function RootLayout() { server: 'http://localhost:4848', userID: authData?.user.id ?? "anon", schema, - // mutators: createMutators(), + mutators: createMutators(authData), auth: cookie, - } as const satisfies ZeroOptions; + } as const satisfies ZeroOptions; }, [authData, cookie]); return ( diff --git a/app/index.tsx b/app/index.tsx index a9247cc..292f05e 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -2,20 +2,30 @@ import { SafeAreaView } from 'react-native-safe-area-context'; import { authClient } from '@/lib/auth-client'; import { Button, Text } from 'react-native'; import { useQuery, useZero } from "@rocicorp/zero/react"; -import { queries } from '@money/shared'; +import { queries, type Mutators, type Schema } from '@money/shared'; export default function HomeScreen() { const { data: session } = authClient.useSession(); + const onLogout = () => { authClient.signOut(); } + const z = useZero(); const [transactions] = useQuery(queries.allTransactions(session)); + const onNew = () => { + z.mutate.transaction.create({ + name: "Uber", + amount: 100, + }) + }; + return ( Hello {session?.user.name}