Skip to content

Commit

Permalink
Merge pull request #5 from rocicorp/aa/v0.12
Browse files Browse the repository at this point in the history
Upgrade to v0.12
  • Loading branch information
aboodman authored Jan 25, 2025
2 parents 7c10aa3 + 5696666 commit 50e9b50
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 43 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint": "eslint ."
},
"dependencies": {
"@rocicorp/zero": "^0.11.2025011402",
"@rocicorp/zero": "^0.12.2025012501",
"ansi-escapes": "^7.0.0",
"jose": "^5.9.6",
"js-cookie": "^3.0.5",
Expand Down
73 changes: 35 additions & 38 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,46 @@

import {
createSchema,
createTableSchema,
definePermissions,
ExpressionBuilder,
TableSchema,
Row,
NOBODY_CAN,
ANYONE_CAN,
table,
string,
boolean,
number,
relationships,
} from '@rocicorp/zero';

const userSchema = createTableSchema({
tableName: 'user',
columns: {
id: 'string',
name: 'string',
partner: 'boolean',
},
primaryKey: 'id',
});
const user = table('user')
.columns({
id: string(),
name: string(),
partner: boolean(),
})
.primaryKey('id');

const messageSchema = createTableSchema({
tableName: 'message',
columns: {
id: 'string',
senderID: 'string',
body: 'string',
timestamp: 'number',
},
primaryKey: 'id',
relationships: {
sender: {
sourceField: 'senderID',
destSchema: userSchema,
destField: 'id',
},
},
});
const message = table('message')
.columns({
id: string(),
senderID: string(),
body: string(),
timestamp: number(),
})
.primaryKey('id');

const messageRelationships = relationships(message, ({one}) => ({
sender: one({
sourceField: ['senderID'],
destSchema: user,
destField: ['id'],
}),
}));

export const schema = createSchema({
version: 1,
tables: {
user: userSchema,
message: messageSchema,
},
export const schema = createSchema(1, {
tables: [user, message],
relationships: [messageRelationships],
});

// The contents of your decoded JWT.
Expand All @@ -58,18 +55,18 @@ type AuthData = {
};

export type Schema = typeof schema;
export type Message = Row<typeof messageSchema>;
export type User = Row<typeof userSchema>;
export type Message = Row<typeof schema.tables.message>;
export type User = Row<typeof schema.tables.user>;

export const permissions = definePermissions<AuthData, Schema>(schema, () => {
const allowIfLoggedIn = (
authData: AuthData,
{cmpLit}: ExpressionBuilder<TableSchema>,
{cmpLit}: ExpressionBuilder<Schema, keyof Schema['tables']>,
) => cmpLit(authData.sub, 'IS NOT', null);

const allowIfMessageSender = (
authData: AuthData,
{cmp}: ExpressionBuilder<typeof messageSchema>,
{cmp}: ExpressionBuilder<Schema, 'message'>,
) => {
return cmp('senderID', '=', authData.sub);
};
Expand Down

0 comments on commit 50e9b50

Please sign in to comment.