UNPKG

nodejs-rigorous

Version:
366 lines (299 loc) 13.1 kB
"use strict"; 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); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var _require = require('../../factory/RigorousError/index'), RigorousError = _require.RigorousError, errorTypes = _require.errorTypes; var mongoCreate = require('./mongoose/create'); var mongoUpdate = require('./mongoose/update'); var mongoDelete = require('./mongoose/delete'); var mongoRead = require('./mongoose/read'); var mongoReadMany = require('./mongoose/read_many'); var rigorousConfig = require('../../config'); var ObjectCrud = /*#__PURE__*/ function () { function ObjectCrud(collectionName, attributesSettings, paginateMaxResultPerPage, beforeCreateTransformation) { _classCallCheck(this, ObjectCrud); this.collectionName = collectionName; this.attributesSettings = attributesSettings; this.beforeCreateTransformation = beforeCreateTransformation; this.paginateMaxResultPerPage = paginateMaxResultPerPage; } _createClass(ObjectCrud, [{ key: "createObject", value: function () { var _createObject = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2(attributesToBeDefined, params) { var _this = this; var instanciateObject, result; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: instanciateObject = {}; _context2.prev = 1; _context2.next = 4; return Promise.all(Object.keys(this.attributesSettings).map( /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(key) { var attr, onInstanciationValue, attrName; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: attr = _this.attributesSettings[key]; onInstanciationValue = attr.on.create.value; attrName = key; if (!(onInstanciationValue === rigorousConfig.TO_BE_DEFINE_WITH_BEFORE_CREATE_TRANSFORMATION)) { _context.next = 5; break; } return _context.abrupt("return"); case 5: if (onInstanciationValue === rigorousConfig.TO_BE_DEFINE_ON_CREATE) { instanciateObject[attrName] = attributesToBeDefined[attrName]; } else { instanciateObject[attrName] = onInstanciationValue; } if (!attr.on.create.transform) { _context.next = 10; break; } _context.next = 9; return attr.on.create.transform(instanciateObject[attrName]); case 9: instanciateObject[attrName] = _context.sent; case 10: case "end": return _context.stop(); } } }, _callee); })); return function (_x3) { return _ref.apply(this, arguments); }; }())); case 4: _context2.next = 6; return mongoCreate(this.collectionName, instanciateObject, this.beforeCreateTransformation, params); case 6: result = _context2.sent; return _context2.abrupt("return", result); case 10: _context2.prev = 10; _context2.t0 = _context2["catch"](1); throw _context2.t0; case 13: case "end": return _context2.stop(); } } }, _callee2, this, [[1, 10]]); })); function createObject(_x, _x2) { return _createObject.apply(this, arguments); } return createObject; }() }, { key: "updateObject", value: function () { var _updateObject = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee4(queryRead, jsonFieldsUpdated, params) { var _this2 = this; var queryUpdate, result; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: queryUpdate = {}; _context4.prev = 1; _context4.next = 4; return Promise.all(Object.keys(this.attributesSettings).map( /*#__PURE__*/ function () { var _ref2 = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee3(key) { var attrSetting, isUpdatable, updateValue; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: if (!(key in jsonFieldsUpdated)) { _context3.next = 15; break; } attrSetting = _this2.attributesSettings[key]; if (attrSetting) { _context3.next = 4; break; } throw new RigorousError(errorTypes.RESPONSE_ERROR_DEVELOPER); case 4: isUpdatable = attrSetting.on.update.updatable; if (!isUpdatable) { _context3.next = 14; break; } updateValue = jsonFieldsUpdated[key]; if (!attrSetting.on.update.transform) { _context3.next = 11; break; } _context3.next = 10; return attrSetting.on.update.transform(updateValue); case 10: updateValue = _context3.sent; case 11: queryUpdate[key] = updateValue; _context3.next = 15; break; case 14: throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION_NOT_UPDATABLE); case 15: case "end": return _context3.stop(); } } }, _callee3); })); return function (_x7) { return _ref2.apply(this, arguments); }; }())); case 4: _context4.next = 6; return mongoUpdate(this.collectionName, queryRead, queryUpdate, params); case 6: result = _context4.sent; return _context4.abrupt("return", result); case 10: _context4.prev = 10; _context4.t0 = _context4["catch"](1); throw _context4.t0; case 13: case "end": return _context4.stop(); } } }, _callee4, this, [[1, 10]]); })); function updateObject(_x4, _x5, _x6) { return _updateObject.apply(this, arguments); } return updateObject; }() }, { key: "deleteObject", value: function () { var _deleteObject = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee5(queryDelete) { var result; return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _context5.prev = 0; _context5.next = 3; return mongoDelete(this.collectionName, queryDelete); case 3: result = _context5.sent; return _context5.abrupt("return", result); case 7: _context5.prev = 7; _context5.t0 = _context5["catch"](0); throw _context5.t0; case 10: case "end": return _context5.stop(); } } }, _callee5, this, [[0, 7]]); })); function deleteObject(_x8) { return _deleteObject.apply(this, arguments); } return deleteObject; }() }, { key: "readObject", value: function () { var _readObject = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee6(mongoose, query, params) { var result; return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: _context6.prev = 0; _context6.next = 3; return mongoRead(mongoose, this.collectionName, query, params); case 3: result = _context6.sent; return _context6.abrupt("return", result); case 7: _context6.prev = 7; _context6.t0 = _context6["catch"](0); throw _context6.t0; case 10: case "end": return _context6.stop(); } } }, _callee6, this, [[0, 7]]); })); function readObject(_x9, _x10, _x11) { return _readObject.apply(this, arguments); } return readObject; }() }, { key: "readManyObject", value: function () { var _readManyObject = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee7(query, lastPaginateId, reverse, params) { var result; return regeneratorRuntime.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: _context7.prev = 0; _context7.next = 3; return mongoReadMany(this.collectionName, query, this.paginateMaxResultPerPage, lastPaginateId, reverse, params); case 3: result = _context7.sent; return _context7.abrupt("return", result); case 7: _context7.prev = 7; _context7.t0 = _context7["catch"](0); throw _context7.t0; case 10: case "end": return _context7.stop(); } } }, _callee7, this, [[0, 7]]); })); function readManyObject(_x12, _x13, _x14, _x15) { return _readManyObject.apply(this, arguments); } return readManyObject; }() }]); return ObjectCrud; }(); module.exports = ObjectCrud;