问题描述
通过以下方式启动Firestore模拟器有何区别?
firebase emulators:start --only firestore
和:
gcloud beta emulators firestore start
两个选项都允许我的python应用通过以下方式实现与数据库的连接:
import google
from google.cloud import firestore
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8081"
os.environ["FIRESTORE_EMULATOR_HOST_PATH"] = "localhost:8081/firestore"
os.environ["FIRESTORE_HOST"] = "http://localhost:8081"
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
client = firestore.Client(credentials=credentials)
我注意到自己的一个不同之处是firebase
似乎尊重我的firebase.json
,尤其是这样指定的主机端口:
{
"emulators": {
"firestore": {
"port": "8081"
}
}
}
另一方面,除非我明确通过gcloud
传递端口,否则firebase.json
会忽略--host-port
而是选择一个随机端口。这是两者之间更大差异的一部分吗?还有其他差异吗?
解决方法
我一直在寻找这两个工具的文档,它们几乎做同样的事情。
使用Firebase tool可以启动多个Firebase产品的仿真器,而gcloud command则可以启动GCP仿真器。 Firestore只是它们共同的一种产品,因此其实用程序应该相同或相似。
关于功能差异,firebase
提供了--import
和--export-on-exit
标志,使您可以在模拟会话之间保存和恢复数据。它还提供了一种可视化security rules如何处理当前查询的方式。
除了这些功能之外,我还要注意设置端口和规则文件的不同方法:
-
firebase emulators
使用firebase.json
文件。 -
gcloud beta emulators
使用标志--host-port
和--rules
实现相同的功能。
请注意,GCP上的Firestore emulator位于beta stage上,因此它的官方支持可能有限,并且可能会发生变化。还要注意如何在GCP's Firestore documentation上使用Firebase CLI代替gcloud。
最后,您应该使用自己喜欢的工具,因为它们都朝着模拟Firestore的相同目标努力。如果您已经在使用Firebase CLI,建议您继续使用它。如果您使用的是gcloud
,请使用它。