feat: add mutations
This commit is contained in:
@@ -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<Schema, NodePgZeroTransaction<typeof db>>;
|
||||
|
||||
// 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(
|
||||
|
||||
@@ -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<Schema>;
|
||||
} as const satisfies ZeroOptions<Schema, Mutators>;
|
||||
}, [authData, cookie]);
|
||||
|
||||
return (
|
||||
|
||||
@@ -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<Schema, Mutators>();
|
||||
const [transactions] = useQuery(queries.allTransactions(session));
|
||||
|
||||
const onNew = () => {
|
||||
z.mutate.transaction.create({
|
||||
name: "Uber",
|
||||
amount: 100,
|
||||
})
|
||||
};
|
||||
|
||||
return (
|
||||
<SafeAreaView>
|
||||
<Text>Hello {session?.user.name}</Text>
|
||||
<Button onPress={onLogout} title="Logout" />
|
||||
<Text>Transactions: {JSON.stringify(transactions, null, 4)}</Text>
|
||||
<Button onPress={onNew} title="New" />
|
||||
</SafeAreaView>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
export * from "./schema";
|
||||
export * from "./queries";
|
||||
export * from "./mutators";
|
||||
export * from "./zql";
|
||||
|
||||
24
shared/src/mutators.ts
Normal file
24
shared/src/mutators.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { Transaction } from "@rocicorp/zero";
|
||||
import type { AuthData } from "./auth";
|
||||
import type { Schema } from "./schema";
|
||||
import { isLoggedIn } from "./zql";
|
||||
|
||||
type Tx = Transaction<Schema>;
|
||||
|
||||
export function createMutators(authData: AuthData | null) {
|
||||
return {
|
||||
transaction: {
|
||||
async create(tx: Tx, { name, amount }: { name: string, amount: number }) {
|
||||
isLoggedIn(authData);
|
||||
await tx.mutate.transaction.insert({
|
||||
id: 'id-' + Math.random().toString(),
|
||||
user_id: authData.user.id,
|
||||
name,
|
||||
amount,
|
||||
})
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
}
|
||||
|
||||
export type Mutators = ReturnType<typeof createMutators>;
|
||||
Reference in New Issue
Block a user