nodejs-rigorous
Version:
Rigorous Framework
64 lines (44 loc) • 2.15 kB
JavaScript
const mongoose = require('mongoose');
const { RigorousError, errorTypes } = require('../../../factory/RigorousError/index');
const mongoRead = require('./read');
const callbackResult = require('./callback_result');
const helper_format_checker = require('../../../helpers/h_format_checker');
module.exports = (
collectionName,
attributeObjectUpdated,
beforeCreateTransformation,
params,
) => {
return new Promise((resolve, reject) => {
try {
if (helper_format_checker.isNil(params)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION); }
if (helper_format_checker.isNil(params.selectAttributesReturned) && helper_format_checker.isObjectEmpty(params.selectAttributesReturned)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION); }
if (beforeCreateTransformation) {
// dynamic Transformation
beforeCreateTransformation(attributeObjectUpdated);
}
// Create
mongoose.model(collectionName).create(attributeObjectUpdated,
(err, resultUnfiltered) => {
callbackResult.exec(err, resultUnfiltered)
.then((result) => {
const ids = [];
if (Array.isArray(result)) {
result.forEach((resultUnit) => {
ids.push(resultUnit.id);
});
} else {
ids.push(result.id);
}
mongoRead(collectionName, { _id: { $in: ids } }, params)
.then((resultJson) => {
return resolve(resultJson);
});
})
.catch((err2) => { return reject(err2); });
});
} catch (errGlobal) {
reject(errGlobal);
}
});
};