User-owned data

Build the most common Jazz pattern end to end: data that belongs to the user who created it.

This recipe covers building an app where users own their own data and no-one else can see it, covering schema, permissions, querying, and inserting.

Schema

There's no need to add an explicit owner column — Jazz tracks who created each row automatically via $createdBy.

schema.ts
import { schema as s } from "jazz-tools";
​
export const schema = s.defineSchema({
todos: s.table({
title: s.string(),
done: s.boolean(),
}),
});

See Defining tables for everything a table can declare.

Permissions

Match $createdBy to session.user to validate whether the current user is the one who created the data. Because $createdBy is set automatically, we can allow every insert with true.

schema.ts
const { definePermissions, session } = schema;
​
const createdByMe = { $createdBy: session.user };
​
export const permissions = definePermissions({
todos: {
read: createdByMe,
insert: true,
update: createdByMe,
delete: createdByMe,
},
});

These rules are enforced on the server. See Declarative Permissions for combinators, allowedTo, and more complex options.

Querying

The table's permissions already scope results to the current user, so queries don't need a separate owner filter.

MyTodos.tsx
export function MyTodos() {
const { data: todos, isLoading, error } = useAll(app.todos.where({ done: false }));
​
if (isLoading) return <p>Loading…</p>;
if (error) return <p>Something went wrong!</p>;
​
return (
<ul>
{todos.map((todo) => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
);
}

See Queries for subscriptions, one-shot queries, and durability tiers.

Inserting

$createdBy is set automatically on insert, so we don't need to set an owner.

AddTodo.tsx
export function AddTodo() {
const db = useDb();
​
function handleAdd(title: string) {
db.insert(app.todos, { title, done: false });
}
​
return <button onClick={() => handleAdd("Buy milk")}>Add</button>;
}

Use db.insert(...).wait({ tier: "..." }) if you need confirmation that the write reached a specific durability tier.

If ownership can be transferred after creation, use an explicit owner_id column instead of $createdBy.

schema.ts
import { schema as s } from "jazz-tools";
​
export const schema = s.defineSchema({
todos: s.table({
title: s.string(),
done: s.boolean(),
owner_id: s.uuid(),
}),
});
​
const { definePermissions, session } = schema;
​
const ownedByMe = { owner_id: session.user };
​
export const permissions = definePermissions({
todos: {
read: ownedByMe,
insert: true,
updateIf: ownedByMe,
updateTo: true,
delete: ownedByMe,
},
});

updateIf checks the row before the update and updateTo: true accepts any new row, so the current owner can rewrite owner_id to transfer the row. A plain update would also enforce the condition on the post-update row and block transfers. See Declarative Permissions for updateIf/updateTo semantics.

insert: true lets any user insert a row with any owner_id, including someone else's. That's the right default if you want users to be able to assign rows to others on creation; otherwise, narrow it to insert: ownedByMe so clients can only create rows they own.