Nested data with permission inheritance
Build a project, task and comment hierarchy whose permissions are inherited, with queries and multi-level inserts.This recipe shows how to model a simple hierarchy, inherit permissions from parent rows, and query/insert at each level.
Schema
A simple schema with three tables linked by foreign keys. A project has tasks, and tasks have comments.
schema.tsimport { schema as s } from "jazz-tools";export const schema = s.defineSchema({comments: s.table({ body: s.string(), taskId: s.uuid() },{task: s.rel("tasks", "taskId"),},),projects: s.table({ name: s.string() },{tasks: s.reverse("tasks", "project"),},),tasks: s.table({ title: s.string(), done: s.boolean(), projectId: s.uuid() },{comments: s.reverse("comments", "task"),project: s.rel("projects", "projectId"),},),});
Inherited permissions
Use allowedTo to inherit access from the parent row. If you can read a project, you can read its tasks, and if you can read a task, you can read its comments.
Projects are only accessible to their creator. Tasks inherit every operation from their project: reading or adding a task needs read access to the project, editing or deleting one needs update or delete access to it. Comments can be read and added by anyone who can read their task, and edited or deleted only by their author.
schema.tsconst { definePermissions, allowedTo, session } = schema;const createdByMe = { $createdBy: session.user };export const permissions = definePermissions({comments: {read: (comment) => allowedTo.read(comment.task),insert: (comment) => allowedTo.read(comment.task),update: createdByMe,delete: createdByMe,},projects: {read: createdByMe,insert: true,update: createdByMe,delete: createdByMe,},tasks: {read: (task) => allowedTo.read(task.project),insert: (task) => allowedTo.read(task.project),update: (task) => allowedTo.update(task.project),delete: (task) => allowedTo.delete(task.project),},});
The argument of allowedTo.read(task.project) is a relation declared on the tasks table in schema.ts, here project: s.rel("projects", "projectId"). Stored column names and inferred aliases are not accepted. See Declarative Permissions for maxDepth and recursive inheritance.
Querying
ProjectTasks.tsxexport function ProjectTasks({ projectId }: { projectId: string }) {const {data: tasks,isLoading,error,} = useAll(app.tasks.where({ projectId }).orderBy("$createdAt", "desc"));if (isLoading) return <p>Loading…</p>;if (error) return <p>Something went wrong!</p>;return (<ul>{tasks.map((task) => (<li key={task.id}>{task.title}</li>))}</ul>);}
See Includes and relations for include(), reverse relations, and select().
Inserting
Insert from the top down — create the project first, then tasks referencing it.
CreateProject.tsxexport function CreateProject() {const db = useDb();async function handleCreate() {const { value: project } = await db.insert(app.projects, {name: "Website redesign",});await db.insert(app.tasks, {title: "Design homepage",done: false,projectId: project.id,});}return <button onClick={handleCreate}>New project</button>;}
Each insert executes locally and syncs in the background. Because permissions inherit downward, anyone who can access the project automatically gets access to its tasks and comments.