首页 > 解决方案 > 我可以在本地运行 Firebase 函数吗?

问题描述

可以firebase-functions从我的笔记本电脑上使用吗?如果没有,是firebase-admin唯一的选择吗?

这里有些例子:

我可以index.js在我的笔记本电脑上创建一个文件,npm installfirebase-admin模块,链接到我的 Firestore 数据库并对数据进行更改就好了,使用admin-credentials. 当我也尝试 npm installfirebase-functions使用 event-triggersonCreate/onWrite/onUpdate/onDelete时,他们没有得到任何更新?

据我了解,使用事件触发器的唯一方法是上传到云函数,因为您需要 Google 的基础设施来使用这些函数,而您不能在本地机器上使用它们,而您可以使用 package.json 来使用它们firebase-admin。您可以使用本地仿真器(?),但它还没有准备好生产并且不适用于该用例(?)。

因此,为了在我的 Firestore 数据库上侦听新事件,仅使用我的笔记本电脑(不是 Google Cloud Functions 平台或其他一些服务器托管选项),我必须.onSnapshot()firebase-adminnpm 使用。

但是,该模块无法缓存,您只能查询整个 firestore 数据库,下载每个文档。

这个对吗?或者有什么方法可以firebase-functions使用firebase-admin+管理员凭据从我的笔记本电脑服务器上工作,就像我将文件上传到云平台一样。我不需要这部分数据在云端,所以我想从笔记本电脑的终端进行更改和调整 firestore 数据库。

标签: firebasegoogle-cloud-platformgoogle-cloud-firestoregoogle-cloud-functions

解决方案


您将需要平衡可扩展性和您想要实现的目标。一种方法是使用Parse Server和与 Express 配合使用的Docker容器。这种方法的优点是它可以灵活地决定解析服务器可以在哪里运行。如果您需要更多处理能力,您可以在笔记本电脑上运行它或将其移至 Google Cloud。但是,值得注意的是容器无法访问所有 Firebase 触发器类型。

我不确定您使用的是哪个操作系统,但对于 Ubuntu,您可以像这样安装 Docker 并运行 Parse Server:

# Update the apt package first 
$ sudo apt-get update

# install dependencies
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common \ 
    git

# Add Docker’s GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# add the Docker repository
$  sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# Update the apt package again
$ sudo apt-get update

# Install Docker
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

$ git clone https://github.com/parse-community/parse-server
$ cd parse-server
$ docker build --tag parse-server .
$ docker run --name my-mongo -d mongo

要运行解析服务器:

$ docker run --name my-parse-server -v config-vol:/parse-server/config \
-p 1337:1337 --link my-mongo:mongo -d parse-server --appId APPLICATION_ID \
--masterKey MASTER_KEY --databaseURI mongodb://mongo/test

要链接 Firebase 和 Docker Parse Server,您需要一个适配器。上面的容器是一个示例,但它应该足以让您开始在笔记本电脑上运行。


推荐阅读