schema.coloa sits in the project root and describes your app’s data: tables, fields, and who may do what. Coloa turns it into Postgres tables and database-enforced access rules. On every deploy, it works out the migration for you.
The one idea to remember: nothing is allowed unless a rule says so. A table with no allow line is invisible to your app code.
// A team task board.
table projects {
owner user @owner
name text
created_at timestamp @default(now())
allow read, update: owner == auth.user
allow read: auth.role == "manager"
allow create: auth.user != null
}
table tasks {
owner user @owner
project ref(projects)
title text
status text @default("todo")
assignee user @optional
note text @sensitive @optional
index(project, status)
allow read: owner == auth.user || assignee == auth.user
allow create: project.owner == auth.user
allow update: owner == auth.user
allow update(status): assignee == auth.user
allow delete: project.owner == auth.user
}
In words: anyone signed in can create a project. Only its owner (or a manager) can see it. Only the project owner can add or delete tasks. A task is visible to the person who created it and to its assignee. The assignee may only change status. note never leaves the server.
table <name> {
<fields>
<indexes>
<rules>
}
lower_snake_case, at most 48 characters, and must not start with coloa or pg_.@primary, Coloa adds id uuid @primary with a generated value.// or # and run to the end of the line.<name> <type> [attributes]
| Type | Stored as | Notes |
|---|---|---|
text |
text | |
int, bigint |
integer, bigint | |
float |
double precision | |
money |
numeric(20,2) | exact; use for prices and amounts |
bool |
boolean | |
uuid |
uuid | |
timestamp |
timestamptz | |
date |
date | |
json |
jsonb | |
user |
text | an end-user id, compared with auth.user |
ref(<table>) |
same type as that table’s primary key | a foreign key; deleting a referenced row is refused |
Fields are required unless marked optional. Write either note text? or note text @optional.
| Attribute | Meaning |
|---|---|
@primary |
The primary key. At most one per table. uuid keys get a generated default. |
@owner |
Must be type user. Filled with the creating user; a row can’t be created for someone else, and the owner can never be changed. Indexed automatically. |
@sensitive |
Personal or secret data (card numbers, ID numbers, phones). Left out of select() unless named explicitly. Masked as [sensitive] in API responses and logs. A function that reads it fails coloa check unless the file contains // coloa-allow-sensitive: <field>. |
@unique |
No two rows share the value. |
@index |
Adds an index on this field. |
@optional |
May be empty (NULL). |
@default(…) |
A literal ("new", 0, true), now() or uuid(). |
index(owner, created_at)
Speeds up filtering and sorting on those columns, in that order. @owner and ref fields are indexed automatically, unless an index you declared already starts with that field.
allow <operations>: <condition>
Operations are read, create (or insert), update, delete, and all for all four. List several with commas: allow read, update: ….
read decides which rows are visible. update and delete only affect rows that are also readable.create checks the new row, and always also requires @owner == auth.user.allow update: owner == auth.user // the owner may change any field
allow update(status): assignee == auth.user // the assignee may change only status
For update(a, b) the condition is checked against the row before the change. Every field the update changes must be covered by a rule that matches. @owner and @primary fields can never be listed.
| You can use | Example |
|---|---|
| A field of the row | owner, status, is_public (a bool field can stand alone) |
| The current user | auth.user (id, or null when signed out), auth.role ("anon" when signed out), auth.email |
One step through a ref field |
project.owner == auth.user, task.assignee == auth.user |
| A matching row in another table | exists(members where members.project == project && members.user == auth.user) |
| Comparisons | == != < <= > >= |
| Membership | auth.role in ("staff", "manager") |
| Literals | "text", 42, true, false, null |
| Combining | && / and, \|\| / or, ! / not, parentheses |
Notes:
x == null means “is empty”; x != null means “has a value”. auth.user != null means “signed in”.allow read: true makes rows public. A create, update or delete rule of true triggers a warning.coloa user role an@shop.vn staff --env prod.ref.field and exists(...)) check the facts directly, whatever the other table’s own read rules say, so tables may refer to each other.exists(...)table members {
project ref(projects)
user user
allow read: user == auth.user || project.owner == auth.user
allow create, delete: project.owner == auth.user
}
table projects {
owner user @owner
name text
allow read: owner == auth.user || exists(members where members.project == id && members.user == auth.user)
}
Inside exists(<table> where …), write <table>.field for the other table’s fields; plain names are the current row’s. exists can’t be nested.
files <name> { … }Uploaded files (receipts, avatars, attachments) are declared like a table. Each file is one row, so the same rules decide who may upload, see and delete it. The file’s contents live in object storage. Apps never get storage credentials: Coloa checks the row first, then reads or writes the contents.
files receipts {
order ref(orders) // your own fields, like a table
max_size 5mb // default 10mb; units b, kb, mb, gb
types "image/*", "application/pdf" // default: any type
allow read, create: order.owner == auth.user
allow delete: owner == auth.user
}
Every files table also has these built-in fields, filled in by Coloa:
| Field | Meaning |
|---|---|
id |
the file’s id (uuid) |
owner |
who uploaded it (@owner: always the uploader) |
name |
the file name given at upload (paths stripped); an update(name) rule may allow renaming |
size |
bytes; can’t be changed |
content_type |
e.g. image/png; can’t be changed |
created_at |
upload time |
Use them over HTTP from the browser or any client. The signed-in user’s session applies:
| Request | Does |
|---|---|
POST /_coloa/files/receipts?order=<id>&name=r.pdf (body = the file, Content-Type set) |
checks allow create, stores the file, returns { file, url } |
GET /_coloa/files/receipts?order=<id> |
the rows you may read, newest first (limit, max 200) |
GET /_coloa/files/receipts/<id> |
the contents, if you may read the row (otherwise 404) |
DELETE /_coloa/files/receipts/<id> |
checks allow delete, removes the row and the contents |
In functions, read the rows like any table: db.from("receipts").select().eq("order", id). Creating and deleting rows from functions is refused, so a row always matches stored contents.
Downloads are served so they can’t run script in your app: HTML, SVG and other active types download as attachments. Uploads and downloads are metered (files <name> in coloa cost), and deleting a preview deletes its files.
Edit the file. coloa dev applies safe changes to your dev database right away. Previews and production get them on deploy or promotion. Check what would happen with coloa schema plan --env prod.
| Change | What happens |
|---|---|
| New table, new optional field, new field with a default, new index, rule change | Applied automatically |
| New required field without a default on an existing table | Refused: add @optional or @default(…) |
| Removing a field or table, changing a type | Destructive: needs --allow-destructive, and a snapshot is taken first in production |
| Renaming | Seen as remove + add (destructive). Add the new field, copy the data, then remove the old one. |
await db.from("tasks").select("id, title, status").eq("project", id).order("created_at");
await db.from("tasks").insert({ project: id, title: "Write copy" });
await db.from("tasks").update({ status: "done" }).eq("id", taskId);
await db.from("tasks").delete().eq("id", taskId);
Rules apply automatically for the signed-in user. Your code never passes the user id for permission checks.
@unique| Message | Fix |
|---|---|
unknown type 'strin' |
Use one of the types above. |
`x` is not a bool; compare it |
Write x == auth.user, not just x. |
@owner field `o` must have type `user` |
Change the type to user. |
new required field … has no default |
Add @optional or @default(…). |
access denied: … has no 'allow read' rule (at runtime) |
Add a rule for that operation. |
you may not change note on tasks (at runtime) |
That user’s update(...) rule doesn’t list the field. |
`size` is built into files tables |
Remove the field: Coloa fills it in. |
`receipts` is a files table: upload with POST /_coloa/files/receipts (in a function) |
Upload from the client with the files API. |