Shared access between users
Build sharing between users with a shares table and existence-based permissions.After user-owned data, the next pattern you're likely to need is sharing. This recipe shows how to let one user grant another access to specific rows using a shares table.
Schema
Add a todoShares table that records which user has access to which todo.
schema.tsimport { schema as s } from "jazz-tools";export const schema = s.defineSchema({todoShares: s.table({ todoId: s.uuid(), user_id: s.uuid(), can_edit: s.boolean() },{todo: s.rel("todos", "todoId"),},),todos: s.table({ title: s.string(), done: s.boolean() },{shares: s.reverse("todoShares", "todo"),},),});
Permissions
The creator can do everything. Share recipients get read access, and optionally edit access via can_edit.
Only the todo's creator can insert and delete share rows. A share row is readable by its recipient
and by the todo's creator, who needs it to list and revoke access.
schema.tsconst { definePermissions, exists, session, anyOf } = schema;const createdByMe = { $createdBy: session.user };export const permissions = definePermissions({todoShares: {read: (todoShare) => anyOf({ user_id: session.user }, todoShare.todo.where(createdByMe)),insert: (todoShare) => todoShare.todo.where(createdByMe),delete: (todoShare) => todoShare.todo.where(createdByMe),},todos: {read: (todo) =>anyOf(createdByMe, exists.todoShares({ todoId: todo.id, user_id: session.user })),insert: true,update: (todo) =>anyOf(createdByMe,exists.todoShares({ todoId: todo.id, user_id: session.user, can_edit: true }),),delete: createdByMe,},});
See Declarative Permissions for more on exists, anyOf, and relation .where.
Granting access
To share a todo, insert a row into todoShares.
export function shareTodo(db: ReturnType<typeof useDb>,todoId: string,recipientAccountId: string,) {db.insert(app.todoShares, {todoId,user_id: recipientAccountId,can_edit: false,});}
Finding the recipient's account
A share names the recipient's Jazz account, which the sharer's browser doesn't know. Look it up in
your backend: find the person in your auth library (for example by an email address the sharer
typed), then await jazz.createAccountFor(user.id) returns their account ID, creating the account
if they don't have one yet; calling it again returns the same ID. Return that ID to the client.
- Match only on details your auth library has verified, such as a verified email address. Otherwise someone could sign up with another person's address and receive their shares.
- Creating the account early has one cost: a guest (device account) can no longer link to that user when they sign up later, so their guest data stays separate. See Users without an account.
To share with someone without knowing who they are in your app, send them a link instead: see Invite links.
Querying shared items
SharedWithMe.tsxexport function SharedWithMe() {const session = useJazzSession();const {data: shares,isLoading,error,} = useAll(session.user?.accountId? app.todoShares.where({ user_id: session.user.accountId }).include({ todo: true }): undefined,);if (isLoading) return <p>Loading…</p>;if (error) return <p>Something went wrong!</p>;return (<ul>{shares?.map((share) =>share.todo ? (<li key={share.id}>{share.todo.title}{share.can_edit ? " (can edit)" : " (read-only)"}</li>) : null,)}</ul>);}
Listing who has access
The creator can list a todo's shares, since they can read them:
SharedWith.tsxexport function SharedWith({ todoId }: { todoId: string }) {const { data: shares = [], db } = useAll(app.todoShares.where({ todoId }));return (<ul>{shares.map((share) => (<li key={share.id}>{share.user_id}{share.can_edit ? " (can edit)" : " (read-only)"}<button onClick={() => unshareTodo(db, share.id)}>Remove</button></li>))}</ul>);}
Revoking access
Delete the share row to revoke access. The server will stop syncing the todo to the former recipient. A recipient who is offline keeps their cached copy until they reconnect; see What a removed member still sees.
export function unshareTodo(db: ReturnType<typeof useDb>, shareId: string) {db.delete(app.todoShares, shareId);}