Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/hello-zero-nuxt.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110// These data structures define your client-side schema.// They must be equal to or a subset of the server-side schema.// Note the "relationships" field, which defines first-class// relationships between tables.// See https://github.com/rocicorp/mono/blob/main/apps/zbugs/shared/schema.ts// for more complex examples, including many-to-many.
import type { ExpressionBuilder, Row } from '@rocicorp/zero'import { ANYONE_CAN, boolean, createSchema, definePermissions, NOBODY_CAN, number, relationships, string, table } from '@rocicorp/zero'
const user = table('user') .columns({ id: string(), name: string(), partner: boolean(), }) .primaryKey('id')
const medium = table('medium') .columns({ id: string(), name: string(), }) .primaryKey('id')
const message = table('message') .columns({ id: string(), senderID: string(), mediumID: string(), body: string(), timestamp: number(), }) .primaryKey('id')
const messageRelationships = relationships(message, ({ one }) => ({ sender: one({ sourceField: ['senderID'], destField: ['id'], destSchema: user, }), medium: one({ sourceField: ['mediumID'], destField: ['id'], destSchema: medium, }),}))
export const schema = createSchema({ tables: [user, medium, message], relationships: [messageRelationships], enableLegacyQueries: true, enableLegacyMutators: true,})
export type Schema = typeof schemaexport type Message = Row<typeof schema.tables.message>export type Medium = Row<typeof schema.tables.medium>export type User = Row<typeof schema.tables.user>
// The contents of your decoded JWT.type AuthData = { sub: string | null}
export const permissions = definePermissions<AuthData, Schema>(schema, () => { const allowIfLoggedIn = ( authData: AuthData, { cmpLit }: ExpressionBuilder<'message', Schema>, ) => cmpLit(authData.sub, 'IS NOT', null)
const allowIfMessageSender = ( authData: AuthData, { cmp }: ExpressionBuilder<'message', Schema>, ) => cmp('senderID', '=', authData.sub ?? '')
return { medium: { row: { insert: NOBODY_CAN, update: { preMutation: NOBODY_CAN, }, delete: NOBODY_CAN, }, }, user: { row: { insert: NOBODY_CAN, update: { preMutation: NOBODY_CAN, }, delete: NOBODY_CAN, }, }, message: { row: { // anyone can insert insert: ANYONE_CAN, // only sender can edit their own messages update: { preMutation: [allowIfMessageSender], }, // must be logged in to delete delete: [allowIfLoggedIn], }, }, }})