เมื่อคุณรวมฟังก์ชันระบบคลาวด์เข้ากับโปรเจ็กต์ของคุณ โค้ดของคุณอาจขยายให้มีฟังก์ชันอิสระมากมาย คุณอาจมีฟังก์ชันมากมายเกินกว่าที่จะใส่ลงในไฟล์เดียวได้อย่างสมเหตุสมผล หรือทีมที่แตกต่างกันอาจปรับใช้กลุ่มฟังก์ชันที่แตกต่างกัน ทำให้เกิดความเสี่ยงที่ทีมหนึ่งจะเขียนทับหรือลบฟังก์ชันของทีมอื่นโดยไม่ตั้งใจ 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
ช่วยลดความซับซ้อนในการจัดการที่เก็บข้อมูลหลายแห่ง เรามาตรวจสอบกรณีที่คุณมีที่เก็บสองแห่งที่แตกต่างกันซึ่งปรับใช้ฟังก์ชันกับโปรเจ็กต์ 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) => {
// ...
});
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 รายการ แต่คุณใส่ตรรกะทั้งหมดของคุณสำหรับแต่ละฟังก์ชันลงในไฟล์ของตัวเองแทนได้ และใช้ไฟล์ต้นฉบับเป็นรายการส่งออกได้:
โหนด 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;
หลาม
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!")
บาร์.ไพ
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'); 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
ขั้นตอนถัดไป
หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับฟังก์ชันคลาวด์ โปรดดู: