Skip to content

Commit 89bd15e

Browse files
committed
add support for custom storage class configured with process.env.STORAGE_CLASS
1 parent ed7fdf7 commit 89bd15e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/server/bootevent.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ function boot (env, language) {
155155
});
156156
} else {
157157
//TODO assume mongo for now, when there are more storage options add a lookup
158-
require('../storage/mongo-storage')(env, function ready(err, store) {
158+
const storage = process.env.STORAGE_CLASS ? require('../storage/custom-storage') : require('../storage/mongo-storage')
159+
storage(env, function ready(err, store) {
159160
// FIXME, error is always null, if there is an error, the index.js will throw an exception
160161
if (err) {
161162
console.info('ERROR CONNECTING TO MONGO', err);

lib/storage/custom-storage.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
*
3+
* @param env
4+
* @param cb
5+
*/
6+
function init (env, cb) {
7+
// detect what storage driver loader class to get
8+
let [driverName, packageName] = process.env.STORAGE_CLASS.split('@')
9+
if (packageName === undefined) {
10+
packageName = driverName
11+
}
12+
driverName = driverName || 'default'
13+
14+
// get the storage driver loader class
15+
// eslint-disable-next-line security/detect-non-literal-require
16+
const module = require(packageName)
17+
const DriverClass = module[driverName]
18+
19+
// pass configuration to storage driver loader, so it can decide what it needs based on the config
20+
const driver = new DriverClass(env)
21+
22+
const callback = (err, res) => {
23+
console.info(`custom storage driver '${DriverClass.name}' from '${packageName}' was loaded`)
24+
cb(err, res)
25+
}
26+
27+
// load the storage driver
28+
if (driver.needsFallback) {
29+
// the driver needs original MongoDB driver
30+
require('./mongo-storage')(env, function ready (err, store) {
31+
if (err) {
32+
return cb(err, store)
33+
}
34+
35+
// pass the MongoDB storage to be used by the driver
36+
driver.setFallback(store)
37+
// load the driver
38+
driver.init(callback)
39+
})
40+
} else {
41+
// we don't need anything special, let's the driver load
42+
driver.init(callback)
43+
}
44+
}
45+
46+
module.exports = init

0 commit comments

Comments
 (0)