【发布时间】:2020-01-10 07:41:44
【问题描述】:
我希望能够在本地运行云功能并针对生产数据的副本进行调试。 有没有办法将在线的数据复制到本地firestore模拟器?
【问题讨论】:
-
请不要导入您的生产数据库进行测试。当您这样做时,您可能会暴露所有客户的个人数据。相反,创建经过清理的暂存数据。
标签: google-cloud-firestore google-cloud-functions firebase-tools
我希望能够在本地运行云功能并针对生产数据的副本进行调试。 有没有办法将在线的数据复制到本地firestore模拟器?
【问题讨论】:
标签: google-cloud-firestore google-cloud-functions firebase-tools
这可以通过现有项目的终端中的一组命令来完成:
1.登录 firebase 和 Gcloud:
firebase login
gcloud auth login
2。查看您的项目列表并连接到一个:
firebase projects:list
firebase use your-project-name
gcloud projects list
gcloud config set project your-project-name
3。使用所选名称将您的生产数据导出到 gcloud 存储桶:
gcloud firestore export gs://your-project-name.appspot.com/your-choosen-folder-name
4.现在将此文件夹复制到您的本地计算机,我直接在函数文件夹中执行此操作:
cd functions
gsutil -m cp -r gs://your-project-name.appspot.com/your-choosen-folder-name .
5.现在我们只想导入这个文件夹。这应该与基本命令一起使用,感谢 Firebase 团队的最新更新 https://github.com/firebase/firebase-tools/pull/2519.
firebase emulators:start --import ./your-choosen-folder-name
查看我在 Medium 上关于它的文章以及为您完成这项工作的简写脚本https://medium.com/firebase-developers/how-to-import-production-data-from-cloud-firestore-to-the-local-emulator-e82ae1c6ed8
注意:最好为它使用不同的存储桶,因为复制到您的项目存储桶中会导致在您的 Firebase 存储中创建文件夹。
如果您对 -m 这样的 gsutil 参数感兴趣,您可以通过执行 gsutil --help 来查看它们的描述。
【讨论】:
firebase emulators:start --import your-choosen-folder-name ?你能分享错误信息吗?只是想看看 firebase 是在抛出超时还是我们可以增加的某种内部超时。
我的方法有点手动,但确实有效。我已经在this useful Github thread 中分享了它,但如果你觉得它们有用,我会在这里列出我所做的步骤:
firebase emulators:start
emulators:export ./mydirectory
gcloud firestore export gs://my-project-bucket-id.appspot.com --collection-ids=myCollection 现在导出位于 Firebase Storage 下以时间戳为名称的文件夹(我的测试没有使用前缀)gsutil cp -r gs://my-project-bucket-id.appspot.com/myCollection ./production_data_export 注意:我是在 Windows 环境中执行此操作的... gsutil 将抛出此错误:"OSError: The filename, directory name , 或卷标语法不正确” 如果该文件夹在 Windows 中包含无效的文件夹名称字符(即冒号)或此错误:“OSError: Invalid argument.9.0 B]” 如果文件夹中的内部文件也包含无效字符。为了能够在本地下载导出文件,请使用有效的 Windows 名称重命名它们(即删除冒号),如下所示:gsutil mv gs://my-project-bucket-id.appspot.com/2020-05-22T02:01:06_86152 gs://my-project-bucket-id.appspot.com/myCollection
firestore_export 并从本地导出文件夹中复制firebase-export-metadata.json 文件。只是为了直观,这是我得到的结构:$ tree .
.
├── local_data_export
│ ├── firebase-export-metadata.json
│ └── firestore_export
│ ├── all_namespaces
│ │ └── all_kinds
│ │ ├── all_namespaces_all_kinds.export_metadata
│ │ └── output-0
│ └── firestore_export.overall_export_metadata
└── production_data_export
├── firebase-export-metadata.json
└── firestore_export
├── all_namespaces
│ └── kind_myCollection
│ ├── all_namespaces_kind_myCollection.export_metadata
│ ├── output-0
│ └── output-1
└── firestore_export.overall_export_metadata
8 directories, 9 files
firebase emulators:start --import=./mock_up_data/production_data_export/
这应该对读者有所帮助,同时我们等待 Firebase 人员提供更强大的解决方案。
【讨论】:
firebase emulators:export ./mydirectory。我知道这很明显,但我确实困惑了一分钟。
您可以使用firestore-backup-restore 将生产数据导出和导入为 JSON 文件。
我写了一个快速技巧来允许在 Firebase Simulator Firestore 实例中导入这些 JSON。
我提出了一个拉取请求,同时提出了这个npm module。
你可以这样使用它:
const firestoreService = require('@crapougnax/firestore-export-import')
const path = require('path')
// list of JSON files generated with the export service
// Must be in the same folder as this script
const collections = ['languages', 'roles']
// Start your firestore emulator for (at least) firestore
// firebase emulators:start --only firestore
// Initiate Firebase Test App
const db = firestoreService.initializeTestApp('test', {
uid: 'john',
email: 'john@doe.com',
})
// Start importing your data
let promises = []
try {
collections.map(collection =>
promises.push(
firestoreService.fixtures(
path.resolve(__dirname, `./${collection}.json`),
[],
[],
db,
),
),
)
Promise.all(promises).then(process.exit)
} catch (err) {
console.error(err)
}
显然,由于这些数据不会保留在模拟器中,因此您通常会将它们注入测试套件的 before() 函数中,甚至在每次测试之前。
【讨论】:
@google-cloud/firestore 的引用;我可以用npm run build 构建它,然后用node ./build/index.js 导出信息。
没有将数据从云项目复制到本地模拟器的内置方法。由于模拟器不保留任何数据,因此您必须在每次运行时重新生成初始数据集。
【讨论】:
我能够制作一些 npm 脚本以从远程导入到本地模拟器,反之亦然。
"serve": "yarn build && firebase emulators:start --only functions,firestore --import=./firestore_export",
"db:update-local-from-remote": "yarn db:backup-remote && gsutil -m cp -r gs://my-firebase-bucket.appspot.com/firestore_export .",
"db:update-remote-from-local": "yarn db:backup-local && yarn db:backup-remote && gsutil -m cp -r ./firestore_export gs://my-firebase-bucket.appspot.com && yarn run db:import-remote",
"db:import-remote": "gcloud firestore import gs://my-firebase-bucket.appspot.com/firestore_export",
"db:backup-local": "firebase emulators:export --force .",
"db:rename-remote-backup-folder": "gsutil mv gs://my-firebase-bucket.appspot.com/firestore_export gs://my-firebase-bucket.appspot.com/firestore_export_$(date +%d-%m-%Y-%H-%M)",
"db:backup-remote": "yarn db:rename-remote-backup-folder && gcloud firestore export gs://my-firebase-bucket.appspot.com/firestore_export"
因此您可以使用以下方式将本地 Firestore 数据导出到远程:
npm db:update-remote-from-local
或者要使用远程更新本地 Firestore 数据,请执行以下操作:
npm db:update-local-from-remote
这些操作将备份远程 Firestore 数据,制作副本并将其存储在 Firebase 存储中。
【讨论】:
db:update-local-from-remote 之后,您只需要重新启动您的模拟器。 github.com/firebase/firebase-tools/pull/1968
我正准备向firebase-tools 添加一个cli 选项,但对node-firestore-import-export 包非常满意。
yarn add -D node-firestore-import-export
"scripts": {
"db:export": "firestore-export -a ./serviceAccountKey.json -b ./data/firestore.json",
"db:import": "firestore-import -a ./serviceAccountKey.json -b ./data/firestore.json",
"db:emulator:export": "export FIRESTORE_EMULATOR_HOST=localhost:8080 && yarn db:export",
"db:emulator:import": "export FIRESTORE_EMULATOR_HOST=localhost:8080 && yarn db:import",
"db:backup": "cp ./data/firestore.json ./data/firestore-$(date +%d-%m-%Y-%H-%M).json",
"dev": "firebase emulators:start --import=./data --export-on-exit=./data",
},
您需要在 Firebase 控制台中创建一个服务帐号。
您可以将GCLOUD_PROJECT 环境变量替换为硬编码值。
open https://console.firebase.google.com/project/$GCLOUD_PROJECT/settings/serviceaccounts/adminsdk
mv ~/Downloads/myProjectHecticKeyName.json ./serviceAccountKey.json
话虽如此,gcloud 工具绝对是生产环境的最佳选择,因为无论如何您都需要 s3 备份。
【讨论】:
我写了一个小脚本来做到这一点:
const db = admin.firestore();
const collections = ['albums', 'artists'];
let rawData: any;
for (const i in collections) {
rawData = fs.readFileSync(`./${collections[i]}.json`);
const arr = JSON.parse(rawData);
for (const j in arr) {
db.collection(collections[i]).add(arr[j])
.then(val => console.log(val))
.catch(err => console.log('ERRO: ', err))
}
}
【讨论】: