أثناء قيامك بدمج وظائف السحابة في مشروعك ، يمكن أن تتوسع التعليمات البرمجية الخاصة بك لتحتوي على العديد من الوظائف المستقلة. قد يكون لديك عدد كبير جدًا من الوظائف بحيث لا يمكن وضعها في ملف واحد بشكل معقول ، أو قد تنشر فرق مختلفة مجموعات مختلفة من الوظائف ، مما يؤدي إلى خطر قيام فريق واحد بالكتابة فوق وظائف فريق آخر أو حذفه عن طريق الخطأ. تقدم وظائف السحابة طرقًا مختلفة لتنظيم التعليمات البرمجية الخاصة بك لتسهيل التنقل في وظائفك وصيانتها.
تنظيم الوظائف في قواعد البيانات
يمكنك استخدام خاصية 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)...
...
وظائف الكتابة في ملفات متعددة
عند بدء استخدام وظائف السحابة ، يمكنك وضع الوظائف القليلة الأولى في ملف واحد:
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
الخطوات التالية
لمعرفة المزيد حول وظائف السحابة ، راجع: