當您將 Cloud Functions 整合到專案中時,您的程式碼可以擴展為包含許多獨立的函數。您可能擁有太多函數而無法合理地放入單一檔案中,或者不同的團隊可能部署不同的函數群組,從而產生一個團隊覆蓋或意外刪除另一個團隊的函數的風險。 Cloud Functions 提供了不同的方式來組織程式碼,以便更輕鬆地導航和維護函數。
在程式碼庫中組織函數
您可以使用firebase.json
中函數配置物件的codebase
屬性來管理跨多個儲存庫或單一儲存庫 monorepo 設定中的子包的大量函數:
# 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 時,您可以將前幾個函數放在一個檔案中:
索引.js
const functions = require('firebase-functions');
exports.foo = functions.https.onRequest((request, response) => {
// ...
});
exports.bar = functions.https.onRequest((request, response) => {
// ...
});
主要.py
from firebase_functions import https_fn
@https_fn.on_request()
def foo(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello foo!")
@https_fn.on_request()
def bar(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello bar!")
如果功能較多,這可能會變得難以管理。相反,您可以將每個函數的所有邏輯放入自己的文件中,並使用原始檔案作為匯出清單:
Node.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;
Python
foo.py
from firebase_functions import https_fn
@https_fn.on_request()
def foo(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello foo!")
酒吧.py
from firebase_functions import https_fn
@https_fn.on_request()
def bar(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello foo!")
主要.py
from fn_impl.foo import *
from fn_impl.bar import *
此設定假定項目目錄結構如下:
my-project
├── firebase.json
└── functions
├── fn_impl
│ ├── __init__.py
│ ├── foo.py
│ └── bar.py
├── main.py
└── requirements.txt
fn_impl
:可以有任何名稱
__init__.py
:必需,但可以為空
集團職能
在許多專案中,功能可以分為應一起部署和維護的邏輯群組。例如,您可能有一組用於報告指標的函數:
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
下一步
要了解有關雲函數的更多信息,請參閱: