CouchGraph: just relax
The documents in CouchDB, handed to the AI as a graph: one question, one query, and nobody has to understand the database.

Time to relax
When CouchDB starts, it writes to the log: "Apache CouchDB has started. Time to relax." It is not a throwaway joke: relax is the project's motto, right under the logo. The idea is that the database is easy to use, takes a beating in production without drama, and a problem in one request stays in that request.
In an operation run by agents, that fits nicely. A collector pulls data from an external API all day and keeps it all in Couch. Every morning the agent has to read that data and answer questions: what came in yesterday, what is stuck, who is the customer behind that order. This post is about how we made that reading easy for the AI: CouchGraph, which hands Couch's documents over as a graph.
Why CouchDB for this
- JSON documents. Every object is a document with an _id and a _rev. There is no schema to migrate: the collector stores what the source returned, as it came.
- Revisions, not locks. Every write creates a new revision (MVCC) in an append-only file. Reads never wait for writes, and a crash halfway through corrupts nothing.
- HTTP is the API. GET, PUT, _find, _changes. You can talk to the database with curl, and every language already knows how.
- Views. Map/reduce in a design doc, indexed incrementally. That is where "every order of this customer" lives.
Under it all, Erlang
CouchDB is written in Erlang, on top of OTP, the same platform that was born for telephone switches: systems that cannot stop, with a lot happening at once. A good part of the motto's relax comes from here.
- Lightweight processes. Thousands of cheap processes, each with its own memory. A request is a process.
- Messages, not shared memory. Processes talk by message, and data is immutable. No locks, no race conditions on shared state.
- Let it crash. When a process breaks, it dies alone and a supervisor starts another. The error stays in the request that failed, it does not take the server down.
HTTP first, replication and clustering
The other half of the relax: Couch was made to live on a network. It is not a database with an HTTP API bolted on; HTTP is its protocol, even between its own nodes.
- HTTP first. Everything is HTTP with JSON: read, write, create an index, replicate, look at the cluster. The collector needs no driver (it is Go with only the standard library), CouchGraph talks HTTP to Couch, and an agent with curl can already check a document. The cache, proxy, TLS and load balancer you already have work as they are.
- Replication is a document. Store a document in the _replicator database with a source and a target, and Couch starts copying, incrementally, reading the source's _changes and remembering where it stopped. The network drops? It comes back and resumes from the checkpoint. Taking the production database to a laptop, to a database just for the agents or to another region is one POST.
- Master-master. Replication both ways, between as many nodes as you like, and all of them take writes. If two sides changed the same document, nothing is lost: Couch picks a winning revision the same way on every node and keeps the others in _conflicts, for someone (or an agent) to settle.
- Cluster. Since version 2, every database is split into shards, and each shard has n copies on different nodes (3 by default). Any node answers any request; reads and writes wait for a quorum. A node goes down and the database keeps answering. For the client it is the same HTTP as a single node.
For CouchGraph this comes for free. It only talks HTTP, so it does not matter whether the other side is one node, a cluster or a replica made just for the agents to read without touching the database the collector writes.
The problem: loose documents
A document is great for keeping what the source returned, and poor at answering relationship questions. An order knows its customer's _id in a customer_ref field; that order's payments are in a view. To answer "what happened to order 7", the agent has to know every view's name, build keys, fetch document by document and join it all in its head. It works. It burns context, gets joins wrong and makes up fields.
The links were already there: the *_ref fields and the views. Something just had to say it out loud, in a format the agent reads in one go. A graph.
The same documents, as a graph
CouchGraph is a GraphQL server that sits in front of a CouchDB database. Every document type becomes a schema type; every *_ref becomes an edge from child to parent (belongsTo); every one-to-many view becomes the way back, from parent to children (hasMany). The agent stops thinking in documents: it thinks of the order and walks the links.
Pick a question and see which part of the graph it walks.
Every node is a Couch document, with the _id the collector gave it. Nothing was copied: the graph is just a way to read.
{
order(id: "order:o_7") {
status
customer { data address { data } }
payments { status data }
shipments { status }
}
}What changes for the agent
| Loose documents | Graph | |
|---|---|---|
| Find out what exists | read loose documents and guess the fields | read the schema: types, fields and links, with descriptions |
| Follow a link | take the customer_ref and ask for another document | ask for the customer field |
| List the children | know the view's name and build the key | ask for the orders field |
| Round trips | one per document | one per question |
| Writing by accident | the same HTTP that reads also writes | there is no mutation |
The biggest win is not even the query. It is the schema. An agent that has never seen the database asks for the introspection, reads the types, the links and the descriptions, and already knows what it can ask. The documentation lives where the data lives, and it cannot go stale without breaking the build.
Where the idea came from
It came from seeing Sibelius's graphql-relay-visualize: an app that draws, with live diagrams, what GraphQL and Relay do under the hood, including the normalized store as a graph of records linked by id. Looking at it, it was obvious that Couch's documents were already a normalized store waiting for someone to draw the edges.
One document, as the collector stores it
The top-level fields belong to the collector, for linking and filtering. The source object goes whole into data.
{
"_id": "order:o_7",
"type": "order",
"source_id": "o_7",
"status": "completed",
"open": false,
"hash": "sha256:…",
"collected_at": "2026-12-01T10:12:00Z",
"customer_ref": "customer:c_1",
"data": { "…": "the raw object the source returned" }
}The view that becomes a hasMany
// _design/store, view orders_by_customer
function (doc) {
if (doc.type === "order" && doc.customer_ref) {
emit(doc.customer_ref, null);
}
}CouchGraph's files
A workspace with one project per database. Each project gets its own route (/store/query), and the workspace locks them all.
couchgraph/
couchgraph.yaml # workspace: connection, port, auth; one project per database
store/
couchgraph.yaml # the database, indexes, design docs
schema/store.graphqls # types, links and queriescouchdb:
url: ${COUCHDB_URL}
user: ${COUCHDB_USER}
password: ${COUCHDB_PASSWORD}
projects:
- ./store # served at /store/query
server:
port: 8080
playground: false
readOnly: true # every mutation is rejected
auth:
enabled: true
requireAuth: true # even reads need a token (JWT)
jwtSecret: ${JWT_SECRET}couchdb:
database: store
schema:
core: true # document, findDocs, queryView next to the typed schema
paths:
- schema/**/*.graphqls
designDocs:
sync: manual # the views belong to the collector, not to CouchGraph
indexes:
- name: idx_type_status
fields: [type, status]The schema
@collection says which document type that type is. @belongsTo reads a *_ref. @hasMany calls a view. @get and @find become the entry points. The quoted descriptions are what the agent reads.
"""
Order collected from the source. data is the raw object.
"""
type Order @collection(type: "order") {
id: ID!
status: String
"true while the order has not reached a final status."
open: Boolean!
data: Map
customer: Customer @belongsTo(field: "customer_ref")
payments: [Payment!]! @hasMany(view: "store/payments_by_order")
shipments: [Shipment!]! @hasMany(view: "store/shipments_by_order")
}
type Customer @collection(type: "customer") {
id: ID!
data: Map
address: Address @belongsTo(field: "address_ref")
orders: [Order!]! @hasMany(view: "store/orders_by_customer")
}
extend type Query {
order(id: ID!): Order @get
orders(status: String, limit: Int = 50, bookmark: String): [Order!]!
@find(selector: "{\"status\": \"$status\"}")
}The question, from outside
It is just a POST. The token lives in an environment variable; never in the prompt, never in a file.
curl -s https://graph.example.com/store/query \
-H "Authorization: Bearer $READ_TOKEN" \
-H "Content-Type: application/json" \
-d '{"query":"{ orders(status: \"processing\") { id customer { data } } }"}'The rules that hold the graph
- Deterministic _id. <type>:<source id>. Collecting again updates in place, and a link never points to a document that changed its name.
- The link is the full _id. customer_ref stores "customer:c_1", not "c_1". belongsTo reads the field and fetches the document, no translation.
- Every hasMany has a view. A one-to-many link only goes into the schema once the view that answers it exists in the design doc.
- The writer owns the views. The collector creates the design doc; CouchGraph only reads it (designDocs: manual). Only one side touches the index.
- The raw object stays in data. Top-level fields (status, open, *_ref) belong to the collector; the source object goes whole into data. The schema never has to chase the source.
- Read only, always with a token. readOnly and requireAuth in the workspace and in every project. The agent can ask the wrong question; it cannot delete the answer.
The procedure
- Collect. A process that only sends GET to the source and stores one document per object, with a hash so nothing changes when nothing changed.
- Link. When storing, the collector fills the *_ref fields with the related document's _id (and stores that document too, if it does not exist yet).
- Index. One view per one-to-many link (orders_by_customer...) and one Mango index per filter the agent will use.
- Describe. A .graphqls with @collection, @belongsTo, @hasMany and the queries. The schema's descriptions are the manual the agent reads.
- Lock and serve. readOnly, requireAuth, a read token for the agent. Playground only when needed, and still with a token.
- Teach the question. In the agent's skill, the URL, the token's variable name and two or three ready queries. It finds the rest in the schema.
How to know it worked
- the agent answers "what happened to order X" with a single query;
- every hasMany edge in the schema has its view in the design doc;
- any mutation comes back rejected, and a query without a token comes back 401;
- collecting everything again changes no _id and creates no revision on an unchanged document;
- a new agent, given only the URL and the token, finds the rest through the schema.
References
- Why CouchDB?the motto's relax, in the project's words
- CouchDB technical overviewdocuments, MVCC, views and the Erlang/OTP implementation
- CouchDB replication_replicator, _changes, checkpoints and conflicts
- CouchDB clustershards, copies and quorum
- CouchDB viewsmap/reduce in the design doc, the base of hasMany
- CouchDB _find (Mango)selectors and indexes, the base of @find
- Erlang/OTPlightweight processes, messages and supervisors
- GraphQL introspectionhow the agent finds out the schema by itself
- sibelius/graphql-relay-visualizeGraphQL and Relay drawn live, where the idea came from
greetz sibelius for graphql-relay-visualize, which sparked this idea
