Getting Started¶
This guide walks you through installing and using Barrel DocDB. You'll have a working database in under 5 minutes.
Prerequisites¶
- Erlang/OTP 27+ (for embedded use or building from source)
- Docker (optional, for standalone deployment)
Installation¶
Your First Database¶
Create a Database¶
Response:
Store a Document¶
Create a document with an auto-generated ID:
curl -X POST http://localhost:8080/db/myapp \
-H "Content-Type: application/json" \
-d '{
"type": "user",
"name": "Alice",
"email": "alice@example.com",
"role": "admin"
}'
Response:
Or store with a specific ID:
curl -X PUT http://localhost:8080/db/myapp/user-alice \
-H "Content-Type: application/json" \
-d '{
"type": "user",
"name": "Alice",
"email": "alice@example.com"
}'
Retrieve a Document¶
Response:
{
"id": "user-alice",
"_rev": "1-abc123...",
"type": "user",
"name": "Alice",
"email": "alice@example.com"
}
Update a Document¶
Include the _rev field from the previous response:
curl -X PUT http://localhost:8080/db/myapp/user-alice \
-H "Content-Type: application/json" \
-d '{
"_rev": "1-abc123...",
"type": "user",
"name": "Alice Smith",
"email": "alice@example.com",
"role": "admin"
}'
Delete a Document¶
Start the Application¶
Create a Database¶
Store a Document¶
With auto-generated ID:
{ok, #{<<"id">> := DocId, <<"rev">> := Rev}} =
barrel_docdb:put_doc(<<"myapp">>, #{
<<"type">> => <<"user">>,
<<"name">> => <<"Alice">>,
<<"email">> => <<"alice@example.com">>
}).
With specific ID:
{ok, #{<<"rev">> := Rev}} =
barrel_docdb:put_doc(<<"myapp">>, #{
<<"id">> => <<"user-alice">>,
<<"type">> => <<"user">>,
<<"name">> => <<"Alice">>
}).
Retrieve a Document¶
{ok, Doc} = barrel_docdb:get_doc(<<"myapp">>, <<"user-alice">>).
%% Doc = #{<<"id">> => <<"user-alice">>, <<"_rev">> => <<"1-...">>, ...}
Update a Document¶
{ok, #{<<"rev">> := NewRev}} =
barrel_docdb:put_doc(<<"myapp">>, Doc#{
<<"name">> => <<"Alice Smith">>,
<<"role">> => <<"admin">>
}).
Delete a Document¶
Querying Documents¶
Barrel DocDB provides declarative queries with automatic path indexing. No need to create indexes manually.
Find all users:
curl -X POST http://localhost:8080/db/myapp/_find \
-H "Content-Type: application/json" \
-d '{
"where": [{"path": ["type"], "value": "user"}]
}'
Find admins:
curl -X POST http://localhost:8080/db/myapp/_find \
-H "Content-Type: application/json" \
-d '{
"where": [
{"path": ["type"], "value": "user"},
{"path": ["role"], "value": "admin"}
]
}'
With pagination:
Watching for Changes¶
Subscribe to real-time changes using MQTT-style path patterns.
Working with Attachments¶
Store binary files (images, documents, etc.) alongside your documents.
Store an attachment:
curl -X PUT http://localhost:8080/db/myapp/user-alice/_attachments/photo.jpg \
-H "Content-Type: image/jpeg" \
--data-binary @photo.jpg
Retrieve an attachment:
Delete an attachment:
Configuration¶
Environment Variables¶
| Variable | Default | Description |
|---|---|---|
BARREL_DATA_DIR |
data/barrel_docdb |
Data directory |
BARREL_HTTP_PORT |
8080 |
HTTP server port |
BARREL_HTTP_ENABLED |
true |
Enable HTTP server |
Erlang Configuration¶
In sys.config:
Next Steps¶
Now that you have Barrel DocDB running, explore these features:
-
Advanced query syntax, operators, and filtering
-
Real-time subscriptions and MQTT-style patterns
-
Sync data between nodes with filtering
-
Query across multiple databases
-
Hot/warm/cold data tiers with automatic migration
-
Complete endpoint documentation
Troubleshooting¶
Document Update Fails with Conflict
If you get a conflict error when updating, ensure you're including the current _rev value from the document. Barrel uses MVCC (Multi-Version Concurrency Control) to prevent lost updates.
Connection Refused
Check that the HTTP server is running:
If using Docker, verify the container is running: