-
Notifications
You must be signed in to change notification settings - Fork 0
/
env-db.js
32 lines (28 loc) · 861 Bytes
/
env-db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const log = require('./log')
const store = {}
const prefix = 'IO_NOISEMACHINES_ENVDB_'
const hasPrefix = (string) => string.indexOf(prefix) > -1
const addPrefix = (string) => prefix + string
log.debug(`env: ${JSON.stringify(process.env)}`)
// sync store with env
const prefixedKeys = Object.keys(process.env).filter(hasPrefix)
prefixedKeys.forEach(key => store[key] = JSON.parse(process.env[key]))
log.debug(`store: ${JSON.stringify(store)}`)
// if (Object.keys(process.env).filter(hasPrefix).length > 0) {
// console.log(`---\n${JSON.stringify(store)}\n---`)
// }
module.exports = {
get (key) {
return store[addPrefix(key)]
},
set (key, value) {
store[addPrefix(key)] = value
},
serialize () {
const serialized = {}
for (const key in store) {
serialized[key] = JSON.stringify(store[key])
}
return serialized
}
}