-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_crud.py
75 lines (58 loc) · 1.91 KB
/
demo_crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from faunadb import query as q
from faunadb.objects import Ref
from faunadb.client import FaunaClient
client = FaunaClient(secret="secret", domain="localhost", scheme="http", port="8443")
# create db
client.query(q.create_database({"name": "my_app"}))
# Accessing the database
# Create an initial server key by using an admin key. The server key has unrestricted access to a single database;
# in this case, the server key will only access the blog post database we just created.
client.query(
q.create_key(
{"database": q.database("my_app"), "role": "server"}
))
# Set up a collection
# Create a collection using the CreateCollection function with a param_object containing the name of the collection.
# We shall name our collection "posts":
client.query(q.create_collection({"name": "posts"}))
# Create an index
# The customary way to access documents within a collection is by specifying a criteria for one of the fields.
# To enable criteria-based searches, we need to first create an index using the path of the field within the document.
client.query(
q.create_index(
{
"name": "posts_by_title",
"source": q.collection("posts"),
"terms": [{"field": ["data", "title"]}]
}
))
client.query(
q.create_index(
{
"name": "posts_by_tags_with_title",
"source": q.collection("posts"),
"terms": [{"field": ["data", "tags"]}],
"values": [{"field": ["data", "title"]}]
}
))
# Create a post
client.query(
q.create(
q.collection("posts"),
{"data": {"title": "What I had for breakfast .."}}
))
# Create several posts
client.query(
q.map_expr(
lambda post_title: q.create(
q.collection("posts"),
{"data": {"title": post_title}}
),
[
"My cat and other marvels",
"Pondering during a commute",
"Deep meanings in a latte"
]
))
# Retrieve posts
client.query(q.get(q.ref(q.collection("posts"), "192903209792046592")))