Skip to content

Data and access rules

An app built on Sovyn stores its data as entries in collections: one collection for books, one for reviews, one for damage reports. Your AI assistant describes them in a file in the project folder, sovyn.build.yaml, the manifest. Next to the collections, the manifest also holds the access rules: who may read, create, change and delete what.

You don’t have to write the manifest yourself. The examples on this page help you understand what your assistant does and what to ask it for.

A small book club app could look like this:

app: bookclub
collections:
books:
fields:
title: { type: text, required: true }
genre: { type: text, values: [novel, nonfiction, poetry], default: novel }
cover: { type: file, accept: [image/jpeg, image/png] }
added_by: { type: user, required: true }
access:
read: authenticated
create: { owner: added_by }
update: { owner: added_by }
delete: { owner: added_by }

Each field has a type:

Type Holds
text text
int a whole number
bool yes or no
datetime a date with time
user a person who uses the app
relation a link to an entry of another collection
file one image or PDF, see Files below

A field can also have:

  • required: true: every entry must have a value.
  • default: the value when none is given. Not for user, relation and file.
  • values: [novel, nonfiction, poetry]: a text field only takes these values. The database checks them.
  • unique: [book, reader] on the collection: no two entries may have the same values in these fields, for example one review per reader and book.

Every collection also has four automatic fields: id, created_at, updated_at and created_by. Only the platform sets them.

A relation field points to an entry of another collection, for example a review to its book. With on_delete it says what happens when that entry is deleted:

  • cascade: the reviews are deleted too.
  • restrict: the book can’t be deleted while reviews point to it.
reviews:
fields:
book: { type: relation, to: books, on_delete: cascade, required: true }
reader: { type: user, required: true }
stars: { type: int, required: true }
unique: [book, reader]

Each collection has a rule per operation: read, create, update and delete. A rule is one of these:

Rule Who may
nobody nobody
authenticated everyone who is signed in
role:manager everyone with the app role manager
{ owner: added_by } only the person named in the field added_by of the entry
{ member: tenancies } everyone with a matching entry in another collection, see below
  • A list of rules means: one of them is enough, for example read: [role:manager, { owner: tenant }].
  • Without a rule, nobody may, and the app can’t see the collection at all.
  • { owner: … } on create means: people can only enter themselves as the owner. The owner is set when the entry is created and never changes afterwards, not by any role and not by the server logic.

A field can have its own read and write rule. Without one, the rule of the collection applies.

status: { type: text, values: [open, done], default: open, write: role:manager }
internal_note: { type: text, read: role:manager }

Here everyone who may read a repair sees its status, but only managers may change it. The internal_note is only visible to managers; for everyone else, it is simply missing.

App roles group the end users of your app, for example tenants and managers. Your assistant names them in the manifest:

auth:
roles: [tenant, manager]
default_role: tenant
  • Every end user has exactly one role. Without an assigned role, they have the default role.
  • The manifest does not say who has which role. You assign roles in the console: open Apps, choose the app and go to the App roles section in the Access tab. There you pick a role for each member of your team, separately for Development and Production. Only owners and admins of the team can change it. A change applies with the next request, without signing in again.
  • People who are invited get their role with the invitation. See Sign-in for end users.

The app may show or hide buttons depending on the role. What someone may really do is decided only by the access rules on the server.

A role grants rights only where a rule names it. No role sees everything automatically: even an app role called admin sees only its own entries under read: { owner: author }. If admins should read everything, add the role to the rule: read: [{ owner: author }, role:admin]. The same goes for owners and admins of your team: their team role governs the console; in the app only their app role counts.

Often access depends on a connection: a tenant may see the repairs of their flat. For this, the app has a membership collection that links a person to a flat:

tenancies:
fields:
tenant: { type: user, required: true }
unit: { type: relation, to: units, on_delete: cascade, required: true }
kind: { type: text, values: [main, sub], default: main }
access:
read: [role:manager, { owner: tenant }]
create: role:manager
update: role:manager
delete: role:manager
repairs:
fields:
title: { type: text, required: true }
unit: { type: relation, to: units, on_delete: cascade, required: true }
access:
read: [role:manager, { member: tenancies, of: unit }]
create: [role:manager, { member: tenancies, of: unit }]
update:
[role:manager, { member: tenancies, of: unit, where: { kind: main } }]
delete: role:manager
  • { member: tenancies, of: unit } allows access when the person has an entry in tenancies that points to the same flat as the repair.
  • where: { kind: main } narrows it down further: only main tenants may change repairs.
  • Deleting the membership entry ends the access with the next request.

A field of type file holds one file per entry, for example the cover of a book or the photo of a damage.

  • Allowed are JPEG, PNG and PDF, at most 10 MB per file. Sovyn looks at the content, not at the file name.
  • accept: [image/jpeg, image/png] on the field allows only some of these types, for example only images. The server checks it.
  • A file can be read by whoever may read the entry and the field. The app gets a link to the file that is valid for five minutes.
  • A file goes with its entry: deleting the entry, putting another file into the field or emptying the field deletes the file.
  • A file that was uploaded but not put into any entry is deleted after 24 hours.
  • There are no thumbnails and no virus scan.

Files count towards the storage of your plan; see Plans and usage.

The app searches its data on the server, filtered by the access rules. A search returns at most 1000 entries at a time, 50 if nothing else is said; the app shows longer lists page by page. What searches can’t do yet is listed in What doesn’t work yet.

When you ask for a change, for example a new field, your assistant changes the manifest and applies it with npx @sovyn/build apply:

  • Adding is easy: new collections, new fields, new values in a values list. A new required field without a default only works while the collection is still empty.
  • In development, the change applies at once, including changed access rules. Production gets it with the next move to production.
  • Removing a field or a collection needs your consent. It disappears from the app, but moves to the trash with its values for 30 days. Going back to an earlier version in that time brings it back. See Versions, development and production.
  • Other changes, such as renaming a field or changing its type, are refused, and nothing changes. Your assistant then finds another way, for example a new field.