Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/zero-vue. Vue bindings for Zero
sync vue zero
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185// 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/src/domain/schema.ts// for more complex examples, including many-to-many.
import type { Row } from '@rocicorp/zero'import { boolean, createBuilder, createSchema, defineMutator, defineMutators, definePermissions, defineQueries, defineQuery, escapeLike, number, relationships, string, table,} from '@rocicorp/zero'import { z } from 'zod'
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().from('sender_id'), mediumID: string().from('medium_id'), 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], enableLegacyMutators: false, enableLegacyQueries: false,})
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>
declare module '@rocicorp/zero' { interface DefaultTypes { schema: typeof schema }}
// The fixtures only use synced queries and custom mutators, so no table-level// permissions are granted. Deploying the empty set (rather than omitting the// export) keeps zero-cache from warning that no permissions were found.export const permissions = definePermissions<unknown, Schema>(schema, () => ({}))
export const zql = createBuilder(schema)
export interface ZeroContext { userID: string | undefined}
declare module '@rocicorp/zero' { interface DefaultTypes { context: ZeroContext }}
export const mutators = defineMutators({ message: { insert: defineMutator( z.object({ mediumID: z.string(), body: z.string(), id: z.string(), timestamp: z.number(), }), async ({ tx, ctx: { userID }, args: { mediumID, body, id, timestamp } }) => { if (!userID) { throw new Error('You must be logged in to add messages') }
return tx.mutate.message.insert({ senderID: userID, mediumID, body, id, timestamp, }) }, ), update: defineMutator( z.object({ id: z.string(), body: z.string() }), async ({ tx, ctx: { userID }, args: { id, body } }) => { const messageToEdit = await tx.run( zql.message.where('id', id).one(), ) if (!messageToEdit) { throw new Error(`Message with id ${id} not found`) }
if (messageToEdit.senderID !== userID) { throw new Error(`You aren't allowed to edit this message`) }
return tx.mutate.message.update({ id, body }) }, ), delete: defineMutator( z.object({ id: z.string() }), async ({ tx, ctx: { userID }, args: { id } }) => { if (!userID) { throw new Error('You must be logged in to delete') }
return tx.mutate.message.delete({ id }) }, ), },})
export const queries = defineQueries({ messages: { all: defineQuery(() => zql.message), filtered: defineQuery( z.object({ filterUser: z.string().optional(), filterText: z.string().optional(), }), ({ args: { filterUser, filterText } }) => { let filtered = zql.message .related('medium', medium => medium.one()) .related('sender', sender => sender.one()) .orderBy('timestamp', 'desc')
if (filterUser) { filtered = filtered.where('senderID', filterUser) }
if (filterText) { filtered = filtered.where('body', 'LIKE', `%${escapeLike(filterText)}%`) }
return filtered }, ), }, users: { all: defineQuery(() => zql.user), }, mediums: { all: defineQuery(() => zql.medium), },})