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

เมื่อผสานรวม 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 คุณอาจใส่ฟังก์ชัน 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 ได้ที่หัวข้อต่อไปนี้