Documents
Store JSON documents in collections, read them by id or list a collection, and control exactly who can see and change them.
Documents are JSON objects stored under a collection and an id, for example carts/7f3a…. Use them for data whose shape varies or that you always read whole: carts, drafts, settings, form submissions. For data you filter, join or aggregate, use tables instead.
Documents are kept in your project's own Postgres database (in the table sov_documents), so they are covered by its backups, and the same row-level security rules language decides who may read them.
#Write, read and delete
// Create or replace
await sovaria.data.docPut('carts', userId, { items: [{ sku: 'BK-1', qty: 2 }] })
const { data } = await sovaria.data.docGet('carts', userId) // { key, data, found }
await sovaria.data.docDelete('carts', userId)
// Every document in one collection (note the colon)
const { documents, truncated } = await sovaria.data.docScan({ prefix: 'carts:', limit: 100 })#Collections, ids and listing
- Collection names and ids may contain letters, digits,
-and_. Nest collections with/:orders/123/notes. - Writing to an existing id replaces the whole document; earlier contents are not kept. In Python,
update()reads the document and merges your fields in. If you need history, write each change as its own document (for examplecarts/<id>/changes). - Listing (
scan) matches keys of the formcollection:id. Use the prefixcarts:for one collection —cartsalone would also match a collection namedcartsArchive. - A scan returns up to
limitdocuments (default 100, at most 10,000) in key order, each as{key, data}.truncated: truemeans there are more; ask for the next page withstart_keyset to the lastkeyyou received (it is included again, so skip it). - Scans only return documents the caller is allowed to read.
#Who can read and write
Console members and the service role key can read and write every document. App users and anonymous visitors get nothing until you allow it. Each document records the app user who created it in owner_id, so the common rules are one line of SQL each. Run them in the SQL editor:
-- Each user reads and writes only their own carts
create policy carts_own on sov_documents for all
using (collection = 'carts' and owner_id = auth.uid()::text)
with check (collection = 'carts' and owner_id = auth.uid()::text);
-- Everyone, signed in or not, can read the public catalogue
create policy catalogue_read on sov_documents for select
using (collection = 'catalogue');
-- Only users with the app role "staff" can change it
create policy catalogue_staff on sov_documents for all
using (collection = 'catalogue' and auth.has_role('staff'))
with check (collection = 'catalogue' and auth.has_role('staff'));You can also allow access per collection with a project policy under Policies (in rules ctx.action is doc:read for gets, queries and listeners, doc:write or doc:delete, and ctx.resource.name is the collection — Policy language (SPL)). A refused write returns 403 with Row policy denied this document change.
No rule, no access. A collection that neither a policy nor a row policy on sov_documents names is closed to app users and visitors: reads, queries, writes and deletes answer 403, never an empty list or a delete that silently did nothing. If your app gets 403 on a collection, add the rule; it doesn't mean the collection is empty. Your team (console, CLI, service key) is not affected.
#History and recovery
| Question | Answer |
|---|---|
| Are earlier versions of a document kept? | No. A write replaces the document and a delete removes it; there is no per-document version history or trash. |
| Does Version history cover documents? | No. Version history: see, undo and restore changes records configuration — policies, tables, functions, settings, buckets, keys — not document contents. |
| Is there an audit trail of document changes? | Not of their contents. Who changed rules and settings is in the audit log (Audit log). |
| How do I get deleted or overwritten documents back? | Documents live in your project's database (sov_documents), so the daily backups include them. Restoring a backup restores the whole database to that day (Backups and recovery). |
If your app needs history (edits to an order, revisions of a page), keep it yourself: write each change as its own document in a sub-collection such as orders/<id>/revisions, with the time and author, and let users read but not edit it (Transactions, batches and field transforms writes the document and its revision together).
#In the console
Collections lists every collection with its document count. Open one to browse (100 documents at a time), edit or delete documents as JSON, import JSON, JSON Lines or CSV and export a collection. The Query, Indexes and Rules tabs run field queries, manage indexes and show who may read and write each collection.
#From the CLI
sov collections list#Limits
- A document is a JSON object (not an array or a single value).
- Documents count towards your database size, which depends on your plan — see Plans, usage and limits.
#Coming from Firebase or Supabase?
| Platform | What this is called there |
|---|---|
| Firebase | Cloud Firestore |
| Supabase | Supabase (JSONB columns) |
| Sovaria | Documents |
Moving an existing app? Migrate from Supabase or Firebase imports users (with their passwords), data, files and rules.