Cloud Functions를 프로젝트에 통합하면 코드가 확장되어 많은 독립 함수를 포함할 수 있습니다. 하나의 파일에 합당하게 맞추기에는 함수가 너무 많거나, 여러 팀이 서로 다른 함수 그룹을 배포하여 한 팀이 덮어쓰거나 실수로 다른 팀의 함수를 삭제할 위험이 있습니다. Cloud Functions는 함수를 더 쉽게 탐색하고 유지 관리할 수 있도록 코드를 구성하는 다양한 방법을 제공합니다.
코드베이스에서 함수 구성
firebase.json
에 있는 functions 구성 객체의 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.
}
codebase
속성은 Firebase CLI v10.7.1 이상에서 지원됩니다.
여러 저장소 관리
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
이 설정은 다음 사용 사례에 적합합니다.
- 단일 리포지토리 설정이 있고 서로 다른 팀이 격리된 패키지에서 자체 함수 정의를 관리하도록 합니다.
- 외부 종속성이 높고 초기화가 오래 걸리는 함수가 있고 해당 함수를 지연 시간에 민감한 다른 함수와 분리하려고 합니다.
이와 같은 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;
그룹 기능
많은 프로젝트에서 기능을 함께 배포하고 유지 관리해야 하는 논리적 그룹으로 분리할 수 있습니다. 예를 들어 메트릭을 보고하는 데 사용되는 함수 그룹이 있을 수 있습니다.
메트릭스.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에 대해 자세히 알아보려면 다음을 참조하세요.