จัดระเบียบหลายฟังก์ชั่น


เมื่อคุณรวม 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 สามารถช่วยลดความซับซ้อนในการจัดการที่เก็บหลายแห่ง ลองตรวจสอบกรณีที่คุณมีที่เก็บสองแห่งที่ปรับใช้ฟังก์ชันกับโปรเจ็กต์ Firebase เดียวกัน:

$  tree .
├── repoA
│   ├── firebase.json
│   └── functions
│       ├── index.js
│       └── package.json
└── repoB
    ├── firebase.json
    └── functions
        ├── index.js
        └── package.json

หากไม่มีคำอธิบายประกอบ Codebase 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)

คุณสามารถหลีกเลี่ยงปัญหานี้ได้โดยเพิ่มคำอธิบายประกอบ codebase เฉพาะในส่วนการกำหนดค่าฟังก์ชันของ firebase.json ในแต่ละที่เก็บโครงการ:

# repoA/firebase.json
"functions": {
  "codebase": "repo-a"
}

# repoB/firebase.json
"functions": {
  "codebase": "repo-b"
}

ด้วยคำอธิบายประกอบของ Codebase 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)...
...

คุณยังสามารถปรับใช้ codebase เฉพาะ:

$ 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;

ฟังก์ชั่นกลุ่ม

ในหลายโปรเจ็กต์ ฟังก์ชันสามารถแยกออกเป็นกลุ่มตรรกะที่ควรปรับใช้และบำรุงรักษาร่วมกัน ตัวอย่างเช่น คุณอาจมีกลุ่มของฟังก์ชันที่ใช้สำหรับเมตริกการรายงาน:

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

ขั้นตอนถัดไป

หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับ Cloud Functions โปรดดูที่: