# Collections in the console: tree, documents, fields and rules
Source: https://sovaria.in/docs/collections-in-the-console

See every collection and subcollection as a tree, scan by collection or key prefix, add documents with typed fields or JSON, import JSON, JSON Lines or CSV, and set who may read, write and delete each collection.

(Diagram: How documents are organised)

## See the whole tree

The **Tree** tab draws your documents as a tree, left to right: the store, its collections, each collection’s documents, each document’s subcollections, and so on. Click a node to open the next level — it loads the first time you open it — and to see it on the right: a collection’s document count, or a document’s fields. **Open in Collections** / **Edit in Collections** jumps to it in the Data tab.

- Large collections show 25 documents at a time; **Show 25 more** adds the next ones.
- **Find a collection or document** highlights matching nodes among the ones you have opened.
- Drag the background to move around; **−**, **+** and **Fit** zoom. **Collapse all** closes every branch.
- The same tree opens first on **Document Scan**.

## Scan by collection or key prefix

**Document Scan › Scan** reads documents straight from the store. Pick a **Collection** (nested ones too, like `carts/cart_1000/items`), or leave it empty and give a **Key prefix** — keys are `collection:documentId`, so `users/alice/` finds everything under that user’s subcollections. Choose a **Limit** and **Run scan**. One collection shows its fields as columns; a scan across collections shows each document’s fields in one column. **JSON** shows each document in full.

## Add a document

Open a collection and choose **Add document**. Leave **Document ID** empty for an automatic one. Under **Fields**, each row is a name, a type and a value; **+ Add field** adds another. The **JSON** tab edits the same document as text, and switching keeps your data.

| Type | Value you enter | Stored as |
|---|---|---|
| string | Any text | `"Ada"` |
| number | A number | `36` |
| boolean | true or false | `true` |
| null | — | `null` |
| timestamp | A date and time | A timestamp (sortable, comparable in queries) |
| map | A JSON object: `{ "city": "Pune" }` | A nested object |
| array | A JSON array: `["a", "b"]` | A list |
| geopoint | `lat, lng` | A location |
| reference | `collection/doc` | A link to another document |
| bytes | Base64 | Binary data |

The form tells you which row is wrong before saving: a missing name, two fields with the same name, a value that isn’t a number, or JSON that doesn’t match the type. Names can’t start with `__` or contain `/`.

## Import many

**Import many** takes a JSON array, one object, JSON Lines or CSV with a header row (`address.city` columns become nested fields), pasted or uploaded. **ID field** names the field to use as each document’s ID (empty: automatic). **Replace documents that already exist** overwrites them; otherwise they are kept and reported. Every 500 documents are written together — all of them or none.

## Who can read and write

The **Rules** tab answers one question for the collection: who can read, write and delete it. It has three parts.

- **Access at a glance**: a table with a row for visitors (not signed in), signed-in users and each app role, and a column for read, write and delete. Every cell is your access policies’ real decision for that kind of user, asked the same way a request is checked: **All documents**, **Their own** (row security keeps them to documents they own) or **No access**. Click a cell to see which rule allowed or refused it. Your team and the service key always have access.
- **Rules**: each rule for the collection with what it allows and for whom. Right-click a rule (or use ⋯) to edit it on the Policies page, show its code or delete it. Rules inherited from production are marked and can only be changed there.
- **Database layer**: the row policies on the document table, in plain words, with their SQL. Most projects only need the rules above.

With no rules yet, the tab offers starting points: signed-in users read and write, signed-in users can read, anyone can read, or one role can do everything. Each shows exactly what it grants and the rule that will run before you add it. **Write your own** opens the policy editor. Rules live on the **Policies** page with your table rules (collections have their own group there) and use three actions:

| Action | Covers |
|---|---|
| `doc:read` | Getting a document, queries and live listeners. |
| `doc:write` | Adding and updating documents. |
| `doc:delete` | Removing documents. |

SPL:
```bash
ALLOW IF ctx.action == "doc:read" AND ctx.resource.name == "orders" AND ctx.actor.is_anonymous == False
ALLOW IF ctx.action == "doc:write" AND ctx.resource.name == "orders" AND "STAFF" IN ctx.actor.roles
```

With no rule that allows them, app users are refused; your team and the service key always have access. Row security on the document table is an optional extra layer for app users: it can narrow what a rule allows (to their own documents, for example) but never widens it.

## From code

JavaScript:
```js
await sov.doc('carts/cart_1001').set({
  customer_id: user.id,
  items: [{ sku: 'tea-01', qty: 2 }],
})
const mine = await sov.collection('carts').where('customer_id', '==', user.id).limit(20).get()
```

CLI:
```bash
sov data doc-put carts cart_1001 '{"customer_id": "u_1", "items": []}'
sov data doc-get carts cart_1001
sov data doc-scan --prefix carts --limit 20
```

> **Tip:** Check a new rule with [Simulate access before you ship](https://sovaria.in/docs/simulation) before you ship it.
