繁体   English   中英

使用 FastAPI 在全球范围内访问 Cloud Firestore 数据库

[英]Access cloud Firestore database globally with FastAPI

我使用 Firestore 作为我的 FastAPI 数据库。 我能够在我的 app/server/app.py 文件中连接到云 Firestore 数据库,但我希望能够在配置后在其他文件中使用/编辑数据库。

我尝试使用“Depends”并尝试将其初始化另一个文件并将其导入所需的文件(没有成功)。 在下面的这个例子中,我启动了 function,然后调用从另一个文件导入的“get_data()”。 我希望“get_data()”(以及其他文件中的任何其他函数)能够编辑已配置一次的数据库。

#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})

使用内置的 pydantic 模型进行解析有很大帮助。

以下是如何通过 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

以下是如何更新用户的示例:

    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)

注意* 将.setmerge=True一起使用与使用.update相同。

此外,模拟器在 Python SDK 中不起作用,因为它们需要 NodeJs 运行时。 这是可能的,但非常困难。 更好的解决方案是使用 FastAPI 读取并点击 Cloud Function 端点进行写入(可以在本地 IP 上模拟这些端点)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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