55// See https://github.com/rocicorp/mono/blob/main/apps/zbugs/src/domain/schema.ts
66// for more complex examples, including many-to-many.
77
8- import type { ExpressionBuilder , Row , TableSchema } from '@rocicorp/zero'
9- import { ANYONE_CAN , createSchema , createTableSchema , definePermissions , NOBODY_CAN } from '@rocicorp/zero'
8+ import type { ExpressionBuilder , Row } from '@rocicorp/zero'
9+ import {
10+ ANYONE_CAN ,
11+ boolean ,
12+ createSchema ,
13+ definePermissions ,
14+ NOBODY_CAN ,
15+ number ,
16+ relationships ,
17+ string ,
18+ table ,
19+ } from '@rocicorp/zero'
1020
11- const userSchema = createTableSchema ( {
12- tableName : 'user' ,
13- columns : {
14- id : 'string' ,
15- name : 'string' ,
16- partner : 'boolean' ,
17- } ,
18- primaryKey : 'id' ,
19- } )
21+ const user = table ( 'user' )
22+ . columns ( {
23+ id : string ( ) ,
24+ name : string ( ) ,
25+ partner : boolean ( ) ,
26+ } )
27+ . primaryKey ( 'id' )
2028
21- const mediumSchema = createTableSchema ( {
22- tableName : 'medium' ,
23- columns : {
24- id : 'string' ,
25- name : 'string' ,
26- } ,
27- primaryKey : 'id' ,
28- } )
29+ const medium = table ( 'medium' )
30+ . columns ( {
31+ id : string ( ) ,
32+ name : string ( ) ,
33+ } )
34+ . primaryKey ( 'id' )
2935
30- const messageSchema = createTableSchema ( {
31- tableName : 'message' ,
32- columns : {
33- id : 'string' ,
34- senderID : 'string' ,
35- mediumID : 'string' ,
36- body : 'string' ,
37- timestamp : 'number' ,
38- } ,
39- primaryKey : 'id' ,
40- relationships : {
41- sender : {
42- sourceField : 'senderID' ,
43- destSchema : userSchema ,
44- destField : 'id' ,
45- } ,
46- medium : {
47- sourceField : 'mediumID' ,
48- destSchema : mediumSchema ,
49- destField : 'id' ,
50- } ,
51- } ,
52- } )
36+ const message = table ( 'message' )
37+ . columns ( {
38+ id : string ( ) ,
39+ senderID : string ( ) ,
40+ mediumID : string ( ) ,
41+ body : string ( ) ,
42+ timestamp : number ( ) ,
43+ } )
44+ . primaryKey ( 'id' )
45+
46+ const messageRelationships = relationships ( message , ( { one } ) => ( {
47+ sender : one ( {
48+ sourceField : [ 'senderID' ] ,
49+ destField : [ 'id' ] ,
50+ destSchema : user ,
51+ } ) ,
52+ medium : one ( {
53+ sourceField : [ 'mediumID' ] ,
54+ destField : [ 'id' ] ,
55+ destSchema : medium ,
56+ } ) ,
57+ } ) )
5358
54- export const schema = createSchema ( {
55- version : 1 ,
56- tables : {
57- user : userSchema ,
58- medium : mediumSchema ,
59- message : messageSchema ,
60- } ,
59+ export const schema = createSchema ( 1 , {
60+ tables : [ user , medium , message ] ,
61+ relationships : [ messageRelationships ] ,
6162} )
6263
6364export type Schema = typeof schema
64- export type Message = Row < typeof messageSchema >
65- export type Medium = Row < typeof mediumSchema >
65+ export type Message = Row < typeof schema . tables . message >
66+ export type Medium = Row < typeof schema . tables . medium >
6667export type User = Row < typeof schema . tables . user >
6768
6869// The contents of your decoded JWT.
@@ -73,12 +74,12 @@ interface AuthData {
7374export const permissions = definePermissions < AuthData , Schema > ( schema , ( ) => {
7475 const allowIfLoggedIn = (
7576 authData : AuthData ,
76- { cmpLit } : ExpressionBuilder < TableSchema > ,
77+ { cmpLit } : ExpressionBuilder < Schema , keyof Schema [ 'tables' ] > ,
7778 ) => cmpLit ( authData . sub , 'IS NOT' , null )
7879
7980 const allowIfMessageSender = (
8081 authData : AuthData ,
81- { cmp } : ExpressionBuilder < typeof messageSchema > ,
82+ { cmp } : ExpressionBuilder < Schema , 'message' > ,
8283 ) => cmp ( 'senderID' , '=' , authData . sub ?? '' )
8384
8485 return {
0 commit comments