phibac developer docs

Collections and records

Each user (the person whose data it is) has one private database on the server. A user's database holds collections, and each collection holds records. This page explains how they are named, described, versioned and kept. The examples use the notes collection from Build your first app.

Collections

A collection is one kind of data in a user's database, such as notes or journal entries. Every user's database can hold the same collections, each with that user's own records.

A collection's name is written like a reversed web address, with at least three parts separated by dots: com.example.notes.note. Names use lower-case letters, digits and -, and are at most 128 characters long. Names starting with core. are reserved for the server's own collections, and names starting with sandbox. for sandbox collections.

The first app (a web app a developer builds) to publish a name owns it, and only that app can publish new versions of it. Use a domain you control. The owning app may read and write the collection's records; another app may only read them, with the user's consent: the user's approval, given on a page the server shows them (see Users, sign-in and consent).

Records

A record is one saved item in a collection, such as one note. Your app gets each record from the SDK (the JavaScript library your page uses) as an object like this:

Property What it is
id The record's global ID (see below).
collection The collection's name, such as com.example.notes.note.
rkey The record's key within the collection (see below).
data The record's fields, such as { text: "Buy milk", pinned: true }.
rev A number that goes up with every change to the record.
status Its status: pending until the server has the latest change, then synced (see Offline writes and sync).
createdAt, updatedAt When the record was created and last changed, as date-and-time strings.

An update changes only the fields you pass and keeps the others; passing null for a field removes it. A record holds at most 256 KB of data: store larger content, such as a photo, as a file and keep the file's ID in a field.

Record keys and global IDs

An rkey (record key) is the ID that identifies a record within its collection. The SDK makes it on the device when your app creates the record, so creating works offline. It is a UUIDv7, a random ID that starts with the time it was made, such as 0193a1f2-7c4e-7a3b-9d2e-5f6a7b8c9d0e. You pass it to get, update and delete.

Your app cannot write to a deleted record again: writing to it fails with record_deleted. To save the data again, create a new record, which gets a new rkey. If the user restores the deleted record from their account area (see History and restore), it comes back under the same rkey, and your app receives it like any other change.

Every record also has a global ID of the form pb://<userId>/<collection>/<rkey>, for example pb://u_01JCZ8Q3M4N5P6R7S8T9V0W1XY/com.example.notes.note/0193a1f2-7c4e-7a3b-9d2e-5f6a7b8c9d0e. It names the user, the collection and the record. A field that links to another record (a recordRef field) holds this ID. Reading a record never fetches the record it links to: take the last part of the ID, the rkey, and get that record from its collection.

Schemas

A schema is the description of a collection: what the collection holds and, for each field, its name, type, rules and description. You write it in pb.config.ts and publish it with phibac deploy. Every collection except a sandbox collection has one.

The server checks every record against the schema. A record with a missing required field, a wrong type or a value out of range is refused with schema_violation, which names the fields to fix.

Field types are string, number, integer, boolean, datetime, enum (one of a list of strings), json (any JSON value), fileRef (a file ID) and recordRef (a record's global ID). Two options matter beyond checking:

  • indexed lets queries filter and sort on the field (see indexed).
  • sensitive marks the field as sensitive on the consent page, where the user decides whether another app may read the collection.

The collection and every field need a description. Consent pages show them to users, so say plainly what the data is.

Versions

A major version is a numbered version of a collection's schema: the version in pb.config.ts. The first one is 1.

  • Additive changes stay in the same major version and become a new minor version (1.0, then 1.1). They are adding an optional field, changing descriptions, indexed or sensitive, widening a range, raising a maximum length and adding enum values.
  • Breaking changes must be published as the next major version (2 after 1). They include removing, renaming or retyping a field, making a field required or no longer required, adding a required field, and narrowing a range. phibac deploy previews every change and publishes nothing if one would break the published major version.

No published schema version is ever deleted: older major versions stay published, and each record keeps the major version it was written with until an update moves it to another. Your app writes the major version it was built for when it passes the generated SCHEMA_MAJORS (from phibac generate) to connect({ schemaMajors }), as the quickstart does. Without it, a new record is written with the latest published major version. An app that reads another app's collection lists the major versions its code understands and receives only records of those.

History and restore

Every change keeps the version it replaced. When a record is updated or deleted, its previous version goes into the record's history, which keeps each version for 30 days along with the app that wrote it and when.

Only the user can restore a version, from their account area: Your data lists their collections; opening one lists its records, including deleted ones whose history is still kept, and each record's history page has a Restore this version button. Restoring saves the old data as a new version of the record, so the version it replaces is kept in the history too. Apps receive the restored record like any other change, including through live updates (the SDK tells your page when data changes). There is no SDK call to read the history or to restore.

Sandbox collections

A sandbox is a collection whose name starts with sandbox., such as sandbox.settings, and that needs no schema. It accepts any JSON object up to 256 KB, and the server does not check its fields.

A sandbox collection is private to the app that writes it. If two apps both use sandbox.settings, each gets its own separate collection. No other app can read it, and the user cannot give another app permission to. Its records count toward the user's storage like any others.

Use a sandbox collection for data only your app needs, such as settings or drafts. When data should be shareable with other apps, publish a schema for it under a real name instead.