entity(name, fields, options).
id (ULID string) and the column order you declared.
Field types
| Method | Type | Notes |
|---|---|---|
field.string() | TEXT | Any UTF-8 string |
field.int() | INTEGER | 64-bit signed |
field.float() | REAL | 64-bit IEEE-754 |
field.bool() | INTEGER (0/1) | Boolean (field.boolean() is an alias) |
field.datetime() | TEXT (ISO-8601) | Store with new Date().toISOString() |
field.richtext() | TEXT | For prose; the client SDK has editors ready |
field.id(entity) | TEXT | Foreign key to another entity’s id |
Modifiers
.optional()— column is nullable.unique()— adds a unique index on that one column
CRDT fields
Pylon rows are backed by Loro docs in CRDT mode. The database row is a projection of that doc, so ordinary queries, indexes, policies, and search keep working while collaborative fields can merge through CRDT updates. Scalar fields default to LWW registers.field.richtext() defaults to
LoroText, and a normal string can be upgraded when you need character-level
merge:
| Field | Default merge behavior |
|---|---|
string, datetime, id(...) | LWW string register |
int, float, bool | LWW scalar register |
richtext | LoroText |
.crdt("text") on string / richtext | LoroText |
.crdt("counter") on int / float | LoroCounter — patch value is the DELTA to apply; concurrent increments add up (+1 from two peers → +2, not LWW-stomp) |
.crdt("list") | LoroList — patch is the target array; first write ships snapshot, subsequent ship deltas |
.crdt("movable-list") | LoroMovableList — same wire shape as list; move-op API for true reordering is a future iteration |
.crdt("tree") | LoroTree — patch is [{id, parent, ...meta}]; reconcile maps user-supplied id → TreeID so concurrent moves merge |
.crdt("lww") | Explicit LWW |
text, counter, list, movable-list, tree) are
implemented end-to-end as of v0.3.100 — broadcasts ship the full Loro snapshot
on first write per row, then incremental deltas (v0.3.105 SQLite, v0.3.107
Postgres) keyed off the last-broadcast version vector.
Field gates: serverOnly & readonly
Two modifiers control how a field flows through the HTTP boundary. They’re additive — the field type, optionality, and indexes still work the same..serverOnly() — the field is stripped from every public response shape: GET /api/entities/<entity>, GET /api/entities/<entity>/<id>, the session-projection /api/auth/session, and sync push deltas. It stays readable from inside server functions via ctx.db.* so your handler can do internal work with it (e.g. webhook receivers look up stripeCustomerId from the Stripe customer id without leaking the value to clients).
If you want the field exposed to a specific client, re-serialize it inside a function return — ctx.db.unsafe.get (post v0.3.160) skips the strip; the default ctx.db.get honors it.
.readonly() — the field is settable on insert but rejected on update. Any PATCH /api/entities/<entity>/<id> payload that mentions the field returns 400 READONLY_FIELD before the policy even runs. Closes the canonical IDOR-via-update-payload shape:
ctx.db.update inside a mutation/action are not blocked by .readonly(). Server code is trusted to enforce its own invariants; readonly is an HTTP-boundary defense, not a hard write-lock.
Indexes
Declare composite or non-unique indexes in the options block:Relationships
Pylon doesn’t have a separate relation primitive — usefield.id("Other") and query with filters. The typed client db.query("Message", { roomId }) narrows by indexed columns.
Schema changes
Editapp.ts, save — pylon dev picks up the change and runs a live migration. Pylon’s storage layer plans the diff (add column, drop index, etc.) and applies it to your database, whether SQLite or Postgres. Destructive operations (dropping a column that has data) require you to bump manifest.version.
Next
Policies
Control who can read and write each row.
Functions
Write server-side logic.