1
1
Database
2
2
========
3
3
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.
5
6
6
7
.. code-block :: python
7
8
9
+ # Create database instance
8
10
db = firebaseApp.database()
9
- db.child(" users" ).child(" Morty" )
10
11
..
11
12
12
13
.. note ::
13
14
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
+ ..
16
34
17
35
18
36
Save Data
@@ -27,7 +45,7 @@ To save data with a unique, auto-generated, timestamp-based key, use the
27
45
28
46
.. code-block :: python
29
47
30
- data = {" name" : " Mortimer 'Morty' Smith " }
48
+ data = {" name" : " Anthony 'Edward' Stark " }
31
49
db.child(" users" ).push(data)
32
50
..
33
51
@@ -39,8 +57,8 @@ below is "Morty".
39
57
40
58
.. code-block :: python
41
59
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)
44
62
..
45
63
46
64
update
@@ -50,7 +68,7 @@ To update data for an existing entry use the ``update()`` method.
50
68
51
69
.. code-block :: python
52
70
53
- db.child(" users" ).child(" Morty " ).update({" name" : " Mortiest Morty " })
71
+ db.child(" users" ).child(" Edward " ).update({" name" : " Tony Stark " })
54
72
..
55
73
56
74
remove
@@ -60,7 +78,7 @@ To delete data for an existing entry use the ``remove()`` method.
60
78
61
79
.. code-block :: python
62
80
63
- db.child(" users" ).child(" Morty " ).remove()
81
+ db.child(" users" ).child(" Edward " ).remove()
64
82
..
65
83
66
84
multi-location updates
@@ -73,11 +91,11 @@ with the ``update()`` method.
73
91
.. code-block :: python
74
92
75
93
data = {
76
- " users/Morty /" : {
77
- " name" : " Mortimer 'Morty' Smith "
94
+ " users/Edward /" : {
95
+ " name" : " Anthony 'Edward' Stark "
78
96
},
79
- " users/Rick /" : {
80
- " name" : " Rick Sanchez "
97
+ " users/Pepper /" : {
98
+ " name" : " Virginia 'Pepper' Potts "
81
99
}
82
100
}
83
101
@@ -91,10 +109,10 @@ To perform multi-location writes to new locations we can use the
91
109
92
110
data = {
93
111
" users/" + ref.generate_key(): {
94
- " name" : " Mortimer 'Morty' Smith "
112
+ " name" : " Anthony 'Edward' Stark "
95
113
},
96
114
" users/" + ref.generate_key(): {
97
- " name" : " Rick Sanchez "
115
+ " name" : " Virginia 'Pepper' Potts "
98
116
}
99
117
}
100
118
@@ -105,54 +123,69 @@ To perform multi-location writes to new locations we can use the
105
123
Retrieve Data
106
124
-------------
107
125
108
-
109
- val
126
+ get
110
127
^^^
111
128
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.
114
130
115
131
.. code-block :: python
116
132
117
133
users = db.child(" users" ).get()
118
- print (users.val()) # {"Morty": {"name": "Mortimer 'Morty' Smith"}, "Rick": {"name": "Rick Sanchez"}}
119
134
..
120
135
121
- key
122
- ^^^
136
+ each
137
+ ^^^^
123
138
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() ``.
125
141
126
142
.. code-block :: python
127
143
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"}
130
151
..
131
152
132
- each
133
- ^^^^
134
153
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.
137
159
138
160
.. code-block :: python
139
161
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"}
144
169
..
145
170
146
- get
171
+ key
147
172
^^^
148
173
149
- To return data from a path simply call the `` get() `` method .
174
+ Calling `` key() `` returns the key for the query data .
150
175
151
176
.. code-block :: python
152
177
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
154
186
..
155
187
188
+
156
189
Conditional Requests
157
190
^^^^^^^^^^^^^^^^^^^^
158
191
@@ -167,9 +200,9 @@ conditional request.
167
200
168
201
.. code-block :: python
169
202
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)
173
206
..
174
207
175
208
If the passed ETag does not match the ETag of the path in the database,
@@ -187,8 +220,8 @@ successful:
187
220
188
221
.. code-block :: python
189
222
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)
192
225
193
226
if " ETag" in response:
194
227
etag = response[" ETag" ] # our ETag was out-of-date
0 commit comments