feat: add auth to zero queries
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import { syncedQuery } from "@rocicorp/zero";
|
||||
import { syncedQueryWithContext } from "@rocicorp/zero";
|
||||
import { z } from "zod";
|
||||
import { builder } from "@money/shared";
|
||||
import type { AuthData } from "./auth";
|
||||
import { isLoggedIn } from "./zql";
|
||||
|
||||
export const queries = {
|
||||
allTransactions: syncedQuery('allTransactions', z.tuple([]), () =>
|
||||
builder.transaction.limit(10)
|
||||
allTransactions: syncedQueryWithContext('allTransactions', z.tuple([]), (authData: AuthData | null) => {
|
||||
isLoggedIn(authData);
|
||||
return builder.transaction
|
||||
.where('user_id', '=', authData.user.id)
|
||||
.limit(10)
|
||||
}
|
||||
),
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { createSchema, table, string, number, createBuilder, definePermissions } from "@rocicorp/zero";
|
||||
import { type Schema as ZeroSchema, createSchema, table, string, number, createBuilder, definePermissions } from "@rocicorp/zero";
|
||||
|
||||
const transaction = table('transaction')
|
||||
.columns({
|
||||
@@ -7,13 +7,20 @@ const transaction = table('transaction')
|
||||
name: string(),
|
||||
amount: number(),
|
||||
})
|
||||
.primaryKey('id');
|
||||
.primaryKey('id').schema;
|
||||
|
||||
export const schema = createSchema({
|
||||
tables: [transaction],
|
||||
export const schema = {
|
||||
tables: { transaction },
|
||||
relationships: {},
|
||||
enableLegacyMutators: false,
|
||||
enableLegacyQueries: false,
|
||||
});
|
||||
} satisfies ZeroSchema;
|
||||
|
||||
// export const schema = createSchema({
|
||||
// tables: [transaction],
|
||||
// enableLegacyMutators: false,
|
||||
// enableLegacyQueries: false,
|
||||
// });
|
||||
|
||||
export const builder = createBuilder(schema);
|
||||
|
||||
|
||||
9
shared/src/zql.ts
Normal file
9
shared/src/zql.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { AuthData } from "./auth";
|
||||
|
||||
export function isLoggedIn(
|
||||
authData: AuthData | null,
|
||||
): asserts authData is AuthData {
|
||||
if (!authData?.user.id) {
|
||||
throw new Error("User is not logged in");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user