// 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 schema export type Message = Row export type Medium = Row export type User = Row // The contents of your decoded JWT. type AuthData = { sub: string | null } export const permissions = definePermissions(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], }, }, } })