-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
169 lines (144 loc) · 4.17 KB
/
config.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const utils = require('@midgar/utils')
const Sequelize = require('sequelize')
/**
* DbConfig class
* Manage configuration stored in database
*
* @param {Midgar} midgar Midgar instance
*/
class DbConfig {
constructor (midgar) {
this.midgar = midgar
this._configs = null
this._configModel = midgar.services.db.models.config
const useCache = midgar.services.cache && (midgar.config.cache.layout == undefined || midgar.config.cache.layout)
this._cache = useCache ? midgar.services.cache : false
this._cacheKey = '@midgar/db-config:config'
}
/**
* Load config from database
*/
async init() {
await this._loadConfig()
}
/**
* Load config from database
* @private
*/
async _loadConfig() {
//if cache is enable
if (this._cache) {
//try to get the config from the cache
const config = await this._cache.get(this._cacheKey)
//if is in cache
if (config !== null && config !== undefined) {
//set the config
this._configs = config
return
}
}
this._configs = {}
//Load the default configuration from the config part
//in the plugin.json file of each plugins
await this._loadPluginsConfig()
//load from the db
await this._loadDbConfig()
//if the cache is enable save the config in the cache
if (this._cache) {
await this._cache.set(this._cacheKey, this._configs)
}
//this.midgar.debug('Config loaded:')
//this.midgar.debug(this._configs)
}
/**
* Load the default configuration from the config part
* in the plugin.json file of each plugins
*/
async _loadPluginsConfig() {
//list plugins async
await utils.asyncMap(this.midgar.pm.plugins, async plugin => {
//if plugin have admin config
if (plugin.config && plugin.config.config) {
//list plugins async
await utils.asyncMap(plugin.config.config, async (value, code) => {
this._configs[code] = {value: value}
})
}
})
}
/**
* Load config from db
*/
async _loadDbConfig() {
//dont trow exception for the case where the database is not installed
try {
//get config from db
const configs = await this._configModel.findAll({})
//set the config on an object
await utils.asyncMap(configs, config => {
this._configs[config.code] = {id: config.id, value: config.value}
})
} catch (error) {
}
}
async get(code) {
if (this._configs[code] !== undefined) {
return this._configs[code].value
}
return null
}
async set(code, value , save = true) {
if (this._configs == null) {
this._configs = {}
}
if (this._configs[code] != undefined) {
//if value change
if (this._configs[code].value != value) {
this._configs[code].value = value
//prevent save unchanged value
} else if (save) {
save = false
}
} else {
this._configs[code] = {value}
}
if (save) {
try {
const id = this._configs[code].id ? this._configs[code].id : null
//save the config in the db
await this._saveConfig(code, value, id)
//if the cache is enable save the config in the cache
if (this._cache) {
//try to get the config from the cache
let config = await this._cache.get(this._cacheKey)
//add config
if (config !== null && config !== undefined) {
config[code] = this._configs[code]
} else {
config = this._configs
}
//update config objet from cache
//commented because this cause some bugs
//this._configs = config
//save cache
await this._cache.set(this._cacheKey, config)
}
} catch (error) {
throw error
}
}
}
async _saveConfig(code, value, id = null) {
if (!id) {
try {
const config = await this._configModel.create({code, value})
this._configs[code].id = config.id
} catch (error) {
this.midgar.error(error)
}
} else {
await this._configModel.update({code, value}, {where: {id: {[Sequelize.Op.eq]: id}}})
}
}
}
module.exports = DbConfig