hola-server 0.3.3 → 0.3.6
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/core/date.js +31 -30
- package/core/meta.js +1 -0
- package/db/db.js +268 -270
- package/package.json +13 -13
- package/test/db/db.js +58 -44
package/core/date.js
CHANGED
|
@@ -1,54 +1,55 @@
|
|
|
1
|
-
const date_format = require(
|
|
1
|
+
const date_format = require("dateformat");
|
|
2
|
+
// import date_format from "dateformat";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Format the date object time part using HH:MM
|
|
5
|
-
* @param {date object to be formatted} date
|
|
6
|
-
* @returns
|
|
6
|
+
* @param {date object to be formatted} date
|
|
7
|
+
* @returns
|
|
7
8
|
*/
|
|
8
9
|
const format_time = (date) => {
|
|
9
|
-
|
|
10
|
+
return date_format(date, "HH:MM");
|
|
10
11
|
};
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Format date object using mm/dd
|
|
14
|
-
* @param {date object to be formatted} date
|
|
15
|
-
* @returns
|
|
15
|
+
* @param {date object to be formatted} date
|
|
16
|
+
* @returns
|
|
16
17
|
*/
|
|
17
|
-
const simple_date = date => {
|
|
18
|
-
|
|
19
|
-
}
|
|
18
|
+
const simple_date = (date) => {
|
|
19
|
+
return date_format(date, "mm/dd");
|
|
20
|
+
};
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Format date obj using yyyymmdd
|
|
23
|
-
* @param {date object to be formatted} date
|
|
24
|
-
* @returns
|
|
24
|
+
* @param {date object to be formatted} date
|
|
25
|
+
* @returns
|
|
25
26
|
*/
|
|
26
|
-
const format_date = date => {
|
|
27
|
-
|
|
28
|
-
}
|
|
27
|
+
const format_date = (date) => {
|
|
28
|
+
return date_format(date, "yyyymmdd");
|
|
29
|
+
};
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* Format data obj using yyyymmdd hh:mm:ss
|
|
32
|
-
* @param {date object to be formatted} date
|
|
33
|
-
* @returns
|
|
33
|
+
* @param {date object to be formatted} date
|
|
34
|
+
* @returns
|
|
34
35
|
*/
|
|
35
|
-
const format_date_time = date => {
|
|
36
|
-
|
|
37
|
-
}
|
|
36
|
+
const format_date_time = (date) => {
|
|
37
|
+
return date_format(date, "yyyymmdd HH:MM:ss");
|
|
38
|
+
};
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
41
|
* Parse the date object using yyyymmdd format
|
|
41
|
-
* @param {date str to be parsed} date
|
|
42
|
-
* @returns
|
|
42
|
+
* @param {date str to be parsed} date
|
|
43
|
+
* @returns
|
|
43
44
|
*/
|
|
44
45
|
const parse_date = (date) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
46
|
+
const year = parseInt(date.substr(0, 4));
|
|
47
|
+
const month = parseInt(date.substr(4, 2));
|
|
48
|
+
const day = parseInt(date.substr(6, 2));
|
|
49
|
+
const dateObj = new Date();
|
|
50
|
+
dateObj.setFullYear(year, month - 1, day);
|
|
51
|
+
dateObj.setHours(0, 0, 0, 0);
|
|
52
|
+
return dateObj;
|
|
53
|
+
};
|
|
53
54
|
|
|
54
|
-
module.exports = { simple_date, format_date, format_time, format_date_time, parse_date }
|
|
55
|
+
module.exports = { simple_date, format_date, format_time, format_date_time, parse_date };
|
package/core/meta.js
CHANGED
|
@@ -166,6 +166,7 @@ class EntityMeta {
|
|
|
166
166
|
this.file_fields = meta.fields.filter(f => f.type === 'file');
|
|
167
167
|
this.upload_fields = this.file_fields && this.file_fields.length > 0 ? this.file_fields.map(f => ({ name: f.name, maxCount: f.max ? f.max : 1 })) : [];
|
|
168
168
|
|
|
169
|
+
set_callback(this, "after_read", meta.after_read);
|
|
169
170
|
set_callback(this, "before_create", meta.before_create);
|
|
170
171
|
set_callback(this, "before_update", meta.before_update);
|
|
171
172
|
set_callback(this, "before_delete", meta.before_delete);
|
package/db/db.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const mongoist = require(
|
|
1
|
+
const mongoist = require("mongoist");
|
|
2
2
|
|
|
3
|
-
const { get_settings } = require(
|
|
4
|
-
const { get_context_value } = require(
|
|
5
|
-
const { format_date_time } = require(
|
|
3
|
+
const { get_settings } = require("../setting");
|
|
4
|
+
const { get_context_value } = require("../http/context");
|
|
5
|
+
const { format_date_time } = require("../core/date");
|
|
6
6
|
|
|
7
7
|
const LOG_LEVEL_DEBUG = 0;
|
|
8
8
|
const LOG_LEVEL_INFO = 1;
|
|
@@ -14,349 +14,347 @@ const LOG_ENTITY = "entity";
|
|
|
14
14
|
const LOG_SYSTEM = "system";
|
|
15
15
|
|
|
16
16
|
const get_session_userid = () => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
const req = get_context_value("req");
|
|
18
|
+
return req && req.session && req.session.user ? req.session.user.id : "";
|
|
19
|
+
};
|
|
20
20
|
|
|
21
21
|
const log_msg = (category, level, msg) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const time = format_date_time(new Date());
|
|
23
|
+
const db = get_db();
|
|
24
|
+
const { col_log } = get_settings().log;
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const req = get_context_value("req");
|
|
27
|
+
const path = req ? req.originalUrl : "";
|
|
28
|
+
const user = req && req.session && req.session.user ? req.session.user.id : "";
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
}
|
|
30
|
+
db.create(col_log, { time: time, category: category, level: level, msg: msg, user: user, path: path }).then(() => {});
|
|
31
|
+
};
|
|
32
32
|
|
|
33
33
|
const is_log_debug = () => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
34
|
+
const { save_db, log_level } = get_settings().log;
|
|
35
|
+
return save_db && log_level <= LOG_LEVEL_DEBUG;
|
|
36
|
+
};
|
|
37
37
|
|
|
38
38
|
const is_log_info = () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
39
|
+
const { save_db, log_level } = get_settings().log;
|
|
40
|
+
return save_db && log_level <= LOG_LEVEL_INFO;
|
|
41
|
+
};
|
|
42
42
|
|
|
43
43
|
const is_log_warn = () => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
44
|
+
const { save_db, log_level } = get_settings().log;
|
|
45
|
+
return save_db && log_level <= LOG_LEVEL_WARN;
|
|
46
|
+
};
|
|
47
47
|
|
|
48
48
|
const is_log_error = () => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
49
|
+
const { save_db, log_level } = get_settings().log;
|
|
50
|
+
return save_db && log_level <= LOG_LEVEL_ERROR;
|
|
51
|
+
};
|
|
52
52
|
|
|
53
53
|
const log_debug = (category, msg) => {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
54
|
+
if (is_log_debug()) {
|
|
55
|
+
log_msg(category, LOG_LEVEL_DEBUG, msg);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
58
|
|
|
59
59
|
const log_info = (category, msg) => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
60
|
+
if (is_log_info()) {
|
|
61
|
+
log_msg(category, LOG_LEVEL_INFO, msg);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
64
|
|
|
65
65
|
const log_warn = (category, msg) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
66
|
+
if (is_log_warn()) {
|
|
67
|
+
log_msg(category, LOG_LEVEL_WARN, msg);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
70
|
|
|
71
71
|
const log_error = (category, msg) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
72
|
+
if (is_log_error()) {
|
|
73
|
+
log_msg(category, LOG_LEVEL_ERROR, msg);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
78
|
* Construct mongodb ObjectId
|
|
79
|
-
* @param {string value of the id} id
|
|
80
|
-
* @returns
|
|
79
|
+
* @param {string value of the id} id
|
|
80
|
+
* @returns
|
|
81
81
|
*/
|
|
82
82
|
const oid = (id) => {
|
|
83
|
-
|
|
83
|
+
return mongoist.ObjectId(id);
|
|
84
84
|
};
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
87
|
* Construct objectId query object
|
|
88
|
-
* @param {string value of the id} id
|
|
88
|
+
* @param {string value of the id} id
|
|
89
89
|
* @returns id query if id is valid otherwise return null
|
|
90
90
|
*/
|
|
91
91
|
const oid_query = (id) => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
try {
|
|
93
|
+
return { _id: oid(id) };
|
|
94
|
+
} catch (e) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
97
|
};
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* Construct in query of ObjectId array
|
|
101
|
-
* @param {string value of the id} ids
|
|
101
|
+
* @param {string value of the id} ids
|
|
102
102
|
* @returns id in query if id is valid otherwise return null
|
|
103
103
|
*/
|
|
104
104
|
const oid_queries = (ids) => {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
try {
|
|
106
|
+
const id_array = ids.map((id) => oid(id));
|
|
107
|
+
return { _id: { $in: id_array } };
|
|
108
|
+
} catch (e) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
111
|
};
|
|
112
112
|
|
|
113
113
|
/**
|
|
114
114
|
* Execute bulk update using the items
|
|
115
|
-
* @param {mongodb collection} col
|
|
116
|
-
* @param {the items to execute bulk update} items
|
|
117
|
-
* @param {the attributes used as search criteria} attrs
|
|
118
|
-
* @returns
|
|
115
|
+
* @param {mongodb collection} col
|
|
116
|
+
* @param {the items to execute bulk update} items
|
|
117
|
+
* @param {the attributes used as search criteria} attrs
|
|
118
|
+
* @returns
|
|
119
119
|
*/
|
|
120
120
|
const bulk_update = async (col, items, attrs) => {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
121
|
+
const bulk = col.initializeOrderedBulkOp();
|
|
122
|
+
for (const item of items) {
|
|
123
|
+
const query = {};
|
|
124
|
+
attrs.forEach(function (attr) {
|
|
125
|
+
query[attr] = item[attr];
|
|
126
|
+
});
|
|
127
|
+
bulk.find(query).upsert().update({ $set: item }, true);
|
|
128
|
+
}
|
|
129
|
+
return await bulk.execute();
|
|
130
130
|
};
|
|
131
131
|
|
|
132
132
|
class DB {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
this.db = mongoist(url, { autoReconnect: true, poolSize: poolSize });
|
|
140
|
-
|
|
141
|
-
this.db.on('error', function (err) {
|
|
142
|
-
if (is_log_error()) {
|
|
143
|
-
log_error(LOG_DB, err);
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Insert Object to db
|
|
150
|
-
* @param {mongodb collection name} code
|
|
151
|
-
* @param {inserted object} obj
|
|
152
|
-
* @returns
|
|
153
|
-
*/
|
|
154
|
-
create(code, obj) {
|
|
155
|
-
const col = this.db[code];
|
|
156
|
-
return col.insert(obj);
|
|
133
|
+
constructor(url, poolSize) {
|
|
134
|
+
if (!url) {
|
|
135
|
+
return;
|
|
157
136
|
}
|
|
158
137
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
138
|
+
this.db = mongoist(url, { autoReconnect: true, poolSize: poolSize });
|
|
139
|
+
|
|
140
|
+
this.db.on("error", function (err) {
|
|
141
|
+
if (is_log_error()) {
|
|
142
|
+
log_error(LOG_DB, err);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Insert Object to db
|
|
149
|
+
* @param {mongodb collection name} code
|
|
150
|
+
* @param {inserted object} obj
|
|
151
|
+
* @returns
|
|
152
|
+
*/
|
|
153
|
+
create(code, obj) {
|
|
154
|
+
const col = this.db[code];
|
|
155
|
+
return col.insert(obj, { checkKeys: false });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Update the object, upsert:true, multi:true
|
|
160
|
+
* @param {mongodb collection name} code
|
|
161
|
+
* @param {*} query
|
|
162
|
+
* @param {*} obj
|
|
163
|
+
* @returns
|
|
164
|
+
*/
|
|
165
|
+
update(code, query, obj) {
|
|
166
|
+
if (is_log_debug()) {
|
|
167
|
+
log_debug(LOG_DB, "updating obj:" + JSON.stringify(obj) + ", for [" + code + "] with query:" + JSON.stringify(query));
|
|
173
168
|
}
|
|
174
169
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
170
|
+
const col = this.db[code];
|
|
171
|
+
return col.update(query, { $set: obj }, { upsert: true, multi: true });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Remove the records from mongodb
|
|
176
|
+
* @param {mongodb collection name} code
|
|
177
|
+
* @param {query to execute delete op} query
|
|
178
|
+
* @returns
|
|
179
|
+
*/
|
|
180
|
+
delete(code, query) {
|
|
181
|
+
if (is_log_debug()) {
|
|
182
|
+
log_debug(LOG_DB, "deleting objects for [" + code + "] with query:" + JSON.stringify(query));
|
|
188
183
|
}
|
|
189
184
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
185
|
+
const col = this.db[code];
|
|
186
|
+
return col.remove(query);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Search the db using query
|
|
191
|
+
* @param {mongodb collection name} code
|
|
192
|
+
* @param {search criteria} query
|
|
193
|
+
* @param {the attributes to load from db} attr
|
|
194
|
+
* @returns
|
|
195
|
+
*/
|
|
196
|
+
find(code, query, attr) {
|
|
197
|
+
if (is_log_debug()) {
|
|
198
|
+
log_debug(LOG_DB, "find objects for [" + code + "] with query:" + JSON.stringify(query) + " and attr:" + JSON.stringify(attr));
|
|
204
199
|
}
|
|
205
200
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
201
|
+
const col = this.db[code];
|
|
202
|
+
return col.find(query, attr);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Find one record from db
|
|
207
|
+
* @param {mongodb collection name} code
|
|
208
|
+
* @param {search criteria} query
|
|
209
|
+
* @param {the attributes to load from db} attr
|
|
210
|
+
* @returns
|
|
211
|
+
*/
|
|
212
|
+
find_one(code, query, attr) {
|
|
213
|
+
if (is_log_debug()) {
|
|
214
|
+
log_debug(LOG_DB, "find_one for [" + code + "] with query:" + JSON.stringify(query) + " and attr:" + JSON.stringify(attr));
|
|
220
215
|
}
|
|
221
216
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
217
|
+
const col = this.db[code];
|
|
218
|
+
return col.findOne(query, attr);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Find the records from db using sort to do sorting
|
|
223
|
+
* @param {mongodb collection name} code
|
|
224
|
+
* @param {search criteria} query
|
|
225
|
+
* @param {sort object to sort the result} sort
|
|
226
|
+
* @param {the attributes of the object to load from db} attr
|
|
227
|
+
* @returns
|
|
228
|
+
*/
|
|
229
|
+
find_sort(code, query, sort, attr) {
|
|
230
|
+
if (is_log_debug()) {
|
|
231
|
+
log_debug(LOG_DB, "find_sort for [" + code + "] with query:" + JSON.stringify(query) + " and attr:" + JSON.stringify(attr) + " and sort:" + JSON.stringify(sort));
|
|
237
232
|
}
|
|
238
233
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
return col.find(query, attr, { sort: sort, skip: skip, limit: limit });
|
|
234
|
+
const col = this.db[code];
|
|
235
|
+
return col.find(query, attr, { sort: sort });
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Find the page records
|
|
240
|
+
* @param {mongodb collection name} code
|
|
241
|
+
* @param {search criteria} query
|
|
242
|
+
* @param {sort object to sort the results} sort
|
|
243
|
+
* @param {the page index to load} page
|
|
244
|
+
* @param {page size } limit
|
|
245
|
+
* @param {the attributes of the object to load from db} attr
|
|
246
|
+
* @returns
|
|
247
|
+
*/
|
|
248
|
+
find_page(code, query, sort, page, limit, attr) {
|
|
249
|
+
if (is_log_debug()) {
|
|
250
|
+
log_debug(LOG_DB, "find_page for [" + code + "] with query:" + JSON.stringify(query) + " and attr:" + JSON.stringify(attr) + " and sort:" + JSON.stringify(sort) + ", page:" + page + ",limit:" + limit);
|
|
257
251
|
}
|
|
258
252
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
253
|
+
const skip = (page - 1) * limit > 0 ? (page - 1) * limit : 0;
|
|
254
|
+
const col = this.db[code];
|
|
255
|
+
return col.find(query, attr, { sort: sort, skip: skip, limit: limit });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The count number of the query
|
|
260
|
+
* @param {mongodb collection name} code
|
|
261
|
+
* @param {search criteria} query
|
|
262
|
+
* @returns
|
|
263
|
+
*/
|
|
264
|
+
count(code, query) {
|
|
265
|
+
if (is_log_debug()) {
|
|
266
|
+
log_debug(LOG_DB, "count for [" + code + "] with query:" + JSON.stringify(query));
|
|
272
267
|
}
|
|
273
268
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
.then(result => result.length > 0 ? result[0].total : 0);
|
|
269
|
+
const col = this.db[code];
|
|
270
|
+
return col.count(query);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Calculate the sum value based on the field and query criteria
|
|
275
|
+
* @param {mongodb collection name} code
|
|
276
|
+
* @param {search criteria} query
|
|
277
|
+
* @param {field name to calculate sum} field
|
|
278
|
+
* @returns
|
|
279
|
+
*/
|
|
280
|
+
sum(code, query, field) {
|
|
281
|
+
if (is_log_debug()) {
|
|
282
|
+
log_debug(LOG_DB, "sum for [" + code + "] with query:" + JSON.stringify(query) + ", and field:" + JSON.stringify(field));
|
|
289
283
|
}
|
|
290
284
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
285
|
+
const col = this.db[code];
|
|
286
|
+
return col.aggregate([{ $match: query }, { $group: { _id: null, total: { $sum: "$" + field + "" } } }]).then((result) => (result.length > 0 ? result[0].total : 0));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Pull the object from array
|
|
291
|
+
* @param {mongodb collection name} code
|
|
292
|
+
* @param {search criteria} query
|
|
293
|
+
* @param {object pulled from the array} ele
|
|
294
|
+
* @returns
|
|
295
|
+
*/
|
|
296
|
+
pull(code, query, ele) {
|
|
297
|
+
if (is_log_debug()) {
|
|
298
|
+
log_debug(LOG_DB, "pull ele [" + JSON.stringify(ele) + "] with query:" + JSON.stringify(query) + ",code:" + code);
|
|
305
299
|
}
|
|
306
300
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
301
|
+
const col = this.db[code];
|
|
302
|
+
return col.update(query, { $pull: ele }, { multi: true });
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Push the object to array
|
|
307
|
+
* @param {mongodb collection name} code
|
|
308
|
+
* @param {search criteria} query
|
|
309
|
+
* @param {object push to the array} ele
|
|
310
|
+
* @returns
|
|
311
|
+
*/
|
|
312
|
+
push(code, query, ele) {
|
|
313
|
+
if (is_log_debug()) {
|
|
314
|
+
log_debug(LOG_DB, "push ele [" + JSON.stringify(ele) + "] with query:" + JSON.stringify(query) + ",code:" + code);
|
|
321
315
|
}
|
|
322
316
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
317
|
+
const col = this.db[code];
|
|
318
|
+
return col.update(query, { $push: ele });
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* add the object to set
|
|
323
|
+
* @param {mongodb collection name} code
|
|
324
|
+
* @param {search criteria} query
|
|
325
|
+
* @param {object added to the set} ele
|
|
326
|
+
* @returns
|
|
327
|
+
*/
|
|
328
|
+
add_to_set(code, query, ele) {
|
|
329
|
+
const col = this.db[code];
|
|
330
|
+
return col.update(query, { $addToSet: ele }, { upsert: true });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Get the mongodb collection
|
|
335
|
+
* @param {mongodb collection name} code
|
|
336
|
+
* @returns
|
|
337
|
+
*/
|
|
338
|
+
col(code) {
|
|
339
|
+
return this.db[code];
|
|
340
|
+
}
|
|
343
341
|
}
|
|
344
342
|
|
|
345
343
|
let db_instance = new DB();
|
|
346
344
|
|
|
347
345
|
/**
|
|
348
|
-
*
|
|
346
|
+
*
|
|
349
347
|
* @returns db instance of mongodb
|
|
350
348
|
*/
|
|
351
349
|
const get_db = () => {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
350
|
+
if (db_instance && db_instance.db) {
|
|
351
|
+
return db_instance;
|
|
352
|
+
} else {
|
|
353
|
+
const mongo = get_settings().mongo;
|
|
354
|
+
|
|
355
|
+
db_instance = new DB(mongo.url, mongo.pool);
|
|
356
|
+
return db_instance;
|
|
357
|
+
}
|
|
358
|
+
};
|
|
361
359
|
|
|
362
360
|
module.exports = { oid, oid_query, oid_queries, bulk_update, get_db, log_debug, log_info, log_warn, log_error, is_log_debug, is_log_info, is_log_warn, is_log_error, LOG_DB, LOG_SYSTEM, LOG_ENTITY, get_session_userid };
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hola-server",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "a meta programming framework used to build nodejs restful api",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "mocha --timeout 10000 --recursive './test/*/*.js'"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"axios": "^0.
|
|
11
|
-
"body-parser": "^1.
|
|
12
|
-
"connect-mongo": "^4.
|
|
10
|
+
"axios": "^0.27.2",
|
|
11
|
+
"body-parser": "^1.20.0",
|
|
12
|
+
"connect-mongo": "^4.6.0",
|
|
13
13
|
"cors": "^2.8.5",
|
|
14
14
|
"dateformat": "^4.5.1",
|
|
15
|
-
"express": "^4.
|
|
16
|
-
"express-sanitizer": "^1.0.
|
|
17
|
-
"express-session": "^1.17.
|
|
18
|
-
"mongodb": "^
|
|
19
|
-
"mongoist": "^2.5.
|
|
20
|
-
"multer": "^1.4.
|
|
15
|
+
"express": "^4.18.1",
|
|
16
|
+
"express-sanitizer": "^1.0.6",
|
|
17
|
+
"express-session": "^1.17.3",
|
|
18
|
+
"mongodb": "^4.7.0",
|
|
19
|
+
"mongoist": "^2.5.4",
|
|
20
|
+
"multer": "^1.4.5-lts.1",
|
|
21
21
|
"unzipper": "^0.10.11"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"chai": "^3.
|
|
25
|
-
"chai-http": "^
|
|
26
|
-
"mocha": "^
|
|
24
|
+
"chai": "^4.3.6",
|
|
25
|
+
"chai-http": "^4.3.0",
|
|
26
|
+
"mocha": "^10.0.0"
|
|
27
27
|
},
|
|
28
28
|
"repository": {
|
|
29
29
|
"type": "git",
|
package/test/db/db.js
CHANGED
|
@@ -1,58 +1,72 @@
|
|
|
1
|
-
const { strictEqual } = require(
|
|
2
|
-
const { get_db } = require(
|
|
1
|
+
const { strictEqual } = require("assert");
|
|
2
|
+
const { get_db } = require("../../db/db");
|
|
3
3
|
const db = get_db();
|
|
4
4
|
const user_code = "user";
|
|
5
5
|
|
|
6
|
-
describe(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
describe("mongodb", function () {
|
|
7
|
+
describe("create", function () {
|
|
8
|
+
it("should create user successfully", async function () {
|
|
9
|
+
const birthday = new Date();
|
|
10
|
+
const user = { name: "user1", age: 10, male: true, birthday: birthday };
|
|
11
|
+
const result = await db.create(user_code, user);
|
|
12
|
+
strictEqual(result.name, "user1");
|
|
13
|
+
strictEqual(result.age, 10);
|
|
14
|
+
strictEqual(result.male, true);
|
|
15
|
+
strictEqual(result.birthday, birthday);
|
|
16
|
+
|
|
17
|
+
await db.delete(user_code, {});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("create", function () {
|
|
22
|
+
it("should create user with dotted key successfully", async function () {
|
|
23
|
+
const birthday = new Date();
|
|
24
|
+
const user = { "name.key": "user1", age: 10, male: true, birthday: birthday };
|
|
25
|
+
const result = await db.create(user_code, user);
|
|
26
|
+
strictEqual(result["name.key"], "user1");
|
|
27
|
+
strictEqual(result.age, 10);
|
|
28
|
+
strictEqual(result.male, true);
|
|
29
|
+
strictEqual(result.birthday, birthday);
|
|
30
|
+
|
|
31
|
+
await db.delete(user_code, {});
|
|
19
32
|
});
|
|
33
|
+
});
|
|
20
34
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
describe("update", function () {
|
|
36
|
+
it("should update user successfully", async function () {
|
|
37
|
+
const birthday = new Date();
|
|
38
|
+
const user = { name: "user1", age: 10, male: true, birthday: birthday };
|
|
39
|
+
await db.create(user_code, user);
|
|
26
40
|
|
|
27
|
-
|
|
28
|
-
|
|
41
|
+
const query = { name: "user1" };
|
|
42
|
+
await db.update(user_code, query, { age: 20 });
|
|
29
43
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
const db_user = await db.find_one(user_code, query);
|
|
45
|
+
strictEqual(db_user.name, "user1");
|
|
46
|
+
strictEqual(db_user.age, 20);
|
|
47
|
+
strictEqual(db_user.male, true);
|
|
34
48
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
49
|
+
const db_user_name = await db.find_one(user_code, query, { age: 1 });
|
|
50
|
+
strictEqual(db_user_name.name, undefined);
|
|
51
|
+
strictEqual(db_user_name.male, undefined);
|
|
52
|
+
strictEqual(db_user_name.birthday, undefined);
|
|
53
|
+
strictEqual(db_user_name.age, 20);
|
|
40
54
|
|
|
41
|
-
|
|
42
|
-
});
|
|
55
|
+
await db.delete(user_code, {});
|
|
43
56
|
});
|
|
57
|
+
});
|
|
44
58
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
59
|
+
describe("count", function () {
|
|
60
|
+
it("should get user count successfully", async function () {
|
|
61
|
+
const birthday = new Date();
|
|
62
|
+
const user = { name: "user1", age: 10, male: true, birthday: birthday };
|
|
63
|
+
await db.create(user_code, user);
|
|
64
|
+
const query = { name: "user1" };
|
|
51
65
|
|
|
52
|
-
|
|
53
|
-
|
|
66
|
+
const user_count = await db.count(user_code, query);
|
|
67
|
+
strictEqual(user_count, 1);
|
|
54
68
|
|
|
55
|
-
|
|
56
|
-
});
|
|
69
|
+
await db.delete(user_code, {});
|
|
57
70
|
});
|
|
58
|
-
});
|
|
71
|
+
});
|
|
72
|
+
});
|