[英]can I make a Cloud Function that only for local Firebase Emulators?
I need to make a function that will be used only for Firebase emulator.我需要制作一个仅用于 Firebase 仿真器的 function。 I need to perform some actions to my Firestore emulators, to populate Firestore emulators using dummy data.
我需要对我的 Firestore 模拟器执行一些操作,以使用虚拟数据填充 Firestore 模拟器。
I write this code in my index.ts
我在我的
index.ts
中写了这段代码
exports.onlyForFirebaseSimulator = functions.https.onRequest(async (req, res) => {
});
I want to make this function available when I run firebase emulators:start
, but it will not be deployed to production when I run firebase deploy
.我想让这个 function 在我运行
firebase emulators:start
时可用,但是当我运行firebase deploy
时它不会部署到生产中。
I have to comment it out manually before deployment.我必须在部署之前手动将其注释掉。 I don't want to manually set it, because it is error prone.
我不想手动设置它,因为它容易出错。
If you are deploying with the Firebase CLI deploy command you could select only the functions to deploy to production by specifying the --only flag as explained on the relevant sections of the docs : eg if you have three functions named addMessage, makeUppercase, and onlyForFirebaseSimulator running the following command:如果您使用Firebase CLI 部署命令进行部署,您可以 select 通过指定 --only 标志仅部署到生产环境,如文档的相关部分所述:例如,如果您有三个名为 addMessage、makeUppercase 和 onlyForFirebaseSimulator 的函数运行以下命令:
firebase deploy --only functions:addMessage,functions:makeUppercase
will not deploy your onlyForFirebaseSimulator to production.不会将您的 onlyForFirebaseSimulator 部署到生产环境。
I think I find the answer.我想我找到了答案。
I am using group functions to organize multiple functions in my index.ts
, I suggest you to read it first, otherwise you will be confused to read my index.ts
.我在我的
index.ts
中使用分组函数来组织多个函数,建议你先阅读它,否则你会混淆阅读我的index.ts
。 I am using Typescript/ES6, but the concept will be the same我使用的是 Typescript/ES6,但概念是一样的
for 'normal' deployment, in index.ts
will be like this对于“正常”部署,在
index.ts
中将是这样的
// Firestore triggers
export * as users from "./firestore/triggers/users/users_triggers";
export * as users_attendedEvents from "./firestore/triggers/users/attendedEvents/attended_events_triggers";
export * as users_followers from "./firestore/triggers/users/followers/followers_triggers";
// Firebase Authentication triggers
export * as auth from "./firebase_authentication/triggers/firebase_auth_triggers";
and now I want to add functions that will not be deployed on production (when I run firebase deploy
) but will be available when emulators is running (when I run firebase emulators:start)现在我想添加不会在生产中部署的功能(当我运行
firebase deploy
时)但在模拟器运行时可用(当我运行 firebase emulators:start 时)
to detect whether the emulators is running or not, we can check its environment variable, process.env.FUNCTIONS_EMULATOR
will be true.要检测模拟器是否正在运行,我们可以检查它的环境变量,
process.env.FUNCTIONS_EMULATOR
是否为真。 check this answer for more info检查此答案以获取更多信息
so my full index.ts
will be like this所以我的完整
index.ts
将是这样的
// Firestore triggers
export * as users from "./firestore/triggers/users/users_triggers";
export * as users_attendedEvents from "./firestore/triggers/users/attendedEvents/attended_events_triggers";
export * as users_followers from "./firestore/triggers/users/followers/followers_triggers";
// Firebase Authentication triggers
export * as auth from "./firebase_authentication/triggers/firebase_auth_triggers";
// The code below will be exported only if emulators are running, to avoid deployment to production server
const isRunningOnEmulator = (process.env.FUNCTIONS_EMULATOR && process.env.FIRESTORE_EMULATOR_HOST);
import * as emulator_functions from "./utilities/emulators/http_triggers/for_testing_via_firestore_emulators";
export const forEmulator = isRunningOnEmulator ? emulator_functions : undefined;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.