Row-level security
A row-level policy limits which rows of a table a request can read or write. Row-level security is available on the Pro plan and above. Managing policies needs the Databases.policies permission.
A policy is one comparison on one column, such as owner = <name of the calling API key>. Policies are never free-form SQL.
Create a policy
This policy lets each API key see and change only the rows whose owner column holds that key's name:
curl -X POST https://api.vaultdb.io/api/databases/shop/policies \
-H "Authorization: Bearer $VAULTDB_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"table_name": "orders",
"column_name": "owner",
"operator": "=",
"value_source": "api_key_name",
"value": "-",
"operation": "all"
}'
| Field | Required | Description |
|---|---|---|
table_name |
Yes | The table the policy applies to. |
column_name |
Yes | The column to compare. |
value |
Yes | The value to compare with. Only used when value_source is literal, but always required. |
operator |
No | =, !=, in or not_in. Default =. For in and not_in, the value is a comma-separated list. |
value_source |
No | Where the comparison value comes from. Default literal. See below. |
operation |
No | select, insert, update, delete or all. Default all. |
value_source takes:
| Value | Compares the column with |
|---|---|
literal |
The value you set. |
account_id |
The id of the calling account. |
api_key_name |
The name of the API key that made the request. A request made with a signed-in session, not an API key, matches no row. |
Values are compared as text.
Returns 201 with { "message", "policy" }.
List and delete policies
curl "https://api.vaultdb.io/api/databases/shop/policies?table=orders" \
-H "Authorization: Bearer $VAULTDB_API_KEY"
curl -X DELETE https://api.vaultdb.io/api/databases/shop/policies/POLICY_ID \
-H "Authorization: Bearer $VAULTDB_API_KEY"
table is optional. Dropping a table deletes its policies.
How policies apply
A table with no policies is open to every request with the right permission. Once a table has policies, every policy that matches the operation applies, and a row must satisfy all of them.
| Operation | Effect |
|---|---|
select |
Reads return only rows that satisfy the policy. Your own filters can narrow the result but never widen it. |
insert |
Each new row must include the policy's column, with a value that satisfies the policy. Otherwise the insert is refused with 403, naming the column. |
update |
Updates change only rows that satisfy the policy. |
delete |
Deletes remove only rows that satisfy the policy. |
all |
All four of the above. |
Policies apply to:
- the REST API (reads, inserts, updates and deletes),
- GraphQL queries and mutations,
- CSV import and CSV and JSON export,
- vector search,
- realtime events: an event carries
dataonly for an inserted row the subscriber'sselectpolicies allow; any other event on that table arrives withoutdata(see realtime for the exact rule), - branches: a new branch starts with a copy of its source's policies.
Policies do not apply to these, because each needs its own permission that gives the holder every row:
- raw SQL and scheduled queries, which need
Databases.sql, - webhooks, which need
Databases.webhooksand send each change to the URL you set.
Do not grant Databases.sql, Databases.webhooks, Databases.policies, Databases.* or * to a key that policies are meant to restrict. Databases.policies can delete the policies themselves.