Quickstart
DefraDB is the database for local-first applications that prioritizes data ownership, personal privacy, and P2P synchronization. It features data encryption and verification, the ability to travel in time through the history of documents, and a multi-write-master architecture. The DefraDB Query Language (DQL) is based on GraphQL.
For more background on the local-first paradigm, see The Edge-First Awakening: Redefining the Foundations of Modern Computing.
Install
Get defradb by downloading the executable appropriate to your system.
Define a secret for DefraDB's keyring and start the local node:
DEFRA_KEYRING_SECRET=<secret> defradb start
To verify the local connection to the node, ping the /health-check HTTP endpoint:
wget -qO- http://localhost:9181/health-check
An online node responds with "Healthy".
-> More information and install options
Interact with the database
You can interact with DefraBD in a few different ways. Most actions can be run with all tools, but some are not available on all options.
- CLI
- HTTP API
- GraphQL
- Explorer
The client CLI commands allow to interact with an instance from the command line.
The HTTP API is available at http://localhost:9181/api/v1/.
A versionless endpoint is also available at http://localhost:9181/api/ and points always to the latest version.
GraphQL clients (ex. Altair) are a popular option to interact with the GraphQL API.
The GraphQL endpoint is available at http://localhost:9181/api/v1/graphql.
A versionless endpoint is also available at http://localhost:9181/api/graphql and points always to the latest version.
The DefraDB Explorer is a web application available at http://localhost:9181, and also hosted at https://explorer.source.network/. It provides a GraphQL client and an interface to the most common instance management operations.
Add collections
Collections are the types into which documents fit, like tables in SQL. Because every document belongs to a collection, you need to create collections before you can insert any data. A collection has a name (ex. Book) and a number of typed fields (ex. title: String).
- CLI
- HTTP API
You can create a collection with the CLI command defradb client collection add.
defradb client collection add '
type Book {
title: String!
plot: String
rating: Float
}
'
You can create a collection via the HTTP API endpoint /collections.
POST http://localhost:9181/api/v1/collections HTTP/2
accept: application/json
content-type: text/plain
type Book {
title: String!
plot: String
rating: Float
}
-> More information on collections
Create documents
- CLI
- HTTP API
- GraphQL API
To create documents of type <type>, use the mutation add_<type> via the CLI command defradb client query. For example, to create a document in the Book collection, use add_Book.
Every add_<type> mutation must return some of the inserted information. Because GraphQL queries only return the exact fields requested, you have to provide a list of return fields (there is no equivalent of the SQL SELECT * syntax).
defradb client query '
mutation {
b1:add_Book(input: {
title: "1984",
plot: "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
rating: 4.20
}) {
_docID
title
}
b2:add_Book(input: {
title: "Lord of the Flies",
plot: "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
rating: 3.70
}) {
_docID
title
}
}
'
{
"data": {
"b1": [
{
"_docID": "bae-546ae840-77c7-51a5-ab0a-b5a893bfa546",
"title": "1984"
}
],
"b2": [
{
"_docID": "bae-6c91c35c-e548-58f8-86a6-d60ab5174072",
"title": "Lord of the Flies"
}
]
}
}
To create documents of type <type>, submit a POST request to the HTTP endpoint /api/v1/collections/<type>. For example, submit a request to /api/v1/collections/Book. The request body should contain the documents information in JSON format.
POST http://localhost:9181/api/v1/collections/Book HTTP/2
accept: application/json
content-type: application/json
[
{
"title": "1984",
"plot": "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
"rating": 4.20
},
{
"title": "Lord of the Flies",
"plot": "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
"rating": 3.70
}
]
To create documents of type <type>, use the mutation add_<type>. For example, to create a document in the Book collection, use add_Book.
Every add_<type> mutation must return some of the inserted information. Because GraphQL queries only return the exact fields requested, you have to provide a list of return fields (there is no equivalent of the SQL SELECT * syntax).
mutation {
b1:add_Book(input: {
title: "1984",
plot: "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
rating: 4.20
}) {
_docID
title
}
b2:add_Book(input: {
title: "Lord of the Flies",
plot: "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
rating: 3.70
}) {
_docID
title
}
}
{
"data": {
"b1": [
{
"_docID": "bae-546ae840-77c7-51a5-ab0a-b5a893bfa546",
"title": "1984"
}
],
"b2": [
{
"_docID": "bae-6c91c35c-e548-58f8-86a6-d60ab5174072",
"title": "Lord of the Flies"
}
]
}
}
_docID is the document's unique identifier, determined by the collection it belongs to and the data it is initialized with.
-> More information on creating documents
Query documents
The basic skeleton of a query is made of the collection you want to fetch from (ex. Book) and the fields you want to return (ex. _docID, title, plot).
- CLI
- HTTP API
- GraphQL API
You can run a query via the CLI command defradb client query.
defradb client query '
{
Book {
_docID
title
plot
}
}
'
{
"data": {
"Book": [
{
"_docID": "bae-526a42c6-c147-57e4-89e0-875d27a1532d",
"plot": "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
"title": "1984"
},
{
"_docID": "bae-9579541e-f15d-506e-a74a-63d00cb3ab56",
"plot": "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
"title": "Lord of the Flies"
}
]
}
}
You can run a query by submitting a POST request to the HTTP endpoint /api/v1/graphql. The body must be a JSON object, with the GraphQL query under the query key. Newlines are not supported within the query string field.
POST http://localhost:9181/api/v1/graphql HTTP/2
accept: application/json
content-type: application/json
{
"query": "{ Book { _docID title plot } }"
}
{
"data": {
"Book": [
{
"_docID": "bae-526a42c6-c147-57e4-89e0-875d27a1532d",
"plot": "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
"title": "1984"
},
{
"_docID": "bae-9579541e-f15d-506e-a74a-63d00cb3ab56",
"plot": "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
"title": "Lord of the Flies"
}
]
}
}
{
Book {
_docID
title
plot
}
}
{
"data": {
"Book": [
{
"_docID": "bae-526a42c6-c147-57e4-89e0-875d27a1532d",
"plot": "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
"title": "1984"
},
{
"_docID": "bae-9579541e-f15d-506e-a74a-63d00cb3ab56",
"plot": "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
"title": "Lord of the Flies"
}
]
}
}