hola-server 0.2.8 → 0.3.1
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/type.js +24 -2
- package/db/entity.js +13 -3
- package/db/gridfs.js +30 -1
- package/http/params.js +13 -2
- package/package.json +1 -1
- package/router/update.js +3 -3
package/core/type.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { has_value } = require('./validate');
|
|
1
|
+
const { has_value, is_undefined } = require('./validate');
|
|
2
2
|
const { encrypt_pwd } = require('./encrypt');
|
|
3
3
|
const type_manager = {};
|
|
4
4
|
|
|
@@ -304,4 +304,26 @@ const convert_type = function (obj, fields) {
|
|
|
304
304
|
return { obj: result, error_field_names: error_field_names };
|
|
305
305
|
}
|
|
306
306
|
|
|
307
|
-
|
|
307
|
+
const convert_update_type = function (obj, fields) {
|
|
308
|
+
const result = {};
|
|
309
|
+
const error_field_names = [];
|
|
310
|
+
|
|
311
|
+
fields.forEach(function (field) {
|
|
312
|
+
const field_value = obj[field.name];
|
|
313
|
+
if (has_value(field_value)) {
|
|
314
|
+
const type_name = field.type ? field.type : "string";
|
|
315
|
+
const type = get_type(type_name);
|
|
316
|
+
const { value, err } = type.convert(field_value);
|
|
317
|
+
if (err) {
|
|
318
|
+
error_field_names.push(field.name);
|
|
319
|
+
} else {
|
|
320
|
+
result[field.name] = value;
|
|
321
|
+
}
|
|
322
|
+
} else if (!is_undefined(field_value)) {
|
|
323
|
+
result[field.name] = "";
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
return { obj: result, error_field_names: error_field_names };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
module.exports = { register_type, convert_type, convert_update_type, get_type }
|
package/db/entity.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { SUCCESS, ERROR, NO_PARAMS, INVALID_PARAMS, DUPLICATE_KEY, NOT_FOUND, REF_NOT_FOUND, REF_NOT_UNIQUE, HAS_REF } = require('../http/code');
|
|
2
2
|
const { validate_required_fields, has_value } = require('../core/validate');
|
|
3
3
|
const { required_params } = require('../http/params');
|
|
4
|
-
const { convert_type, get_type } = require('../core/type');
|
|
4
|
+
const { convert_type, convert_update_type, get_type } = require('../core/type');
|
|
5
5
|
const { get_entity_meta } = require('../core/meta');
|
|
6
6
|
const { unique, map_array_to_obj } = require('../core/array');
|
|
7
7
|
const { LOG_ENTITY, get_db, oid_query, oid_queries, is_log_debug, is_log_error, log_debug, log_error, get_session_userid } = require('./db');
|
|
@@ -317,7 +317,7 @@ class Entity {
|
|
|
317
317
|
*
|
|
318
318
|
*/
|
|
319
319
|
async update_entity(_id, param_obj) {
|
|
320
|
-
const { obj, error_field_names } =
|
|
320
|
+
const { obj, error_field_names } = convert_update_type(param_obj, this.meta.update_fields);
|
|
321
321
|
if (error_field_names.length > 0) {
|
|
322
322
|
if (is_log_error()) {
|
|
323
323
|
log_error(LOG_ENTITY, "update_entity error fields:" + JSON.stringify(error_field_names));
|
|
@@ -399,7 +399,7 @@ class Entity {
|
|
|
399
399
|
*
|
|
400
400
|
*/
|
|
401
401
|
async batch_update_entity(_ids, param_obj) {
|
|
402
|
-
const { obj, error_field_names } =
|
|
402
|
+
const { obj, error_field_names } = convert_update_type(param_obj, this.meta.update_fields);
|
|
403
403
|
if (error_field_names.length > 0) {
|
|
404
404
|
if (is_log_error()) {
|
|
405
405
|
log_error(LOG_ENTITY, "batch_update_entity error fields:" + JSON.stringify(error_field_names));
|
|
@@ -524,6 +524,16 @@ class Entity {
|
|
|
524
524
|
|
|
525
525
|
const results = await this.find(query, attrs);
|
|
526
526
|
if (results && results.length == 1) {
|
|
527
|
+
if (this.meta.after_read) {
|
|
528
|
+
const { code, err } = await this.meta.after_read(_id, attr_names, results[0]);
|
|
529
|
+
if (err || code != SUCCESS) {
|
|
530
|
+
if (is_log_error()) {
|
|
531
|
+
log_error(LOG_ENTITY, "after_read error:" + JSON.stringify(err) + ", with code:" + code);
|
|
532
|
+
}
|
|
533
|
+
return { code: code, err: err };
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
527
537
|
const converted = await this.convert_ref_attrs(results);
|
|
528
538
|
if (converted && converted.length == 1) {
|
|
529
539
|
if (is_log_debug()) {
|
package/db/gridfs.js
CHANGED
|
@@ -77,6 +77,24 @@ class GridFS {
|
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
* @param {mongodb bucket name} bucketname
|
|
83
|
+
* @param {the file name} filename
|
|
84
|
+
* @param {the dest file name} dest_file
|
|
85
|
+
*/
|
|
86
|
+
async pipe_file(bucketname, filename, dest_filename) {
|
|
87
|
+
const bucket = new GridFSBucket(this.db, { chunkSizeBytes: 1024 * 1024, bucketName: bucketname });
|
|
88
|
+
const stream = bucket.openDownloadStreamByName(filename);
|
|
89
|
+
const write_stream = require("fs").createWriteStream(dest_filename);
|
|
90
|
+
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
stream.pipe(write_stream)
|
|
93
|
+
.on('error', err => reject(err))
|
|
94
|
+
.on('finish', item => resolve(item));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
80
98
|
/**
|
|
81
99
|
* Delete the files by file name
|
|
82
100
|
* @param {bucket name} bucketname
|
|
@@ -179,6 +197,17 @@ const read_file = async (collection, filename, response) => {
|
|
|
179
197
|
await instance.read_file(collection, filename, response);
|
|
180
198
|
}
|
|
181
199
|
|
|
200
|
+
/**
|
|
201
|
+
* pipe file from gridfs
|
|
202
|
+
* @param {collection name} collection
|
|
203
|
+
* @param {file name} filename
|
|
204
|
+
* @param {dest file name} dest_filename
|
|
205
|
+
*/
|
|
206
|
+
const pipe_file = async (collection, filename, dest_filename) => {
|
|
207
|
+
const instance = await get_gridfs_instance();
|
|
208
|
+
await instance.pipe_file(collection, filename, dest_filename);
|
|
209
|
+
}
|
|
210
|
+
|
|
182
211
|
/**
|
|
183
212
|
* delete file from gridfs
|
|
184
213
|
* @param {collection name} collection
|
|
@@ -189,4 +218,4 @@ const delete_file = async (collection, filename) => {
|
|
|
189
218
|
await instance.delete_files(collection, filename);
|
|
190
219
|
}
|
|
191
220
|
|
|
192
|
-
module.exports = { set_file_fields, save_file_fields_to_db, save_file, read_file, delete_file };
|
|
221
|
+
module.exports = { set_file_fields, save_file_fields_to_db, save_file, read_file, pipe_file, delete_file };
|
package/http/params.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { has_value } = require('../core/validate');
|
|
1
|
+
const { has_value, is_undefined } = require('../core/validate');
|
|
2
2
|
|
|
3
3
|
const parse_params = function (input, params) {
|
|
4
4
|
const obj = {};
|
|
@@ -18,6 +18,17 @@ const post_params = function (req, params) {
|
|
|
18
18
|
return parse_params(req.body, params);
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
const post_update_params = function (req, params) {
|
|
22
|
+
const input = req.body;
|
|
23
|
+
const obj = {};
|
|
24
|
+
params.forEach(function (param) {
|
|
25
|
+
if (!is_undefined(input[param])) {
|
|
26
|
+
obj[param] = input[param];
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return obj;
|
|
30
|
+
};
|
|
31
|
+
|
|
21
32
|
const required_params = function (input, params) {
|
|
22
33
|
const obj = {};
|
|
23
34
|
let passed = true;
|
|
@@ -43,4 +54,4 @@ const required_post_params = function (req, params) {
|
|
|
43
54
|
return required_params(req.body, params);
|
|
44
55
|
};
|
|
45
56
|
|
|
46
|
-
module.exports = { get_params, post_params, required_get_params, required_post_params, required_params }
|
|
57
|
+
module.exports = { get_params, post_params, post_update_params, required_get_params, required_post_params, required_params }
|
package/package.json
CHANGED
package/router/update.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { set_file_fields, save_file_fields_to_db } = require('../db/gridfs');
|
|
2
|
-
const { required_post_params,
|
|
2
|
+
const { required_post_params, post_update_params } = require('../http/params');
|
|
3
3
|
const { SUCCESS, NO_PARAMS } = require('../http/code');
|
|
4
4
|
const { has_value } = require('../core/validate');
|
|
5
5
|
const { wrap_http } = require('../http/error');
|
|
@@ -27,7 +27,7 @@ const init_update_router = function (router, meta) {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
const param_obj =
|
|
30
|
+
const param_obj = post_update_params(req, meta.field_names);
|
|
31
31
|
set_file_fields(meta, req, param_obj);
|
|
32
32
|
|
|
33
33
|
const { code, err } = await entity.update_entity(params["_id"], param_obj);
|
|
@@ -49,7 +49,7 @@ const init_update_router = function (router, meta) {
|
|
|
49
49
|
return;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
const param_obj =
|
|
52
|
+
const param_obj = post_update_params(req, meta.field_names);
|
|
53
53
|
set_file_fields(meta, req, param_obj);
|
|
54
54
|
|
|
55
55
|
const { code, err } = await entity.batch_update_entity(params["_ids"], param_obj);
|