简体   繁体   中英

Access cloud Firestore database globally with FastAPI

I am using Firestore as my database with FastAPI. I am able to connect to the cloud Firestore database in my app/server/app.py file, but I want to be able to use/edit the database in other files after it is configured.

I have tried using "Depends" and tried initializing it another file and importing it to desired files (without success). In this example below, I have the startup function, which then calls "get_data()" which has been imported from another file. I would like to "get_data()" (as well as any other functions in other files) to be able to edit the database which has been configured once.

#app/server/app.py
from fastapi import FastAPI
from app.server.routes.users import router as User
from app.server.routes.users import get_data
import firebase_admin
import os
from firebase_admin import credentials, firestore, initialize_app, auth

os.environ["FIREBASE_AUTH_EMULATOR_HOST"] =  "127.0.0.1:9099"
os.environ["FIRESTORE_EMULATOR_HOST"] = "127.0.0.1:8080"

app = FastAPI()
app.include_router(User, tags=["User"], prefix="/a1")

#initialize database
cred = credentials.Certificate("path_to_file.json") 
firebase_admin.initialize_app(cred) 
db = firestore.client()

@app.on_event("startup")
async def start_up():
    await get_data()
#app/server/routes/users.py
from fastapi import APIRouter, HTTPException, Request
from fastapi_utils.tasks import repeat_every

router = APIRouter()

@repeat_every(seconds=5, max_repetitions=1)
async def get_data():
    #would like to be able to add/edit database here...for example:
    #db.collection("collection1").document("doc1").set({"a":1})

It helps a lot to use the built in pydantic models for parsing.

Here is an example of how to get users by email:

    async def get_user_by_email(self, email: str) -> FirestoreUser:
        """Retrieves a user from Firestore with the given email address.
        Args:
            email (str): users email
        Raises:
            `firebase_admin.exceptions`: [Firebase Admin Exceptions](https://firebase.google.com/docs/reference/admin/python/firebase_admin.exceptions)
            Exception: Multiple users found at email address
            Exception: No users found at email address
        Returns:
            FirestoreUser: Firebase 10X User object
        """
        users_found: List[FirestoreUser] = []
        user: FirestoreUser = {}

        user_docs = self.db.collection("users").where("email", "==", email).stream()
        for doc in user_docs:
            users_found.append(doc.to_dict())
        if len(users_found) > 1:
            raise Exception("Multiple users found at email address ", email)
        if len(users_found) == 0:
            raise Exception("No user found at email address ", email)
        user = users_found[0]
        return user

Here is an example of how to update a user:

    async def update_user(self, userData, userId):
        """Update an individual user
        Args:
            user (User): User to be updated
            userId (str): User ID
        Raises:
            `firebase_admin.exceptions`: [Firebase Admin Exceptions](https://firebase.google.com/docs/reference/admin/python/firebase_admin.exceptions)
        """
        user_ref = self.db.collection("courses").document(userId)
        user_ref.set(userData.dict(), merge=True)

Note * Using .set with merge=True is the same as using .update .

Also- Emulators do not work in the Python SDK as they require a NodeJs runtime. It's possible but very difficult. A better solution would be to use FastAPI to read and hit Cloud Function endpoints to write (Those can be emulated on a local IP).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM