CRUD Examples
This page is a follow-up to How to Create, Retrieve, Update, and Delete Objects in FaunaDB. Here you will find further examples of working with objects in FaunaDB including databases, keys, classes, user-defined functions, and indexes.
Database Examples
Reading or writing instances of databases requires an admin key.
Create a Database
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "create_database": { "object": { "name": "annuvin" } } }'
var adminClient = new FaunaClient(secret: adminKey);
adminClient.Query(CreateDatabase(Obj("name", "annuvin")));
FaunaClient adminClient = FaunaClient.builder()
.withSecret(adminKey)
.build();
System.out.println(
adminClient.query(CreateDatabase(Obj("name", Value("annuvin")))
).get());
adminClient := f.NewFaunaClient(adminKey)
result, _ := adminClient.Query(f.CreateDatabase(f.Obj{"name": "annuvin"}))
fmt.Println(result)
val adminClient = FaunaClient(secret = adminKey)
adminClient.query(CreateDatabase(Obj("name" -> "annuvin")))
adminClient = FaunaClient(secret=adminKey)
adminClient.query(q.create_database({"name": "annuvin"}))
$admin_client = Fauna::Client.new(secret: adminKey)
$admin_client.query do
create_database name: 'annuvin'
end
let adminClient = FaunaDB.Client(secret: adminKey)
adminClient.query(CreateDatabase(Obj("name" => "annuvin")))
var adminClient = new faunadb.Client({
secret: adminKey
});
adminClient.query(q.CreateDatabase({ name: "annuvin" }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
ref: ref(id = "annuvin", class = ref(id = "databases")),
ts: 1527628862798548,
name: "annuvin"
}
map[ref:{annuvin 0xc4200f6f60 <nil>} ts:1520223296616581 name:annuvin]
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{ ref: Ref(id=annuvin, class=Ref(id=databases)),
ts: 1520223296616581,
name: 'annuvin' }
Paginate All Databases
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "paginate": { "@ref": "databases" } }'
adminClient.Query(Paginate(Ref("databases")));
System.out.println(
adminClient.query(Paginate(Ref("databases")))
.get());
result, _ := adminClient.Query(f.Paginate(f.Ref("databases")))
fmt.Println(result)
adminClient.query(Paginate(Ref("databases")))
adminClient.query(q.paginate(Ref("databases")))
$admin_client.query do
paginate ref('databases')
end
adminClient.query(Paginate(Ref("databases")))
adminClient.query(q.Paginate(q.Databases(null))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
}
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
{
data: [
ref(id = "prydain", class = ref(id = "databases")),
ref(id = "caledonia", class = ref(id = "databases")),
ref(id = "annuvin", class = ref(id = "databases"))
]
}
map[data:[{prydain 0xc4200e1360 <nil>} {caledonia 0xc4200e14a0 <nil>} {annuvin 0xc4200e1600 <nil>}]]
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
{ data:
[ Ref(id=prydain, class=Ref(id=databases)),
Ref(id=caledonia, class=Ref(id=databases)),
Ref(id=annuvin, class=Ref(id=databases)) ] }
Get a Database
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "get": { "database": "annuvin" } }'
adminClient.Query(Get(Database("annuvin")));
System.out.println(
adminClient.query(Get(Database("annuvin")))
.get());
result, _ := adminClient.Query(f.Get(f.Database("annuvin")))
fmt.Println(result)
adminClient.query(Get(Database("annuvin")))
adminClient.query(q.get(q.database("annuvin")))
$admin_client.query do
get database('annuvin')
end
adminClient.query(Get(Database("annuvin")))
adminClient.query(q.Get(q.Database("annuvin")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
ref: ref(id = "annuvin", class = ref(id = "databases")),
ts: 1527869789382310,
name: "annuvin"
}
map[ref:{annuvin 0xc420118a80 <nil>} ts:1520223296616581 name:annuvin]
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
{ ref: Ref(id=annuvin, class=Ref(id=databases)),
ts: 1520223296616581,
name: 'annuvin' }
Rename a Database
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{
"update": { "database": "annuvin" },
"params": { "object": { "name": "llyr" } }
}'
adminClient.Query(
Update(Database("annuvin"), Obj("name", "llyr")));
System.out.println(
adminClient.query(
Update(Database(Value("annuvin")), Obj("name", Value("llyr"))))
.get());
result, _ := adminClient.Query(
f.Update(f.Database("annuvin"), f.Obj{"name": "llyr"}),
)
fmt.Println(result)
adminClient.query(
Update(Database("annuvin"), Obj("name" -> "llyr")))
adminClient.query(
q.update(q.database("annuvin"), {"name": "llyr"}))
$admin_client.query do
update database('annuvin'), name: 'llyr'
end
adminClient.query(
Update(
ref: Database("annuvin"),
to: Obj("name" => "llyr")
)
)
adminClient.query(q.Update(q.Database("annuvin"), { name: "llyr" }))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
{
ref: ref(id = "llyr", class = ref(id = "databases")),
ts: 1527628862910072,
name: "llyr"
}
map[ref:{llyr 0xc42014ee20 <nil>} ts:1520223296736896 name:llyr]
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
{ ref: Ref(id=llyr, class=Ref(id=databases)),
ts: 1520223296736896,
name: 'llyr' }
Annotate a Database
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{
"update": { "database": "llyr" },
"params": { "object": { "data": { "object": { "env": "test" } } } }
}'
adminClient.Query(
Update(Database("llyr"), Obj("data", Obj("env", "test"))));
System.out.println(
adminClient.query(
Update(
Database(Value("llyr")),
Obj("data", Obj("env", Value("test")))
)
)
.get());
result, _ := adminClient.Query(
f.Update(
f.Database("llyr"),
f.Obj{"data": f.Obj{"env": "test"}},
),
)
fmt.Println(result)
adminClient.query(
Update(Database("llyr"), Obj("data" -> Obj("env" -> "test"))))
adminClient.query(
q.update(q.database("llyr"), {"data": {"env": "test"}}))
$admin_client.query do
update database('llyr'), data: { env: 'test' }
end
adminClient.query(
Update(
ref: Database("llyr"),
to: Obj("data" => Obj("env" => "test"))
)
)
adminClient.query(
q.Update(q.Database("llyr"), { data: { env: "test" } }))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
ref: ref(id = "llyr", class = ref(id = "databases")),
ts: 1527628862969290,
name: "llyr",
data: {env: "test"}
}
map[ref:{llyr 0xc420141660 <nil>} ts:1520223296774224 name:llyr data:map[env:test]]
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{ ref: Ref(id=llyr, class=Ref(id=databases)),
ts: 1527631840251296,
name: 'llyr',
data: { env: 'test' } }
Delete a Database
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "delete": { "database": "llyr" } }'
adminClient.Query(Delete(Database("llyr")));
System.out.println(
adminClient.query(Delete(Database(Value("llyr"))))
.get());
result, _ := adminClient.Query(f.Delete(f.Database("llyr")))
fmt.Println(result)
adminClient.query(Delete(Database("llyr")))
adminClient.query(q.delete(q.database("llyr")))
$admin_client.query do
delete database('llyr')
end
adminClient.query(Delete(ref: Database("llyr")))
adminClient.query(q.Delete(q.Database("llyr")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
ref: ref(id = "llyr", class = ref(id = "databases")),
ts: 1527628862969290,
name: "llyr",
data: {env: "test"}
}
map[ref:{llyr 0xc420119300 <nil>} ts:1520223296774224 name:llyr data:map[env:test]]
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
{ ref: Ref(id=llyr, class=Ref(id=databases)),
ts: 1527631840251296,
name: 'llyr',
data: { env: 'test' } }
Key Examples
Reading or writing keys requires an admin key.
Create a Key
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{
"create_key": {
"object": {
"database": { "database": "caledonia" },
"role": "server"
}
}
}'
var adminClient = new FaunaClient(secret: adminKey);
adminClient.Query(
CreateKey(
Obj("database", Database("caledonia"), "role", "server")));
FaunaClient adminClient = FaunaClient.builder()
.withSecret(adminKey)
.build();
System.out.println(
adminClient.query(
CreateKey(
Obj(
"database", Database(Value("caledonia")),
"role", Value("server")
)
)
)
.get());
adminClient := f.NewFaunaClient(adminKey)
result, _ := adminClient.Query(
f.CreateKey(
f.Obj{"database": f.Database("caledonia"), "role": "server"},
),
)
fmt.Println(result)
val adminClient = FaunaClient(secret = adminKey)
adminClient.query(
CreateKey(
Obj("database" -> Database("caledonia"), "role" -> "server")))
adminClient = FaunaClient(secret=adminKey)
adminClient.query(
q.create_key(
{"database": q.database("caledonia"), "role": "server"}
))
$admin_client = Fauna::Client.new(secret: adminKey)
$admin_client.query do
create_key database: database('caledonia'), role: 'server'
end
let adminClient = FaunaDB.Client(secret: adminKey)
adminClient.query(
CreateKey(
Obj(
"database" => Database("caledonia"),
"role" => "server"
)
)
)
var adminClient = new faunadb.Client({
secret: adminKey
});
adminClient.query(
q.CreateKey(
{ database: q.Database("caledonia"), role: "server" }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
ref: ref(id = "200673528521949696", class = ref(id = "keys")),
ts: 1527636040123044,
database: ref(id = "caledonia", class = ref(id = "databases")),
role: "server",
secret: "fnAC6lgM9NACAOCQemdRxGyeMykaG3ZhxIBGtv3l",
hashed_secret: "$2a$05$mVYDsiIfEBfXgZYqZvocSuy4J45wqwyY8Dh.V4eRasETNEV01ovJS"
}
map[
ref:{192900703888343552 0xc4200e1c80 <nil>}
ts:1520223296908587
database:{caledonia 0xc4200e1e20 <nil>}
role:server
secret:fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j
hashed_secret:$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui
]
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{ ref: Ref(id=200669511526908416, class=Ref(id=keys)),
ts: 1527632209151020,
database: Ref(id=caledonia, class=Ref(id=databases)),
role: 'server',
secret: 'fnACyOvbh9ACAOtRQDQSefokn4iuMf9zn_FakPAx',
hashed_secret: '$2a$05$dszFRic/tkKzsG0rrUj3Fu7.nSoYuJL7BdnHXepx4XTzfcWtVEZRC' }
Paginate All Keys
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "paginate": { "@ref": "keys" } }'
adminClient.Query(Paginate(Ref("keys")));
System.out.println(adminClient.query(Paginate((Ref("keys")))).get());
result, _ := adminClient.Query(f.Paginate(f.Keys()))
fmt.Println(result)
adminClient.query(Paginate(Ref("keys")))
adminClient.query(q.paginate(Ref("keys")))
$admin_client.query do
paginate ref('keys')
end
adminClient.query(Paginate(Ref("keys")))
adminClient.query(q.Paginate(q.Keys(null)))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
}
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
{
data: [
ref(id = "200647741322297856", class = ref(id = "keys")),
ref(id = "200673528521949696", class = ref(id = "keys"))
]
}
map[data:[{192900703858983424 0xc4201beb80 <nil>} {192900703888343552 0xc4201becc0 <nil>}]]
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
{ data:
[ Ref(id=192900703858983424, class=Ref(id=keys)),
Ref(id=192900703888343552, class=Ref(id=keys)) ] }
Get a Key
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "get": { "@ref": "keys/192900703888343552" } }'
adminClient.Query(Get(Ref("keys/192900703888343552")));
System.out.println(
adminClient.query(
Get(Ref(keys(),"192900703888343552))
).get());
result, _ := adminClient.Query(f.Get(f.RefClass(f.Keys(), "192900703888343552")))
fmt.Println(result)
adminClient.query(Get(Ref("keys/192900703888343552")))
adminClient.query(q.get(Ref("keys/192900703888343552")))
$admin_client.query do
get ref('keys/192900703888343552')
end
adminClient.query(Get(Ref("keys/192900703888343552")))
adminClient.query(q.Get(q.Ref(q.Keys(null), "192900703888343552")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
ref: ref(id = "192900703888343552", class = ref(id = "keys")),
ts: 1527636039961513,
database: ref(id = "caledonia", class = ref(id = "databases")),
role: "server",
hashed_secret: "$2a$05$mVYDsiIfEBfXgZYqZvocSuy4J45wqwyY8Dh.V4eRasETNEV01ovJS"
}
map[
ref:{192900703888343552 0xc420119560 <nil>}
ts:1520223296908587
database:{caledonia 0xc420119700 <nil>}
role:server
hashed_secret:$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui
]
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
{ ref: Ref(id=192900703888343552, class=Ref(id=keys)),
ts: 1527632209151020,
database: Ref(id=caledonia, class=Ref(id=databases)),
role: 'server',
hashed_secret: '$2a$05$dszFRic/tkKzsG0rrUj3Fu7.nSoYuJL7BdnHXepx4XTzfcWtVEZRC' }
Delete a Key
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "delete": { "@ref": "keys/192900703858983424" } }'
adminClient.Query(Delete(Ref("keys/192900703858983424")));
System.out.println(
adminClient.query(
Delete((keys(),"192900703858983424"))
).get());
result, _ := adminClient.Query(f.Delete(f.RefClass(f.Keys(), "192900703858983424")))
fmt.Println(result)
adminClient.query(Delete(Ref("keys/192900703858983424")))
adminClient.query(q.delete(Ref("keys/192900703858983424")))
$admin_client.query do
delete ref('keys/192900703858983424')
end
adminClient.query(
Delete(ref: Ref("keys/192900703858983424"))
)
adminClient.query(q.Delete(q.Ref(q.Keys(null), "192900703858983424")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
}
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
{
ref: ref(id = "192900703858983424", class = ref(id = "keys")),
ts: 1527636039961513,
database: ref(id = "caledonia", class = ref(id = "databases")),
role: "server",
hashed_secret: "$2a$05$mVYDsiIfEBfXgZYqZvocSuy4J45wqwyY8Dh.V4eRasETNEV01ovJS"
}
map[
ref:{192900703858983424 0xc42010dd00 <nil>}
ts:1520223296878348
database:{caledonia 0xc42010dea0 <nil>}
role:server
hashed_secret:$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy
]
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
{ ref: Ref(id=192900703858983424, class=Ref(id=keys)),
ts: 1527632209151020,
database: Ref(id=caledonia, class=Ref(id=databases)),
role: 'server',
hashed_secret: '$2a$05$dszFRic/tkKzsG0rrUj3Fu7.nSoYuJL7BdnHXepx4XTzfcWtVEZRC' }
Class Examples
Reading or writing classes requires a server key.
Create a Class
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "create_class": { "object": { "name": "spells" } } }'
client.Query(CreateClass(Obj("name", "spells")));
System.out.println(
client.query(CreateClass(Obj("name", Value("spells"))))
.get());
result, _ := client.Query(f.CreateClass(f.Obj{"name": "spells"}))
fmt.Println(result)
client.query(CreateClass(Obj("name" -> "spells")))
client.query(q.create_class({"name": "spells"}))
$client.query do
create_class name: 'spells'
end
client.query(CreateClass(Obj("name" => "spells")))
client.query(q.CreateClass({ name: "spells" }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
ref: ref(id = "spells", class = ref(id = "classes")),
ts: 1536604018339083,
history_days: 30,
name: "spells"
}
map[ref:{spells 0xc42010d9a0 <nil>} ts:1520223297210738 history_days:30 name:spells]
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{ ref: Ref(id=spells, class=Ref(id=classes))
ts: 1520223297210738,
history_days: 30,
name: 'spells' }
Paginate All Classes
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "paginate": { "@ref": "classes" } }'
client.Query(Paginate(Ref("classes")));
System.out.println(client.query(Paginate(Ref("classes"))).get());
result, _ := client.Query(f.Paginate(f.Classes()))
fmt.Println(result)
client.query(Paginate(Ref("classes")))
client.query(q.paginate(Ref("classes")))
$client.query do
paginate ref('classes')
end
client.query(Paginate(Ref("classes")))
client.query(q.Paginate(q.Classes(null)))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
}
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
{ data: [
ref(id = "decrepit_huts", class = ref(id = "classes")),
ref(id = "spellbooks", class = ref(id = "classes")),
ref(id = "characters", class = ref(id = "classes")),
ref(id = "spells", class = ref(id = "classes"))
]}
map[data:[
{decrepit_huts 0xc420118200 <nil>}
{spellbooks 0xc420118340 <nil>}
{characters 0xc4201184a0 <nil>}
{spells 0xc4201185e0 <nil>}
]]
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
{ data:
[ Ref(id=decrepit_huts, class=Ref(id=classes)),
Ref(id=spellbooks, class=Ref(id=classes)),
Ref(id=characters, class=Ref(id=classes)),
Ref(id=spells, class=Ref(id=classes)) ] }
Get a Class
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "get": { "class": "spells" } }'
client.Query(Get(Class("spells")));
System.out.println(
client.query(Get(Class(Value("spells"))))
.get());
result, _ := client.Query(f.Get(f.Class("spells")))
fmt.Println(result)
client.query(Get(Class("spells")))
client.query(q.get(q.class_expr("spells")))
$client.query do
get class_('spells')
end
client.query(Get(Class("spells")))
client.query(q.Get(q.Class("spells")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
ref: ref(id = "spells", class = ref(id = "classes")),
ts: 1536271845615375,
history_days: 30,
name: "spells"
}
map[ref:{spells 0xc4201e8080 <nil>} ts:1520223297210738 history_days:30 name:spells]
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
{ ref: Ref(id=spells, class=Ref(id=classes))
ts: 1520223297210738,
history_days: 30,
name: 'spells' }
Rename a Class
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"update": { "class": "decrepit_huts" },
"params": { "object": { "name": "dilapidated_huts" } }
}'
client.Query(
Update(
Class("decrepit_huts"),
Obj("name", "dilapidated_huts")));
System.out.println(
client.query(
Update(
Class(Value("decrepit_huts")),
Obj("name", Value("dilapidated_huts"))
)
).get());
result, _ := client.Query(
f.Update(
f.Class("decrepit_huts"),
f.Obj{"name": "dilapidated_huts"},
),
)
fmt.Println(result)
client.query(
Update(
Class("decrepit_huts"),
Obj("name" -> "dilapidated_huts")))
client.query(
q.update(
q.class_expr("decrepit_huts"),
{"name": "dilapidated_huts"}
))
$client.query do
update class_('decrepit_huts'), name: 'dilapidated_huts'
end
client.query(
Update(
ref: Class("decrepit_huts"),
to: Obj("name" => "dilapidated_huts")
)
)
client.query(
q.Update(
q.Class("decrepit_huts"), { name: "dilapidated_huts" }))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
ref: ref(id = "dilapidated_huts", class = ref(id = "classes")),
ts: 1527714658200596,
history_days: 30,
name: "dilapidated_huts"
}
map[ref:{dilapidated_huts 0xc4201d43e0 <nil>} ts:1520223297276685 history_days:30 name:dilapidated_huts]
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{ ref: Ref(id=dilapidated_huts, class=Ref(id=classes)),
ts: 1520223297276685,
history_days: 30,
name: 'dilapidated_huts' }
Delete a Class
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "delete": { "class": "dilapidated_huts" } }'
client.Query(Delete(Class("dilapidated_huts")));
System.out.println(
client.query(Delete(Class(Value("dilapidated_huts"))))
.get());
result, _ := client.Query(f.Delete(f.Class("dilapidated_huts")))
fmt.Println(result)
client.query(Delete(Class("dilapidated_huts")))
client.query(q.delete(q.class_expr("dilapidated_huts")))
$client.query do
delete class_('dilapidated_huts')
end
client.query(Delete(ref: Class("dilapidated_huts")))
client.query(q.Delete(q.Class("dilapidated_huts")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
ref: ref(id = "magical_creatures", class = ref(id = "classes")),
ts: 1536603582490368,
history_days: 30,
name: "dilapidated_huts"
}
map[ref:{dilapidated_huts 0xc4201d6940 <nil>} ts:1520223297276685 history_days:30 name:dilapidated_huts]
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
{ ref: Ref(id=dilapidated_huts, class=Ref(id=classes)),
ts: 1520223297276685,
history_days: 30,
name: 'dilapidated_huts' }
Create an Instance in a Class
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create": { "class": "spells" },
"params": {
"object": {
"data": {
"object": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
}
}
}'
client.Query(
Create(
Class("spells"),
Obj(
"data", Obj("name", "Fire Beak", "element", Arr("air", "fire"))
)));
System.out.println(
client.query(
Create(
Class(Value("spells")),
Obj( "data",
Obj(
"name", Value("Fire Beak"),
"element", Arr(Value("air"), Value("fire"))
)
)
)
).get());
result, _ := client.Query(
f.Create(
f.Class("spells"),
f.Obj{
"data": f.Obj{"name": "Fire Beak", "element": f.Arr{"air", "fire"}},
},
),
)
fmt.Println(result)
client.query(
Create(
Class("spells"),
Obj(
"data" -> Obj("name" -> "Fire Beak", "element" -> Arr("air", "fire"))
)))
client.query(
q.create(
q.class_expr("spells"),
{"data": {"name": "Fire Beak", "element": ["air", "fire"]}}
))
$client.query do
create class_('spells'),
data: { name: 'Fire Beak', element: ['air', 'fire'] }
end
client.query(
Create(
at: Class("spells"),
Obj(
"data" => Obj(
"name" => "Fire Beak",
"element" => Arr("air", "fire")
)
)
)
)
client.query(
q.Create(
q.Class("spells"), { data: { name: "Fire Beak", element: ["air", "fire"] } }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
}
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
{
ref: ref(id = "181388642046968320", class = ref(id = "spells", class = ref(id = "classes"))),
ts: 1536604015999279,
data: {name: "Fire Beak", element: ["air", "fire"]}
}
map[
ref:{192900707573039616 0xc4201e6600 <nil>}
ts:1520223300419731
data:map[name:Fire Beak element:[air fire]]
]
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
{ ref: Ref(id=200669801878651393, class=Ref(id=spells, class=Ref(id=classes))),
ts: 1527632486061801,
data: { name: 'Fire Beak', element: [ 'air', 'fire' ] } }
User-Defined Function Examples
Creating and updating a function requires a server key.
Create a User-Defined Function
Let’s create a function to allow our authors to create new blog posts. We’ll set it up to allow the author to provide a title and body. This way authors are constrained by what data they can store on a post instance.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_function": {
"object": {
"name": "create_entry",
"body": {
"query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "class": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "object": { "call": "public" } },
"role": "server"
}
}
}'
// Not available in this language yet.
System.out.println(
client.query(
CreateFunction(
Obj(
"name", Value("create_entry"),
"body" , Query(
Lambda(
Arr(Value("title"),Value("body")),
Create(
Class("posts"),
Obj( "data", Obj("title", Var("title"),"body", Var("body")))
)
)
),
"permissions", Obj("call", Value("public")),
"role", Value("server")
)
)
).get());
result, _ := client.Query(f.CreateFunction(f.Obj{
"name": "create_entry",
"permissions": f.Obj{"call": "public"},
"role": "server",
"body": f.Query(f.Lambda(
f.Arr{"title", "body"},
f.Create(f.Class("posts"), f.Obj{
"data": f.Obj{
"title": f.Var("title"),
"body": f.Var("body"),
},
}),
)),
}))
fmt.Println(result)
// Not available in this language yet.
# Not available in this language yet.
# Not available in this language yet.
// Not available in this language yet.
client.query(
q.CreateFunction({
name: "create_entry",
body: q.Query(
q.Lambda(
["title", "body"],
q.Create(q.Class("posts"), { data: { title: q.Var("title"), body: q.Var("body") }}))),
permissions: { call: "public" },
role: "server"
}))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "functions/create_entry" },
"class": { "@ref": "functions" },
"ts": 1520223330303720,
"name": "create_entry",
"body": {
"@query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "class": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "call": "public" },
"role": "server"
}
}
{
"ref": { "@ref": "functions/create_entry" },
"class": { "@ref": "functions" },
"ts": 1520223330303720,
"name": "create_entry",
"body": {
"@query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "class": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "call": "public" },
"role": "server"
}
{
ref: ref(id = "create_entry", class = ref(id = "functions")),
ts: 1529338141262176,
name: "create_entry",
body: QueryV({
lambda=[title, body],
expr={create={class=posts}, params={object={data={object={title={var=title}, body={var=body}}}}}}
}),
permissions: {call: "public"},
role: "server"
}
map[
ref:{create_entry 0xc4201bb900 <nil>}
ts:1520223330303720
name:create_entry
body:{[123 34 108 97 109 98 100 97 34 58 91 34 116 105 116 108 101 34 44 34 98 111 100 121 34 93 44 34 101 120 112 114 34 58 123 34 99 114 101 97 116 101 34 58 123 34 99 108 97 115 115 34 58 34 112 111 115 116 115 34 125 44 34 112 97 114 97 109 115 34 58 123 34 111 98 106 101 99 116 34 58 123 34 100 97 116 97 34 58 123 34 111 98 106 101 99 116 34 58 123 34 98 111 100 121 34 58 123 34 118 97 114 34 58 34 98 111 100 121 34 125 44 34 116 105 116 108 101 34 58 123 34 118 97 114 34 58 34 116 105 116 108 101 34 125 125 125 125 125 125 125]}
permissions:map[call:public]
role:server
]
{
"ref": { "@ref": "functions/create_entry" },
"class": { "@ref": "functions" },
"ts": 1520223330303720,
"name": "create_entry",
"body": {
"@query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "class": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "call": "public" },
"role": "server"
}
{
"ref": { "@ref": "functions/create_entry" },
"class": { "@ref": "functions" },
"ts": 1520223330303720,
"name": "create_entry",
"body": {
"@query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "class": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "call": "public" },
"role": "server"
}
{
"ref": { "@ref": "functions/create_entry" },
"class": { "@ref": "functions" },
"ts": 1520223330303720,
"name": "create_entry",
"body": {
"@query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "class": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "call": "public" },
"role": "server"
}
{
"ref": { "@ref": "functions/create_entry" },
"class": { "@ref": "functions" },
"ts": 1520223330303720,
"name": "create_entry",
"body": {
"@query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "class": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "call": "public" },
"role": "server"
}
{ ref: Ref(id=create_entry, class=Ref(id=functions)),
ts: 1527633126019327,
name: 'create_entry',
body: Query("[object Object]"),
permissions: { call: 'public' },
role: 'server' }
Create and Login Credentials to Call a User-Defined Function
Now we can create a new author and use their credentials.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create": { "class": "authors" },
"params": {
"object": {
"data": { "object": { "name": "Patty Smith" } },
"credentials": { "object": { "password": "123456" } }
}
}
}'
// Not available in this language yet.
System.out.println(
client.query(
Create(
Class(Value("authors")),
Obj(
"data", Obj("name", Value("Patty Smith")),
"credientials", Obj("password", Value("123456"))
)
)
).get());
result, _ := client.Query(f.Create(
f.Class("authors"),
f.Obj{
"data": f.Obj{"name": "Patty Smith"},
"credentials": f.Obj{"password": "123456"},
},
))
fmt.Println(result)
// Not available in this language yet.
# Not available in this language yet.
# Not available in this language yet.
// Not available in this language yet.
client.query(q.Create(q.Class("authors"), {
data: { name: "Patty Smith" },
credentials: { password: "123456" }
}))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
}
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
{
ref: ref(id = "201420401501274624", class = ref(id = "authors", class = ref(id = "classes"))),
ts: 1528348313700278,
data: {
name: "Patty Smith"
}
}
map[ref:{192900738957967872 0xc4201ab040 <nil>} ts:1520223330352930 data:map[name:Patty Smith]]
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
{ ref: Ref(id=192900738957967872, class=Ref(id=authors, class=Ref(id=classes))),
ts: 1527633688652829,
data: { name: 'Patty Smith' } }
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"login": { "@ref": "classes/authors/192900738957967872" },
"params": { "object": { "password": "123456" } }
}'
// Not available in this language yet.
System.out.println(
client.query(
Login(
RefV(id = "202129854791942656", class = RefV(id = "authors", class = RefV(id = "classes"))),
Obj("password", Value("123456"))
)
).get());
result, _ := client.Query(f.Login(
f.RefClass(f.Class("authors"), "192900738957967872"),
f.Obj{"password": "123456"},
))
fmt.Println(result)
// Not available in this language yet.
# Not available in this language yet.
# Not available in this language yet.
// Not available in this language yet.
client.query(q.Login(
q.Ref(q.Class("authors"), "192900738957967872"),
{ password: "123456" }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
}
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
{
ref: ref(id = "202129854841225728", class = ref(id = "tokens")),
ts: 1529024901104219,
instance: ref(id = "202129854791942656", class = ref(id = "authors", class = ref(id = "classes"))),
secret: "fnECzhwINFACAAAAAAAAAAAARysJFAyxCh8fdSPiclj-lBeC-DQ"
}
map[
ref:{192900739000959488 0xc4201fe100 <nil>}
ts:1520223330375483
instance:{192900738957967872 0xc4201fe380 <nil>}
secret:fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg
]
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
{ ref: Ref(id=192900739000959488, class=Ref(id=tokens)),
ts: 1520223330375483,
instance: Ref(id=192900738957967872, class=Ref(id=authors, class=Ref(id=classes))),
secret: 'fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg' }
After the author has logged in, they can use their secret to call the
create_entry
function that we created.
curl https://db.fauna.com/ \
-u fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg: \
-d '{
"call": { "function": "create_entry" },
"arguments": [ "First Post Title", "This is my first blog post!" ]
}'
// Not available in this language yet.
System.out.println(
client.query(
Call(
Function("create_entry"),
Arr(Value("First Post Title"),Value("this is my first blog post!"))
)
).get());
result, _ := client.Query(f.Call(
f.Function("create_entry"),
f.Arr{"First Post Title", "This is my first blog post!"},
))
fmt.Println(result)
// Not available in this language yet.
# Not available in this language yet.
# Not available in this language yet.
// Not available in this language yet.
client.query(
q.Call(q.Function("create_entry"),
[ "First Post Title", "This is my first blog post!" ]))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
}
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
{
ref: ref(id = "201474441226486272", class = ref(id = "posts", class = ref(id = "classes"))),
ts: 1528399850000762,
data: [
title: "First Post Title",
body: "this is my first blog post!"
]
}
map[ref:{192900739108962816 0xc4201d3720 <nil>} ts:1520223330453871 data:map[title:First Post Title body:This is my first blog post!]]
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
{ ref: Ref(id=192900739108962816, class=Ref(id=posts, class=Ref(id=classes))),
ts: 1520223330453871,
data:
{ title: 'First Post Title',
body: 'This is my first blog post!' } }
Index Examples
Creating and updating an index requires a server key.
Create an Index
Let’s create an index to list all spells' names:
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "all_spell_names",
"source": { "class": "spells" },
"values": [ { "object": { "field": [ "data", "name" ] } } ]
}
}
}'
client.Query(
CreateIndex(
Obj(
"name", "all_spell_names",
"source", Class("spells"),
"values", Arr(Obj("field", Arr("data", "name")))
)));
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("all_spell_names"),
"source", Class(Value("spells")),
"values", Arr(Obj("field", Arr(Value("data"), Value("name"))))
)))
.get());
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "all_spell_names",
"source": f.Class("spells"),
"values": f.Arr{f.Obj{"field": f.Arr{"data", "name"}}},
},
),
)
fmt.Println(result)
client.query(
CreateIndex(
Obj(
"name" -> "all_spell_names",
"source" -> Class("spells"),
"values" -> Arr(Obj("field" -> Arr("data", "name")))
)))
client.query(
q.create_index(
{
"name": "all_spell_names",
"source": q.class_expr("spells"),
"values": [{"field": ["data", "name"]}]
}
))
$client.query do
create_index name: 'all_spell_names',
source: class_('spells'),
values: [{ field: ['data', 'name'] }]
end
client.query(
CreateIndex(
Obj(
"name" => "all_spell_names",
"source" => Class("spells"),
"values" => Arr(Obj("field" => Arr("data", "name")))
)
)
)
client.query(
q.CreateIndex(
{ name: "all_spell_names",
source: q.Class("spells"),
values: [{ field: ["data", "name"] }] }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "indexes/all_spell_names" },
"class": { "@ref": "indexes" },
"ts": 1520223300947688,
"active": false,
"partitions": 8,
"name": "all_spell_names",
"source": { "@ref": "classes/spells" },
"values": [ { "field": [ "data", "name" ] } ]
}
}
{
"ref": { "@ref": "indexes/all_spell_names" },
"class": { "@ref": "indexes" },
"ts": 1520223300947688,
"active": false,
"partitions": 8,
"name": "all_spell_names",
"source": { "@ref": "classes/spells" },
"values": [ { "field": [ "data", "name" ] } ]
}
{
ref: ref(id = "all_spell_names", class = ref(id = "indexes")),
ts: 1536257513918009,
active: false,
partitions: 8,
name: "all_spell_names",
source: ref(id = "spells", class = ref(id = "classes")),
values: [{field: ["data", "name"]}]
}
map[ref:{all_spell_names 0xc420365a00 <nil>} ts:1520223300947688 active:false partitions:8 name:all_spell_names source:{spells 0xc420365c60 <nil>} values:[map[field:[data name]]]]
{
"ref": { "@ref": "indexes/all_spell_names" },
"class": { "@ref": "indexes" },
"ts": 1520223300947688,
"active": false,
"partitions": 8,
"name": "all_spell_names",
"source": { "@ref": "classes/spells" },
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/all_spell_names" },
"class": { "@ref": "indexes" },
"ts": 1520223300947688,
"active": false,
"partitions": 8,
"name": "all_spell_names",
"source": { "@ref": "classes/spells" },
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/all_spell_names" },
"class": { "@ref": "indexes" },
"ts": 1520223300947688,
"active": false,
"partitions": 8,
"name": "all_spell_names",
"source": { "@ref": "classes/spells" },
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/all_spell_names" },
"class": { "@ref": "indexes" },
"ts": 1520223300947688,
"active": false,
"partitions": 8,
"name": "all_spell_names",
"source": { "@ref": "classes/spells" },
"values": [ { "field": [ "data", "name" ] } ]
}
{ ref: Ref(id=all_spell_names, class=Ref(id=indexes)),
ts: 1527625252080602,
active: false,
partitions: 8,
name: 'all_spell_names',
source: Ref(id=spells, class=Ref(id=classes)),
values: [ { field: [Array] } ] }
It’s also possible to specify the terms that are going to be used to locate entries on the index. In this case, we’ll create an index that allow us to find spells by their elements:
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "spells_by_element_with_name",
"source": { "class": "spells" },
"terms": [ { "object": { "field": [ "data", "element" ] } } ],
"values": [ { "object": { "field": [ "data", "name" ] } } ]
}
}
}'
client.Query(
CreateIndex(
Obj(
"name", "spells_by_element_with_name",
"source", Class("spells"),
"terms", Arr(Obj("field", Arr("data", "element"))),
"values", Arr(Obj("field", Arr("data", "name")))
)));
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("spells_by_element_with_name"),
"source", Class(Value("spells")),
"terms", Arr(Obj("field", Arr(Value("data"), Value("element")))),
"values", Arr(Obj("field", Arr(Value("data"), Value("name"))))
)))
.get());
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "spells_by_element_with_name",
"source": f.Class("spells"),
"terms": f.Arr{f.Obj{"field": f.Arr{"data", "element"}}},
"values": f.Arr{f.Obj{"field": f.Arr{"data", "name"}}},
},
),
)
fmt.Println(result)
client.query(
CreateIndex(
Obj(
"name" -> "spells_by_element_with_name",
"source" -> Class("spells"),
"terms" -> Arr(Obj("field" -> Arr("data", "element"))),
"values" -> Arr(Obj("field" -> Arr("data", "name")))
)))
client.query(
q.create_index(
{
"name": "spells_by_element_with_name",
"source": q.class_expr("spells"),
"terms": [{"field": ["data", "element"]}],
"values": [{"field": ["data", "name"]}]
}
))
$client.query do
create_index name: 'spells_by_element_with_name',
source: class_('spells'),
terms: [{ field: ['data', 'element'] }],
values: [{ field: ['data', 'name'] }]
end
client.query(
CreateIndex(
Obj(
"name" => "spells_by_element_with_name",
"source" => Class("spells"),
"terms" => Arr(Obj("field" => Arr("data", "element"))),
"values" => Arr(Obj("field" => Arr("data", "name")))
)
)
)
client.query(
q.CreateIndex(
{ name: "spells_by_element_with_name",
source: q.Class("spells"),
terms: [{ field: ["data", "element"] }],
values: [{ field: ["data", "name"] }] }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223308546298,
"active": false,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223308546298,
"active": false,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
ref: ref(id = "spells_by_element_with_name", class = ref(id = "indexes")),
ts: 1527714659590871,
active: false,
partitions: 1,
name: "spells_by_element_with_name",
source: ref(id = "spells", class = ref(id = "classes")),
terms: [ {field: ["data", "element"]} ],
values: [ {field: ["data", "name"]} ]
}
map[
ref:{spells_by_element_with_name 0xc420396b40 <nil>}
ts:1520223308546298
active:false
partitions:1
name:spells_by_element_with_name
source:{spells 0xc420396da0 <nil>}
terms:[map[field:[data element]]]
values:[map[field:[data name]]]
]
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223308546298,
"active": false,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223308546298,
"active": false,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223308546298,
"active": false,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223308546298,
"active": false,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{ ref: Ref(id=spells_by_element_with_name, class=Ref(id=indexes)),
ts: 1527625636424120,
active: false,
partitions: 1,
name: 'spells_by_element_with_name',
source: Ref(id=spells, class=Ref(id=classes)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] } ] }
Indexes can also cover multiple values, which affect the order of their results. See below section on ordering for more information.
Here is an example of an index that allows you to search for a spell by
its element and get back both the spell name and ref
:
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "spells_with_ref_by_element_name",
"source": { "class": "spells" },
"terms": [ { "object": { "field": [ "data", "element" ] } } ],
"values": [
{ "object": { "field": [ "data", "name" ] } },
{ "object": { "field": [ "ref" ] } }
]
}
}
}'
client.Query(
CreateIndex(
Obj(
"name", "spells_with_ref_by_element_name",
"source", Class("spells"),
"terms", Arr(Obj("field", Arr("data", "element"))),
"values", Arr(
Obj("field", Arr("data", "name")),
Obj("field", Arr("ref"))
)
)));
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("spells_with_ref_by_element_name"),
"source", Class(Value("spells")),
"terms", Arr(Obj("field", Arr(Value("data"), Value("element")))),
"values", Arr(
Obj("field", Arr(Value("data"), Value("name"))),
Obj("field", Arr(Value("ref")))
)
)))
.get());
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "spells_with_ref_by_element_name",
"source": f.Class("spells"),
"terms": f.Arr{f.Obj{"field": f.Arr{"data", "element"}}},
"values": f.Arr{
f.Obj{"field": f.Arr{"data", "name"}},
f.Obj{"field": f.Arr{"ref"}},
},
},
),
)
fmt.Println(result)
client.query(
CreateIndex(
Obj(
"name" -> "spells_with_ref_by_element_name",
"source" -> Class("spells"),
"terms" -> Arr(Obj("field" -> Arr("data", "element"))),
"values" -> Arr(
Obj("field" -> Arr("data", "name")),
Obj("field" -> Arr("ref"))
)
)))
client.query(
q.create_index(
{
"name": "spells_with_ref_by_element_name",
"source": q.class_expr("spells"),
"terms": [{"field": ["data", "element"]}],
"values": [{"field": ["data", "name"]}, {"field": ["ref"]}]
}
))
$client.query do
create_index name: 'spells_with_ref_by_element_name',
source: class_('spells'),
terms: [{ field: ['data', 'element'] }],
values: [{ field: ['data', 'name'] }, { field: ['ref'] }]
end
client.query(
CreateIndex(
Obj(
"name" => "spells_with_ref_by_element_name",
"source" => Class("spells"),
"terms" => Arr(Obj("field" => Arr("data", "element"))),
"values" => Arr(
Obj("field" => Arr("data", "name")),
Obj("field" => Arr("ref"))
)
)
)
)
client.query(
q.CreateIndex(
{ name: "spells_with_ref_by_element_name",
source: q.Class("spells"),
terms: [{ field: ["data", "element"] }],
values: [{ field: ["data", "name"] }, { field: ["ref"] }] }))
.then((ret) => console.log(ret))
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "indexes/spells_with_ref_by_element_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309117205,
"active": false,
"partitions": 1,
"name": "spells_with_ref_by_element_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] }, { "field": [ "ref" ] } ]
}
}
{
"ref": { "@ref": "indexes/spells_with_ref_by_element_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309117205,
"active": false,
"partitions": 1,
"name": "spells_with_ref_by_element_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] }, { "field": [ "ref" ] } ]
}
{
ref: ref(id = "spells_with_ref_by_element_name", class = ref(id = "indexes")),
ts: 1527714659603218,
active: false,
partitions: 1,
name: "spells_with_ref_by_element_name",
source: ref(id = "spells", class = ref(id = "classes")),
terms: [{field: ["data", "element"]}],
values: [
{field: ["data", "name"]},
{field: ["ref"]}
]
}
map[
ref:{spells_with_ref_by_element_name 0xc4203d2260 <nil>}
ts:1520223309117205
active:false
partitions:1
name:spells_with_ref_by_element_name
source:{spells 0xc4203d24c0 <nil>}
terms:[map[field:[data element]]]
values:[map[field:[data name]] map[field:[ref]]]
]
{
"ref": { "@ref": "indexes/spells_with_ref_by_element_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309117205,
"active": false,
"partitions": 1,
"name": "spells_with_ref_by_element_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] }, { "field": [ "ref" ] } ]
}
{
"ref": { "@ref": "indexes/spells_with_ref_by_element_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309117205,
"active": false,
"partitions": 1,
"name": "spells_with_ref_by_element_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] }, { "field": [ "ref" ] } ]
}
{
"ref": { "@ref": "indexes/spells_with_ref_by_element_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309117205,
"active": false,
"partitions": 1,
"name": "spells_with_ref_by_element_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] }, { "field": [ "ref" ] } ]
}
{
"ref": { "@ref": "indexes/spells_with_ref_by_element_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309117205,
"active": false,
"partitions": 1,
"name": "spells_with_ref_by_element_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] }, { "field": [ "ref" ] } ]
}
{ ref: Ref(id=spells_with_ref_by_element_name, class=Ref(id=indexes)),
ts: 1527625316880457,
active: false,
partitions: 1,
name: 'spells_with_ref_by_element_name',
source: Ref(id=spells, class=Ref(id=classes)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] }, { field: [Array] } ] }
Paginate All Indexes
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "paginate": { "@ref": "indexes" } }'
client.Query(Paginate(Ref("indexes")));
System.out.println(
client.query(Paginate(Ref("indexes")))
.get());
result, _ := client.Query(f.Paginate(f.Ref("indexes")))
fmt.Println(result)
client.query(Paginate(Ref("indexes")))
client.query(q.paginate(Ref("indexes")))
$client.query do
paginate ref('indexes')
end
client.query(Paginate(Ref("indexes")))
client.query(q.Paginate(q.Indexes(null)))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [
{ "@ref": "indexes/all_spells" },
{ "@ref": "indexes/spells_by_name" },
{ "@ref": "indexes/spells_by_element" },
{ "@ref": "indexes/spells_by_element_and_name" },
{ "@ref": "indexes/spellbooks_by_owner" },
{ "@ref": "indexes/spells_by_spellbook" },
{ "@ref": "indexes/all_spell_names" },
{ "@ref": "indexes/spells_by_element_with_name" },
{ "@ref": "indexes/spells_with_ref_by_element_name" },
{ "@ref": "indexes/latest_spells_by_element" }
]
}
}
{
"data": [
{ "@ref": "indexes/all_spells" },
{ "@ref": "indexes/spells_by_name" },
{ "@ref": "indexes/spells_by_element" },
{ "@ref": "indexes/spells_by_element_and_name" },
{ "@ref": "indexes/spellbooks_by_owner" },
{ "@ref": "indexes/spells_by_spellbook" },
{ "@ref": "indexes/all_spell_names" },
{ "@ref": "indexes/spells_by_element_with_name" },
{ "@ref": "indexes/spells_with_ref_by_element_name" },
{ "@ref": "indexes/latest_spells_by_element" }
]
}
{
data: [
ref(id = "all_spell_names", class = ref(id = "indexes")),
ref(id = "spellbooks_by_owner", class = ref(id = "indexes")),
ref(id = "spells_by_element", class = ref(id = "indexes")),
ref(id = "spells_by_spellbook", class = ref(id = "indexes")),
ref(id = "spells_by_element_with_name", class = ref(id = "indexes")),
ref(id = "spells_with_ref_by_element_name", class = ref(id = "indexes")),
ref(id = "latest_spells_by_element", class = ref(id = "indexes")),
ref(id = "elements_of_spells", class = ref(id = "indexes"))
]
}
map[data:[
{all_spells 0xc4201fe5e0 <nil>}
{spells_by_name 0xc4201fe720 <nil>}
{spells_by_element 0xc4201fe880 <nil>}
{spells_by_element_and_name 0xc4201fe9c0 <nil>}
{spellbooks_by_owner 0xc4201feb00 <nil>}
{spells_by_spellbook 0xc4201fec40 <nil>}
{all_spell_names 0xc4201fed80 <nil>}
{spells_by_element_with_name 0xc4201feec0 <nil>}
{spells_with_ref_by_element_name 0xc4201ff000 <nil>}
{latest_spells_by_element 0xc4201ff140 <nil>}
]]
{
"data": [
{ "@ref": "indexes/all_spells" },
{ "@ref": "indexes/spells_by_name" },
{ "@ref": "indexes/spells_by_element" },
{ "@ref": "indexes/spells_by_element_and_name" },
{ "@ref": "indexes/spellbooks_by_owner" },
{ "@ref": "indexes/spells_by_spellbook" },
{ "@ref": "indexes/all_spell_names" },
{ "@ref": "indexes/spells_by_element_with_name" },
{ "@ref": "indexes/spells_with_ref_by_element_name" },
{ "@ref": "indexes/latest_spells_by_element" }
]
}
{
"data": [
{ "@ref": "indexes/all_spells" },
{ "@ref": "indexes/spells_by_name" },
{ "@ref": "indexes/spells_by_element" },
{ "@ref": "indexes/spells_by_element_and_name" },
{ "@ref": "indexes/spellbooks_by_owner" },
{ "@ref": "indexes/spells_by_spellbook" },
{ "@ref": "indexes/all_spell_names" },
{ "@ref": "indexes/spells_by_element_with_name" },
{ "@ref": "indexes/spells_with_ref_by_element_name" },
{ "@ref": "indexes/latest_spells_by_element" }
]
}
{
"data": [
{ "@ref": "indexes/all_spells" },
{ "@ref": "indexes/spells_by_name" },
{ "@ref": "indexes/spells_by_element" },
{ "@ref": "indexes/spells_by_element_and_name" },
{ "@ref": "indexes/spellbooks_by_owner" },
{ "@ref": "indexes/spells_by_spellbook" },
{ "@ref": "indexes/all_spell_names" },
{ "@ref": "indexes/spells_by_element_with_name" },
{ "@ref": "indexes/spells_with_ref_by_element_name" },
{ "@ref": "indexes/latest_spells_by_element" }
]
}
{
"data": [
{ "@ref": "indexes/all_spells" },
{ "@ref": "indexes/spells_by_name" },
{ "@ref": "indexes/spells_by_element" },
{ "@ref": "indexes/spells_by_element_and_name" },
{ "@ref": "indexes/spellbooks_by_owner" },
{ "@ref": "indexes/spells_by_spellbook" },
{ "@ref": "indexes/all_spell_names" },
{ "@ref": "indexes/spells_by_element_with_name" },
{ "@ref": "indexes/spells_with_ref_by_element_name" },
{ "@ref": "indexes/latest_spells_by_element" }
]
}
{ data: [
Ref(id=all_spells, class=Ref(id=indexes)),
Ref(id=spells_by_name, class=Ref(id=indexes)),
Ref(id=spells_by_element, class=Ref(id=indexes)),
Ref(id=spells_by_element_and_name, class=Ref(id=indexes)),
Ref(id=spellbooks_by_owner, class=Ref(id=indexes)),
Ref(id=spells_by_spellbook, class=Ref(id=indexes)),
Ref(id=all_spell_names, class=Ref(id=indexes)),
Ref(id=spells_by_element_with_name, class=Ref(id=indexes)),
Ref(id=spells_with_ref_by_element_name, class=Ref(id=indexes)),
Ref(id=latest_spells_by_element, class=Ref(id=indexes)) ] }
Get an Index
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "get": { "index": "spells_by_element_with_name" } }'
client.Query(Get(Index("spells_by_element_with_name")));
System.out.println(
client.query(Get(Index(Value("spells_by_element_with_name"))))
.get());
result, _ := client.Query(f.Get(f.Index("spells_by_element_with_name")))
fmt.Println(result)
client.query(Get(Index("spells_by_element_with_name")))
client.query(q.get(q.index("spells_by_element_with_name")))
$client.query do
get index('spells_by_element_with_name')
end
client.query(Get(Index("spells_by_element_with_name")))
client.query(q.Get(q.Index("spells_by_element_with_name")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309016000,
"active": true,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309016000,
"active": true,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
ref: ref(id = "spells_by_element_with_name", class = ref(id = "indexes")),
ts: 1527714659590871,
active: true,
partitions: 1,
name: "spells_by_element_with_name",
source: ref(id = "spells", class = ref(id = "classes")),
terms: [ {field: ["data", "element"]} ],
values: [ {field: ["data", "name"]} ]
}
map[
ref:{spells_by_element_with_name 0xc4203c1300 <nil>}
ts:1520223309016000
active:true
partitions:1
name:spells_by_element_with_name
source:{spells 0xc4203c1560 <nil>}
values:[map[field:[data name]]]
terms:[map[field:[data element]]]
]
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309016000,
"active": true,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309016000,
"active": true,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309016000,
"active": true,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_element_with_name" },
"class": { "@ref": "indexes" },
"ts": 1520223309016000,
"active": true,
"partitions": 1,
"name": "spells_by_element_with_name",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{ ref: Ref(id=spells_by_element_with_name, class=Ref(id=indexes)),
ts: 1527625636424120,
active: false,
partitions: 1,
name: 'spells_by_element_with_name',
source: Ref(id=spells, class=Ref(id=classes)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] } ] }
Query an Index
Indexes created without specifying the terms
field return all indexed
values
on the indexed class.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "paginate": { "match": { "index": "all_spell_names" } } }'
client.Query(Paginate(Match(Index("all_spell_names"))));
System.out.println(
client.query(Paginate(Match(Index(Value("all_spell_names")))))
.get());
result, _ := client.Query(f.Paginate(f.Match(f.Index("all_spell_names"))))
fmt.Println(result)
client.query(Paginate(Match(Index("all_spell_names"))))
client.query(q.paginate(q.match(q.index("all_spell_names"))))
$client.query do
paginate match(index('all_spell_names'))
end
client.query(
Paginate(Match(index: Index("all_spell_names")))
)
client.query(q.Paginate(q.Match(q.Index("all_spell_names"))))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
}
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
{
data: ["Fire Beak", "Hippo's Wallow", "Water Dragon's Claw"]
}
map[data:[Fire Beak Hippo's Wallow Water Dragon's Claw]]
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
{ data: [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ] }
For indexes where the terms
field was defined, entries are located
using the indexed terms.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"paginate": {
"match": { "index": "spells_by_element_with_name" },
"terms": "fire"
}
}'
client.Query(
Paginate(Match(Index("spells_by_element_with_name"), "fire")));
System.out.println(
client.query(
Paginate(
Match(
Index(Value("spells_by_element_with_name")),
Value("fire")))
).get());
result, _ := client.Query(
f.Paginate(
f.MatchTerm(
f.Index("spells_by_element_with_name"),
"fire",
),
),
)
fmt.Println(result)
client.query(
Paginate(Match(Index("spells_by_element_with_name"), "fire")))
client.query(
q.paginate(
q.match(q.index("spells_by_element_with_name"), "fire")
))
$client.query do
paginate match(index('spells_by_element_with_name'), 'fire')
end
client.query(
Paginate(
Match(
index: Index("spells_by_element_with_name"),
terms: "fire"
)
)
)
client.query(
q.Paginate(
q.Match(q.Index("spells_by_element_with_name"), "fire")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": { "data": [ "Fire Beak", "Water Dragon's Claw" ] }
}
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
{ data: ["Fire Beak", "Water Dragon's Claw"] }
map[data:[Fire Beak Water Dragon's Claw]]
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
{ data: [ "Fire Beak", "Water Dragon's Claw" ] }
Indexes return all their covered values. Here we’re searching for all
the spells with element “fire” using an index that will return both
the spell name and its ref
:
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"paginate": {
"match": { "index": "spells_with_ref_by_element_name" },
"terms": "fire"
}
}'
client.Query(
Paginate(
Match(Index("spells_with_ref_by_element_name"), "fire")));
System.out.println(
client.query(
Paginate(
Match(
Index(Value("spells_with_ref_by_element_name")),
Value("fire")
)
)
).get());
result, _ := client.Query(
f.Paginate(
f.MatchTerm(
f.Index("spells_with_ref_by_element_name"),
"fire",
),
),
)
fmt.Println(result)
client.query(
Paginate(
Match(Index("spells_with_ref_by_element_name"), "fire")))
client.query(
q.paginate(
q.match(q.index("spells_with_ref_by_element_name"), "fire")
))
$client.query do
paginate match(index('spells_with_ref_by_element_name'), 'fire')
end
client.query(
Paginate(
Match(
index: Index("spells_with_ref_by_element_name"),
terms: "fire"
)
)
)
client.query(
q.Paginate(
q.Match(q.Index("spells_with_ref_by_element_name"), "fire")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
}
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
{
data: [
[
"Fire Beak",
ref(id = "181388642046968320", class = ref(id = "spells", class = ref(id = "classes")))
],
[
"Water Dragon's Claw",
ref(id = "181388642071085568", class = ref(id = "spells", class = ref(id = "classes")))
]
]
}
map[data:[[Fire Beak {192900707573039616 0xc420374e40 <nil>}] [Water Dragon's Claw {192900707595059712 0xc4203750c0 <nil>}]]]
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
{ data: [
[ "Fire Beak",
Ref(id=192900707573039616, class=Ref(id=spells, class=Ref(id=classes))) ],
[ "Water Dragon's Claw",
Ref(id=192900707595059712, class=Ref(id=spells, class=Ref(id=classes))) ] }
Indexes with Ordering and Transformations
Let’s create an index to return the latest spells updated. To do this we will create an index which is sorted reversely by each spell’s last updated timestamp.
We’ll specify terms on this index so we can find spells by element.
We’ll also use the casefold
function as a transformation to the
specified term which will make the entries lowercase in the index.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "latest_spells_by_element",
"source": { "class": "spells" },
"terms": [
{
"object": {
"field": [ "data", "element" ],
"transform": "casefold"
}
}
],
"values": [
{ "object": { "field": [ "ts" ], "reverse": true } },
{ "object": { "field": [ "data", "name" ] } },
{ "object": { "field": [ "ref" ] } }
]
}
}
}'
client.Query(
CreateIndex(
Obj(
"name", "latest_spells_by_element",
"source", Class("spells"),
"terms", Arr(
Obj("field", Arr("data", "element"), "transform", "casefold")
),
"values", Arr(
Obj("field", Arr("ts"), "reverse", true),
Obj("field", Arr("data", "name")),
Obj("field", Arr("ref"))
)
)));
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("latest_spells_by_element"),
"source", Class(Value("spells")),
"terms", Arr(
Obj(
"field", Arr(Value("data"), Value("element")),
"transform", Value("casefold")
)
),
"values", Arr(
Obj("field", Arr(Value("ts")), "reverse", Value(true)),
Obj("field", Arr(Value("data"), Value("name"))),
Obj("field", Arr(Value("ref")))
)
)
)
).get());
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "latest_spells_by_element",
"source": f.Class("spells"),
"terms": f.Arr{
f.Obj{
"field": f.Arr{"data", "element"},
"transform": "casefold",
},
},
"values": f.Arr{
f.Obj{"field": f.Arr{"ts"}, "reverse": true},
f.Obj{"field": f.Arr{"data", "name"}},
f.Obj{"field": f.Arr{"ref"}},
},
},
),
)
fmt.Println(result)
client.query(
CreateIndex(
Obj(
"name" -> "latest_spells_by_element",
"source" -> Class("spells"),
"terms" -> Arr(
Obj(
"field" -> Arr("data", "element"),
"transform" -> "casefold"
)
),
"values" -> Arr(
Obj("field" -> Arr("ts"), "reverse" -> true),
Obj("field" -> Arr("data", "name")),
Obj("field" -> Arr("ref"))
)
)))
client.query(
q.create_index(
{
"name": "latest_spells_by_element",
"source": q.class_expr("spells"),
"terms": [{"field": ["data", "element"], "transform": "casefold"}],
"values": [
{"field": ["ts"], "reverse": True},
{"field": ["data", "name"]},
{"field": ["ref"]}
]
}
))
$client.query do
create_index name: 'latest_spells_by_element',
source: class_('spells'),
terms: [{ field: ['data', 'element'], transform: 'casefold' }],
values: [
{ field: ['ts'], reverse: true },
{ field: ['data', 'name'] },
{ field: ['ref'] }
]
end
client.query(
CreateIndex(
Obj(
"name" => "latest_spells_by_element",
"source" => Class("spells"),
"terms" => Arr(
Obj(
"field" => Arr("data", "element"),
"transform" => "casefold"
)
),
"values" => Arr(
Obj("field" => Arr("ts"), "reverse" => true),
Obj("field" => Arr("data", "name")),
Obj("field" => Arr("ref"))
)
)
)
)
client.query(
q.CreateIndex(
{ name: "latest_spells_by_element",
source: q.Class("spells"),
terms: [{ field: ["data", "element"], transform: "casefold" }],
values: [
{ field: ["ts"], reverse: true },
{ field: ["data", "name"] },
{ field: ["ref"] } ] }))
.then((ret) => console.log(ret))
{
ref: ref(id = "latest_spells_by_element", class = ref(id = "indexes")),
ts: 1529022834916476,
active: false,
partitions: 1,
name: "latest_spells_by_element",
source: ref(id = "spells", class = ref(id = "classes")),
terms: [{
field: ["data", "element"],
transform: "casefold"
}],
values: [
{field: ["ts"], reverse: true},
{field: ["data", "name"]},
{field: ["ref"]}
]
}
map[
ref:{latest_spells_by_element 0xc4201553e0 <nil>}
name:latest_spells_by_element
ts:1528319321952315
active:false
partitions:1
source:{spells 0xc420155640 <nil>}
terms:[map[field:[data element] transform:casefold]]
values:[map[field:[ts] reverse:true] map[field:[data name]] map[field:[ref]]]
]
{ ref: Ref(id=latest_spells_by_element, class=Ref(id=indexes)),
ts: 1527625370644230,
active: false,
partitions: 1,
name: 'latest_spells_by_element',
source: Ref(id=spells, class=Ref(id=classes)),
terms: [ { field: [Array], transform: 'casefold' } ],
values:
[ { field: [Array], reverse: true },
{ field: [Array] },
{ field: [Array] } ] }
When querying the index, you can use the casefold
function to convert
the query terms to lowercase. Using casefold
in a query to an index
configured with casefold
essentially makes a case-insensitive query.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"paginate": {
"match": { "index": "latest_spells_by_element" },
"terms": { "casefold": "FIRE" }
}
}'
client.Query(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))));
System.out.println(
client.query(
Paginate(
Match(
Index(Value("latest_spells_by_element")),
Casefold(Value("FIRE")))))
.get());
result, _ := client.Query(
f.Paginate(
f.MatchTerm(
f.Index("latest_spells_by_element"),
f.Casefold("FIRE"),
),
),
)
fmt.Println(result)
client.query(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))))
client.query(
q.paginate(
q.match(
q.index("latest_spells_by_element"),
q.casefold("FIRE")
)
))
$client.query do
paginate match(index('latest_spells_by_element'), casefold('FIRE'))
end
client.query(
Paginate(
Match(
index: Index("latest_spells_by_element"),
terms: Casefold("FIRE")
)
)
)
client.query(
q.Paginate(
q.Match(
q.Index("latest_spells_by_element"),
q.Casefold("FIRE"))))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
}
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
data: [
[
1527719986560591,
"Fire Beak",
ref(id = "181388642046968320", class = ref(id = "spells", class = ref(id = "classes")))
],
[
1527719986495363,
"Water Dragon's Claw",
ref(id = "181388642071085568", class = ref(id = "spells", class = ref(id = "classes")))
]
]
}
map[data:[
[1520223300442183 Water Dragon's Claw {192900707595059712 0xc4202e4ce0 <nil>}]
[1520223300419731 Fire Beak {192900707573039616 0xc4202e4f80 <nil>}]
]]
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{ data:
[ [ 1520223300442183,
"Water Dragon's Claw",
Ref(id=192900707595059712, class=Ref(id=spells, class=Ref(id=classes))) ],
[ [ 1520223300419731
"Fire Beak",
Ref(id=192900707573039616, class=Ref(id=spells, class=Ref(id=classes))) ] ] }
Since timestamp was added to the index to facilitate sorting, it might
not be required for the query to return timestamp values to the
application layer. In that case, it’s possible to use the map
function
to extract only the desired fields.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"map": {
"lambda": [ "_", "name", "ref" ],
"expr": [ { "var": "name" }, { "var": "ref" } ]
},
"collection": {
"paginate": {
"match": { "index": "latest_spells_by_element" },
"terms": { "casefold": "FIRE" }
}
}
}'
client.Query(
Map(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))),
Lambda(
Arr("_", "name", "ref"),
Arr(Var("name"), Var("ref")))));
System.out.println(
client.query(
Map(
Paginate(
Match(
Index(Value("latest_spells_by_element")),
Casefold(Value("FIRE")))),
Lambda(
Arr(Value("_"), Value("name"), Value("ref")),
Arr(Var("name"), Var("ref")))))
.get());
result, _ := client.Query(
f.Map(
f.Paginate(
f.MatchTerm(
f.Index("latest_spells_by_element"),
f.Casefold("FIRE"),
),
),
f.Lambda(
f.Arr{"_", "name", "ref"},
f.Arr{f.Var("name"), f.Var("ref")},
),
),
)
fmt.Println(result)
client.query(
Map(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))),
Lambda { (_, name, ref) => Arr(name, ref) }))
client.query(
q.map_expr(
q.lambda_expr(
["_", "name", "ref"],
[q.var("name"), q.var("ref")]
),
q.paginate(
q.match(
q.index("latest_spells_by_element"),
q.casefold("FIRE")
)
)
))
$client.query do
map paginate(match(index('latest_spells_by_element'), casefold('FIRE'))),
lambda_expr(['_', 'name', 'ref'], [var('name'), var('ref')])
end
client.query(
Map(
collection: Paginate(
Match(
index: Index("latest_spells_by_element"),
terms: Casefold("FIRE")
)
),
to: Lambda(
vars: "_", "name", "ref"
in: Arr(Var("name"), Var("ref"))
)
)
)
client.query(
q.Map(
q.Paginate(
q.Match(
q.Index("latest_spells_by_element"),
q.Casefold("FIRE"))),
q.Lambda(["_", "name", "ref"], [q.Var("name"), q.Var("ref")])))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
}
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
data: [
[
"Fire Beak",
ref(id = "181388642046968320", class = ref(id = "spells", class = ref(id = "classes")))
],
[
"Water Dragon's Claw",
ref(id = "181388642071085568", class = ref(id = "spells", class = ref(id = "classes")))
]
]
}
map[data:[
[Water Dragon's Claw {192900707595059712 0xc4201fc680 <nil>}]
[Fire Beak {192900707573039616 0xc4201fc900 <nil>}]
]]
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
{ data:
[ [ "Water Dragon's Claw",
Ref(id=192900707595059712, class=Ref(id=spells, class=Ref(id=classes))) ],
[ "Fire Beak",
Ref(id=192900707573039616, class=Ref(id=spells, class=Ref(id=classes))) ] ] }
Update an Index
It is disallowed to update an index in a way that changes its shape.
This means no changing the source or the terms. You can, however, change
the name of an index or its uniqueness. If you update the unique
field, it will not remove existing duplicated items from the index.
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"update": { "index": "spells_by_element_with_name" },
"params": { "object": { "name": "spells_by_kind" } }
}'
client.Query(
Update(
Index("spells_by_element_with_name"),
Obj("name", "spells_by_kind")));
System.out.println(
client.query(
Update(
Index(Value("spells_by_element_with_name")),
Obj("name", Value("spells_by_kind"))
)
)
.get());
result, _ := client.Query(
f.Update(
f.Index("spells_by_element_with_name"),
f.Obj{"name": "spells_by_kind"},
),
)
fmt.Println(result)
client.query(
Update(
Index("spells_by_element_with_name"),
Obj("name" -> "spells_by_kind")))
client.query(
q.update(
q.index("spells_by_element_with_name"),
{"name": "spells_by_kind"}
))
$client.query do
update index('spells_by_element_with_name'),
name: 'spells_by_kind'
end
client.query(
Update(
ref: Index("spells_by_element_with_name"),
to: Obj("name" => "spells_by_kind")
)
)
client.query(
q.Update(
q.Index("spells_by_element_with_name"),
{ name: "spells_by_kind" }))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
ref: ref(id = "spells_by_kind", class = ref(id = "indexes")),
ts: 1527720046964825,
active: true,
partitions: 1,
name: "spells_by_kind",
source: ref(id = "spells", class = ref(id = "classes")),
terms: [ {field: ["data", "element"]} ],
values: [ {field: ["data", "name"]} ]
}
map[
ref:{spells_by_kind 0xc4201ff520 <nil>}
ts:1520223330123219
active:true
partitions:1
name:spells_by_kind
source:{spells 0xc4201ff780 <nil>}
terms:[map[field:[data element]]]
values:[map[field:[data name]]]
]
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{ ref: Ref(id=spells_by_kind, class=Ref(id=indexes)),
ts: 1527630282459547,
active: true,
partitions: 1,
name: 'spells_by_kind',
source: Ref(id=spells, class=Ref(id=classes)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] } ] }
Delete an Index
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "delete": { "index": "spells_by_kind" } }'
client.Query(Delete(Index("spells_by_kind")));
System.out.println(client.query(
Delete(Index(Value("spells_by_kind")))).get());
result, _ := client.Query(f.Delete(f.Index("spells_by_kind")))
fmt.Println(result)
client.query(Delete(Index("spells_by_kind")))
client.query(q.delete(q.index("spells_by_kind")))
$client.query do
delete index('spells_by_kind')
end
client.query(Delete(ref: Index("spells_by_kind")))
client.query(q.Delete(q.Index("spells_by_kind")))
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
ref: ref(id = "spells_by_kind", class = ref(id = "indexes")),
ts: 1527720046964825,
active: true,
partitions: 1,
name: "spells_by_kind",
source: ref(id = "spells", class = ref(id = "classes")),
terms: [ {field: ["data", "element"]} ],
values: [{field: ["data", "name"]}]
}
map[
ref:{spells_by_kind 0xc4201f3240 <nil>}
ts:1520223330123219
active:true
partitions:1
name:spells_by_kind
source:{spells 0xc4201f34a0 <nil>}
terms:[map[field:[data element]]]
values:[map[field:[data name]]]
]
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{
"ref": { "@ref": "indexes/spells_by_kind" },
"class": { "@ref": "indexes" },
"ts": 1520223330123219,
"active": true,
"partitions": 1,
"name": "spells_by_kind",
"source": { "@ref": "classes/spells" },
"terms": [ { "field": [ "data", "element" ] } ],
"values": [ { "field": [ "data", "name" ] } ]
}
{ ref: Ref(id=spells_by_kind, class=Ref(id=indexes)),
ts: 1527625346513000,
active: true,
partitions: 1,
name: 'spells_by_kind',
source: Ref(id=spells, class=Ref(id=classes)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] }, { field: [Array] } ] }
Was this article helpful?
We're sorry to hear that.
Tell us how we can improve!
documentation@fauna.com
Thank you for your feedback!