nodejs-rigorous
Version:
Rigorous Framework
48 lines (32 loc) • 1.72 kB
JavaScript
const { RigorousError, errorTypes } = require('../../../factory/RigorousError/index');
const callbackResult = require('./callback_result');
const patch = require('./patch');
const helper_format_checker = require('../../../helpers/h_format_checker');
/**
* params.virtualReturnedFields = [{ path: 'owner', select: { xx: 1, yy: 1 } }]
*/
module.exports = (mongoose, collectionName, query, params) => {
return new Promise((resolve, reject) => {
try {
if (helper_format_checker.isNil(params)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION_OP_PARAMS_MISSING); }
if (helper_format_checker.isNil(params.selectAttributesReturned) && helper_format_checker.isObjectEmpty(params.selectAttributesReturned)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION); }
const { selectAttributesReturned } = params;
const { virtualReturnedFields } = params;
const op = mongoose.model(collectionName)
.findOne(query)
.select(selectAttributesReturned);
if (virtualReturnedFields) {
virtualReturnedFields.forEach((virtualFieldReturned) => { op.populate(virtualFieldReturned); });
}
op.exec((err, resultUnfiltered) => {
callbackResult.exec(err, resultUnfiltered)
.then((resultOne) => {
const resultJson = patch.leanWithId(resultOne);
return resolve(resultJson);
}).catch((err2) => { return reject(err2); });
});
} catch (errGlobal) {
reject(errGlobal);
}
});
};