node-rigorous
Version:
Rigorous Framework
47 lines (33 loc) • 1.24 kB
JavaScript
const mongoose = require('mongoose');
const RigorousError = require('../../../facades/RigorousError');
const errorsMessages = require('../../../etc/errorsMessages');
const mongoRead = require('./read');
module.exports = async (modelName, queryRead, attributeObjectUpdated, params) => {
try {
const op = mongoose.model(modelName)
.findOneAndUpdate(
queryRead,
attributeObjectUpdated,
{ new: true },
).select({ _id: 1 });
// Update
const result = await op.exec();
if (result === null) { throw new RigorousError(errorsMessages.DataNotFoundError); }
const ids = [];
if (Array.isArray(result)) {
result.forEach((resultUnit) => {
ids.push(resultUnit._id);
});
} else {
ids.push(result._id);
}
const resultJson = await mongoRead(modelName, { _id: { $in: ids } }, params);
return resultJson;
} catch (errGlobal) {
if (errGlobal instanceof RigorousError) {
throw errGlobal;
} else {
throw new RigorousError(errorsMessages.OperationError, errGlobal);
}
}
};