เมื่อผสานรวม 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 และมีทีมต่างๆ จัดการคำจำกัดความฟังก์ชันของตนเองในแพ็กเกจที่แยกต่างหาก
- คุณมีฟังก์ชันที่มีการอ้างอิงภายนอกจำนวนมากและการเริ่มต้นที่ใช้เวลานาน และต้องการแยกฟังก์ชันนั้นออกจากฟังก์ชันอื่นๆ ที่มีความละเอียดอ่อนต่อเวลาในการตอบสนอง
หากต้องการรองรับการตั้งค่า 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 คุณอาจใส่ฟังก์ชันแรกๆ ไว้ในไฟล์เดียว
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 ได้ที่