not-options 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/models/options.js +188 -180
package/package.json
CHANGED
package/src/models/options.js
CHANGED
|
@@ -1,193 +1,201 @@
|
|
|
1
|
-
const log = require(
|
|
1
|
+
const log = require("not-log")(module, "Options Model");
|
|
2
2
|
try {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const MODEL_NAME = "Options";
|
|
4
|
+
const Increment = require("not-node").Increment;
|
|
5
|
+
const notError = require("not-error").notError;
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
const FIELDS = [
|
|
8
|
+
"id",
|
|
9
|
+
"value",
|
|
10
|
+
[
|
|
11
|
+
"active",
|
|
12
|
+
{
|
|
13
|
+
default: true,
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
"createdAt",
|
|
17
|
+
"updatedAt",
|
|
18
|
+
];
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
exports.FIELDS = FIELDS;
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
exports.keepNotExtended = false;
|
|
23
|
+
exports.thisModelName = MODEL_NAME;
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
exports.enrich = {
|
|
26
|
+
versioning: true,
|
|
27
|
+
increment: true,
|
|
28
|
+
validators: true,
|
|
29
|
+
};
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const metaExtend = require("not-meta").extend;
|
|
32
|
+
const metaModel = require("not-meta").Model;
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
const ActionList = ["search"];
|
|
35
|
+
const MODULE_OPTIONS_PREFIX = "MODULE_";
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
37
|
+
exports.thisStatics = {
|
|
38
|
+
getModuleOptionsName(moduleName) {
|
|
39
|
+
return MODULE_OPTIONS_PREFIX + moduleName.toLowerCase();
|
|
40
|
+
},
|
|
41
|
+
readModuleOptions(moduleName) {
|
|
42
|
+
let name = this.getModuleOptionsName(moduleName);
|
|
43
|
+
return this.findOne({
|
|
44
|
+
id: name,
|
|
45
|
+
active: true,
|
|
46
|
+
__latest: true,
|
|
47
|
+
__closed: false,
|
|
48
|
+
})
|
|
49
|
+
.exec()
|
|
50
|
+
.then((result) => {
|
|
51
|
+
if (result) {
|
|
52
|
+
try {
|
|
53
|
+
return JSON.parse(result.value);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
log.error(e);
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
return {};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
async writeModuleOptions(moduleName, options) {
|
|
64
|
+
let name = this.getModuleOptionsName(moduleName);
|
|
65
|
+
let value =
|
|
66
|
+
typeof options === "string" ? options : JSON.stringify(options);
|
|
67
|
+
let exists = await this.countWithFilter({
|
|
68
|
+
id: name,
|
|
69
|
+
__latest: true,
|
|
70
|
+
__closed: false,
|
|
71
|
+
});
|
|
72
|
+
if (exists) {
|
|
73
|
+
await this.updateOne(
|
|
74
|
+
{
|
|
75
|
+
id: name,
|
|
76
|
+
__latest: true,
|
|
77
|
+
__closed: false,
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
value,
|
|
81
|
+
updatedAt: Date.now(),
|
|
82
|
+
}
|
|
83
|
+
).exec();
|
|
84
|
+
let item = await this.findOne({
|
|
85
|
+
id: name,
|
|
86
|
+
__latest: true,
|
|
87
|
+
__closed: false,
|
|
88
|
+
}).exec();
|
|
89
|
+
if (typeof item !== "undefined" && item !== null) {
|
|
90
|
+
return this.saveVersion(item._id);
|
|
91
|
+
} else {
|
|
92
|
+
throw new notError(
|
|
93
|
+
"-options for module version not saved, empty response",
|
|
94
|
+
{
|
|
95
|
+
id: name,
|
|
96
|
+
item,
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
return this.add({
|
|
102
|
+
id: name,
|
|
103
|
+
value,
|
|
104
|
+
active: true,
|
|
105
|
+
updatedAt: Date.now(),
|
|
106
|
+
});
|
|
56
107
|
}
|
|
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
|
-
active: true,
|
|
97
|
-
updatedAt: Date.now()
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
getAllAsObject(whitelist = false) {
|
|
102
|
-
return this.find({
|
|
103
|
-
'active': true,
|
|
104
|
-
__latest: true,
|
|
105
|
-
__closed: false
|
|
106
|
-
}).exec()
|
|
107
|
-
.then((results) => {
|
|
108
|
-
let options = {};
|
|
109
|
-
if (Array.isArray(results)) {
|
|
110
|
-
results.forEach((item) => {
|
|
111
|
-
if (whitelist && Array.isArray(whitelist)) {
|
|
112
|
-
if (!whitelist.includes(item.id)) {
|
|
113
|
-
return;
|
|
108
|
+
},
|
|
109
|
+
getAllAsObject(whitelist = false) {
|
|
110
|
+
return this.find({
|
|
111
|
+
active: true,
|
|
112
|
+
__latest: true,
|
|
113
|
+
__closed: false,
|
|
114
|
+
})
|
|
115
|
+
.exec()
|
|
116
|
+
.then((results) => {
|
|
117
|
+
let options = {};
|
|
118
|
+
if (Array.isArray(results)) {
|
|
119
|
+
results.forEach((item) => {
|
|
120
|
+
if (whitelist && Array.isArray(whitelist)) {
|
|
121
|
+
if (!whitelist.includes(item.id)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
options[item.id] = item.value;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return options;
|
|
129
|
+
});
|
|
130
|
+
},
|
|
131
|
+
async initIfNotExists(opts) {
|
|
132
|
+
let names = Object.keys(opts);
|
|
133
|
+
let res = await this.find({
|
|
134
|
+
__closed: false,
|
|
135
|
+
__latest: true,
|
|
136
|
+
id: {
|
|
137
|
+
$in: names,
|
|
138
|
+
},
|
|
139
|
+
}).exec();
|
|
140
|
+
let existed = res.map((itm) => itm.id);
|
|
141
|
+
for (let name of names) {
|
|
142
|
+
if (!existed.includes(name)) {
|
|
143
|
+
await this.add({
|
|
144
|
+
id: name,
|
|
145
|
+
...opts[name],
|
|
146
|
+
});
|
|
114
147
|
}
|
|
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
|
-
let len = data.length;
|
|
163
|
-
if (close) {
|
|
164
|
-
await this.closeExisting();
|
|
165
|
-
}
|
|
166
|
-
//adding new or modifing existing
|
|
167
|
-
for (let itm of Array.from(data)) {
|
|
168
|
-
//keep track of maximum ID, to later change in 'inc' collection
|
|
169
|
-
if (itm.optionsID > maxID) {
|
|
170
|
-
maxID = itm.optionsID;
|
|
171
|
-
}
|
|
172
|
-
delete itm._id;
|
|
173
|
-
itm._versions;
|
|
174
|
-
}
|
|
175
|
-
await this.insertMany(data);
|
|
176
|
-
log.log(`Imported ${len}`);
|
|
177
|
-
await Increment.rebase(MODEL_NAME, maxID + 1);
|
|
178
|
-
log.log(`ID cursor moved to ${maxID}`);
|
|
179
|
-
},
|
|
180
|
-
async importAsObject(obj, close = false){
|
|
181
|
-
if (close) {
|
|
182
|
-
await this.closeExisting();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
metaExtend(metaModel, exports.thisStatics, ActionList, {
|
|
188
|
-
MODEL_NAME
|
|
189
|
-
});
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
async bulkExport() {
|
|
151
|
+
return this.listAll();
|
|
152
|
+
},
|
|
153
|
+
async closeExisting() {
|
|
154
|
+
//closing existing records
|
|
155
|
+
let all = await this.listAll();
|
|
156
|
+
for (let rec of Array.from(all)) {
|
|
157
|
+
await rec.close();
|
|
158
|
+
}
|
|
159
|
+
log.log(`Existing records closed`);
|
|
160
|
+
},
|
|
161
|
+
/**
|
|
162
|
+
* import of many docs in one go,
|
|
163
|
+
* if close==true, all existing will be closed
|
|
164
|
+
* @param {array} data array of docs
|
|
165
|
+
* @param {boolean} close close existing documents
|
|
166
|
+
* @returns {Promise}
|
|
167
|
+
*/
|
|
168
|
+
async bulkImport(data, close = false) {
|
|
169
|
+
let maxID = 0;
|
|
170
|
+
data = Array.from(data);
|
|
171
|
+
let len = data.length;
|
|
172
|
+
if (close) {
|
|
173
|
+
await this.closeExisting();
|
|
174
|
+
}
|
|
175
|
+
//adding new or modifing existing
|
|
176
|
+
for (let itm of Array.from(data)) {
|
|
177
|
+
//keep track of maximum ID, to later change in 'inc' collection
|
|
178
|
+
if (itm.optionsID > maxID) {
|
|
179
|
+
maxID = itm.optionsID;
|
|
180
|
+
}
|
|
181
|
+
delete itm._id;
|
|
182
|
+
itm._versions;
|
|
183
|
+
}
|
|
184
|
+
await this.insertMany(data);
|
|
185
|
+
log.log(`Imported ${len}`);
|
|
186
|
+
await Increment.rebase(MODEL_NAME, maxID + 1);
|
|
187
|
+
log.log(`ID cursor moved to ${maxID}`);
|
|
188
|
+
},
|
|
189
|
+
async importAsObject(obj, close = false) {
|
|
190
|
+
if (close) {
|
|
191
|
+
await this.closeExisting();
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
};
|
|
190
195
|
|
|
196
|
+
metaExtend(metaModel, exports.thisStatics, ActionList, {
|
|
197
|
+
MODEL_NAME,
|
|
198
|
+
});
|
|
191
199
|
} catch (e) {
|
|
192
|
-
|
|
200
|
+
log.error(e);
|
|
193
201
|
}
|