Skip to content

4. Write a schema

A schema is one .olv file: the aggregates (what the data is), indexes, and the profiles (who may do what with it). The language is specified in the schema language; this page writes a small bank, deploys it, and changes it.

The schema

schema bank version 1

aggregate Customer {
  key id: string(12)
  1 name:   string(80)
  2 branch: string(4)

  child Account [number: string(20)] unique {
    1 currency: string(3)
    2 balance:  decimal(18,2)
    3 status:   enum { open = "O", closed = "C" }

    child Entry [posted: timestamp desc, seq: int32] {   // newest first
      1 amount: decimal(18,2)
      2 memo:   text
    }
  }
}

index account_by_number on Customer/Account(number) unique

profile app for service "app" {
  consistency strong

  get    Customer/{id}/**
  list   Customer limit 100
  list   Customer/{id}/Account
  range  Customer/{id}/Account/{n}/Entry by posted limit 100
  insert Customer
  insert Customer/{id}/Account
  insert Customer/{id}/Account/{n}/Entry
  update Customer/{id}/Account/{n}  fields(balance, status)
  lookup account_by_number -> Customer
}

What each part says:

  • aggregate Customer: the root type and its key; numbered, typed fields (the number is the field's identity across versions); child types with their own keys. Entry has a two-part key whose first part sorts newest first.
  • index: an entry per account by its number, kept in the same transaction as the account (add async for one written after the commit).
  • profile app for service "app": what the principal app may do. Each grant is a verb and a path: get Customer/{id}/** reads a customer and everything under it, update ... fields(balance, status) changes only those fields, lookup uses the index. Anything not granted is refused (403). Profiles can also hide fields (except), mask them (mask Customer.tax_id partial(4)), restrict rows (rows Customer where branch = $user.branch) and allow reads as of a version (consistency strong, as_of allowed); the fuller example is the specification's bank.olv.

Check it and deploy it

oliviadb schema check validates the file; oliviadb profile check verifies that every path a profile grants is served by a key range or an index rather than a scan. Deploying is naming the file in the configuration and restarting:

oliviadb schema check bank.olv
oliviadb profile check bank.olv
cat > config.json <<EOF
{"listen": "127.0.0.1:8443", "native_listen": "127.0.0.1:7655", "dir": "data",
 "tls": {"cert": "server.pem", "key": "server.key"},
 "schema": "bank.olv",
 "api_keys": [
   {"sha256": "$(oliviadb-server --hash-key "$(cat api-key)")", "tenant": "acme", "principal": "app", "profiles": ["read", "memory"]},
   {"sha256": "$(oliviadb-server --hash-key "$(cat api-key-globex)")", "tenant": "globex", "principal": "app", "profiles": ["read", "memory"]}]}
EOF
kill "$(cat oliviadb.pid)"; while kill -0 "$(cat oliviadb.pid)" 2>/dev/null; do sleep 0.1; done
oliviadb-server --config config.json >> server.log 2>&1 &
echo $! > oliviadb.pid
until curl -fsS --cacert server.pem https://localhost:8443/readyz; do sleep 0.2; done

With the container image: mount the file and name it, and name the principal whose profile it is:

docker rm -f oliviadb    # the container of step 1; its data stay in the volume oliviadb-data
docker run -d --name oliviadb -p 8443:8443 -p 7655:7655 \
  -e OLIVIADB_NATIVE=1 -e OLIVIADB_PRINCIPAL=app -e OLIVIADB_TENANT=acme \
  -e OLIVIADB_SCHEMA=/etc/oliviadb/bank.olv -v "$PWD/bank.olv:/etc/oliviadb/bank.olv:ro" \
  -v oliviadb-data:/data ghcr.io/oliviadb/oliviadb:0.1

The server's log names the aggregates it serves (native aggregates: Customer) and any it cannot serve, with the reason.

First data

. ./oliviadb.env
H="Authorization: Token $OLIVIADB_KEY"
curl -fsS -X POST "$OLIVIADB_URL/v1/aggregates/Customer/C1" -H "$H" -H 'content-type: application/json' \
  -d '{"fields": {"name": "Ada Lovelace", "branch": "B1"}}'
curl -fsS -X POST "$OLIVIADB_URL/v1/aggregates/Customer/C1/Account/A1" -H "$H" -H 'content-type: application/json' \
  -d '{"fields": {"currency": "EUR", "balance": "100.00", "status": "open"}}'
curl -fsS "$OLIVIADB_URL/v1/aggregates/Customer/C1?tree=true" -H "$H"
# The other tenant has its own namespace: no C1 there.
test "$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Token $(cat api-key-globex)" \
  "$OLIVIADB_URL/v1/aggregates/Customer/C1")" = 404

Change it later

A schema changes by a new version of the file. Data already stored stays as it was written: every stored version records the layout it was written with, and reads show the fields a newer layout adds with their defaults. Before restarting with the new file, plan it against the deployed one; the plan refuses changes that would break stored data or callers, and a server started with such a schema refuses to start, naming them (schema evolution): changing a key, reusing a field number with another type, removing a field a profile grants, moving a type.

Version 2 adds the customer's e-mail address (which the app profile may update), a Card type before Account, and lets app watch the accounts' changes. A new type may go anywhere: the server records the codes the deployed types' keys were written with (every node type has one, part of every stored key) and keeps them, giving a new type a free code between its neighbours:

schema bank version 2

aggregate Customer {
  key id: string(12)
  1 name:   string(80)
  2 branch: string(4)
  3 email:  string(120)

  child Card [pan: string(19)] unique {
    1 account: ref ../Account
  }

  child Account [number: string(20)] unique {
    1 currency: string(3)
    2 balance:  decimal(18,2)
    3 status:   enum { open = "O", closed = "C" }

    child Entry [posted: timestamp desc, seq: int32] {
      1 amount: decimal(18,2)
      2 memo:   text
    }
  }
}

index account_by_number on Customer/Account(number) unique

profile app for service "app" {
  consistency strong

  get    Customer/{id}/**
  list   Customer limit 100
  list   Customer/{id}/Account
  range  Customer/{id}/Account/{n}/Entry by posted limit 100
  insert Customer
  insert Customer/{id}/Account
  insert Customer/{id}/Account/{n}/Entry
  insert Customer/{id}/Card
  update Customer/{id}               fields(email)
  update Customer/{id}/Account/{n}  fields(balance, status)
  lookup account_by_number -> Customer
  watch  Customer/*/Account
}
cp bank.olv bank-v1.olv           # the deployed version, to plan against
cp bank-v2.olv bank.olv           # your edited file
oliviadb schema plan bank.olv --against bank-v1.olv
kill "$(cat oliviadb.pid)"; while kill -0 "$(cat oliviadb.pid)" 2>/dev/null; do sleep 0.1; done
oliviadb-server --config config.json >> server.log 2>&1 &
echo $! > oliviadb.pid
until curl -fsS --cacert server.pem https://localhost:8443/readyz; do sleep 0.2; done
. ./oliviadb.env
# What version 1 wrote reads as it was: the customer (with the new field's default, ""),
# and its account, an Account with its balance.
TREE=$(curl -fsS "$OLIVIADB_URL/v1/aggregates/Customer/C1?tree=true" -H "Authorization: Token $OLIVIADB_KEY")
echo "$TREE" | grep -q '"balance":"100.00"'
echo "$TREE" | grep -q '"path":"Customer/C1/Account/A1","type":"Account"'

The plan prints one line per change and plan accepted (exit code 0), or the refused changes (exit code 2). Keep the deployed file under version control: it is what the next plan compares against.

With your coding agent

With the OliviaDB skills installed (step 0), the agent does this step from one request, and runs the commands above:

Ask your agent

Using the oliviadb-develop skill, write an OliviaDB schema for customers with accounts and account entries, an index on the account number, and a profile for our service app that may read customers whole, open accounts and post entries; check it and deploy it to the running server.

Next: connect an application.