为Firebase组织云功能 [英] Organize Cloud Functions for Firebase

查看:37
本文介绍了为Firebase组织云功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

组织用于Firebase的所有Cloud Functions的最佳实践是什么?

What is the best practice to organize all our Cloud Functions for Firebase?

我从示例GitHub存储库中看到,所有功能都驻留在单个index.js中文件.

I see from the sample GitHub repository that all functions reside in a single index.js file.

我猜对于更大的项目,有一种更好的方法可以在不同的文件/目录中组织Firebase的Cloud Functions.

I guess for bigger project that there is a better approach to organize Cloud Functions for Firebase in different files/directory.

推荐答案

我按提供者和资源在名为triggers的文件夹中组织事件处理程序.例如.其中auth是提供者,而user是资源;文件夹functions/triggers/auth/user包含onCreate.jsonDelete.js,分别欢迎和清理用户.

I organize my event handlers by provider and resource in a folder called triggers. E.g. where auth is the provider and user is the resource; the folder functions/triggers/auth/user contains an onCreate.js and onDelete.js, which welcomes and cleans up a user respectively.

+--/auth
|  +--/user
|     +--/onCreate.js
|     +--/onDelete.js
+--/database
+--/storage

您可以使用require函数导出特定的触发器:

You can export a particular trigger by using the require function:

exports.onCreateAuthUser = require('./triggers/auth/user/onCreate');    
exports.onDeleteAuthUser = require('./triggers/auth/user/onDelete');

我走了一步,创建了一个脚本,该脚本会自动为我导出功能.我将文件的扩展名更改为f.js,然后递归搜索triggers目录.对于找到的每个文件,通过分解目录和文件路径来构造函数名称.

I went a step further and created a script that automatically exports the functions for me. I change the extension of the files to f.js and search recursively the triggers directory. For each file found, the function name is concocted by breaking down the directory and file path.

此结构的灵感来自检查firebase-functions npm软件包的内部.

This structure was inspired by inspecting the internals of the firebase-functions npm package.

这篇关于为Firebase组织云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆