Create, retrieve, update, and delete documents in Fauna
Fauna allows you to store documents and query them in a relational fashion. This section walks you through a basic example of creating, retrieving, updating, and deleting (CRUD) documents in Fauna, including working with collections. If you are new to Fauna, make sure to check out our Glossary for definitions.
Introduction
To demonstrate how to perform CRUD operations in Fauna, we are going to use the example of blog posts: creating blog posts, updating them with additional attributes, and querying for specific posts.
The steps are:
-
Make sure that the Requirements are met.
We have set up this example so you can follow along from start to finish. Feel free to skip straight to Create a post if you are just looking for examples of the create, retrieve, update, and delete process.
Requirements
This section walks you through setting up your environment, installing a driver, importing the driver, obtaining an admin key, and instantiating the client.
Supported runtimes
Before you install the driver, it’s important to ensure you’re running a compatible version of the language runtime and have satisfied other dependencies.
Fauna’s wire protocol is HTTP, so you can use tools like curl
to
inspect the API. However, we recommend using an official driver whenever
possible.
The JavaScript driver supports and is tested on:
-
Node.js
-
LTS (v4)
-
Stable (v6)
-
v0.12.x
-
-
Chrome
-
Firefox
-
Safari
-
Internet Explorer 11
Currently, the driver is tested on Go versions:
-
1.5
-
1.6
-
1.7
-
1.8
Compatible with:
-
.NET Standard 1.5 and 2.0
-
.NET Framework 4.5, 4.5.1, 4.6, 4.6.1, 4.7
-
iOS 9.0+
-
OSX 10.10+
-
tvOS 9.0+
-
watchOS 2.0+
-
Xcode 8
-
Swift 3
-
Jackson for JSON parsing.
-
Async HTTP client and Netty for the HTTP transport.
-
Joda Time for date and time manipulation.
-
OkHttp client for the HTTP transport.
-
Google Guava, for collections and ListenableFutures.
-
Java 7
-
Google Guava, for collections and ListenableFutures.
-
Jackson for JSON parsing.
-
Async HTTP client and Netty for the HTTP transport.
-
Joda Time for date and time manipulation.
-
Scala 2.11.x
The following versions of Python are supported:
-
Python 2.7
-
Python 3.3
-
Python 3.4
-
Python 3.5
-
Python 3.6
Install the driver
Consult your operating system package manager if the curl
command is
not available on your machine, or use a different HTTP client.
To install the JavaScript driver, run this in the terminal:
npm install --save faunadb
See faunadb
on NPM for more
information.
The browser release can be found in the
fauna/faunadb-js-release
repository.
This release can be installed via bower:
bower install faunadb
Or via CDN:
<script src="//cdn.jsdelivr.net/gh/fauna/faunadb-js-release@latest/faunadb.js"></script>
The minified version of the driver can also be used via CDN:
<script src="//cdn.jsdelivr.net/gh/fauna/faunadb-js-release@latest/faunadb-min.js"></script>
To install the Go driver, run this in the terminal:
go get github.com/fauna/faunadb-go
First install the Nuget package by adding the package reference to your MSBuild project:
<PackageReference Include="FaunaDB.Client" Version="2.0.0" />
or by using your IDE and searching for FaunaDB.Client
.
pod 'FaunaDB', '~> 2.0.0'
github 'fauna/faunadb-swift'
.Package(url: "https://github.com/fauna/faunadb-swift.git", Version(2, 0, 0))
Download from the Maven central repository:
faunadb-java/pom.xml
<dependencies>
...
<dependency>
<groupId>com.faunadb</groupId>
<artifactId>faunadb-java</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
...
</dependencies>
faunadb-android/pom.xml
<dependencies>
...
<dependency>
<groupId>com.faunadb</groupId>
<artifactId>faunadb-android</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
...
</dependencies>
faunadb-scala/sbt
libraryDependencies += ("com.faunadb" %% "faunadb-scala" % "2.1.0")
pip install faunadb
Import the driver
The curl
command should be in your shell path.
var faunadb = require('faunadb'),
q = faunadb.query;
This is the recommended require stanza. The faunadb.query
module
contains all of the functions to create Fauna Query expressions.
Similarly with es6 modules:
import faunadb, { query as q } from "faunadb"
The CDN package exposes a global faunadb
variable.
We recommend that you import this driver with an alias import such as:
import (
"fmt"
f "github.com/fauna/faunadb-go/faunadb"
)
Import the client and the query language helpers.
using FaunaDB.Client;
using FaunaDB.Types;
using static FaunaDB.Query.Language;
import FaunaDB
import com.faunadb.client.*;
import com.faunadb.client.types.*;
import com.faunadb.client.types.Value.*;
import faunadb.FaunaClient
import faunadb.query._
import faunadb.values._
from faunadb import query as q
from faunadb.objects import Ref
from faunadb.client import FaunaClient
Obtain an admin key
To follow along, you need an admin key, which you can create using the Dashboard.
Instantiate an admin client
HTTP is stateless, so the only initialization is providing your Fauna
key secret as part of the basic authentication headers. In curl
you do
that with the -u
flag.
var adminClient = new faunadb.Client({ secret: 'YOUR_FAUNADB_ADMIN_SECRET' });
adminClient := f.NewFaunaClient("YOUR_FAUNADB_ADMIN_SECRET")
var adminClient = new FaunaClient(secret: YOUR_FAUNADB_ADMIN_SECRET);
let adminClient = FaunaDB.Client(secret: "YOUR_FAUNADB_ADMIN_SECRET")
FaunaClient adminClient = FaunaClient.builder()
.withSecret("YOUR_FAUNADB_ADMIN_SECRET")
.build();
val adminClient = FaunaClient(secret = "YOUR_FAUNADB_ADMIN_SECRET")
adminClient = FaunaClient(secret="YOUR_FAUNADB_ADMIN_SECRET")
Create a database
Create a database called my_app
in Fauna:
adminClient.Query(CreateDatabase(Obj("name", "my_app")));
{
"ref": { "@ref": "databases/my_app" },
"class": { "@ref": "databases" },
"ts": 1520225686389954,
"name": "my_app"
}
curl https://db.fauna.com/ \
-u fnACrVRyZhACAOvXOTRzOpg1Qqq5oPHKMfOLgcHQ: \
-d '{ "create_database": { "object": { "name": "my_app" } } }'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "databases/my_app" },
"class": { "@ref": "databases" },
"ts": 1520225686389954,
"name": "my_app"
}
}
result, _ := adminClient.Query(f.CreateDatabase(f.Obj{"name": "my_app"}))
fmt.Println(result)
map[ref:{my_app 0xc420126220 <nil>} ts:1520225686389954 name:my_app]
System.out.println(
adminClient.query(CreateDatabase(Obj("name", Value("my_app")))
).get());
{
ref: ref(id = "my_app", collection = ref(id = "databases")),
ts: 1527146032847201,
name: "my_app"
}
adminClient.query(
q.CreateDatabase({ name: 'my_app' })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=my_app, collection=Ref(id=databases)),
ts: 1528127545620042,
name: 'my_app' }
adminClient.query(q.create_database({"name": "my_app"}))
{
"ref": { "@ref": "databases/my_app" },
"class": { "@ref": "databases" },
"ts": 1520225686389954,
"name": "my_app"
}
adminClient.query(CreateDatabase(Obj("name" -> "my_app")))
{
"ref": { "@ref": "databases/my_app" },
"class": { "@ref": "databases" },
"ts": 1520225686389954,
"name": "my_app"
}
adminClient.query(CreateDatabase(Obj("name" => "my_app")))
{
"ref": { "@ref": "databases/my_app" },
"class": { "@ref": "databases" },
"ts": 1520225686389954,
"name": "my_app"
}
Create a server key to access the my_app
database
Create a server key. The server key has unrestricted access to a single
database; in this case, the server key allows access only to the
my_app
database that we just created.
adminClient.Query(
CreateKey(
Obj("database", Database("my_app"), "role", "server")));
{
"ref": { "@ref": "keys/192903209473278464" },
"class": { "@ref": "keys" },
"ts": 1520225686419239,
"database": { "@ref": "databases/my_app" },
"role": "server",
"secret": "fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq",
"hashed_secret": "$2a$05$k9x7YbL5CYY9KIMhW6RaIOILyEwY3IHuX0NWvK9iUMWk8TJ7k1H8O"
}
curl https://db.fauna.com/ \
-u fnACrVRyZhACAOvXOTRzOpg1Qqq5oPHKMfOLgcHQ: \
-d '{
"create_key": {
"object": { "database": { "database": "my_app" }, "role": "server" }
}
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "keys/192903209473278464" },
"class": { "@ref": "keys" },
"ts": 1520225686419239,
"database": { "@ref": "databases/my_app" },
"role": "server",
"secret": "fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq",
"hashed_secret": "$2a$05$k9x7YbL5CYY9KIMhW6RaIOILyEwY3IHuX0NWvK9iUMWk8TJ7k1H8O"
}
}
result, _ := adminClient.Query(
f.CreateKey(
f.Obj{"database": f.Database("my_app"), "role": "server"},
),
)
fmt.Println(result)
map[
ref:{192903209473278464 0xc42011ca60 <nil>}
ts:1520225686419239
database:{my_app 0xc42011cc00 <nil>}
role:server
secret:fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq
hashed_secret:$2a$05$k9x7YbL5CYY9KIMhW6RaIOILyEwY3IHuX0NWvK9iUMWk8TJ7k1H8O
]
System.out.println(
adminClient.query(
CreateKey(
Obj(
"database", Database(Value("my_app")),
"role", Value("server")
)
)
).get());
{
ref: ref(id = "200313471592563200", collection = ref(id = "keys")),
ts: 1536604026291379,
database: ref(id = "my_app", collection = ref(id = "databases")),
role: "server",
secret: "fnAC6lgMjxACAFGhYyPLmQGQD3tCZkXWklDs1L6k",
hashed_secret: "$2a$05$yQwmJEw4GrJViCqIDi0fa.4yz.dEDi3voIqAb6f7z3tvOHe4VZuHG"
}
adminClient.query(
q.CreateKey({
database: q.Database('my_app'),
role: 'server',
})
)
.then((ret) => console.log(ret))
{ ref: Ref(id=201188951594107392, collection=Ref(id=keys)),
ts: 1528127585813144,
database: Ref(id=my_app, collection=Ref(id=databases)),
role: 'server',
secret: 'fnACysRJGIACAHiL_5f0UxHlPFIZgq876ptMNJ72',
hashed_secret: '$2a$05$wm6Zwl8dhlY3hFbJj0JrHuSt4YszzsDRd9KdxVbxg98yVOMJiEp0i' }
adminClient.query(
q.create_key(
{"database": q.database("my_app"), "role": "server"}
))
{
"ref": { "@ref": "keys/192903209473278464" },
"class": { "@ref": "keys" },
"ts": 1520225686419239,
"database": { "@ref": "databases/my_app" },
"role": "server",
"secret": "fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq",
"hashed_secret": "$2a$05$k9x7YbL5CYY9KIMhW6RaIOILyEwY3IHuX0NWvK9iUMWk8TJ7k1H8O"
}
adminClient.query(
CreateKey(
Obj("database" -> Database("my_app"), "role" -> "server")))
{
"ref": { "@ref": "keys/192903209473278464" },
"class": { "@ref": "keys" },
"ts": 1520225686419239,
"database": { "@ref": "databases/my_app" },
"role": "server",
"secret": "fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq",
"hashed_secret": "$2a$05$k9x7YbL5CYY9KIMhW6RaIOILyEwY3IHuX0NWvK9iUMWk8TJ7k1H8O"
}
adminClient.query(
CreateKey(
Obj(
"database" => Database("my_app"),
"role" => "server"
)
)
)
{
"ref": { "@ref": "keys/192903209473278464" },
"class": { "@ref": "keys" },
"ts": 1520225686419239,
"database": { "@ref": "databases/my_app" },
"role": "server",
"secret": "fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq",
"hashed_secret": "$2a$05$k9x7YbL5CYY9KIMhW6RaIOILyEwY3IHuX0NWvK9iUMWk8TJ7k1H8O"
}
Save this key’s secret; it is only displayed once, and if you lose it, you would have to generate a new one. The key is used to perform all of the remaining database setup steps.
Your key will be different than the key you see displayed in the command output in this documentation. The key you saved just above will be different. |
Instantiate a client that has server key privileges
Instantiate a client that uses the server key that we just set up, to
perform the rest of the tasks in this tutorial. Be sure to copy the
secret returned in the previous step and replace
YOUR_FAUNADB_SERVER_SECRET
with that value.
var serverClient = new faunadb.Client({ secret: 'YOUR_FAUNADB_SERVER_SECRET' });
serverClient := f.NewFaunaClient("YOUR_FAUNADB_SERVER_SECRET")
var serverClient = new FaunaClient(secret: YOUR_FAUNADB_SERVER_SECRET);
let serverClient = FaunaDB.Client(secret: "YOUR_FAUNADB_SERVER_SECRET")
FaunaClient serverClient = FaunaClient.builder()
.withSecret("YOUR_FAUNADB_SERVER_SECRET")
.build();
val serverClient = FaunaClient(secret = "YOUR_FAUNADB_SERVER_SECRET")
serverClient = FaunaClient(secret="YOUR_FAUNADB_SERVER_SECRET")
Set up a collection
Fauna stores data in the form of nested containers. A database contains collections, and collections contain documents. Each document belongs to a specific collection. So in order to create a document for a post, we need to first create a collection for posts.
Create a collection using the CreateCollection
function with a
param_object
containing the name
of the collection. We shall name
our collection "posts":
serverClient.Query(CreateCollection(Obj("name", "posts")));
{
"ref": { "@ref": "classes/posts" },
"class": { "@ref": "classes" },
"ts": 1520225686488810,
"history_days": 30,
"name": "posts"
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{ "create_collection": { "object": { "name": "posts" } } }'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/posts" },
"class": { "@ref": "classes" },
"ts": 1520225686488810,
"history_days": 30,
"name": "posts"
}
}
result, _ := serverClient.Query(f.CreateCollection(f.Obj{"name": "posts"}))
fmt.Println(result)
map[ref:{posts 0xc4203068e0 <nil>} ts:1520225686488810 history_days:30 name:posts]
System.out.println(
serverClient.query(CreateCollection(Obj("name", Value("posts"))))
.get());
{
ref: ref(id = "posts", collection = ref(id = "collections")),
ts: 1536604026446481,
history_days: 30,
name: "posts"
}
serverClient.query(
q.CreateCollection({ name: 'posts' })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=posts, collection=Ref(id=collections)),
ts: 1527349751848648,
history_days: 30,
name: 'posts' }
serverClient.query(q.create_collection({"name": "posts"}))
{
"ref": { "@ref": "classes/posts" },
"class": { "@ref": "classes" },
"ts": 1520225686488810,
"history_days": 30,
"name": "posts"
}
serverClient.query(CreateCollection(Obj("name" -> "posts")))
{
"ref": { "@ref": "classes/posts" },
"class": { "@ref": "classes" },
"ts": 1520225686488810,
"history_days": 30,
"name": "posts"
}
serverClient.query(CreateCollection(Obj("name" => "posts")))
{
"ref": { "@ref": "classes/posts" },
"class": { "@ref": "classes" },
"ts": 1520225686488810,
"history_days": 30,
"name": "posts"
}
Create an index
Before we create any document, let’s ensure that we can easily access
them. We do this by creating an index using the CreateIndex
function.
We create this index now to help make the examples clear; in production,
you can create an index at any time.
The customary way to access documents within a collection is by specifying a criteria for one of the fields. To enable criteria-based searches, we need to first create an index using the path of the field within the document.
Create an index on the post’s title:
serverClient.Query(
CreateIndex(
Obj(
"name", "posts_by_title",
"source", Collection("posts"),
"terms", Arr(Obj("field", Arr("data", "title")))
)));
{
"ref": { "@ref": "indexes/posts_by_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686568953,
"active": false,
"partitions": 1,
"name": "posts_by_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "title" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{
"create_index": {
"object": {
"name": "posts_by_title",
"source": { "collection": "posts" },
"terms": [ { "object": { "field": [ "data", "title" ] } } ]
}
}
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "indexes/posts_by_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686568953,
"active": false,
"partitions": 1,
"name": "posts_by_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "title" ] } ]
}
}
result, _ := serverClient.Query(
f.CreateIndex(
f.Obj{
"name": "posts_by_title",
"source": f.Collection("posts"),
"terms": f.Arr{f.Obj{"field": f.Arr{"data", "title"}}},
},
),
)
fmt.Println(result)
map[ref:{posts_by_title 0xc4203663e0 <nil>} ts:1520225686568953 active:false partitions:1 name:posts_by_title source:{posts 0xc420366640 <nil>} terms:[map[field:[data title]]]]
System.out.println(
serverClient.query(
CreateIndex(
Obj(
"name", Value("posts_by_title"),
"source", Collection(Value("posts")),
"terms", Arr(Obj("field", Arr(Value("data"), Value("title"))))
)
)).get());
{
ref: ref(id = "posts_by_title", collection = ref(id = "indexes")),
ts: 1527206084051852,
active: false,
partitions: 1,
name: "posts_by_title",
source: ref(id = "posts", collection = ref(id = "collections")),
terms: [ {field: ["data", "title"]} ]
}
serverClient.query(
q.CreateIndex({
name: 'posts_by_title',
source: q.Collection('posts'),
terms: [{ field: ['data', 'title'] }],
})
)
.then((ret) => console.log(ret))
{ ref: Ref(id=posts_by_title, collection=Ref(id=indexes)),
ts: 1527350163658503,
active: false,
partitions: 1,
name: 'posts_by_title',
source: Ref(id=posts, collection=Ref(id=collections)),
terms: [ { field: [Array] } ] }
serverClient.query(
q.create_index(
{
"name": "posts_by_title",
"source": q.collection("posts"),
"terms": [{"field": ["data", "title"]}]
}
))
{
"ref": { "@ref": "indexes/posts_by_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686568953,
"active": false,
"partitions": 1,
"name": "posts_by_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "title" ] } ]
}
serverClient.query(
CreateIndex(
Obj(
"name" -> "posts_by_title",
"source" -> Collection("posts"),
"terms" -> Arr(Obj("field" -> Arr("data", "title")))
)))
{
"ref": { "@ref": "indexes/posts_by_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686568953,
"active": false,
"partitions": 1,
"name": "posts_by_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "title" ] } ]
}
serverClient.query(
CreateIndex(
Obj(
"name" => "posts_by_title",
"source" => Collection("posts"),
"terms" => Arr(Obj("field" => Arr("data", "title")))
)
)
)
{
"ref": { "@ref": "indexes/posts_by_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686568953,
"active": false,
"partitions": 1,
"name": "posts_by_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "title" ] } ]
}
It is also possible to specify the values
of the document that should
be returned when querying the index. We also create an index for a
post’s tags:
serverClient.Query(
CreateIndex(
Obj(
"name", "posts_by_tags_with_title",
"source", Collection("posts"),
"terms", Arr(Obj("field", Arr("data", "tags"))),
"values", Arr(Obj("field", Arr("data", "title")))
)));
{
"ref": { "@ref": "indexes/posts_by_tags_with_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686587489,
"active": false,
"partitions": 1,
"name": "posts_by_tags_with_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "tags" ] } ],
"values": [ { "field": [ "data", "title" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{
"create_index": {
"object": {
"name": "posts_by_tags_with_title",
"source": { "collection": "posts" },
"terms": [ { "object": { "field": [ "data", "tags" ] } } ],
"values": [ { "object": { "field": [ "data", "title" ] } } ]
}
}
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "indexes/posts_by_tags_with_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686587489,
"active": false,
"partitions": 1,
"name": "posts_by_tags_with_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "tags" ] } ],
"values": [ { "field": [ "data", "title" ] } ]
}
}
result, _ := serverClient.Query(
f.CreateIndex(
f.Obj{
"name": "posts_by_tags_with_title",
"source": f.Collection("posts"),
"terms": f.Arr{f.Obj{"field": f.Arr{"data", "tags"}}},
"values": f.Arr{f.Obj{"field": f.Arr{"data", "title"}}},
},
),
)
fmt.Println(result)
map[
ref:{posts_by_tags_with_title 0xc42031ccc0 <nil>}
ts:1520225686587489
active:false
partitions:1
name:posts_by_tags_with_title
source:{posts 0xc42031cf20 <nil>}
terms:[map[field:[data tags]]]
values:[map[field:[data title]]]
]
System.out.println(
serverClient.query(
CreateIndex(
Obj(
"name", Value("posts_by_tags_with_title"),
"source", Collection(Value("posts")),
"terms", Arr(Obj("field", Arr(Value("data"), Value("tags")))),
"values", Arr(Obj("field", Arr(Value("data"), Value("title"))))
)
)).get());
{
ref: ref(id = "posts_by_tags_with_title", collection = ref(id = "indexes")),
ts: 1527206084101804,
active: false,
partitions: 1,
name: "posts_by_tags_with_title",
source: ref(id = "posts", collection = ref(id = "collections")),
terms: [ {field: ["data", "tags"]} ],
values: [ {field: ["data", "title"]} ]
}
serverClient.query(
q.CreateIndex({
name: 'posts_by_tags_with_title',
source: q.Collection('posts'),
terms: [{ field: ['data', 'tags'] }],
values: [{ field: ['data', 'title'] }],
})
)
.then((ret) => console.log(ret))
{ ref: Ref(id=posts_by_tags_with_title, collection=Ref(id=indexes)),
ts: 1527350198235760,
active: false,
partitions: 1,
name: 'posts_by_tags_with_title',
source: Ref(id=posts, collection=Ref(id=collections)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] } ] }
serverClient.query(
q.create_index(
{
"name": "posts_by_tags_with_title",
"source": q.collection("posts"),
"terms": [{"field": ["data", "tags"]}],
"values": [{"field": ["data", "title"]}]
}
))
{
"ref": { "@ref": "indexes/posts_by_tags_with_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686587489,
"active": false,
"partitions": 1,
"name": "posts_by_tags_with_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "tags" ] } ],
"values": [ { "field": [ "data", "title" ] } ]
}
serverClient.query(
CreateIndex(
Obj(
"name" -> "posts_by_tags_with_title",
"source" -> Collection("posts"),
"terms" -> Arr(Obj("field" -> Arr("data", "tags"))),
"values" -> Arr(Obj("field" -> Arr("data", "title")))
)))
{
"ref": { "@ref": "indexes/posts_by_tags_with_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686587489,
"active": false,
"partitions": 1,
"name": "posts_by_tags_with_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "tags" ] } ],
"values": [ { "field": [ "data", "title" ] } ]
}
serverClient.query(
CreateIndex(
Obj(
"name" => "posts_by_tags_with_title",
"source" => Collection("posts"),
"terms" => Arr(Obj("field" => Arr("data", "tags"))),
"values" => Arr(Obj("field" => Arr("data", "title")))
)
)
)
{
"ref": { "@ref": "indexes/posts_by_tags_with_title" },
"class": { "@ref": "indexes" },
"ts": 1520225686587489,
"active": false,
"partitions": 1,
"name": "posts_by_tags_with_title",
"source": { "@ref": "classes/posts" },
"terms": [ { "field": [ "data", "tags" ] } ],
"values": [ { "field": [ "data", "title" ] } ]
}
Create a post
Posts are created by calling the Create
function with the ref
of the
posts collection and a param_object
that specifies the structure of
the document to be created, as well as permissions for the document. The
post’s data is included in the param_object
's data
field.
serverClient.Query(
Create(
Collection("posts"),
Obj("data", Obj("title", "What I had for breakfast .."))));
{
"ref": { "@ref": "classes/posts/192903209680896512" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686617873,
"data": { "title": "What I had for breakfast .." }
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{
"create": { "collection": "posts" },
"params": {
"object": {
"data": { "object": { "title": "What I had for breakfast .." } }
}
}
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/posts/192903209680896512" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686617873,
"data": { "title": "What I had for breakfast .." }
}
}
result, _ := serverClient.Query(
f.Create(
f.Collection("posts"),
f.Obj{"data": f.Obj{"title": "What I had for breakfast .."}},
),
)
fmt.Println(result)
map[ref:{192903209680896512 0xc420370a40 <nil>} ts:1520225686617873 data:map[title:What I had for breakfast ..]]
System.out.println(
serverClient.query(
Create(
Collection(Value("posts")),
Obj(
"data", Obj(
"title", Value("What I had for breakfast ..")
)
)
)
).get());
{
ref: ref(id = "210077143904813568", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1536604026594000,
data: {title: "What I had for breakfast .."}
}
serverClient.query(
q.Create(
q.Collection('posts'),
{ data: { title: 'What I had for breakfast ..' } },
)
)
.then((ret) => console.log(ret))
{ ref:
Ref(id=200372984285757952, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527349418742172,
data: { title: 'What I had for breakfast ..' } }
serverClient.query(
q.create(
q.collection("posts"),
{"data": {"title": "What I had for breakfast .."}}
))
{
"ref": { "@ref": "classes/posts/192903209680896512" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686617873,
"data": { "title": "What I had for breakfast .." }
}
serverClient.query(
Create(
Collection("posts"),
Obj("data" -> Obj("title" -> "What I had for breakfast .."))))
{
"ref": { "@ref": "classes/posts/192903209680896512" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686617873,
"data": { "title": "What I had for breakfast .." }
}
serverClient.query(
Create(
at: Collection("posts"),
Obj(
"data" => Obj("title" => "What I had for breakfast ..")
)
)
)
{
"ref": { "@ref": "classes/posts/192903209680896512" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686617873,
"data": { "title": "What I had for breakfast .." }
}
The Create
function returns the post document just created. As you can
see in the output, the ref
of the document is an
automatically-generated identifier that is unique to the document within
its database.
Using the Ref
function, you can
specify an id to use instead of the auto-generated id, but it must be
unique. For example:
Value result = await serverClient.Query(
Create(
Ref(Collection("posts"), 1),
Obj("data", Obj("title", "The first post"))
)
);
ObjectV(ref: RefV(id = "1", collection = RefV(id = "posts", collection = RefV(id = "collections"))),ts: LongV(1587596508180000),data: ObjectV(title: StringV(The first post)))
result, err := serverClient.Query(
f.Create(
f.RefCollection(f.Collection("posts"), 1),
f.Obj{"data": f.Obj{"title": "The first post"}}))
if (err != nil) {
fmt.Println(err)
} else {
fmt.Println(result)
}
{"object":{"data":{"object":{"title":"The first post"}},"ref":{"@ref":{"collection":{"@ref":{"collection":{"@ref":{"id":"collections"}},"id":"posts"}},"id":"1"}},"ts":1587596382540000}}
System.out.println(
serverClient.query(
Create(
Ref(Collection(Value("posts")), Value(1)),
Obj(
"data", Obj(
"title", Value("The first post")
)
)
)
).get());
{ref: ref(id = "1", collection = ref(id = "posts", collection = ref(id = "collections"))), ts: 1587595695090000, data: {title: "The first post"}}
serverClient.query(
q.Create(
q.Ref(q.Collection('posts'), '1'),
{ data: { title: 'The first post' } },
)
)
.then((ret) => console.log(ret))
{
ref: Ref(Collection("posts"), "1"),
ts: 1587595447990000,
data: { title: 'The first post' }
}
print(serverClient.query(
q.create(
q.ref(q.collection("posts"), 1),
{"data": {"title": "The first post"}}
)
))
{'ref': Ref(id=1, collection=Ref(id=posts, collection=Ref(id=collections))), 'ts': 1587596259820000, 'data': {'title': 'The first post'}}
println(Await.result(
serverClient.query(
Create(
Ref(Collection("posts"), 1),
Obj("data" -> Obj("title" -> "The first post"))
)
),
5.seconds
))
{ref: ref(id = "1", collection = ref(id = "posts", collection = ref(id = "collections"))), ts: 1587595971600000, data: {title: "The first post"}}
Create several posts
It can quickly become tedious to repeat the Create
function for
multiple posts.
This is where Fauna’s transaction language really shines. Let’s use a
Map
function to create several posts at once:
serverClient.Query(
Map(
Arr(
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte"
),
post_title => Create(
Collection("posts"),
Obj("data", Obj("title", post_title)))));
[
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
},
{
"ref": { "@ref": "classes/posts/192903209792047616" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Pondering during a commute" }
},
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
]
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{
"map": {
"lambda": "post_title",
"expr": {
"create": { "collection": "posts" },
"params": {
"object": {
"data": { "object": { "title": { "var": "post_title" } } }
}
}
}
},
"collection": [
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte"
]
}'
HTTP/1.1 200 OK
{
"resource": [
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
},
{
"ref": { "@ref": "classes/posts/192903209792047616" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Pondering during a commute" }
},
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
]
}
result, _ := serverClient.Query(
f.Map(
f.Arr{
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte",
},
f.Lambda(
"post_title",
f.Create(
f.Collection("posts"),
f.Obj{"data": f.Obj{"title": f.Var("post_title")}},
),
),
),
)
fmt.Println(result)
[
map[ref:{192903209792046592 0xc420339c20 <nil>} ts:1520225686723969 data:map[title:My cat and other marvels]]
map[ref:{192903209792047616 0xc420339f00 <nil>} ts:1520225686723969 data:map[title:Pondering during a commute]]
map[ref:{192903209792045568 0xc420380200 <nil>} ts:1520225686723969 data:map[title:Deep meanings in a latte]]
]
System.out.println(
serverClient.query(
Map(
Arr(
Value("My cat and other marvels"),
Value("Pondering during a commute"),
Value("Deep meanings in a latte")
),
Lambda(
Value("post_title"),
Create(
Collection(Value("posts")),
Obj("data",
Obj(
"title", Var("post_title")
)
)
)
)
)
).get());
[
{
ref: ref(id = "192903209792046592", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1536604026800813,
data: {title: "My cat and other marvels"}
},
{
ref: ref(id = "192903209792047616", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1536604026800813,
data: {title: "Pondering during a commute"}
},
{
ref: ref(id = "192903209792045568", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1536604026800813,
data: {title: "Deep meanings in a latte"}
}
]
serverClient.query(
q.Map(
[
'My cat and other marvels',
'Pondering during a commute',
'Deep meanings in a latte',
],
q.Lambda(
'post_title',
q.Create(
q.Collection('posts'),
{ data: { title: q.Var('post_title') } },
)
),
)
)
.then((ret) => console.log(ret))
[ { ref:
Ref(id=200373080062689792, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527349510087794,
data: { title: 'My cat and other marvels' } },
{ ref:
Ref(id=200373080062690816, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527349510087794,
data: { title: 'Pondering during a commute' } },
{ ref:
Ref(id=200373080062691840, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527349510087794,
data: { title: 'Deep meanings in a latte' } } ]
serverClient.query(
q.map_(
lambda post_title: q.create(
q.collection("posts"),
{"data": {"title": post_title}}
),
[
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte"
]
))
[
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
},
{
"ref": { "@ref": "classes/posts/192903209792047616" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Pondering during a commute" }
},
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
]
serverClient.query(
Map(
Arr(
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte"
),
Lambda { post_title =>
Create(
Collection("posts"),
Obj("data" -> Obj("title" -> post_title)))
}))
[
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
},
{
"ref": { "@ref": "classes/posts/192903209792047616" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Pondering during a commute" }
},
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
]
serverClient.query(
Map(
collection: Arr(
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte"
),
to: { postTitle in
Create(
at: Collection("posts"),
Obj("data" => Obj("title" => postTitle))
)
}
)
)
[
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
},
{
"ref": { "@ref": "classes/posts/192903209792047616" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Pondering during a commute" }
},
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
]
Using the Map
function, we can restructure the data as an array and
wrap the Create
in a lambda function, which then runs over each
document in the collection. The anonymous lambda function specifies a
variable post_title
which is used as a placeholder in the parameters
sent to the create
function. This way, multiple documents in a
collection can be created using a single query.
Retrieve posts
The easiest way to retrieve a document is by using its reference value:
serverClient.Query(Get(Ref(Collection("posts"), "192903209792046592")));
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{ "get": { "@ref": "classes/posts/192903209792046592" } }'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
}
result, _ := serverClient.Query(f.Get(f.RefCollection(f.Collection("posts"), "192903209792046592")))
fmt.Println(result)
map[ref:{192903209792046592 0xc420338e80 <nil>} ts:1520225686723969 data:map[title:My cat and other marvels]]
System.out.println(
serverClient.query(Get(Ref(Collection("posts"),Value("192903209792046592"))))
.get()
);
{
ref: ref(id = "192903209792046592", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1527206084284540,
data: {title: "My cat and other marvels"}
}
serverClient.query(
q.Get(q.Ref(q.Collection('posts'), '192903209792046592'))
)
.then((ret) => console.log(ret))
{ ref:
Ref(id=192903209792046592, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527350638301882,
data: { title: 'My cat and other marvels' } }
serverClient.query(q.get(q.ref(q.collection("posts"), "192903209792046592")))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
serverClient.query(Get(Ref(Collection("posts"), "192903209792046592")))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
serverClient.query(Get(Ref(Collection("posts"), "192903209792046592")))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
You can query for posts with a specific title using the match
function
and the index we created earlier:
serverClient.Query(
Get(
Match(Index("posts_by_title"), "My cat and other marvels")));
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{
"get": {
"match": { "index": "posts_by_title" },
"terms": "My cat and other marvels"
}
}'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
}
result, _ := serverClient.Query(
f.Get(
f.MatchTerm(
f.Index("posts_by_title"),
"My cat and other marvels",
),
),
)
fmt.Println(result)
map[ref:{192903209792046592 0xc420338e80 <nil>} ts:1520225686723969 data:map[title:My cat and other marvels]]
System.out.println(
serverClient.query(
Get(
Match(
Index(Value("posts_by_title")),
Value("My cat and other marvels")))).get());
{
ref: ref(id = "192903209792046592", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1527206084284540,
data: {title: "My cat and other marvels"}
}
serverClient.query(
q.Get(
q.Match(q.Index('posts_by_title'), 'My cat and other marvels')
)
)
.then((ret) => console.log(ret))
{ ref:
Ref(id=192903209792046592, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527088583491065,
data: { title: 'My cat and other marvels' } }
serverClient.query(
q.get(
q.match(
q.index("posts_by_title"),
"My cat and other marvels"
)
))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
serverClient.query(
Get(
Match(Index("posts_by_title"), "My cat and other marvels")))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
serverClient.query(
Get(
Match(
index: Index("posts_by_title"),
terms: "My cat and other marvels"
)
)
)
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "My cat and other marvels" }
}
The match
function returns a logical set of elements, which can be
combined with other sets with set-operations like join
, intersect
,
subtract
, etc.
Update posts
You can easily modify documents by supplying the new data along with the reference to the document. For example, we want to add tags to each of our blog posts:
serverClient.Query(
Update(
Ref(Collection("posts"), "192903209792046592"),
Obj("data", Obj("tags", Arr("pet", "cute")))));
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686763243,
"data": {
"title": "My cat and other marvels",
"tags": [ "pet", "cute" ]
}
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{
"update": { "@ref": "classes/posts/192903209792046592" },
"params": {
"object": { "data": { "object": { "tags": [ "pet", "cute" ] } } }
}
}'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686763243,
"data": {
"title": "My cat and other marvels",
"tags": [ "pet", "cute" ]
}
}
}
result, _ := serverClient.Query(
f.Update(
f.RefCollection(f.Collection("posts"), "192903209792046592"),
f.Obj{"data": f.Obj{"tags": f.Arr{"pet", "cute"}}},
),
)
fmt.Println(result)
map[ref:{192903209792046592 0xc42036fc00 <nil>} ts:1520225686763243 data:map[title:My cat and other marvels tags:[pet cute]]]
System.out.println(
serverClient.query(
Update(
Ref(Collection("posts"),Value(192903209792046592L)),
Obj("data", Obj("tags", Arr(Value("pet"), Value("cute"))))))
.get());
{
ref: ref(id = "192903209792046592", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1527206084367212,
data: {
title: "My cat and other marvels",
tags: ["pet", "cute"]
}
}
serverClient.query(
q.Update(
q.Ref(q.Collection('posts'), '192903209792046592'),
{ data: { tags: ['pet', 'cute'] } },
)
)
.then((ret) => console.log(ret))
{ ref:
Ref(id=192903209792046592, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527350534706104,
data:
{ title: 'My cat and other marvels', tags: [ 'pet', 'cute' ] } }
serverClient.query(
q.update(
q.ref(q.collection("posts"), "192903209792046592"),
{"data": {"tags": ["pet", "cute"]}}
))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686763243,
"data": {
"title": "My cat and other marvels",
"tags": [ "pet", "cute" ]
}
}
serverClient.query(
Update(
Ref(Collection("posts"), "192903209792046592"),
Obj("data" -> Obj("tags" -> Arr("pet", "cute")))))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686763243,
"data": {
"title": "My cat and other marvels",
"tags": [ "pet", "cute" ]
}
}
serverClient.query(
Update(
ref: Ref(Collection("posts"), "192903209792046592"),
to: Obj("data" => Obj("tags" => Arr("pet", "cute")))
)
)
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686763243,
"data": {
"title": "My cat and other marvels",
"tags": [ "pet", "cute" ]
}
}
The Update
function updates specific fields in a document. It
preserves the old fields if they are not specified in params
. In the
case of nested values (known as objects, due to the JSON data format),
the old and the new values are merged. If null
is specified as a value
for a field, it is removed.
Replace posts
The Replace
function replaces the document’s data with the fields
provided in params
. Old fields not mentioned in params
are removed.
serverClient.Query(
Replace(
Ref(Collection("posts"), "192903209792046592"),
Obj("data", Obj("title", "My dog and other marvels"))));
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686781061,
"data": { "title": "My dog and other marvels" }
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{
"replace": { "@ref": "classes/posts/192903209792046592" },
"params": {
"object": {
"data": { "object": { "title": "My dog and other marvels" } }
}
}
}'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686781061,
"data": { "title": "My dog and other marvels" }
}
}
result, _ := serverClient.Query(
f.Replace(
f.RefCollection(f.Collection("posts"), "192903209792046592"),
f.Obj{"data": f.Obj{"title": "My dog and other marvels"}},
),
)
fmt.Println(result)
map[ref:{192903209792046592 0xc42035c0c0 <nil>} ts:1520225686781061 data:map[title:My dog and other marvels]]
System.out.println(
serverClient.query(
Replace(
Ref(Collection("posts"),Value(192903209792046592L)),
Obj("data", Obj("title", Value("My dog and other marvels")))))
.get());
{
ref: ref(id = "192903209792046592", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1527206084395803,
data: { title: "My dog and other marvels" }
}
serverClient.query(
q.Replace(
q.Ref(q.Collection('posts'), '192903209792046592'),
{ data: { title: 'My dog and other marvels' } },
)
)
.then((ret) => console.log(ret))
{ ref:
Ref(id=192903209792046592, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527350638301882,
data: { title: 'My dog and other marvels' } }
serverClient.query(
q.replace(
q.ref(q.collection("posts"), "192903209792046592"),
{"data": {"title": "My dog and other marvels"}}
))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686781061,
"data": { "title": "My dog and other marvels" }
}
serverClient.query(
Replace(
Ref(Collection("posts"), "192903209792046592"),
Obj("data" -> Obj("title" -> "My dog and other marvels"))))
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686781061,
"data": { "title": "My dog and other marvels" }
}
serverClient.query(
Replace(
ref: Ref(Collection("posts"), "192903209792046592"),
with: Obj(
"data" => Obj("title" => "My dog and other marvels")
)
)
)
{
"ref": { "@ref": "classes/posts/192903209792046592" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686781061,
"data": { "title": "My dog and other marvels" }
}
Note that the title
has been updated, but tags
has been deleted.
Delete a post
Lastly, a post can be removed using the Delete
function:
serverClient.Query(Delete(Ref(Collection("posts"), "192903209792045568")));
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{ "delete": { "@ref": "classes/posts/192903209792045568" } }'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
}
result, _ := serverClient.Query(
f.Delete(f.RefCollection(f.Collection("posts"), "192903209792045568")),
)
fmt.Println(result)
map[ref:{192903209792045568 0xc420341060 <nil>} ts:1520225686723969 data:map[title:Deep meanings in a latte]]
System.out.println(
serverClient.query(
Delete(Ref(Collection("posts"),Value(192903209792045568L))))
.get());
{
ref: ref(id = "192903209792045568", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1536604026800813,
data: {title: "Deep meanings in a latte"}
}
serverClient.query(
q.Delete(
q.Ref(q.Collection('posts'), '192903209792045568')
)
)
.then((ret) => console.log(ret))
{ ref:
Ref(id=192903209792045568, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1527349510087794,
data: { title: 'Deep meanings in a latte' } }
serverClient.query(q.delete(q.ref(q.collection("posts"), "192903209792045568")))
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
serverClient.query(Delete(Ref(Collection("posts"), "192903209792045568")))
{
"ref": { "@ref": "classes/posts/192903209792045568" },
"class": { "@ref": "classes/posts" },
"ts": 1520225686723969,
"data": { "title": "Deep meanings in a latte" }
}
serverClient.query(
Delete(ref: Ref(Collection("posts"), "192903209792045568"))
)
serverClient.Query(Get(Ref(Collection("posts"), "192903209792045568")));
{
"errors": [
{
"position": [ ],
"code": "instance not found",
"description": "Document not found."
}
]
}
curl https://db.fauna.com/ \
-u fnACrVRybLACAOytRXDMleFgdUZKXcJfMdzyjsRq: \
-d '{ "get": { "@ref": "classes/posts/192903209792045568" } }'
HTTP/1.1 404 Not Found
{
"errors": [
{
"position": [ ],
"code": "instance not found",
"description": "Document not found."
}
]
}
_, err := serverClient.Query(f.Get(f.RefCollection(f.Collection("posts"), "192903209792045568")))
fmt.Println(err)
Response error 404. Errors: [](document not found): Document not found.
System.out.println(
serverClient.query(Get(Ref(Collection("posts"),Value("192903209792045568"))))
.get());
ERROR java.util.concurrent.ExecutionException: com.faunadb.client.errors.NotFoundException: document not found: Document not found.
serverClient.query(
q.Get(q.Ref(q.Collection('posts'), '192903209792045568'))
)
.then((ret) => console.log(ret))
.catch((ret) => console.log(ret))
[NotFound: document not found]
serverClient.query(q.get(q.ref(q.collection("posts"), "192903209792045568")))
{
"errors": [
{
"position": [ ],
"code": "instance not found",
"description": "Document not found."
}
]
}
serverClient.query(Get(Ref(Collection("posts"), "192903209792045568")))
{
"errors": [
{
"position": [ ],
"code": "instance not found",
"description": "Document not found."
}
]
}
serverClient.query(Get(Ref(Collection("posts"), "192903209792045568")))
{
"errors": [
{
"position": [ ],
"code": "instance not found",
"description": "Document not found."
}
]
}
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!