node-rigorous
Version:
Rigorous Framework
93 lines (73 loc) • 2.94 kB
JavaScript
;
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
var mongoose = require('mongoose');
var _require = require('../../../factory/RigorousError/index'),
RigorousError = _require.RigorousError,
errorTypes = _require.errorTypes;
var mongoRead = require('./read');
module.exports =
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(modelName, queryRead, attributeObjectUpdated, params) {
var op, result, ids, resultJson;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
op = mongoose.model(modelName).findOneAndUpdate(queryRead, attributeObjectUpdated, {
"new": true
}).select({
_id: 1
}); // Update
_context.next = 4;
return op.exec();
case 4:
result = _context.sent;
if (!(result === null)) {
_context.next = 7;
break;
}
throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_FOUND);
case 7:
ids = [];
if (Array.isArray(result)) {
result.forEach(function (resultUnit) {
ids.push(resultUnit.id);
});
} else {
ids.push(result.id);
}
_context.next = 11;
return mongoRead(modelName, {
_id: {
$in: ids
}
}, params);
case 11:
resultJson = _context.sent;
return _context.abrupt("return", resultJson);
case 15:
_context.prev = 15;
_context.t0 = _context["catch"](0);
if (!(_context.t0 instanceof RigorousError)) {
_context.next = 21;
break;
}
throw _context.t0;
case 21:
throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION, _context.t0);
case 22:
case "end":
return _context.stop();
}
}
}, _callee, null, [[0, 15]]);
}));
return function (_x, _x2, _x3, _x4) {
return _ref.apply(this, arguments);
};
}();