简体   繁体   English

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

[英]Access cloud Firestore database globally with FastAPI

I am using Firestore as my database with FastAPI.我使用 Firestore 作为我的 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.我能够在我的 app/server/app.py 文件中连接到云 Firestore 数据库,但我希望能够在配置后在其他文件中使用/编辑数据库。

I have tried using "Depends" and tried initializing it another file and importing it to desired files (without success).我尝试使用“Depends”并尝试将其初始化另一个文件并将其导入所需的文件(没有成功)。 In this example below, I have the startup function, which then calls "get_data()" which has been imported from another file.在下面的这个例子中,我启动了 function,然后调用从另一个文件导入的“get_data()”。 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.我希望“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})

It helps a lot to use the built in pydantic models for parsing.使用内置的 pydantic 模型进行解析有很大帮助。

Here is an example of how to get users by email:以下是如何通过 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 .注意* 将.setmerge=True一起使用与使用.update相同。

Also- Emulators do not work in the Python SDK as they require a NodeJs runtime.此外,模拟器在 Python SDK 中不起作用,因为它们需要 NodeJs 运行时。 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).更好的解决方案是使用 FastAPI 读取并点击 Cloud Function 端点进行写入(可以在本地 IP 上模拟这些端点)。

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

相关问题 Firebase 客户端对您的 Cloud Firestore 数据库的访问将在 3 天后到期 - Firebase Client access to your Cloud Firestore database expiring in 3 day(s) Cloud Firestore、nodejs(云函数)、同步访问 - Cloud Firestore , nodejs (cloud functions), synchronize access 无法从 Cloud Firestore 数据库下载图像 - Cannot download image from Cloud Firestore database Firebase 云功能不更新 Firestore 数据库中的数据 - Firebase Cloud function not updating data in Firestore database 使用 Cloud Firestore 数据库 API 创建云 Firestore 数据库,其中“database_id”为“(默认)”不起作用 - using Cloud Firestore Database API to create cloud firestore database with`database_id` of '(default)' not working 从 Cloud Firestore 数据库检索数据时出错 - Error retrieving data from Cloud Firestore database 在云 Firestore 数据库中注册和登录用户 - Register and SignIn users in cloud firestore database Cloud Firestore 数据库在身份验证后不显示用户? - Cloud Firestore Database is not displaying users after authentication? 如何在将数据作为实时数据库添加到 Cloud Firestore 之前获取 Cloud Firestore 文档的唯一 ID - How to get cloud firestore document unique Id before adding data to cloud firestore as Realtime database Cloud Firestore 的安全规则 - 用户只能访问他们自己的文档 - Security Rules for Cloud Firestore - User access only their own docs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM