Access Control
Access control defines which ShareDB operations are allowed for a collection. Access files are server-only model files and are removed from client bundles by the TeamPlay Babel plugin.
Define Rules
Access rules live in access.ts:
The object can contain four keys:
create: controls new document creation.read: controls reading existing documents.update: controls writes to existing documents.delete: controls document deletion.
Each rule can be:
true: always allow the operation.false: deny the operation.- omitted: deny the operation.
- a function: decide from the operation context.
{ fn }: ShareDB access validator object form.
Validator functions can return a boolean or a promise resolving to a boolean.
Backend Enablement
Enable access control on the backend:
When global access control is enabled, collections without registered rules are denied by default. This is the safe production behavior: forgetting an access.ts file does not accidentally open a collection.
When global access control is disabled, collections remain open unless they are explicitly protected by forced rules or serverOnlyCollections.
Forced Rules
Some framework-owned collections need protection even when an app has not enabled global access control yet. Mark those rules as forced:
Forced rules are registered even when createBackend({ accessControl: false }) is used. In that mode, only forced collections and server-only collections are checked; other collections keep the normal open behavior.
If global access control is enabled, forced rules behave like normal rules. The force option only controls whether the collection is protected when global access control is off.
Server-Only Collections
Use serverOnlyCollections for collections that should never be accessed by clients through ShareDB:
Server-only collections deny client read, create, update, and delete operations. Server code can still use them through server-side database access.
Rule Contexts
create receives the new document:
Shape:
read receives the existing document:
Shape:
update receives the document before and after the operation, plus raw ShareDB ops:
Shape:
delete receives the existing document:
Shape:
Document And Session Types
The first generic is the document shape. The second generic is the session shape.
If you omit the session generic, session defaults to:
Custom Rule Values
If your ShareDB access setup uses a custom validator that accepts extra rule values, pass the third generic:
Only use this when your backend access validator is configured to understand those values.
Client Security
Access rules should stay server-only. The TeamPlay Babel plugin removes accessControl() calls from client bundles:
This lets client code import the same file graph without bundling authorization logic.