เมื่อคุณผสานรวม Cloud Functions เข้ากับโปรเจ็กต์ โค้ดอาจขยายใหญ่ขึ้นจนมีฟังก์ชันอิสระจำนวนมาก คุณอาจมีฟังก์ชันมากเกินกว่าที่จะใส่ลงในไฟล์เดียวได้อย่างสมเหตุสมผล หรือทีมต่างๆ อาจทำให้ใช้งานได้ซึ่งฟังก์ชันกลุ่มต่างๆ ซึ่งอาจทำให้ทีมหนึ่งเขียนทับหรือลบฟังก์ชันของอีกทีมหนึ่งโดยไม่ได้ตั้งใจ Cloud Functions มีวิธีต่างๆ ในการจัดระเบียบโค้ดเพื่อให้ง่ายต่อการนำทางและดูแลรักษาฟังก์ชัน
จัดระเบียบฟังก์ชันในฐานของโค้ด
คุณสามารถใช้พร็อพเพอร์ตี้ codebase ของออบเจ็กต์การกำหนดค่าฟังก์ชันใน firebase.json เพื่อจัดการฟังก์ชันจำนวนมากในที่เก็บหลายรายการหรือแพ็กเกจย่อยภายในที่เก็บเดียวในการตั้งค่า 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 ช่วยลดความซับซ้อนในการจัดการที่เก็บหลายรายการได้ ลองดูตัวอย่างกรณีที่คุณมีที่เก็บ 2 รายการที่แตกต่างกันซึ่งทำให้ฟังก์ชันใช้งานได้ในโปรเจ็กต์ 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 และมีทีมต่างๆ จัดการการกำหนดฟังก์ชันของตนเองในแพ็กเกจที่แยกกัน
- คุณมีฟังก์ชันที่มีทรัพยากร Dependency ภายนอกจำนวนมากและการเริ่มต้นใช้งานที่ใช้เวลานาน และต้องการแยกฟังก์ชันดังกล่าวออกจากฟังก์ชันอื่นๆ ที่ไวต่อเวลาในการตอบสนอง
หากต้องการรองรับการตั้งค่า monorepo เช่นนี้ ให้กำหนดการกำหนดค่าฟังก์ชันหลายรายการใน 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 คุณอาจใส่ฟังก์ชัน 2-3 ฟังก์ชันแรกในไฟล์เดียว
index.js
const functions = require('firebase-functions/v1');
exports.foo = functions.https.onRequest((request, response) => {
// ...
});
exports.bar = functions.https.onRequest((request, response) => {
// ...
});
main.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!")
การจัดการฟังก์ชันมากกว่า 2-3 ฟังก์ชันอาจทำได้ยาก คุณสามารถใส่ตรรกะทั้งหมดสำหรับแต่ละฟังก์ชันไว้ในไฟล์ของฟังก์ชันเอง และใช้ไฟล์แหล่งที่มาเป็นรายการการส่งออกแทน
Node.js
foo.js
const functions = require('firebase-functions/v1'); exports.foo = functions.https.onRequest((request, response) => { // ... });
bar.js
const functions = require('firebase-functions/v1'); 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!")
bar.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!")
main.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/v1'); 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 ได้ที่