hola-server 0.3.4 → 0.3.7

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 CHANGED
@@ -1,54 +1,55 @@
1
- const date_format = require('dateformat');
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
- return date_format(date, "HH:MM");
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
- return date_format(date, "mm/dd");
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
- return date_format(date, "yyyymmdd");
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
- return date_format(date, "yyyymmdd HH:MM:ss");
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
- const year = parseInt(date.substr(0, 4));
46
- const month = parseInt(date.substr(4, 2));
47
- const day = parseInt(date.substr(6, 2));
48
- const dateObj = new Date();
49
- dateObj.setFullYear(year, month - 1, day);
50
- dateObj.setHours(0, 0, 0, 0);
51
- return dateObj;
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/core/type.js CHANGED
@@ -123,7 +123,7 @@ register_type(number_type);
123
123
  const string_type = {
124
124
  name: "string",
125
125
  convert: function (value) {
126
- return { value: value + "" };
126
+ return { value: value ? (value + "").trim() : "" };
127
127
  }
128
128
  }
129
129
 
package/db/db.js CHANGED
@@ -1,8 +1,8 @@
1
- const mongoist = require('mongoist');
1
+ const mongoist = require("mongoist");
2
2
 
3
- const { get_settings } = require('../setting');
4
- const { get_context_value } = require('../http/context');
5
- const { format_date_time } = require('../core/date');
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
- const req = get_context_value("req");
18
- return req && req.session && req.session.user ? req.session.user.id : '';
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
- const time = format_date_time(new Date());
23
- const db = get_db();
24
- const { col_log } = get_settings().log;
22
+ const time = format_date_time(new Date());
23
+ const db = get_db();
24
+ const { col_log } = get_settings().log;
25
25
 
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 : '';
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
- db.create(col_log, { time: time, category: category, level: level, msg: msg, user: user, path: path }).then(() => { });
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
- const { save_db, log_level } = get_settings().log;
35
- return save_db && log_level <= LOG_LEVEL_DEBUG;
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
- const { save_db, log_level } = get_settings().log;
40
- return save_db && log_level <= LOG_LEVEL_INFO;
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
- const { save_db, log_level } = get_settings().log;
45
- return save_db && log_level <= LOG_LEVEL_WARN;
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
- const { save_db, log_level } = get_settings().log;
50
- return save_db && log_level <= LOG_LEVEL_ERROR;
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
- if (is_log_debug()) {
55
- log_msg(category, LOG_LEVEL_DEBUG, msg);
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
- if (is_log_info()) {
61
- log_msg(category, LOG_LEVEL_INFO, msg);
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
- if (is_log_warn()) {
67
- log_msg(category, LOG_LEVEL_WARN, msg);
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
- if (is_log_error()) {
73
- log_msg(category, LOG_LEVEL_ERROR, msg);
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
- return mongoist.ObjectId(id);
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
- try {
93
- return { _id: oid(id) };
94
- } catch (e) {
95
- return null;
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
- try {
106
- const id_array = ids.map(id => oid(id));
107
- return { _id: { $in: id_array } };
108
- } catch (e) {
109
- return null;
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
- 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();
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
- constructor(url, poolSize) {
135
- if (!url) {
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
- * Update the object, upsert:true, multi:true
161
- * @param {mongodb collection name} code
162
- * @param {*} query
163
- * @param {*} obj
164
- * @returns
165
- */
166
- update(code, query, obj) {
167
- if (is_log_debug()) {
168
- log_debug(LOG_DB, "updating obj:" + JSON.stringify(obj) + ", for [" + code + "] with query:" + JSON.stringify(query));
169
- }
170
-
171
- const col = this.db[code];
172
- return col.update(query, { "$set": obj }, { upsert: true, multi: true });
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
- * Remove the records from mongodb
177
- * @param {mongodb collection name} code
178
- * @param {query to execute delete op} query
179
- * @returns
180
- */
181
- delete(code, query) {
182
- if (is_log_debug()) {
183
- log_debug(LOG_DB, "deleting objects for [" + code + "] with query:" + JSON.stringify(query));
184
- }
185
-
186
- const col = this.db[code];
187
- return col.remove(query);
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
- * Search the db using query
192
- * @param {mongodb collection name} code
193
- * @param {search criteria} query
194
- * @param {the attributes to load from db} attr
195
- * @returns
196
- */
197
- find(code, query, attr) {
198
- if (is_log_debug()) {
199
- log_debug(LOG_DB, "find objects for [" + code + "] with query:" + JSON.stringify(query) + " and attr:" + JSON.stringify(attr));
200
- }
201
-
202
- const col = this.db[code];
203
- return col.find(query, attr);
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
- * Find one record from db
208
- * @param {mongodb collection name} code
209
- * @param {search criteria} query
210
- * @param {the attributes to load from db} attr
211
- * @returns
212
- */
213
- find_one(code, query, attr) {
214
- if (is_log_debug()) {
215
- log_debug(LOG_DB, "find_one for [" + code + "] with query:" + JSON.stringify(query) + " and attr:" + JSON.stringify(attr));
216
- }
217
-
218
- const col = this.db[code];
219
- return col.findOne(query, attr);
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
- * Find the records from db using sort to do sorting
224
- * @param {mongodb collection name} code
225
- * @param {search criteria} query
226
- * @param {sort object to sort the result} sort
227
- * @param {the attributes of the object to load from db} attr
228
- * @returns
229
- */
230
- find_sort(code, query, sort, attr) {
231
- if (is_log_debug()) {
232
- log_debug(LOG_DB, "find_sort for [" + code + "] with query:" + JSON.stringify(query) + " and attr:" + JSON.stringify(attr) + " and sort:" + JSON.stringify(sort));
233
- }
234
-
235
- const col = this.db[code];
236
- return col.find(query, attr, { sort: sort });
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
- * Find the page records
241
- * @param {mongodb collection name} code
242
- * @param {search criteria} query
243
- * @param {sort object to sort the results} sort
244
- * @param {the page index to load} page
245
- * @param {page size } limit
246
- * @param {the attributes of the object to load from db} attr
247
- * @returns
248
- */
249
- find_page(code, query, sort, page, limit, attr) {
250
- if (is_log_debug()) {
251
- 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);
252
- }
253
-
254
- const skip = (page - 1) * limit > 0 ? (page - 1) * limit : 0;
255
- const col = this.db[code];
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
- * The count number of the query
261
- * @param {mongodb collection name} code
262
- * @param {search criteria} query
263
- * @returns
264
- */
265
- count(code, query) {
266
- if (is_log_debug()) {
267
- log_debug(LOG_DB, "count for [" + code + "] with query:" + JSON.stringify(query));
268
- }
269
-
270
- const col = this.db[code];
271
- return col.count(query);
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
- * Calculate the sum value based on the field and query criteria
276
- * @param {mongodb collection name} code
277
- * @param {search criteria} query
278
- * @param {field name to calculate sum} field
279
- * @returns
280
- */
281
- sum(code, query, field) {
282
- if (is_log_debug()) {
283
- log_debug(LOG_DB, "sum for [" + code + "] with query:" + JSON.stringify(query) + ", and field:" + JSON.stringify(field));
284
- }
285
-
286
- const col = this.db[code];
287
- return col.aggregate([{ "$match": query }, { "$group": { _id: null, total: { "$sum": "$" + field + "" } } }])
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
- * Pull the object from array
293
- * @param {mongodb collection name} code
294
- * @param {search criteria} query
295
- * @param {object pulled from the array} ele
296
- * @returns
297
- */
298
- pull(code, query, ele) {
299
- if (is_log_debug()) {
300
- log_debug(LOG_DB, "pull ele [" + JSON.stringify(ele) + "] with query:" + JSON.stringify(query) + ",code:" + code);
301
- }
302
-
303
- const col = this.db[code];
304
- return col.update(query, { "$pull": ele }, { multi: true });
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
- * Push the object to array
309
- * @param {mongodb collection name} code
310
- * @param {search criteria} query
311
- * @param {object push to the array} ele
312
- * @returns
313
- */
314
- push(code, query, ele) {
315
- if (is_log_debug()) {
316
- log_debug(LOG_DB, "push ele [" + JSON.stringify(ele) + "] with query:" + JSON.stringify(query) + ",code:" + code);
317
- }
318
-
319
- const col = this.db[code];
320
- return col.update(query, { "$push": ele });
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
- * add the object to set
325
- * @param {mongodb collection name} code
326
- * @param {search criteria} query
327
- * @param {object added to the set} ele
328
- * @returns
329
- */
330
- add_to_set(code, query, ele) {
331
- const col = this.db[code];
332
- return col.update(query, { "$addToSet": ele }, { upsert: true });
333
- };
334
-
335
- /**
336
- * Get the mongodb collection
337
- * @param {mongodb collection name} code
338
- * @returns
339
- */
340
- col(code) {
341
- return this.db[code];
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
- if (db_instance && db_instance.db) {
353
- return db_instance;
354
- } else {
355
- const mongo = get_settings().mongo;
356
-
357
- db_instance = new DB(mongo.url, mongo.pool);
358
- return db_instance;
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.4",
3
+ "version": "0.3.7",
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.21.1",
11
- "body-parser": "^1.19.0",
12
- "connect-mongo": "^4.4.1",
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.17.1",
16
- "express-sanitizer": "^1.0.5",
17
- "express-session": "^1.17.1",
18
- "mongodb": "^3.6.5",
19
- "mongoist": "^2.5.3",
20
- "multer": "^1.4.2",
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.5.0",
25
- "chai-http": "^2.0.1",
26
- "mocha": "^2.4.5"
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('assert');
2
- const { get_db } = require('../../db/db');
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('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
- });
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
- describe('update', function () {
22
- it('should update user successfully', async function () {
23
- const birthday = new Date();
24
- const user = { "name": "user1", age: 10, male: true, birthday: birthday };
25
- await db.create(user_code, user);
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
- const query = { "name": "user1" };
28
- await db.update(user_code, query, { age: 20 });
41
+ const query = { name: "user1" };
42
+ await db.update(user_code, query, { age: 20 });
29
43
 
30
- const db_user = await db.find_one(user_code, query);
31
- strictEqual(db_user.name, "user1");
32
- strictEqual(db_user.age, 20);
33
- strictEqual(db_user.male, true);
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
- const db_user_name = await db.find_one(user_code, query, { "age": 1 });
36
- strictEqual(db_user_name.name, undefined);
37
- strictEqual(db_user_name.male, undefined);
38
- strictEqual(db_user_name.birthday, undefined);
39
- strictEqual(db_user_name.age, 20);
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
- await db.delete(user_code, {});
42
- });
55
+ await db.delete(user_code, {});
43
56
  });
57
+ });
44
58
 
45
- describe('count', function () {
46
- it('should get user count successfully', async function () {
47
- const birthday = new Date();
48
- const user = { "name": "user1", age: 10, male: true, birthday: birthday };
49
- await db.create(user_code, user);
50
- const query = { "name": "user1" };
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
- const user_count = await db.count(user_code, query);
53
- strictEqual(user_count, 1)
66
+ const user_count = await db.count(user_code, query);
67
+ strictEqual(user_count, 1);
54
68
 
55
- await db.delete(user_code, {});
56
- });
69
+ await db.delete(user_code, {});
57
70
  });
58
- });
71
+ });
72
+ });