base-repository
Version:
[](https://travis-ci.org/joehua87/base-repository)
544 lines (440 loc) • 14.5 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseRequestQuery = parseRequestQuery;
exports.createController = createController;
var debug = require('debug')('base-repository:create-controller');
// Export here for easy testing
function parseRequestQuery(request) {
var filter = request.query.filter;
var projection = request.query.projection;
var sort = request.query.sort;
var getAll = request.query.getAll === 'true';
var page = parseInt(request.query.page, 10);
var limit = parseInt(request.query.limit, 10);
var select = { projection: projection, sort: sort, page: page, limit: limit, getAll: getAll };
// Remove to use default params in Base Repository
if (!projection) {
delete select.projection;
}
if (!sort) {
delete select.sort;
}
if (isNaN(page)) {
delete select.page;
}
if (isNaN(limit)) {
delete select.limit;
}
return { filter: filter, select: select };
}
function createController(repository) {
var _marked = [query, update, validateUpdate, create, insert, getByKey, getById, getByIds, getByFilter, deleteById, addChild, removeChild, getConfig, getSchema].map(regeneratorRuntime.mark);
function query() {
var _parseRequestQuery, filter, select, response;
return regeneratorRuntime.wrap(function query$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_parseRequestQuery = parseRequestQuery(this.request);
filter = _parseRequestQuery.filter;
select = _parseRequestQuery.select;
_context.next = 5;
return repository.query(filter, select);
case 5:
response = _context.sent;
this.status = 200;
this.body = response;
case 8:
case 'end':
return _context.stop();
}
}
}, _marked[0], this);
}
function update() {
var entity, response;
return regeneratorRuntime.wrap(function update$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
entity = this.request.body;
if (!entity._id) {
this.throw(400, 'Body required _id to update');
}
_context2.next = 4;
return repository.update(entity._id, entity);
case 4:
response = _context2.sent;
this.status = 201;
this.body = response;
case 7:
case 'end':
return _context2.stop();
}
}
}, _marked[1], this);
}
function validateUpdate() {
var entity, response, pattern, matches;
return regeneratorRuntime.wrap(function validateUpdate$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
entity = this.request.body;
if (!entity._id) {
this.throw(400, 'Body required _id to update');
}
_context3.prev = 2;
_context3.next = 5;
return repository.validateUpdate(entity._id, entity);
case 5:
response = _context3.sent;
this.status = 201;
this.body = response;
_context3.next = 13;
break;
case 10:
_context3.prev = 10;
_context3.t0 = _context3['catch'](2);
if (_context3.t0.name === 'ValidationError') {
this.throw(400, 'Validate fail at: ' + Object.keys(_context3.t0.errors).join(', '));
} else {
pattern = /E11000 duplicate key error index: (.*?)\.(.*?) dup key: ({ : ".*?" })/;
matches = pattern.exec(_context3.t0.message);
if (matches) {
this.throw(400, 'Duplicated at: ' + matches[2] + ' ' + matches[3]);
}
this.throw(400, _context3.t0);
}
case 13:
case 'end':
return _context3.stop();
}
}
}, _marked[2], this, [[2, 10]]);
}
function create() {
var filter, entity, _response;
return regeneratorRuntime.wrap(function create$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
filter = this.request.query.filter;
entity = this.request.body;
_context4.prev = 2;
_context4.next = 5;
return repository.create(filter, entity);
case 5:
_response = _context4.sent;
this.status = 201;
this.body = _response;
_context4.next = 14;
break;
case 10:
_context4.prev = 10;
_context4.t0 = _context4['catch'](2);
if (_context4.t0.name === 'ValidationError') {
this.throw(400, 'Validate fail at: ' + Object.keys(_context4.t0.errors).join(', '));
}
this.throw(400, _context4.t0);
case 14:
case 'end':
return _context4.stop();
}
}
}, _marked[3], this, [[2, 10]]);
}
function insert() {
var entities, _response2;
return regeneratorRuntime.wrap(function insert$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
entities = this.request.body;
_context5.prev = 1;
_context5.next = 4;
return repository.insert(entities);
case 4:
_response2 = _context5.sent;
this.status = 201;
this.body = _response2;
_context5.next = 13;
break;
case 9:
_context5.prev = 9;
_context5.t0 = _context5['catch'](1);
if (_context5.t0.name === 'ValidationError') {
this.throw(400, 'Validate fail at: ' + Object.keys(_context5.t0.errors).join(', '));
}
this.throw(400, _context5.t0);
case 13:
case 'end':
return _context5.stop();
}
}
}, _marked[4], this, [[1, 9]]);
}
function getByKey() {
var key, response;
return regeneratorRuntime.wrap(function getByKey$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
key = this.params.key;
if (!key) {
this.throw(400, 'Request params required key');
}
_context6.next = 4;
return repository.getByKey(key);
case 4:
response = _context6.sent;
this.status = 200;
this.body = response;
case 7:
case 'end':
return _context6.stop();
}
}
}, _marked[5], this);
}
function getById() {
var id, projection, response;
return regeneratorRuntime.wrap(function getById$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
id = this.params.id;
projection = this.request.query.projection;
debug('get by id: ' + id);
if (!id) {
this.throw(400, 'Params require query');
}
_context7.next = 6;
return repository.getById(id, projection);
case 6:
response = _context7.sent;
debug('get bt id response: ' + response);
this.status = 200;
this.body = response;
case 10:
case 'end':
return _context7.stop();
}
}
}, _marked[6], this);
}
function getByIds() {
var ids, projection, response;
return regeneratorRuntime.wrap(function getByIds$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
ids = this.request.query.ids || [];
projection = this.request.query.projection;
if (typeof ids === 'string') {
ids = [ids];
}
debug('get by id: ' + ids.join(', '));
if (!ids) {
this.throw(400, 'Params require query');
}
_context8.next = 7;
return repository.getByIds(ids, projection);
case 7:
response = _context8.sent;
debug('get bt id response: ' + response);
this.status = 200;
this.body = response;
case 11:
case 'end':
return _context8.stop();
}
}
}, _marked[7], this);
}
function getByFilter() {
var filter, sort, projection, response;
return regeneratorRuntime.wrap(function getByFilter$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
filter = this.request.query.filter;
sort = this.request.query.sort;
projection = this.request.query.projection;
debug('get by filter (sort = ' + sort + ')');
debug(filter);
_context9.next = 7;
return repository.getByFilter(filter, { sort: sort, projection: projection });
case 7:
response = _context9.sent;
this.status = 200;
this.body = response;
case 10:
case 'end':
return _context9.stop();
}
}
}, _marked[8], this);
}
function deleteById() {
var id, _response3;
return regeneratorRuntime.wrap(function deleteById$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
id = this.params.id;
if (!id) {
this.throw(400, 'Request params required id');
}
_context10.prev = 2;
_context10.next = 5;
return repository.deleteById(id);
case 5:
_response3 = _context10.sent;
this.status = 200;
this.body = _response3;
_context10.next = 13;
break;
case 10:
_context10.prev = 10;
_context10.t0 = _context10['catch'](2);
this.throw(400, _context10.t0);
case 13:
case 'end':
return _context10.stop();
}
}
}, _marked[9], this, [[2, 10]]);
}
function addChild() {
var id, field, item, _response4, _pattern, _matches;
return regeneratorRuntime.wrap(function addChild$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
id = this.params.id;
if (!id) {
this.throw(400, 'Request params required id');
}
field = this.params.field;
if (!field) {
this.throw(400, 'Request params required field');
}
item = this.request.body;
if (!item) {
this.throw(400, 'Body require item to add');
}
_context11.prev = 6;
_context11.next = 9;
return repository.addChild(id, field, item);
case 9:
_response4 = _context11.sent;
this.status = 201;
this.body = _response4;
_context11.next = 17;
break;
case 14:
_context11.prev = 14;
_context11.t0 = _context11['catch'](6);
if (_context11.t0.name === 'ValidationError') {
this.throw(400, 'Validate fail at: ' + Object.keys(_context11.t0.errors).join(', '));
} else {
_pattern = /E11000 duplicate key error index: (.*?)\.(.*?) dup key: ({ : ".*?" })/;
_matches = _pattern.exec(_context11.t0.message);
if (_matches) {
this.throw(400, 'Duplicated at: ' + _matches[2] + ' ' + _matches[3]);
}
this.throw(400, _context11.t0);
}
case 17:
case 'end':
return _context11.stop();
}
}
}, _marked[10], this, [[6, 14]]);
}
function removeChild() {
var id, field, itemId, _response5;
return regeneratorRuntime.wrap(function removeChild$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
id = this.params.id;
if (!id) {
this.throw(400, 'Request params required id');
}
field = this.params.field;
if (!field) {
this.throw(400, 'Request params required field');
}
itemId = this.params.itemId;
if (!itemId) {
this.throw(400, 'Request params required itemId');
}
_context12.prev = 6;
_context12.next = 9;
return repository.removeChild(id, field, itemId);
case 9:
_response5 = _context12.sent;
this.status = 200;
this.body = _response5;
_context12.next = 17;
break;
case 14:
_context12.prev = 14;
_context12.t0 = _context12['catch'](6);
this.throw(400, _context12.t0);
case 17:
case 'end':
return _context12.stop();
}
}
}, _marked[11], this, [[6, 14]]);
}
function getConfig() {
return regeneratorRuntime.wrap(function getConfig$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
this.status = 200;
this.body = repository.getConfig();
case 2:
case 'end':
return _context13.stop();
}
}
}, _marked[12], this);
}
function getSchema() {
return regeneratorRuntime.wrap(function getSchema$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
this.status = 200;
this.body = repository.getSchema();
case 2:
case 'end':
return _context14.stop();
}
}
}, _marked[13], this);
}
return {
query: query,
create: create,
insert: insert,
update: update,
validateUpdate: validateUpdate,
getByKey: getByKey,
getById: getById,
getByIds: getByIds,
getByFilter: getByFilter,
deleteById: deleteById,
addChild: addChild,
removeChild: removeChild,
getConfig: getConfig,
getSchema: getSchema
};
}