Cloud Functions をプロジェクトに統合すると、コードが拡張されて多くの独立した関数が含まれる可能性があります。機能が多すぎて 1 つのファイルに適切に収まらない場合や、別のチームが別のグループの機能をデプロイする可能性があるため、あるチームが別のチームの機能を上書きしたり、誤って削除したりするリスクが生じます。 Cloud Functions は、関数のナビゲートと保守を容易にするためにコードを編成するさまざまな方法を提供します。
コードベースで関数を編成する
firebase.json の functions 構成オブジェクトのcodebase
プロパティを使用して、1 つのリポジトリ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.
}
codebase
プロパティは、Firebase CLI v10.7.1 以降でサポートされています。
複数のリポジトリの管理
codebase
プロパティは、複数のリポジトリの管理を簡素化するのに役立ちます。関数を同じ Firebase プロジェクトにデプロイする 2 つの異なるリポジトリがある場合を調べてみましょう。
$ 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 は単一の deploy コマンドですべてのパッケージから関数をデプロイします。
$ 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 を使い始めるとき、最初のいくつかの関数を 1 つのファイルに入れることができます。
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
nightlyReport になります。
関数をデプロイするときに、アクションを 1 つのグループに制限できます。
firebase deploy --only functions:metrics
次のステップ
Cloud Functions の詳細については、以下を参照してください。