我想为Firebase创建多个云功能,并从一个项目同时部署它们。我还想将每个函数分离到一个单独的文件中。目前,我可以创建多个函数,如果我把它们都放在index.js,如:
exports.foo = functions.database.ref('/foo').onWrite(event => {
...
});
exports.bar = functions.database.ref('/bar').onWrite(event => {
...
});
然而,我想把foo和酒吧在单独的文件。我试了一下:
/functions
|--index.js (blank)
|--foo.js
|--bar.js
|--package.json
foo.js在哪里
exports.foo = functions.database.ref('/foo').onWrite(event => {
...
});
bar.js是
exports.bar = functions.database.ref('/bar').onWrite(event => {
...
});
有没有一种方法可以在不把所有函数都放在index.js中的情况下实现这一点?
Node 8 LTS现在可以与Cloud/Firebase函数一起使用,您可以使用扩展操作符执行以下操作:
/ package.json
"engines": {
"node": "8"
},
/ index.js
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
module.exports = {
...require("./lib/foo.js"),
// ...require("./lib/bar.js") // add as many as you like
};
/lib/foo.js
const functions = require("firebase-functions");
const admin = require("firebase-admin");
exports.fooHandler = functions.database
.ref("/food/{id}")
.onCreate((snap, context) => {
let id = context.params["id"];
return admin
.database()
.ref(`/bar/${id}`)
.set(true);
});
啊,Firebase负载节点模块的云函数通常,所以这是有效的
结构:
/functions
|--index.js
|--foo.js
|--bar.js
|--package.json
index.js:
const functions = require('firebase-functions');
const fooModule = require('./foo');
const barModule = require('./bar');
exports.foo = functions.database.ref('/foo').onWrite(fooModule.handler);
exports.bar = functions.database.ref('/bar').onWrite(barModule.handler);
foo.js:
exports.handler = (event) => {
...
};
bar.js:
exports.handler = (event) => {
...
};
org大纲是一个更简单的体系结构模式,可以将方法分离到不同的文件中,并在index.js文件中的一行中导出。
本示例中项目的架构如下:
projectDirectory
index.js
podcast.js
profile.js
index.js
const admin = require('firebase-admin');
const podcast = require('./podcast');
const profile = require('./profile');
admin.initializeApp();
exports.getPodcast = podcast.getPodcast();
exports.removeProfile = profile.removeProfile();
podcast.js
const functions = require('firebase-functions');
exports.getPodcast = () => functions.https.onCall(async (data, context) => {
...
return { ... }
});
同样的模式将用于概要文件中的removeProfile方法。
为了保持简单(但能完成工作),我个人是这样构造我的代码的。
布局
├── /src/
│ ├── index.ts
│ ├── foo.ts
│ ├── bar.ts
| ├── db.ts
└── package.json
foo.ts
import * as functions from 'firebase-functions';
export const fooFunction = functions.database()......... {
//do your function.
}
export const someOtherFunction = functions.database().......... {
// do the thing.
}
bar.ts
import * as functions from 'firebase-functions';
export const barFunction = functions.database()......... {
//do your function.
}
export const anotherFunction = functions.database().......... {
// do the thing.
}
db.ts
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
export const firestore = admin.firestore();
export const realtimeDb = admin.database();
index.ts
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
admin.initializeApp(functions.config().firebase);
// above codes only needed if you use firebase admin
export * from './foo';
export * from './bar';
适用于任何嵌套级别的目录。也只需遵循目录中的模式即可。
这要归功于@zaidfazil的答案
我也在为云函数寻找最佳的文件夹结构,所以我决定分享我的想法:
+ /src
| - index.ts
| + /events
| | - moduleA_events.ts
| | - moduleB_events.ts
| + /service
| | - moduleA_services.ts
| | - moduleB_services.ts
| + /model
| | - objectA.ts
| | - objectB.ts
| | - objectC.ts
/ src /索引。Ts此文件作为应用程序中所有可用事件(函数)的入口点,如数据库事件,HTTPS请求,计划函数。然而,函数不是直接在index.js中声明的,而是在事件文件夹indead中声明的。代码示例:
出口。user = require("./events/userEvents")
出口。order = require("./events/orderEvents")
出口。product = require("./events/productEvents")
注意:根据GCF官方文档,这种方法会自动将所有函数重命名为“模块-函数”模式。示例:如果在userEvents中有"userCreated"函数。ts, firebase将重命名此函数为"user-userCreated"
/src/events this folder should only contain cloud functions declarations and should not handle business logic directly. For the actual business, you should call custom functions from your /service folder (which maps the same modules as in the events folder). Code sample for userEvents.ts:
exports.userCreated = functions.firestore.document("/users/{documentId}").onCreate(async (snapshot) => {
userServices.sendWelcomeEmail()
}
/src/service the actual busienss logic that will connect with other firebase services such as firestore, storage, auth. You can also import your /model layer here (typescript only).
/src/model the interfaces used in typescript to ensure strong typed functions and objects.
正如您所注意到的,这种方法主要基于MVC和OOP原则。有很多关于我们是否应该在无服务器环境中使用函数式编程的争论。由于我的后台背景是Java和c#,我在这里介绍的文件夹结构对我来说似乎更自然,然而,我很想知道当转向函数式编程方法时,这种文件夹结构会有什么不同。