Skip to content

Commit ac697bc

Browse files
authored
Add code snippets for firestore modules. (#628)
* Add code snippets for firestore modules. * fix: clarified snippet names and fixed newline. * fix: Removed var tags. These won't work as I intended it to since html is escaped when using includecode.
1 parent eb8226b commit ac697bc

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

snippets/firestore/__init__.py

Whitespace-only changes.

snippets/firestore/firestore.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2022 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from firebase_admin import firestore
16+
17+
# pylint: disable=invalid-name
18+
def init_firestore_client():
19+
# [START init_firestore_client]
20+
import firebase_admin
21+
from firebase_admin import firestore
22+
23+
# Application Default credentials are automatically created.
24+
app = firebase_admin.initialize_app()
25+
db = firestore.client()
26+
# [END init_firestore_client]
27+
28+
def init_firestore_client_application_default():
29+
# [START init_firestore_client_application_default]
30+
import firebase_admin
31+
from firebase_admin import credentials
32+
from firebase_admin import firestore
33+
34+
# Use the application default credentials.
35+
cred = credentials.ApplicationDefault()
36+
37+
firebase_admin.initialize_app(cred)
38+
db = firestore.client()
39+
# [END init_firestore_client_application_default]
40+
41+
def init_firestore_client_service_account():
42+
# [START init_firestore_client_service_account]
43+
import firebase_admin
44+
from firebase_admin import credentials
45+
from firebase_admin import firestore
46+
47+
# Use a service account.
48+
cred = credentials.Certificate('path/to/serviceAccount.json')
49+
50+
app = firebase_admin.initialize_app(cred)
51+
52+
db = firestore.client()
53+
# [END init_firestore_client_service_account]
54+
55+
def read_data():
56+
import firebase_admin
57+
from firebase_admin import firestore
58+
59+
app = firebase_admin.initialize_app()
60+
db = firestore.client()
61+
62+
# [START read_data]
63+
doc_ref = db.collection('users').document('alovelace')
64+
doc = doc_ref.get()
65+
if doc.exists:
66+
return f'data: {doc.to_dict()}'
67+
return "Document does not exist."
68+
# [END read_data]
69+
70+
def add_data():
71+
import firebase_admin
72+
from firebase_admin import firestore
73+
74+
app = firebase_admin.initialize_app()
75+
db = firestore.client()
76+
77+
# [START add_data]
78+
doc_ref = db.collection("users").document("alovelace")
79+
doc_ref.set({
80+
"first": "Ada",
81+
"last": "Lovelace",
82+
"born": 1815
83+
})
84+
# [END add_data]

snippets/firestore/firestore_async.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Copyright 2022 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import asyncio
16+
17+
from firebase_admin import firestore_async
18+
19+
# pylint: disable=invalid-name
20+
def init_firestore_async_client():
21+
# [START init_firestore_async_client]
22+
import firebase_admin
23+
from firebase_admin import firestore_async
24+
25+
# Application Default credentials are automatically created.
26+
app = firebase_admin.initialize_app()
27+
db = firestore_async.client()
28+
# [END init_firestore_async_client]
29+
30+
def init_firestore_async_client_application_default():
31+
# [START init_firestore_async_client_application_default]
32+
import firebase_admin
33+
from firebase_admin import credentials
34+
from firebase_admin import firestore_async
35+
36+
# Use the application default credentials.
37+
cred = credentials.ApplicationDefault()
38+
39+
firebase_admin.initialize_app(cred)
40+
db = firestore_async.client()
41+
# [END init_firestore_async_client_application_default]
42+
43+
def init_firestore_async_client_service_account():
44+
# [START init_firestore_async_client_service_account]
45+
import firebase_admin
46+
from firebase_admin import credentials
47+
from firebase_admin import firestore_async
48+
49+
# Use a service account.
50+
cred = credentials.Certificate('path/to/serviceAccount.json')
51+
52+
app = firebase_admin.initialize_app(cred)
53+
54+
db = firestore_async.client()
55+
# [END init_firestore_async_client_service_account]
56+
57+
def close_async_sessions():
58+
import firebase_admin
59+
from firebase_admin import firestore_async
60+
61+
# [START close_async_sessions]
62+
app = firebase_admin.initialize_app()
63+
db = firestore_async.client()
64+
65+
# Perform firestore tasks...
66+
67+
# Delete app to ensure that all the async sessions are closed gracefully.
68+
firebase_admin.delete_app(app)
69+
# [END close_async_sessions]
70+
71+
async def read_data():
72+
import firebase_admin
73+
from firebase_admin import firestore_async
74+
75+
app = firebase_admin.initialize_app()
76+
db = firestore_async.client()
77+
78+
# [START read_data]
79+
doc_ref = db.collection('users').document('alovelace')
80+
doc = await doc_ref.get()
81+
if doc.exists:
82+
return f'data: {doc.to_dict()}'
83+
# [END read_data]
84+
85+
async def add_data():
86+
import firebase_admin
87+
from firebase_admin import firestore_async
88+
89+
app = firebase_admin.initialize_app()
90+
db = firestore_async.client()
91+
92+
# [START add_data]
93+
doc_ref = db.collection("users").document("alovelace")
94+
await doc_ref.set({
95+
"first": "Ada",
96+
"last": "Lovelace",
97+
"born": 1815
98+
})
99+
# [END add_data]
100+
101+
def firestore_async_client_with_asyncio_eventloop():
102+
# [START firestore_async_client_with_asyncio_eventloop]
103+
import asyncio
104+
import firebase_admin
105+
from firebase_admin import firestore_async
106+
107+
app = firebase_admin.initialize_app()
108+
db = firestore_async.client()
109+
110+
# Create coroutine to add user data.
111+
async def add_data():
112+
doc_ref = db.collection("users").document("alovelace")
113+
print("Start adding user...")
114+
await doc_ref.set({
115+
"first": "Ada",
116+
"last": "Lovelace",
117+
"born": 1815
118+
})
119+
print("Done adding user!")
120+
121+
# Another corutine with secondary tasks we want to complete.
122+
async def while_waiting():
123+
print("Start other tasks...")
124+
await asyncio.sleep(2)
125+
print("Finished with other tasks!")
126+
127+
# Initialize an eventloop to execute tasks until completion.
128+
loop = asyncio.get_event_loop()
129+
tasks = [add_data(), while_waiting()]
130+
loop.run_until_complete(asyncio.gather(*tasks))
131+
firebase_admin.delete_app(app)
132+
# [END firestore_async_client_with_asyncio_eventloop]

0 commit comments

Comments
 (0)