Under the hood
When access changes (move-in and move-out)
When someone gains or loses access, the engine sends exactly the rows to add and the ids to remove. No data ever reaches a device whose user may not read it.
Under the hood
You don't need this page to use clientdb. It explains how things work inside, for when you're curious or debugging. The short version is How it works, in 5 pictures.
In the todo app, Alice adds Bob to the "Launch" project, later makes him an admin, and one day removes him. Each time, Bob's devices must end up holding exactly what the rules now allow him: the project's todos arrive when he joins, private todos and the invite code arrive when he becomes an admin, and everything leaves when he is removed. You write no code for this; the engine works it out from your rules, using the groups they compile to.
Access changes in three ways.
Someone joins or leaves a group
Adding a member, promoting or demoting one, or removing one changes which groups that user belongs to. Their devices receive a groups action:
- Added groups: the device receives the rows of those groups for eager models, with their eager relations, in the same response (when pulling) or packet (over WebSocket). Lazy models aren't sent: what the device loaded of them reads as
"partial"again, collections and server queries that something observes (a component on screen) load again at once, and the rest the next time they're read. - Removed groups: the device removes every row it can no longer reach through any of its groups. A transaction not yet sent that changes a removed row is refused whole with
AccessLost(never sent, so its changes to rows the device still reads are undone too, and the rejection is reported); a transaction already sent waits for the server's answer. Lazy collections and server queries load again as for added groups, so a collection on screen settles as complete with the rows that remain.
A load or server query page that was in flight when the groups changed is asked again, so rows the viewer just lost never land from an answer computed before.
Alice makes Bob an admin of Launch
everyone in project.members|launch ← upsert member bob { role: "admin" }
Bob ← groups { add: ["project.admin|launch"] }
← upsert todo T-2 (private todos of the project)
← upsert project launch (again, now with inviteCode)
Alice makes Bob a plain member again
Bob ← groups { remove: ["project.admin|launch"] }
removes T-2; keeps project launch, without inviteCodeRestricted fields follow the same groups: a row Bob already holds arrives again when a new group reveals one of its restricted fields, and a removed group hides the field while the row stays.
The engine's onAccessRemoved hook is called with the session and the groups it lost (reason: "rules"), for audit logs. It receives group names, never row data.
A claim is granted or withdrawn
Claims come from your session callback, which the server asks again on every request and, on a live WebSocket, every few minutes. When the answer changes, the device gets the same groups action for the claim's group: support staff who go off shift lose the projects they could read only as support. onAccessRemoved is called with reason: "claim".
A row moves
A write that changes which groups a row belongs to (making a todo private, moving it to another project) re-tags the row. One entry is logged with the old and new groups, and each device receives what its user is now allowed:
| Recipient | Receives |
|---|---|
| in the new groups | the full row (for a move-in the row is complete, never a patch) |
| only in the old groups | an id-only delete marked moved: true, with no data |
| in neither | nothing |
Carol makes T-1 private (project.members|launch → user|carol, project.admin|launch)
Carol, Alice ← upsert todo T-1 { isPrivate: true }
Bob ← delete todo T-1 { moved: true }A moved delete removes only that row from the device. A real delete also removes the rows that cascade from it, and leaves a tombstone so a stale update can't bring it back; a move leaves none, so if access returns, the row is delivered and applied again.
Children follow their parent
Children whose read rule is exactly $.canRead(child.parent) follow their parent. When a todo with 2,000 comments becomes private, the comments are re-tagged in bulk, one UPDATE … RETURNING per level, and users who lose the todo lose its comments with it. In the benchmark this takes 50 ms instead of 358 ms for re-routing row by row.
Bulk changes
Rows written outside clientdb and picked up with engine.resync() are re-routed the same way. Above resyncInvalidateThreshold rows of one model (default 10,000), devices get one invalidate for the model and reload it, instead of thousands of individual changes.
While offline
A device whose user was removed from a group and added back while it was offline receives, on reconnecting, the group change and the rows of the group as they are now, not a replay of history it had no access to.
Speed
From the server benchmark (a project with 20,000 todos everyone reads and 5,000 only admins read):
| Change | Time until the user's device reflects it |
|---|---|
| Promote: 5,000 admin-only todos arrive | 31.5 ms |
| Demote: admin-only todos removed | 12 ms |
| Make one todo admin-only: move-out | 3.6 ms |
Checked against a reference implementation
Property-based tests run random sequences of writes, promotions, moves and reconnects against a reference implementation of the rules. After every step, each simulated device must hold exactly the rows the rules allow, and the wire must never carry data its recipient may not read.