Skip to content

Commit 7a499ed

Browse files
docs: update database guide
1 parent 234e0fc commit 7a499ed

File tree

1 file changed

+74
-41
lines changed

1 file changed

+74
-41
lines changed

docs/guide/database.rst

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
Database
22
========
33

4-
You can build paths to your data by using the ``child()`` method.
4+
The database service allows you to run CRUD operations to your Firebase Realtime
5+
Database, and also perform complex queries while doing so.
56

67
.. code-block:: python
78
9+
# Create database instance
810
db = firebaseApp.database()
9-
db.child("users").child("Morty")
1011
..
1112
1213
.. note::
1314
Each of the following methods accepts a user token:
14-
``get()``, ``push()``, ``set()``, ``update()``,
15-
``remove()`` and ``stream()``.
15+
:ref:`get()<guide/database:get>`, :ref:`push()<guide/database:push>`,
16+
:ref:`set()<guide/database:set>`, :ref:`update()<guide/database:update>`,
17+
:ref:`remove()<guide/database:remove>` and
18+
:ref:`stream()<guide/database:streaming>`.
19+
20+
21+
Build Path
22+
----------
23+
24+
You can build paths to your data by using the ``child()`` method.
25+
26+
.. code-block:: python
27+
28+
db.child("users").child("Edward")
29+
30+
# Alternate ways
31+
db.child("users", "Edward")
32+
db.child("users/Edward")
33+
..
1634
1735

1836
Save Data
@@ -27,7 +45,7 @@ To save data with a unique, auto-generated, timestamp-based key, use the
2745

2846
.. code-block:: python
2947
30-
data = {"name": "Mortimer 'Morty' Smith"}
48+
data = {"name": "Anthony 'Edward' Stark"}
3149
db.child("users").push(data)
3250
..
3351
@@ -39,8 +57,8 @@ below is "Morty".
3957

4058
.. code-block:: python
4159
42-
data = {"name": "Mortimer 'Morty' Smith"}
43-
db.child("users").child("Morty").set(data)
60+
data = {"name": "Anthony 'Edward' Stark"}
61+
db.child("users").child("Edward").set(data)
4462
..
4563
4664
update
@@ -50,7 +68,7 @@ To update data for an existing entry use the ``update()`` method.
5068

5169
.. code-block:: python
5270
53-
db.child("users").child("Morty").update({"name": "Mortiest Morty"})
71+
db.child("users").child("Edward").update({"name": "Tony Stark"})
5472
..
5573
5674
remove
@@ -60,7 +78,7 @@ To delete data for an existing entry use the ``remove()`` method.
6078

6179
.. code-block:: python
6280
63-
db.child("users").child("Morty").remove()
81+
db.child("users").child("Edward").remove()
6482
..
6583
6684
multi-location updates
@@ -73,11 +91,11 @@ with the ``update()`` method.
7391
.. code-block:: python
7492
7593
data = {
76-
"users/Morty/": {
77-
"name": "Mortimer 'Morty' Smith"
94+
"users/Edward/": {
95+
"name": "Anthony 'Edward' Stark"
7896
},
79-
"users/Rick/": {
80-
"name": "Rick Sanchez"
97+
"users/Pepper/": {
98+
"name": "Virginia 'Pepper' Potts"
8199
}
82100
}
83101
@@ -91,10 +109,10 @@ To perform multi-location writes to new locations we can use the
91109
92110
data = {
93111
"users/"+ref.generate_key(): {
94-
"name": "Mortimer 'Morty' Smith"
112+
"name": "Anthony 'Edward' Stark"
95113
},
96114
"users/"+ref.generate_key(): {
97-
"name": "Rick Sanchez"
115+
"name": "Virginia 'Pepper' Potts"
98116
}
99117
}
100118
@@ -105,54 +123,69 @@ To perform multi-location writes to new locations we can use the
105123
Retrieve Data
106124
-------------
107125

108-
109-
val
126+
get
110127
^^^
111128

112-
Queries return a PyreResponse object. Calling ``val()`` on these objects
113-
returns the query data.
129+
To return data from a path simply call the ``get()`` method.
114130

115131
.. code-block:: python
116132
117133
users = db.child("users").get()
118-
print(users.val()) # {"Morty": {"name": "Mortimer 'Morty' Smith"}, "Rick": {"name": "Rick Sanchez"}}
119134
..
120135
121-
key
122-
^^^
136+
each
137+
^^^^
123138

124-
Calling ``key()`` returns the key for the query data.
139+
Returns a list of objects on each of which you can call ``val()`` and
140+
``key()``.
125141

126142
.. code-block:: python
127143
128-
user = db.child("users").get()
129-
print(user.key()) # users
144+
users = db.child("users").get()
145+
for user in users.each():
146+
print(user.key(), user.val())
147+
148+
# Output:
149+
# Edward {name": "Anthony 'Edward' Stark"}
150+
# Pepper {'name': "Virginia 'Pepper' Potts"}
130151
..
131152
132-
each
133-
^^^^
134153

135-
Returns a list of objects on each of which you can call ``val()`` and
136-
``key()``.
154+
val
155+
^^^
156+
157+
Queries return a PyreResponse object. Calling ``val()`` on these objects
158+
returns the query data.
137159

138160
.. code-block:: python
139161
140-
all_users = db.child("users").get()
141-
for user in all_users.each():
142-
print(user.key()) # Morty
143-
print(user.val()) # {name": "Mortimer 'Morty' Smith"}
162+
users = db.child('users').child('Edward').get()
163+
164+
for user in users.each():
165+
print(user.val())
166+
167+
# Output:
168+
# {'name': "Anthony 'Edward' Stark"}
144169
..
145170
146-
get
171+
key
147172
^^^
148173

149-
To return data from a path simply call the ``get()`` method.
174+
Calling ``key()`` returns the key for the query data.
150175

151176
.. code-block:: python
152177
153-
all_users = db.child("users").get()
178+
users = db.child("users").get()
179+
180+
for user in users.each():
181+
print(user.key())
182+
183+
# Output:
184+
# Edward
185+
# Pepper
154186
..
155187
188+
156189
Conditional Requests
157190
^^^^^^^^^^^^^^^^^^^^
158191

@@ -167,9 +200,9 @@ conditional request.
167200

168201
.. code-block:: python
169202
170-
etag = db.child("users").child("Morty").get_etag()
171-
data = {"name": "Mortimer 'Morty' Smith"}
172-
db.child("users").child("Morty").conditional_set(data, etag)
203+
etag = db.child("users").child("Edward").get_etag()
204+
data = {"name": "Tony Stark"}
205+
db.child("users").child("Edward").conditional_set(data, etag)
173206
..
174207
175208
If the passed ETag does not match the ETag of the path in the database,
@@ -187,8 +220,8 @@ successful:
187220

188221
.. code-block:: python
189222
190-
etag = db.child("users").child("Morty").get_etag()
191-
response = db.child("users").child("Morty").conditional_remove(etag)
223+
etag = db.child("users").child("Edward").get_etag()
224+
response = db.child("users").child("Edward").conditional_remove(etag)
192225
193226
if "ETag" in response:
194227
etag = response["ETag"] # our ETag was out-of-date

0 commit comments

Comments
 (0)