当您将 Cloud Functions 集成到您的项目中时,您的代码可以扩展以包含许多独立的函数。您可能有太多功能无法合理地放入一个文件中,或者不同的团队可能会部署不同的功能组,从而产生一个团队覆盖或意外删除另一个团队的功能的风险。 Cloud Functions 提供了不同的方式来组织您的代码,以便更轻松地导航和维护您的函数。
在代码库中组织函数
您可以使用codebase
中函数配置对象的代码库属性来管理单个存储库firebase.json
设置中跨多个存储库或子包的大量函数:
# firebase.json
"functions": {
"codebase": "my-codebase"
# NOTE: Codebase must be less than 63 characters and can contain only
# lowercase letters, numeric characters, underscores, and dashes.
}
Firebase CLI v10.7.1 及更高版本支持codebase
属性。
管理多个存储库
codebase
属性可以帮助简化多个存储库的管理。让我们来看一个案例,您有两个不同的存储库将函数部署到同一个 Firebase 项目:
$ tree .
├── repoA
│ ├── firebase.json
│ └── functions
│ ├── index.js
│ └── package.json
└── repoB
├── firebase.json
└── functions
├── index.js
└── package.json
如果没有代码库注释,Firebase CLI 会提示您在部署时删除其他存储库中定义的函数:
$ (cd repoA && firebase deploy --only functions)
...
i functions: preparing functions directory for uploading...
✔ functions: functions folder uploaded successfully
The following functions are found in your project but do not exist in your local source code:
fn1FromRepoB
fn2FromRepoB
...
? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. (y/N)
您可以通过在每个项目存储库的firebase.json
的功能配置部分添加一个唯一的代码库注释来避免这个问题:
# repoA/firebase.json
"functions": {
"codebase": "repo-a"
}
# repoB/firebase.json
"functions": {
"codebase": "repo-b"
}
使用代码库注释,Firebase CLI 不再提示您删除在直接存储库之外定义的函数:
$ (cd repoA && firebase deploy --only functions)
...
i functions: preparing functions directory for uploading...
✔ functions: functions folder uploaded successfully
# Gleefully ignores functions from repoB
i functions: creating Node.js 16 function fnFromRepoA (us-central1)...
✔ Deploy Complete!
管理多个源包 (monorepo)
codebase
属性可以帮助简化单个存储库中多个源包的管理。让我们检查一个案例,您有一个 firebase 项目目录,其中的函数定义分布在几个子包中:
$ tree .
├── firebase.json
├── teamA
│ ├── index.js
│ └── package.json
└── teamB
├── index.js
└── package.json
此设置适合以下用例:
- 您有一个monorepo设置,并且有不同的团队在一个独立的包中管理他们自己的函数定义。
- 您有一个具有严重外部依赖性和长时间运行初始化的函数,并且希望将该函数与其他对延迟敏感的函数隔离开来。
要支持这样的 monrepo 设置,请在firebase.json
中定义多个函数配置:
"functions": [
{
"source": "teamA",
"codebase": "team-a"
},
{
"source": "teamB",
"codebase": "team-b"
},
]
使用此配置,Firebase CLI 在单个部署命令中部署所有包中的功能:
$ firebase deploy --only functions
i deploying functions
i functions: preparing codebase team-a for deployment
i functions: preparing codebase team-b for deployment
i functions: creating Node.js 16 function team-a:helloATeam(us-central1)...
i functions: creating Node.js 16 function team-b:helloBTeam(us-central1)...
...
您还可以部署特定的代码库:
$ firebase deploy --only functions:team-b
i deploying functions
i functions: preparing codebase team-b for deployment
i functions: updating Node.js 16 function team-b:helloBTeam(us-central1)...
...
在多个文件中编写函数
开始使用 Cloud Functions 时,您可能会将前几个函数放在一个文件中:
index.js
const functions = require('firebase-functions'); exports.foo = functions.https.onRequest((request, response) => { // ... }); exports.bar = functions.https.onRequest((request, response) => { // ... });
使用多个函数可能会变得难以管理。相反,您可以将每个函数的所有逻辑放在它自己的文件中,并将index.js
文件用作简单的导出列表:
foo.js
const functions = require('firebase-functions'); exports.foo = functions.https.onRequest((request, response) => { // ... });
bar.js
const functions = require('firebase-functions'); exports.bar = functions.https.onRequest((request, response) => { // ... });
index.js
const foo = require('./foo'); const bar = require('./bar'); exports.foo = foo.foo; exports.bar = bar.bar;
群组功能
在许多项目中,功能可以分成逻辑组,这些组应该一起部署和维护。例如,您可能有一组用于报告指标的函数:
metrics.js
const functions = require('firebase-functions'); exports.usageStats = functions.https.onRequest((request, response) => { // ... }); exports.nightlyReport = functions.https.onRequest((request, response) => { // ... });
在将它们导出到index.js
文件中时,您可以将这些函数放入一个组中:
index.js
// Export both functions from metrics.js in the "metrics" group: // - metrics-usageStats // - metrics-nightlyReport exports.metrics = require('./metrics');
部署时,函数将以其组名作为前缀,因此在此示例中,函数将被命名为metrics-usageStats
和metrics-nightlyReport
。
部署功能时,您可以将操作限制为单个组:
firebase deploy --only functions:metrics
下一步
要了解有关 Cloud Functions 的更多信息,请参阅: