Overview
This is a cookbook of examples that demonstrate various Fauna queries, including Databases, Keys, Collections, User-defined functions, and Indexes.
Databases
Reading or writing database definitions requires an admin key.
Create a database
var adminClient = new FaunaClient(secret: adminKey);
adminClient.Query(CreateDatabase(Obj("name", "annuvin")));
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "create_database": { "object": { "name": "annuvin" } } }'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
}
adminClient := f.NewFaunaClient(adminKey)
result, _ := adminClient.Query(f.CreateDatabase(f.Obj{"name": "annuvin"}))
fmt.Println(result)
map[ref:{annuvin 0xc4200f6f60 <nil>} ts:1520223296616581 name:annuvin]
FaunaClient adminClient = FaunaClient.builder()
.withSecret(adminKey)
.build();
System.out.println(
adminClient.query(CreateDatabase(Obj("name", Value("annuvin")))
).get());
{
ref: ref(id = "annuvin", collection = ref(id = "databases")),
ts: 1527628862798548,
name: "annuvin"
}
var adminClient = new faunadb.Client({
secret: adminKey
})
adminClient.query(
q.CreateDatabase({ name: 'annuvin' })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=annuvin, collection=Ref(id=databases)),
ts: 1520223296616581,
name: 'annuvin' }
adminClient = FaunaClient(secret=adminKey)
adminClient.query(q.create_database({"name": "annuvin"}))
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
$admin_client = Fauna::Client.new(secret: adminKey)
$admin_client.query do
create_database name: 'annuvin'
end
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
val adminClient = FaunaClient(secret = adminKey)
adminClient.query(CreateDatabase(Obj("name" -> "annuvin")))
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
let adminClient = FaunaDB.Client(secret: adminKey)
adminClient.query(CreateDatabase(Obj("name" => "annuvin")))
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
Paginate all databases
adminClient.Query(Paginate(Ref("databases")));
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "paginate": { "@ref": "databases" } }'
HTTP/1.1 200 OK
{
"resource": {
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
}
result, _ := adminClient.Query(f.Paginate(f.Ref("databases")))
fmt.Println(result)
map[data:[{prydain 0xc4200e1360 <nil>} {caledonia 0xc4200e14a0 <nil>} {annuvin 0xc4200e1600 <nil>}]]
System.out.println(
adminClient.query(Paginate(Ref("databases")))
.get());
{
data: [
ref(id = "prydain", collection = ref(id = "databases")),
ref(id = "caledonia", collection = ref(id = "databases")),
ref(id = "annuvin", collection = ref(id = "databases"))
]
}
adminClient.query(
q.Paginate(q.Databases())
)
.then((ret) => console.log(ret))
{ data:
[ Ref(id=prydain, collection=Ref(id=databases)),
Ref(id=caledonia, collection=Ref(id=databases)),
Ref(id=annuvin, collection=Ref(id=databases)) ] }
adminClient.query(q.paginate(Ref("databases")))
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
$admin_client.query do
paginate ref('databases')
end
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
adminClient.query(Paginate(Ref("databases")))
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
adminClient.query(Paginate(Ref("databases")))
{
"data": [
{ "@ref": "databases/prydain" },
{ "@ref": "databases/caledonia" },
{ "@ref": "databases/annuvin" }
]
}
Get a database
adminClient.Query(Get(Database("annuvin")));
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "get": { "database": "annuvin" } }'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
}
result, _ := adminClient.Query(f.Get(f.Database("annuvin")))
fmt.Println(result)
map[ref:{annuvin 0xc420118a80 <nil>} ts:1520223296616581 name:annuvin]
System.out.println(
adminClient.query(Get(Database("annuvin")))
.get());
{
ref: ref(id = "annuvin", collection = ref(id = "databases")),
ts: 1527869789382310,
name: "annuvin"
}
adminClient.query(
q.Get(q.Database('annuvin'))
)
.then((ret) => console.log(ret))
{ ref: Ref(id=annuvin, collection=Ref(id=databases)),
ts: 1520223296616581,
name: 'annuvin' }
adminClient.query(q.get(q.database("annuvin")))
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
$admin_client.query do
get database('annuvin')
end
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
adminClient.query(Get(Database("annuvin")))
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
adminClient.query(Get(Database("annuvin")))
{
"ref": { "@ref": "databases/annuvin" },
"class": { "@ref": "databases" },
"ts": 1520223296616581,
"name": "annuvin"
}
Rename a database
adminClient.Query(
Update(Database("annuvin"), Obj("name", "llyr")));
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{
"update": { "database": "annuvin" },
"params": { "object": { "name": "llyr" } }
}'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
}
result, _ := adminClient.Query(
f.Update(f.Database("annuvin"), f.Obj{"name": "llyr"}),
)
fmt.Println(result)
map[ref:{llyr 0xc42014ee20 <nil>} ts:1520223296736896 name:llyr]
System.out.println(
adminClient.query(
Update(Database(Value("annuvin")), Obj("name", Value("llyr"))))
.get());
{
ref: ref(id = "llyr", collection = ref(id = "databases")),
ts: 1527628862910072,
name: "llyr"
}
adminClient.query(
q.Update(q.Database('annuvin'), { name: 'llyr' })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=llyr, collection=Ref(id=databases)),
ts: 1520223296736896,
name: 'llyr' }
adminClient.query(
q.update(q.database("annuvin"), {"name": "llyr"}))
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
$admin_client.query do
update database('annuvin'), name: 'llyr'
end
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
adminClient.query(
Update(Database("annuvin"), Obj("name" -> "llyr")))
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
adminClient.query(
Update(
ref: Database("annuvin"),
to: Obj("name" => "llyr")
)
)
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296736896,
"name": "llyr"
}
Annotate a database
adminClient.Query(
Update(Database("llyr"), Obj("data", Obj("env", "test"))));
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{
"update": { "database": "llyr" },
"params": { "object": { "data": { "object": { "env": "test" } } } }
}'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
}
result, _ := adminClient.Query(
f.Update(
f.Database("llyr"),
f.Obj{"data": f.Obj{"env": "test"}},
),
)
fmt.Println(result)
map[ref:{llyr 0xc420141660 <nil>} ts:1520223296774224 name:llyr data:map[env:test]]
System.out.println(
adminClient.query(
Update(
Database(Value("llyr")),
Obj("data", Obj("env", Value("test")))
)
)
.get());
{
ref: ref(id = "llyr", collection = ref(id = "databases")),
ts: 1527628862969290,
name: "llyr",
data: {env: "test"}
}
adminClient.query(
q.Update(q.Database('llyr'), { data: { env: 'test' } })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=llyr, collection=Ref(id=databases)),
ts: 1527631840251296,
name: 'llyr',
data: { env: 'test' } }
adminClient.query(
q.update(q.database("llyr"), {"data": {"env": "test"}}))
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
$admin_client.query do
update database('llyr'), data: { env: 'test' }
end
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
adminClient.query(
Update(Database("llyr"), Obj("data" -> Obj("env" -> "test"))))
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
adminClient.query(
Update(
ref: Database("llyr"),
to: Obj("data" => Obj("env" => "test"))
)
)
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
Delete a database
adminClient.Query(Delete(Database("llyr")));
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "delete": { "database": "llyr" } }'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
}
result, _ := adminClient.Query(f.Delete(f.Database("llyr")))
fmt.Println(result)
map[ref:{llyr 0xc420119300 <nil>} ts:1520223296774224 name:llyr data:map[env:test]]
System.out.println(
adminClient.query(Delete(Database(Value("llyr"))))
.get());
{
ref: ref(id = "llyr", collection = ref(id = "databases")),
ts: 1527628862969290,
name: "llyr",
data: {env: "test"}
}
adminClient.query(
q.Delete(q.Database('llyr'))
)
.then((ret) => console.log(ret))
{ ref: Ref(id=llyr, collection=Ref(id=databases)),
ts: 1527631840251296,
name: 'llyr',
data: { env: 'test' } }
adminClient.query(q.delete(q.database("llyr")))
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
$admin_client.query do
delete database('llyr')
end
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
adminClient.query(Delete(Database("llyr")))
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
adminClient.query(Delete(ref: Database("llyr")))
{
"ref": { "@ref": "databases/llyr" },
"class": { "@ref": "databases" },
"ts": 1520223296774224,
"name": "llyr",
"data": { "env": "test" }
}
Keys
Reading or writing keys requires an admin key.
Create a key
var adminClient = new FaunaClient(secret: adminKey);
adminClient.Query(
CreateKey(
Obj("database", Database("caledonia"), "role", "server")));
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{
"create_key": {
"object": {
"database": { "database": "caledonia" },
"role": "server"
}
}
}'
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"
}
}
adminClient := f.NewFaunaClient(adminKey)
result, _ := adminClient.Query(
f.CreateKey(
f.Obj{"database": f.Database("caledonia"), "role": "server"},
),
)
fmt.Println(result)
map[
ref:{192900703888343552 0xc4200e1c80 <nil>}
ts:1520223296908587
database:{caledonia 0xc4200e1e20 <nil>}
role:server
secret:fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j
hashed_secret:$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui
]
FaunaClient adminClient = FaunaClient.builder()
.withSecret(adminKey)
.build();
System.out.println(
adminClient.query(
CreateKey(
Obj(
"database", Database(Value("caledonia")),
"role", Value("server")
)
)
)
.get());
{
ref: ref(id = "200673528521949696", collection = ref(id = "keys")),
ts: 1527636040123044,
database: ref(id = "caledonia", collection = ref(id = "databases")),
role: "server",
secret: "fnAC6lgM9NACAOCQemdRxGyeMykaG3ZhxIBGtv3l",
hashed_secret: "$2a$05$mVYDsiIfEBfXgZYqZvocSuy4J45wqwyY8Dh.V4eRasETNEV01ovJS"
}
var adminClient = new faunadb.Client({
secret: adminKey
})
adminClient.query(
q.CreateKey({
database: q.Database('caledonia'),
role: 'server',
})
)
.then((ret) => console.log(ret))
{ ref: Ref(id=200669511526908416, collection=Ref(id=keys)),
ts: 1527632209151020,
database: Ref(id=caledonia, collection=Ref(id=databases)),
role: 'server',
secret: 'fnACyOvbh9ACAOtRQDQSefokn4iuMf9zn_FakPAx',
hashed_secret: '$2a$05$dszFRic/tkKzsG0rrUj3Fu7.nSoYuJL7BdnHXepx4XTzfcWtVEZRC' }
adminClient = FaunaClient(secret=adminKey)
adminClient.query(
q.create_key(
{"database": q.database("caledonia"), "role": "server"}
))
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
$admin_client = Fauna::Client.new(secret: adminKey)
$admin_client.query do
create_key database: database('caledonia'), role: 'server'
end
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
val adminClient = FaunaClient(secret = adminKey)
adminClient.query(
CreateKey(
Obj("database" -> Database("caledonia"), "role" -> "server")))
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
let adminClient = FaunaDB.Client(secret: adminKey)
adminClient.query(
CreateKey(
Obj(
"database" => Database("caledonia"),
"role" => "server"
)
)
)
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"secret": "fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
Paginate all keys
adminClient.Query(Paginate(Ref("keys")));
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "paginate": { "@ref": "keys" } }'
HTTP/1.1 200 OK
{
"resource": {
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
}
result, _ := adminClient.Query(f.Paginate(f.Keys()))
fmt.Println(result)
map[data:[{192900703858983424 0xc4201beb80 <nil>} {192900703888343552 0xc4201becc0 <nil>}]]
System.out.println(adminClient.query(Paginate((Ref("keys")))).get());
{
data: [
ref(id = "200647741322297856", collection = ref(id = "keys")),
ref(id = "200673528521949696", collection = ref(id = "keys"))
]
}
adminClient.query(q.Paginate(q.Keys()))
.then((ret) => console.log(ret))
{ data:
[ Ref(id=192900703858983424, collection=Ref(id=keys)),
Ref(id=192900703888343552, collection=Ref(id=keys)) ] }
adminClient.query(q.paginate(Ref("keys")))
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
$admin_client.query do
paginate ref('keys')
end
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
adminClient.query(Paginate(Ref("keys")))
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
adminClient.query(Paginate(Ref("keys")))
{
"data": [
{ "@ref": "keys/192900703858983424" },
{ "@ref": "keys/192900703888343552" }
]
}
Get a key
adminClient.Query(Get(Ref(Keys(), "192900703888343552")));
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "get": { "@ref": "keys/192900703888343552" } }'
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"
}
}
result, _ := adminClient.Query(f.Get(f.RefCollection(f.Keys(), "192900703888343552")))
fmt.Println(result)
map[
ref:{192900703888343552 0xc420119560 <nil>}
ts:1520223296908587
database:{caledonia 0xc420119700 <nil>}
role:server
hashed_secret:$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui
]
System.out.println(
adminClient.query(
Get(Ref(keys(),"192900703888343552))
).get());
{
ref: ref(id = "192900703888343552", collection = ref(id = "keys")),
ts: 1527636039961513,
database: ref(id = "caledonia", collection = ref(id = "databases")),
role: "server",
hashed_secret: "$2a$05$mVYDsiIfEBfXgZYqZvocSuy4J45wqwyY8Dh.V4eRasETNEV01ovJS"
}
adminClient.query(
q.Get(q.Ref(q.Keys(), '192900703888343552'))
)
.then((ret) => console.log(ret))
{ ref: Ref(id=192900703888343552, collection=Ref(id=keys)),
ts: 1527632209151020,
database: Ref(id=caledonia, collection=Ref(id=databases)),
role: 'server',
hashed_secret: '$2a$05$dszFRic/tkKzsG0rrUj3Fu7.nSoYuJL7BdnHXepx4XTzfcWtVEZRC' }
adminClient.query(q.get(q.ref(q.keys(), "192900703888343552")))
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
$admin_client.query do
get ref(keys(), '192900703888343552')
end
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
adminClient.query(Get(Ref(Keys(), "192900703888343552")))
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
adminClient.query(Get(Ref(Keys(), "192900703888343552")))
{
"ref": { "@ref": "keys/192900703888343552" },
"class": { "@ref": "keys" },
"ts": 1520223296908587,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$5pGW0Td0qoLZbHJJVls0AOyq4PMbSsrMxejGPaEZpYGhFUx/HHQui"
}
Delete a key
adminClient.Query(Delete(Ref(Keys(), "192900703858983424")));
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
curl https://db.fauna.com/ \
-u fnACrVIq8BACAHNB8hzHWDxMpcYiQkJgPKtZGtIn: \
-d '{ "delete": { "@ref": "keys/192900703858983424" } }'
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"
}
}
result, _ := adminClient.Query(f.Delete(f.RefCollection(f.Keys(), "192900703858983424")))
fmt.Println(result)
map[
ref:{192900703858983424 0xc42010dd00 <nil>}
ts:1520223296878348
database:{caledonia 0xc42010dea0 <nil>}
role:server
hashed_secret:$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy
]
System.out.println(
adminClient.query(
Delete((keys(), "192900703858983424"))
).get());
{
ref: ref(id = "192900703858983424", collection = ref(id = "keys")),
ts: 1527636039961513,
database: ref(id = "caledonia", collection = ref(id = "databases")),
role: "server",
hashed_secret: "$2a$05$mVYDsiIfEBfXgZYqZvocSuy4J45wqwyY8Dh.V4eRasETNEV01ovJS"
}
adminClient.query(q.Delete(q.Ref(q.Keys(), '192900703858983424')))
.then((ret) => console.log(ret))
{ ref: Ref(id=192900703858983424, collection=Ref(id=keys)),
ts: 1527632209151020,
database: Ref(id=caledonia, collection=Ref(id=databases)),
role: 'server',
hashed_secret: '$2a$05$dszFRic/tkKzsG0rrUj3Fu7.nSoYuJL7BdnHXepx4XTzfcWtVEZRC' }
adminClient.query(q.delete(Ref(Keys(), "192900703858983424")))
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
$admin_client.query do
delete ref(keys(), '192900703858983424')
end
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
adminClient.query(Delete(Ref(Keys(), "192900703858983424")))
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
adminClient.query(
Delete(ref: Ref(Keys(), "192900703858983424"))
)
{
"ref": { "@ref": "keys/192900703858983424" },
"class": { "@ref": "keys" },
"ts": 1520223296878348,
"database": { "@ref": "databases/caledonia" },
"role": "server",
"hashed_secret": "$2a$05$63Xx8dWefALiC3K0qtJnLutmx2lKbv8MnEGky6fyTp/l.bbYYMRhy"
}
Collections
Reading or writing collections requires a server key.
Create a collection
client.Query(CreateCollection(Obj("name", "spells")));
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "create_collection": { "object": { "name": "spells" } } }'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
}
result, _ := client.Query(f.CreateCollection(f.Obj{"name": "spells"}))
fmt.Println(result)
map[ref:{spells 0xc42010d9a0 <nil>} ts:1520223297210738 history_days:30 name:spells]
System.out.println(
client.query(CreateCollection(Obj("name", Value("spells"))))
.get());
{
ref: ref(id = "spells", collection = ref(id = "collections")),
ts: 1536604018339083,
history_days: 30,
name: "spells"
}
client.query(
q.CreateCollection({ name: 'spells' })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=spells, collection=Ref(id=collections))
ts: 1520223297210738,
history_days: 30,
name: 'spells' }
client.query(q.create_collection({"name": "spells"}))
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
$client.query do
create_collection name: 'spells'
end
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
client.query(CreateCollection(Obj("name" -> "spells")))
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
client.query(CreateCollection(Obj("name" => "spells")))
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
Paginate all collections
client.Query(Paginate(Ref("collections")));
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "paginate": { "@ref": "classes" } }'
HTTP/1.1 200 OK
{
"resource": {
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
}
result, _ := client.Query(f.Paginate(f.Collections()))
fmt.Println(result)
map[data:[
{decrepit_huts 0xc420118200 <nil>}
{spellbooks 0xc420118340 <nil>}
{characters 0xc4201184a0 <nil>}
{spells 0xc4201185e0 <nil>}
]]
System.out.println(client.query(Paginate(Ref("collections"))).get());
{ data: [
ref(id = "decrepit_huts", collection = ref(id = "collections")),
ref(id = "spellbooks", collection = ref(id = "collections")),
ref(id = "characters", collection = ref(id = "collections")),
ref(id = "spells", collection = ref(id = "collections"))
]}
client.query(
q.Paginate(q.Collections())
)
.then((ret) => console.log(ret))
{ data:
[ Ref(id=decrepit_huts, collection=Ref(id=collections)),
Ref(id=spellbooks, collection=Ref(id=collections)),
Ref(id=characters, collection=Ref(id=collections)),
Ref(id=spells, collection=Ref(id=collections)) ] }
client.query(q.paginate(Ref("collections")))
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
$client.query do
paginate ref('collections')
end
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
client.query(Paginate(Ref("collections")))
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
client.query(Paginate(Ref("collections")))
{
"data": [
{ "@ref": "classes/decrepit_huts" },
{ "@ref": "classes/spellbooks" },
{ "@ref": "classes/characters" },
{ "@ref": "classes/spells" }
]
}
Get a collection
client.Query(Get(Collection("spells")));
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "get": { "collection": "spells" } }'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
}
result, _ := client.Query(f.Get(f.Collection("spells")))
fmt.Println(result)
map[ref:{spells 0xc4201e8080 <nil>} ts:1520223297210738 history_days:30 name:spells]
System.out.println(
client.query(Get(Collection(Value("spells"))))
.get());
{
ref: ref(id = "spells", collection = ref(id = "collections")),
ts: 1536271845615375,
history_days: 30,
name: "spells"
}
client.query(
q.Get(q.Collection('spells'))
)
.then((ret) => console.log(ret))
{ ref: Ref(id=spells, collection=Ref(id=collections))
ts: 1520223297210738,
history_days: 30,
name: 'spells' }
client.query(q.get(q.collection("spells")))
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
$client.query do
get collection_('spells')
end
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
client.query(Get(Collection("spells")))
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
client.query(Get(Collection("spells")))
{
"ref": { "@ref": "classes/spells" },
"class": { "@ref": "classes" },
"ts": 1520223297210738,
"history_days": 30,
"name": "spells"
}
Rename a collection
client.Query(
Update(
Collection("decrepit_huts"),
Obj("name", "dilapidated_huts")));
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"update": { "collection": "decrepit_huts" },
"params": { "object": { "name": "dilapidated_huts" } }
}'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
}
result, _ := client.Query(
f.Update(
f.Collection("decrepit_huts"),
f.Obj{"name": "dilapidated_huts"},
),
)
fmt.Println(result)
map[ref:{dilapidated_huts 0xc4201d43e0 <nil>} ts:1520223297276685 history_days:30 name:dilapidated_huts]
System.out.println(
client.query(
Update(
Collection(Value("decrepit_huts")),
Obj("name", Value("dilapidated_huts"))
)
).get());
{
ref: ref(id = "dilapidated_huts", collection = ref(id = "collections")),
ts: 1527714658200596,
history_days: 30,
name: "dilapidated_huts"
}
client.query(
q.Update(
q.Collection('decrepit_huts'),
{ name: 'dilapidated_huts' },
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=dilapidated_huts, collection=Ref(id=collections)),
ts: 1520223297276685,
history_days: 30,
name: 'dilapidated_huts' }
client.query(
q.update(
q.collection("decrepit_huts"),
{"name": "dilapidated_huts"}
))
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
$client.query do
update collection_('decrepit_huts'), name: 'dilapidated_huts'
end
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
client.query(
Update(
Collection("decrepit_huts"),
Obj("name" -> "dilapidated_huts")))
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
client.query(
Update(
ref: Collection("decrepit_huts"),
to: Obj("name" => "dilapidated_huts")
)
)
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
Delete a collection
client.Query(Delete(Collection("dilapidated_huts")));
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "delete": { "collection": "dilapidated_huts" } }'
HTTP/1.1 200 OK
{
"resource": {
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
}
result, _ := client.Query(f.Delete(f.Collection("dilapidated_huts")))
fmt.Println(result)
map[ref:{dilapidated_huts 0xc4201d6940 <nil>} ts:1520223297276685 history_days:30 name:dilapidated_huts]
System.out.println(
client.query(Delete(Collection(Value("dilapidated_huts"))))
.get());
{
ref: ref(id = "magical_creatures", collection = ref(id = "collections")),
ts: 1536603582490368,
history_days: 30,
name: "dilapidated_huts"
}
client.query(
q.Delete(q.Collection('dilapidated_huts'))
)
.then((ret) => console.log(ret))
{ ref: Ref(id=dilapidated_huts, collection=Ref(id=collections)),
ts: 1520223297276685,
history_days: 30,
name: 'dilapidated_huts' }
client.query(q.delete(q.collection("dilapidated_huts")))
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
$client.query do
delete collection_('dilapidated_huts')
end
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
client.query(Delete(Collection("dilapidated_huts")))
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
client.query(Delete(ref: Collection("dilapidated_huts")))
{
"ref": { "@ref": "classes/dilapidated_huts" },
"class": { "@ref": "classes" },
"ts": 1520223297276685,
"history_days": 30,
"name": "dilapidated_huts"
}
Create a document in a collection
client.Query(
Create(
Collection("spells"),
Obj(
"data", Obj("name", "Fire Beak", "element", Arr("air", "fire"))
)));
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create": { "collection": "spells" },
"params": {
"object": {
"data": {
"object": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
}
}
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
}
result, _ := client.Query(
f.Create(
f.Collection("spells"),
f.Obj{
"data": f.Obj{"name": "Fire Beak", "element": f.Arr{"air", "fire"}},
},
),
)
fmt.Println(result)
map[
ref:{192900707573039616 0xc4201e6600 <nil>}
ts:1520223300419731
data:map[name:Fire Beak element:[air fire]]
]
System.out.println(
client.query(
Create(
Collection(Value("spells")),
Obj( "data",
Obj(
"name", Value("Fire Beak"),
"element", Arr(Value("air"), Value("fire"))
)
)
)
).get());
{
ref: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))),
ts: 1536604015999279,
data: {name: "Fire Beak", element: ["air", "fire"]}
}
client.query(
q.Create(
q.Collection('spells'),
{
data: {
name: 'Fire Beak',
element: ['air', 'fire'],
},
},
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=200669801878651393, collection=Ref(id=spells, collection=Ref(id=collections))),
ts: 1527632486061801,
data: { name: 'Fire Beak', element: [ 'air', 'fire' ] } }
client.query(
q.create(
q.collection("spells"),
{"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" ] }
}
$client.query do
create collection_('spells'),
data: { name: 'Fire Beak', element: ['air', 'fire'] }
end
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
client.query(
Create(
Collection("spells"),
Obj(
"data" -> Obj("name" -> "Fire Beak", "element" -> Arr("air", "fire"))
)))
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
client.query(
Create(
at: Collection("spells"),
Obj(
"data" => Obj(
"name" => "Fire Beak",
"element" => Arr("air", "fire")
)
)
)
)
{
"ref": { "@ref": "classes/spells/192900707573039616" },
"class": { "@ref": "classes/spells" },
"ts": 1520223300419731,
"data": { "name": "Fire Beak", "element": [ "air", "fire" ] }
}
User-defined functions
Creating and updating a function requires:
-
An admin or server key. See Keys and Permissions for details.
-
The privilege to create/update a function, granted in an Attribute-based access control (ABAC) role.
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 document.
// Not available in this language yet.
{
"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"
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_function": {
"object": {
"name": "create_entry",
"body": {
"query": {
"lambda": [ "title", "body" ],
"expr": {
"create": { "collection": "posts" },
"params": {
"object": {
"title": { "var": "title" },
"body": { "var": "body" }
}
}
}
}
},
"permissions": { "object": { "call": "public" } },
"role": "server"
}
}
}'
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"
}
}
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.Collection("posts"), f.Obj{
"data": f.Obj{
"title": f.Var("title"),
"body": f.Var("body"),
},
}),
)),
}))
fmt.Println(result)
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
]
System.out.println(
client.query(
CreateFunction(
Obj(
"name", Value("create_entry"),
"body" , Query(
Lambda(
Arr(Value("title"),Value("body")),
Create(
Collection("posts"),
Obj( "data", Obj("title", Var("title"),"body", Var("body")))
)
)
),
"permissions", Obj("call", Value("public")),
"role", Value("server")
)
)
).get());
{
ref: ref(id = "create_entry", collection = ref(id = "functions")),
ts: 1529338141262176,
name: "create_entry",
body: QueryV({
lambda=[title, body],
expr={create={collection=posts}, params={object={data={object={title={var=title}, body={var=body}}}}}}
}),
permissions: {call: "public"},
role: "server"
}
client.query(
q.CreateFunction({
name: 'create_entry',
body: q.Query(
q.Lambda(
['title', 'body'],
q.Create(
q.Collection('posts'),
{
data: {
title: q.Var('title'),
body: q.Var('body'),
},
},
),
)
),
permissions: { call: 'public' },
role: 'server',
})
)
.then((ret) => console.log(ret))
{ ref: Ref(id=create_entry, collection=Ref(id=functions)),
ts: 1527633126019327,
name: 'create_entry',
body: Query("[object Object]"),
permissions: { call: 'public' },
role: 'server' }
# Not available in this language yet.
{
"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"
}
# Not available in this language yet.
{
"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"
}
// Not available in this language yet.
{
"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"
}
// Not available in this language yet.
{
"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"
}
Create credentials, login, and call a user-defined function
Now we can create a new author and use their credentials.
// Not available in this language yet.
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create": { "collection": "authors" },
"params": {
"object": {
"data": { "object": { "name": "Patty Smith" } },
"credentials": { "object": { "password": "123456" } }
}
}
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
}
result, _ := client.Query(f.Create(
f.Collection("authors"),
f.Obj{
"data": f.Obj{"name": "Patty Smith"},
"credentials": f.Obj{"password": "123456"},
},
))
fmt.Println(result)
map[ref:{192900738957967872 0xc4201ab040 <nil>} ts:1520223330352930 data:map[name:Patty Smith]]
System.out.println(
client.query(
Create(
Collection(Value("authors")),
Obj(
"data", Obj("name", Value("Patty Smith")),
"credentials", Obj("password", Value("123456"))
)
)
).get());
{
ref: ref(id = "201420401501274624", collection = ref(id = "authors", collection = ref(id = "collections"))),
ts: 1528348313700278,
data: {
name: "Patty Smith"
}
}
client.query(
q.Create(
q.Collection('authors'),
{
data: { name: 'Patty Smith' },
credentials: { password: '123456' },
}
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=192900738957967872, collection=Ref(id=authors, collection=Ref(id=collections))),
ts: 1527633688652829,
data: { name: 'Patty Smith' } }
# Not available in this language yet.
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
# Not available in this language yet.
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
// Not available in this language yet.
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
// Not available in this language yet.
{
"ref": { "@ref": "classes/authors/192900738957967872" },
"class": { "@ref": "classes/authors" },
"ts": 1520223330352930,
"data": { "name": "Patty Smith" }
}
// Not available in this language yet.
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"login": { "@ref": "classes/authors/192900738957967872" },
"params": { "object": { "password": "123456" } }
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
}
result, _ := client.Query(f.Login(
f.RefCollection(f.Collection("authors"), "192900738957967872"),
f.Obj{"password": "123456"},
))
fmt.Println(result)
map[
ref:{192900739000959488 0xc4201fe100 <nil>}
ts:1520223330375483
document:{192900738957967872 0xc4201fe380 <nil>}
secret:fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg
]
System.out.println(
client.query(
Login(
Ref(Collections("authors"), Value(201420401501274624L)),
Obj("password", Value("123456"))
)
).get());
{
ref: ref(id = "202129854841225728", collection = ref(id = "tokens")),
ts: 1529024901104219,
document: ref(id = "201420401501274624", collection = ref(id = "authors", collection = ref(id = "collections"))),
secret: "fnECzhwINFACAAAAAAAAAAAARysJFAyxCh8fdSPiclj-lBeC-DQ"
}
client.query(
q.Login(
q.Ref(q.Collection('authors'), '192900738957967872'),
{ password: '123456' }
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=192900739000959488, collection=Ref(id=tokens)),
ts: 1520223330375483,
document: Ref(id=192900738957967872, collection=Ref(id=authors, collection=Ref(id=collections))),
secret: 'fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg' }
# Not available in this language yet.
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
# Not available in this language yet.
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
// Not available in this language yet.
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
// Not available in this language yet.
{
"ref": { "@ref": "tokens/192900739000959488" },
"class": { "@ref": "tokens" },
"ts": 1520223330375483,
"instance": { "@ref": "classes/authors/192900738957967872" },
"secret": "fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg"
}
The value in the secret
field is the authentication token representing
the author that we just created. A new client connection object needs to
be created using this secret, so that subsequent queries are performed
as the author. See the User
authentication tutorial for more details.
The following query calls the create_entry
function that we created
earlier:
// Not available in this language yet.
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
curl https://db.fauna.com/ \
-u fnECrVIzORACAAKtUir3AAIAq-5ZBLtxuAEhY0cju64xXi_HUhg: \
-d '{
"call": { "function": "create_entry" },
"arguments": [ "First Post Title", "This is my first blog post!" ]
}'
HTTP/1.1 201 Created
{
"resource": {
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
}
result, _ := client.Query(f.Call(
f.Function("create_entry"),
f.Arr{"First Post Title", "This is my first blog post!"},
))
fmt.Println(result)
map[ref:{192900739108962816 0xc4201d3720 <nil>} ts:1520223330453871 data:map[title:First Post Title body:This is my first blog post!]]
System.out.println(
client.query(
Call(
Function("create_entry"),
Arr(Value("First Post Title"),Value("this is my first blog post!"))
)
).get());
{
ref: ref(id = "201474441226486272", collection = ref(id = "posts", collection = ref(id = "collections"))),
ts: 1528399850000762,
data: [
title: "First Post Title",
body: "this is my first blog post!"
]
}
client.query(
q.Call(
q.Function('create_entry'),
[ 'First Post Title', 'This is my first blog post!' ],
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=192900739108962816, collection=Ref(id=posts, collection=Ref(id=collections))),
ts: 1520223330453871,
data:
{ title: 'First Post Title',
body: 'This is my first blog post!' } }
# Not available in this language yet.
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
# Not available in this language yet.
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
// Not available in this language yet.
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
// Not available in this language yet.
{
"ref": { "@ref": "classes/posts/192900739108962816" },
"class": { "@ref": "classes/posts" },
"ts": 1520223330453871
}
Indexes
Creating and updating an index requires a server key.
Create an index
Let’s create an index to list all spells' names:
client.Query(
CreateIndex(
Obj(
"name", "all_spell_names",
"source", Collection("spells"),
"values", Arr(Obj("field", Arr("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" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "all_spell_names",
"source": { "collection": "spells" },
"values": [ { "object": { "field": [ "data", "name" ] } } ]
}
}
}'
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" ] } ]
}
}
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "all_spell_names",
"source": f.Collection("spells"),
"values": f.Arr{f.Obj{"field": f.Arr{"data", "name"}}},
},
),
)
fmt.Println(result)
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]]]]
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("all_spell_names"),
"source", Collection(Value("spells")),
"values", Arr(Obj("field", Arr(Value("data"), Value("name"))))
)))
.get());
{
ref: ref(id = "all_spell_names", collection = ref(id = "indexes")),
ts: 1536257513918009,
active: false,
partitions: 8,
name: "all_spell_names",
source: ref(id = "spells", collection = ref(id = "collections")),
values: [{field: ["data", "name"]}]
}
client.query(
q.CreateIndex(
{
name: 'all_spell_names',
source: q.Collection('spells'),
values: [{ field: ['data', 'name'] }],
},
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=all_spell_names, collection=Ref(id=indexes)),
ts: 1527625252080602,
active: false,
partitions: 8,
name: 'all_spell_names',
source: Ref(id=spells, collection=Ref(id=collections)),
values: [ { field: [Array] } ] }
client.query(
q.create_index(
{
"name": "all_spell_names",
"source": q.collection("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" ] } ]
}
$client.query do
create_index name: 'all_spell_names',
source: collection_('spells'),
values: [{ field: ['data', 'name'] }]
end
{
"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" ] } ]
}
client.query(
CreateIndex(
Obj(
"name" -> "all_spell_names",
"source" -> Collection("spells"),
"values" -> Arr(Obj("field" -> Arr("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" ] } ]
}
client.query(
CreateIndex(
Obj(
"name" => "all_spell_names",
"source" => Collection("spells"),
"values" => Arr(Obj("field" => Arr("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" ] } ]
}
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:
client.Query(
CreateIndex(
Obj(
"name", "spells_by_element_with_name",
"source", Collection("spells"),
"terms", Arr(Obj("field", Arr("data", "element"))),
"values", Arr(Obj("field", Arr("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" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "spells_by_element_with_name",
"source": { "collection": "spells" },
"terms": [ { "object": { "field": [ "data", "element" ] } } ],
"values": [ { "object": { "field": [ "data", "name" ] } } ]
}
}
}'
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" ] } ]
}
}
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "spells_by_element_with_name",
"source": f.Collection("spells"),
"terms": f.Arr{f.Obj{"field": f.Arr{"data", "element"}}},
"values": f.Arr{f.Obj{"field": f.Arr{"data", "name"}}},
},
),
)
fmt.Println(result)
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]]]
]
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("spells_by_element_with_name"),
"source", Collection(Value("spells")),
"terms", Arr(Obj("field", Arr(Value("data"), Value("element")))),
"values", Arr(Obj("field", Arr(Value("data"), Value("name"))))
)))
.get());
{
ref: ref(id = "spells_by_element_with_name", collection = ref(id = "indexes")),
ts: 1527714659590871,
active: false,
partitions: 1,
name: "spells_by_element_with_name",
source: ref(id = "spells", collection = ref(id = "collections")),
terms: [ {field: ["data", "element"]} ],
values: [ {field: ["data", "name"]} ]
}
client.query(
q.CreateIndex(
{ name: 'spells_by_element_with_name',
source: q.Collection('spells'),
terms: [{ field: ['data', 'element'] }],
values: [{ field: ['data', 'name'] }],
},
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=spells_by_element_with_name, collection=Ref(id=indexes)),
ts: 1527625636424120,
active: false,
partitions: 1,
name: 'spells_by_element_with_name',
source: Ref(id=spells, collection=Ref(id=collections)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] } ] }
client.query(
q.create_index(
{
"name": "spells_by_element_with_name",
"source": q.collection("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" ] } ]
}
$client.query do
create_index name: 'spells_by_element_with_name',
source: collection_('spells'),
terms: [{ field: ['data', 'element'] }],
values: [{ field: ['data', 'name'] }]
end
{
"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" ] } ]
}
client.query(
CreateIndex(
Obj(
"name" -> "spells_by_element_with_name",
"source" -> Collection("spells"),
"terms" -> Arr(Obj("field" -> Arr("data", "element"))),
"values" -> Arr(Obj("field" -> Arr("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" ] } ]
}
client.query(
CreateIndex(
Obj(
"name" => "spells_by_element_with_name",
"source" => Collection("spells"),
"terms" => Arr(Obj("field" => Arr("data", "element"))),
"values" => Arr(Obj("field" => Arr("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" ] } ]
}
Indexes can also cover multiple values, which affect the order of their results. See the ordering section for more information.
The length of the field values specified for the terms or
values fields must not exceed 32k bytes. The maximum size of an
index entry, which is comprised of the terms and values
content (and some overhead to distinguish multiple fields),
must not exceed 64k bytes. If an index entry is too large, the
query that created/updated the index entry fails.
|
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
:
client.Query(
CreateIndex(
Obj(
"name", "spells_with_ref_by_element_name",
"source", Collection("spells"),
"terms", Arr(Obj("field", Arr("data", "element"))),
"values", Arr(
Obj("field", Arr("data", "name")),
Obj("field", Arr("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" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "spells_with_ref_by_element_name",
"source": { "collection": "spells" },
"terms": [ { "object": { "field": [ "data", "element" ] } } ],
"values": [
{ "object": { "field": [ "data", "name" ] } },
{ "object": { "field": [ "ref" ] } }
]
}
}
}'
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" ] } ]
}
}
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "spells_with_ref_by_element_name",
"source": f.Collection("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)
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]]]
]
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("spells_with_ref_by_element_name"),
"source", Collection(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());
{
ref: ref(id = "spells_with_ref_by_element_name", collection = ref(id = "indexes")),
ts: 1527714659603218,
active: false,
partitions: 1,
name: "spells_with_ref_by_element_name",
source: ref(id = "spells", collection = ref(id = "collections")),
terms: [{field: ["data", "element"]}],
values: [
{field: ["data", "name"]},
{field: ["ref"]}
]
}
client.query(
q.CreateIndex(
{
name: 'spells_with_ref_by_element_name',
source: q.Collection('spells'),
terms: [{ field: ['data', 'element'] }],
values: [{ field: ['data', 'name'] }, { field: ['ref'] }],
},
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=spells_with_ref_by_element_name, collection=Ref(id=indexes)),
ts: 1527625316880457,
active: false,
partitions: 1,
name: 'spells_with_ref_by_element_name',
source: Ref(id=spells, collection=Ref(id=collections)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] }, { field: [Array] } ] }
client.query(
q.create_index(
{
"name": "spells_with_ref_by_element_name",
"source": q.collection("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" ] } ]
}
$client.query do
create_index name: 'spells_with_ref_by_element_name',
source: collection_('spells'),
terms: [{ field: ['data', 'element'] }],
values: [{ field: ['data', 'name'] }, { field: ['ref'] }]
end
{
"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" ] } ]
}
client.query(
CreateIndex(
Obj(
"name" -> "spells_with_ref_by_element_name",
"source" -> Collection("spells"),
"terms" -> Arr(Obj("field" -> Arr("data", "element"))),
"values" -> Arr(
Obj("field" -> Arr("data", "name")),
Obj("field" -> Arr("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" ] } ]
}
client.query(
CreateIndex(
Obj(
"name" => "spells_with_ref_by_element_name",
"source" => Collection("spells"),
"terms" => Arr(Obj("field" => Arr("data", "element"))),
"values" => Arr(
Obj("field" => Arr("data", "name")),
Obj("field" => Arr("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" ] } ]
}
Paginate all indexes
client.Query(Paginate(Ref("indexes")));
{
"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" }
]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "paginate": { "@ref": "indexes" } }'
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" }
]
}
}
result, _ := client.Query(f.Paginate(f.Ref("indexes")))
fmt.Println(result)
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>}
]]
System.out.println(
client.query(Paginate(Ref("indexes")))
.get());
{
data: [
ref(id = "all_spell_names", collection = ref(id = "indexes")),
ref(id = "spellbooks_by_owner", collection = ref(id = "indexes")),
ref(id = "spells_by_element", collection = ref(id = "indexes")),
ref(id = "spells_by_spellbook", collection = ref(id = "indexes")),
ref(id = "spells_by_element_with_name", collection = ref(id = "indexes")),
ref(id = "spells_with_ref_by_element_name", collection = ref(id = "indexes")),
ref(id = "latest_spells_by_element", collection = ref(id = "indexes")),
ref(id = "elements_of_spells", collection = ref(id = "indexes"))
]
}
client.query(q.Paginate(q.Indexes()))
.then((ret) => console.log(ret))
{ data: [
Ref(id=all_spells, collection=Ref(id=indexes)),
Ref(id=spells_by_name, collection=Ref(id=indexes)),
Ref(id=spells_by_element, collection=Ref(id=indexes)),
Ref(id=spells_by_element_and_name, collection=Ref(id=indexes)),
Ref(id=spellbooks_by_owner, collection=Ref(id=indexes)),
Ref(id=spells_by_spellbook, collection=Ref(id=indexes)),
Ref(id=all_spell_names, collection=Ref(id=indexes)),
Ref(id=spells_by_element_with_name, collection=Ref(id=indexes)),
Ref(id=spells_with_ref_by_element_name, collection=Ref(id=indexes)),
Ref(id=latest_spells_by_element, collection=Ref(id=indexes)) ] }
client.query(q.paginate(Ref("indexes")))
{
"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" }
]
}
$client.query do
paginate ref('indexes')
end
{
"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" }
]
}
client.query(Paginate(Ref("indexes")))
{
"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" }
]
}
client.query(Paginate(Ref("indexes")))
{
"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" }
]
}
Get an index
client.Query(Get(Index("spells_by_element_with_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" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "get": { "index": "spells_by_element_with_name" } }'
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" ] } ]
}
}
result, _ := client.Query(f.Get(f.Index("spells_by_element_with_name")))
fmt.Println(result)
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]]]
]
System.out.println(
client.query(Get(Index(Value("spells_by_element_with_name"))))
.get());
{
ref: ref(id = "spells_by_element_with_name", collection = ref(id = "indexes")),
ts: 1527714659590871,
active: true,
partitions: 1,
name: "spells_by_element_with_name",
source: ref(id = "spells", collection = ref(id = "collections")),
terms: [ {field: ["data", "element"]} ],
values: [ {field: ["data", "name"]} ]
}
client.query(
q.Get(q.Index('spells_by_element_with_name'))
)
.then((ret) => console.log(ret))
{ ref: Ref(id=spells_by_element_with_name, collection=Ref(id=indexes)),
ts: 1527625636424120,
active: false,
partitions: 1,
name: 'spells_by_element_with_name',
source: Ref(id=spells, collection=Ref(id=collections)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] } ] }
client.query(q.get(q.index("spells_by_element_with_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" ] } ]
}
$client.query do
get index('spells_by_element_with_name')
end
{
"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" ] } ]
}
client.query(Get(Index("spells_by_element_with_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" ] } ]
}
client.query(Get(Index("spells_by_element_with_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" ] } ]
}
Query an index
Indexes created without specifying the terms
field return all indexed
values
on the indexed collection.
client.Query(Paginate(Match(Index("all_spell_names"))));
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "paginate": { "match": { "index": "all_spell_names" } } }'
HTTP/1.1 200 OK
{
"resource": {
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
}
result, _ := client.Query(f.Paginate(f.Match(f.Index("all_spell_names"))))
fmt.Println(result)
map[data:[Fire Beak Hippo's Wallow Water Dragon's Claw]]
System.out.println(
client.query(Paginate(Match(Index(Value("all_spell_names")))))
.get());
{
data: ["Fire Beak", "Hippo's Wallow", "Water Dragon's Claw"]
}
client.query(
q.Paginate(q.Match(q.Index('all_spell_names')))
)
.then((ret) => console.log(ret))
{ data: [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ] }
client.query(q.paginate(q.match(q.index("all_spell_names"))))
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
$client.query do
paginate match(index('all_spell_names'))
end
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
client.query(Paginate(Match(Index("all_spell_names"))))
{
"data": [ "Fire Beak", "Hippo's Wallow", "Water Dragon's Claw" ]
}
client.query(
Paginate(Match(index: Index("all_spell_names")))
)
{
"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.
client.Query(
Paginate(Match(Index("spells_by_element_with_name"), "fire")));
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"paginate": {
"match": { "index": "spells_by_element_with_name" },
"terms": "fire"
}
}'
HTTP/1.1 200 OK
{
"resource": { "data": [ "Fire Beak", "Water Dragon's Claw" ] }
}
result, _ := client.Query(
f.Paginate(
f.MatchTerm(
f.Index("spells_by_element_with_name"),
"fire",
),
),
)
fmt.Println(result)
map[data:[Fire Beak Water Dragon's Claw]]
System.out.println(
client.query(
Paginate(
Match(
Index(Value("spells_by_element_with_name")),
Value("fire")))
).get());
{ data: ["Fire Beak", "Water Dragon's Claw"] }
client.query(
q.Paginate(
q.Match(q.Index('spells_by_element_with_name'), 'fire')
)
)
.then((ret) => console.log(ret))
{ data: [ "Fire Beak", "Water Dragon's Claw" ] }
client.query(
q.paginate(
q.match(q.index("spells_by_element_with_name"), "fire")
))
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
$client.query do
paginate match(index('spells_by_element_with_name'), 'fire')
end
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
client.query(
Paginate(Match(Index("spells_by_element_with_name"), "fire")))
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
client.query(
Paginate(
Match(
index: Index("spells_by_element_with_name"),
terms: "fire"
)
)
)
{ "data": [ "Fire Beak", "Water Dragon's Claw" ] }
Indexes return all their values field values. Here we’re searching for
all of the spells with element fire
, using an index that returns both
the spell name and its reference:
client.Query(
Paginate(
Match(Index("spells_with_ref_by_element_name"), "fire")));
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"paginate": {
"match": { "index": "spells_with_ref_by_element_name" },
"terms": "fire"
}
}'
HTTP/1.1 200 OK
{
"resource": {
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
}
result, _ := client.Query(
f.Paginate(
f.MatchTerm(
f.Index("spells_with_ref_by_element_name"),
"fire",
),
),
)
fmt.Println(result)
map[data:[[Fire Beak {192900707573039616 0xc420374e40 <nil>}] [Water Dragon's Claw {192900707595059712 0xc4203750c0 <nil>}]]]
System.out.println(
client.query(
Paginate(
Match(
Index(Value("spells_with_ref_by_element_name")),
Value("fire")
)
)
).get());
{
data: [
[
"Fire Beak",
ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections")))
],
[
"Water Dragon's Claw",
ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))
]
]
}
client.query(
q.Paginate(
q.Match(q.Index('spells_with_ref_by_element_name'), 'fire')
)
)
.then((ret) => console.log(ret))
{ data: [
[ "Fire Beak",
Ref(id=192900707573039616, collection=Ref(id=spells, collection=Ref(id=collections))) ],
[ "Water Dragon's Claw",
Ref(id=192900707595059712, collection=Ref(id=spells, collection=Ref(id=collections))) ] }
client.query(
q.paginate(
q.match(q.index("spells_with_ref_by_element_name"), "fire")
))
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
$client.query do
paginate match(index('spells_with_ref_by_element_name'), 'fire')
end
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
client.query(
Paginate(
Match(Index("spells_with_ref_by_element_name"), "fire")))
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
client.query(
Paginate(
Match(
index: Index("spells_with_ref_by_element_name"),
terms: "fire"
)
)
)
{
"data": [
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
],
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
]
]
}
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.
client.Query(
CreateIndex(
Obj(
"name", "latest_spells_by_element",
"source", Collection("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"))
)
)));
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"create_index": {
"object": {
"name": "latest_spells_by_element",
"source": { "collection": "spells" },
"terms": [
{
"object": {
"field": [ "data", "element" ],
"transform": "casefold"
}
}
],
"values": [
{ "object": { "field": [ "ts" ], "reverse": true } },
{ "object": { "field": [ "data", "name" ] } },
{ "object": { "field": [ "ref" ] } }
]
}
}
}'
result, _ := client.Query(
f.CreateIndex(
f.Obj{
"name": "latest_spells_by_element",
"source": f.Collection("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)
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]]]
]
System.out.println(
client.query(
CreateIndex(
Obj(
"name", Value("latest_spells_by_element"),
"source", Collection(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());
{
ref: ref(id = "latest_spells_by_element", collection = ref(id = "indexes")),
ts: 1529022834916476,
active: false,
partitions: 1,
name: "latest_spells_by_element",
source: ref(id = "spells", collection = ref(id = "collections")),
terms: [{
field: ["data", "element"],
transform: "casefold"
}],
values: [
{field: ["ts"], reverse: true},
{field: ["data", "name"]},
{field: ["ref"]}
]
}
client.query(
q.CreateIndex(
{
name: 'latest_spells_by_element',
source: q.Collection('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, collection=Ref(id=indexes)),
ts: 1527625370644230,
active: false,
partitions: 1,
name: 'latest_spells_by_element',
source: Ref(id=spells, collection=Ref(id=collections)),
terms: [ { field: [Array], transform: 'casefold' } ],
values:
[ { field: [Array], reverse: true },
{ field: [Array] },
{ field: [Array] } ] }
client.query(
q.create_index(
{
"name": "latest_spells_by_element",
"source": q.collection("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: collection_('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" -> Collection("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(
CreateIndex(
Obj(
"name" => "latest_spells_by_element",
"source" => Collection("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"))
)
)
)
)
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.
client.Query(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))));
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"paginate": {
"match": { "index": "latest_spells_by_element" },
"terms": { "casefold": "FIRE" }
}
}'
HTTP/1.1 200 OK
{
"resource": {
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
}
result, _ := client.Query(
f.Paginate(
f.MatchTerm(
f.Index("latest_spells_by_element"),
f.Casefold("FIRE"),
),
),
)
fmt.Println(result)
map[data:[
[1520223300442183 Water Dragon's Claw {192900707595059712 0xc4202e4ce0 <nil>}]
[1520223300419731 Fire Beak {192900707573039616 0xc4202e4f80 <nil>}]
]]
System.out.println(
client.query(
Paginate(
Match(
Index(Value("latest_spells_by_element")),
Casefold(Value("FIRE")))))
.get());
{
data: [
[
1527719986560591,
"Fire Beak",
ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections")))
],
[
1527719986495363,
"Water Dragon's Claw",
ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))
]
]
}
client.query(
q.Paginate(
q.Match(
q.Index('latest_spells_by_element'),
q.Casefold('FIRE'),
)
)
)
.then((ret) => console.log(ret))
{ data:
[ [ 1520223300442183,
"Water Dragon's Claw",
Ref(id=192900707595059712, collection=Ref(id=spells, collection=Ref(id=collections))) ],
[ [ 1520223300419731
"Fire Beak",
Ref(id=192900707573039616, collection=Ref(id=spells, collection=Ref(id=collections))) ] ] }
client.query(
q.paginate(
q.match(
q.index("latest_spells_by_element"),
q.casefold("FIRE")
)
))
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
$client.query do
paginate match(index('latest_spells_by_element'), casefold('FIRE'))
end
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
client.query(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))))
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
client.query(
Paginate(
Match(
index: Index("latest_spells_by_element"),
terms: Casefold("FIRE")
)
)
)
{
"data": [
[
1520223300442183,
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
1520223300419731,
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
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.
client.Query(
Map(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))),
Lambda(
Arr("_", "name", "ref"),
Arr(Var("name"), Var("ref")))));
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
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" }
}
}
}'
HTTP/1.1 200 OK
{
"resource": {
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
}
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)
map[data:[
[Water Dragon's Claw {192900707595059712 0xc4201fc680 <nil>}]
[Fire Beak {192900707573039616 0xc4201fc900 <nil>}]
]]
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());
{
data: [
[
"Fire Beak",
ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections")))
],
[
"Water Dragon's Claw",
ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))
]
]
}
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))
{ data:
[ [ "Water Dragon's Claw",
Ref(id=192900707595059712, collection=Ref(id=spells, collection=Ref(id=collections))) ],
[ "Fire Beak",
Ref(id=192900707573039616, collection=Ref(id=spells, collection=Ref(id=collections))) ] ] }
client.query(
q.map_(
q.lambda_(
["_", "name", "ref"],
[q.var("name"), q.var("ref")]
),
q.paginate(
q.match(
q.index("latest_spells_by_element"),
q.casefold("FIRE")
)
)
))
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
$client.query do
map paginate(match(index('latest_spells_by_element'), casefold('FIRE'))),
lambda_expr(['_', 'name', 'ref'], [var('name'), var('ref')])
end
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
client.query(
Map(
Paginate(
Match(Index("latest_spells_by_element"), Casefold("FIRE"))),
Lambda { (_, name, ref) => Arr(name, ref) }))
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
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"))
)
)
)
{
"data": [
[
"Water Dragon's Claw",
{ "@ref": "classes/spells/192900707595059712" }
],
[
"Fire Beak",
{ "@ref": "classes/spells/192900707573039616" }
]
]
}
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 does not remove existing duplicated items from the index.
client.Query(
Update(
Index("spells_by_element_with_name"),
Obj("name", "spells_by_kind")));
{
"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" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{
"update": { "index": "spells_by_element_with_name" },
"params": { "object": { "name": "spells_by_kind" } }
}'
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" ] } ]
}
}
result, _ := client.Query(
f.Update(
f.Index("spells_by_element_with_name"),
f.Obj{"name": "spells_by_kind"},
),
)
fmt.Println(result)
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]]]
]
System.out.println(
client.query(
Update(
Index(Value("spells_by_element_with_name")),
Obj("name", Value("spells_by_kind"))
)
)
.get());
{
ref: ref(id = "spells_by_kind", collection = ref(id = "indexes")),
ts: 1527720046964825,
active: true,
partitions: 1,
name: "spells_by_kind",
source: ref(id = "spells", collection = ref(id = "collections")),
terms: [ {field: ["data", "element"]} ],
values: [ {field: ["data", "name"]} ]
}
client.query(
q.Update(
q.Index('spells_by_element_with_name'),
{ name: 'spells_by_kind' },
)
)
.then((ret) => console.log(ret))
{ ref: Ref(id=spells_by_kind, collection=Ref(id=indexes)),
ts: 1527630282459547,
active: true,
partitions: 1,
name: 'spells_by_kind',
source: Ref(id=spells, collection=Ref(id=collections)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] } ] }
client.query(
q.update(
q.index("spells_by_element_with_name"),
{"name": "spells_by_kind"}
))
{
"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" ] } ]
}
$client.query do
update index('spells_by_element_with_name'),
name: 'spells_by_kind'
end
{
"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" ] } ]
}
client.query(
Update(
Index("spells_by_element_with_name"),
Obj("name" -> "spells_by_kind")))
{
"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" ] } ]
}
client.query(
Update(
ref: Index("spells_by_element_with_name"),
to: Obj("name" => "spells_by_kind")
)
)
{
"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" ] } ]
}
Delete an index
client.Query(Delete(Index("spells_by_kind")));
{
"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" ] } ]
}
curl https://db.fauna.com/ \
-u fnACrVIrDDACAFiX3FN4PhwADpl7dtPRhWObP08j: \
-d '{ "delete": { "index": "spells_by_kind" } }'
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" ] } ]
}
}
result, _ := client.Query(f.Delete(f.Index("spells_by_kind")))
fmt.Println(result)
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]]]
]
System.out.println(client.query(
Delete(Index(Value("spells_by_kind")))).get());
{
ref: ref(id = "spells_by_kind", collection = ref(id = "indexes")),
ts: 1527720046964825,
active: true,
partitions: 1,
name: "spells_by_kind",
source: ref(id = "spells", collection = ref(id = "collections")),
terms: [ {field: ["data", "element"]} ],
values: [{field: ["data", "name"]}]
}
client.query(
q.Delete(q.Index('spells_by_kind'))
)
.then((ret) => console.log(ret))
{ ref: Ref(id=spells_by_kind, collection=Ref(id=indexes)),
ts: 1527625346513000,
active: true,
partitions: 1,
name: 'spells_by_kind',
source: Ref(id=spells, collection=Ref(id=collections)),
terms: [ { field: [Array] } ],
values: [ { field: [Array] }, { field: [Array] } ] }
client.query(q.delete(q.index("spells_by_kind")))
{
"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" ] } ]
}
$client.query do
delete index('spells_by_kind')
end
{
"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" ] } ]
}
client.query(Delete(Index("spells_by_kind")))
{
"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" ] } ]
}
client.query(Delete(ref: Index("spells_by_kind")))
{
"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" ] } ]
}
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!