تنظيم دوال متعددة


أثناء دمج دوال Cloud في مشروعك، يمكن أن تتوسع التعليمة البرمجية لتحتوي على العديد من الدوال المستقلة. قد يكون لديك عدد كبير جدًا من الدوال التي لتلائم ملف واحد بشكل معقول، أو قد تنشر فرق مختلفة مجموعات مختلفة من الدوال، مما قد يؤدي إلى خطر استبدال فريق واحد أو حذف وظائف فريق آخر عن طريق الخطأ. توفر الدوال السحابية طرقًا مختلفة لتنظيم التعليمات البرمجية الخاصة بك لتسهيل التنقل والحفاظ على الدوال.

تنظيم الدوال في قواعد الرموز

يمكنك استخدام السمة codebase لكائن إعداد الدوال في firebase.json لإدارة مجموعة كبيرة من الدوال عبر مستودعات أو حِزم فرعية متعددة ضمن إعداد أحادي مستودع واحد:

# 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 في الإصدار 10.7.1 من واجهة سطر الأوامر في Firebase والإصدارات الأحدث.

إدارة المستودعات المتعددة

يمكن أن تساعد السمة codebase في تبسيط إدارة المستودعات المتعددة. لنفحص الحالة التي يكون لديك فيها مستودعان مختلفان ينشران وظائف إلى نفس مشروع Firebase:

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

بدون تعليقات قاعدة الترميز، كانت ستطلب منك واجهة سطر الأوامر في Firebase حذف الدوال المحدَّدة في المستودع الآخر وقت النشر:

$ (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 تطلب منك حذف الدوال المحدّدة خارج مستودعك الفوري:

$ (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 دوال من جميع الحِزم في أمر نشر واحد:

$ 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)...
...

كتابة الدوالّ في ملفات متعدّدة

عند بدء استخدام دوال السحابة، يمكنك وضع الدوال القليلة الأولى في ملف واحد:

ملف index.js

const functions = require('firebase-functions');
exports.foo = functions.https.onRequest((request, response) => {
  // ...
});
exports.bar = functions.https.onRequest((request, response) => {
  // ...
});

رئيسي.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!")

قد يصبح من الصعب إدارة هذا مع أكثر من بضع دوال. بدلاً من ذلك، يمكنك وضع كل المنطق لكل دالة في ملفها الخاص واستخدام ملف المصدر كقائمة من عمليات التصدير:

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: مطلوب، ولكن يمكن أن يكون فارغًا.

دوال المجموعات

في العديد من المشروعات، يمكن فصل الدوال إلى مجموعات منطقية يجب نشرها وصيانتها معًا. على سبيل المثال، قد يكون لديك مجموعة من الدوال المستخدمة لإعداد التقارير عن المقاييس:

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

الخطوات التالية

لمزيد من المعلومات عن دوال السحابة، راجع: