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


เมื่อผสานรวม 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.
}

Firebase CLI v10.7.1 ขึ้นไปรองรับพร็อพเพอร์ตี้ codebase

การจัดการที่เก็บหลายแหล่ง

พร็อพเพอร์ตี้ 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)

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

หากต้องการรองรับการตั้งค่า 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 คุณอาจใส่ฟังก์ชัน 2-3 ฟังก์ชันแรกในไฟล์เดียว

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

Node.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;

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: ต้องระบุ แต่เว้นว่างได้

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

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

เมตริก.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 โปรดดูที่