mongodb-cg-lib
Version:
Library to make basic MongoDB operations
46 lines (38 loc) • 1.49 kB
JavaScript
const { constants, log, helpers } = require('utils-nxg-cg');
const { objectMongodb, extraProp } = require('./objects');
const { mongoTransform } = require('./transforms');
/**
* Method for process the data user and execute query single or batch by transactions
* @param msg
* @param cfg
* @param test
* @returns {Promise<array|object>}
*/
const mongoProcess = async (msg = {}, cfg = {}, test = false) => {
try {
const { data } = msg;
let properties = { ...objectMongodb };
let extraProps = { ...extraProp };
if (!test && !data) {
throw Error(`${constants.ERROR_PROPERTY} data`);
}
const valid = helpers.validProperties(properties, data, cfg);
if (valid) {
helpers.validProperties(extraProps, data, cfg, true);
properties = { ...properties, ...extraProps };
if (properties.crud == "insert") {
if (!helpers.validateBase64(properties.content)) {
properties.content = Buffer.from(JSON.stringify(properties.content)).toString("base64");
}
if (!helpers.isObjectValid(properties.content)) {
throw Error(constants.ERROR_JSON_FORMAT);
}
}
return await mongoTransform(properties);
}
} catch (e) {
log.error(e);
throw Error(e.toString());
}
};
module.exports = { mongoProcess };