Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = (req, res) => {
_.set(req.options, 'criteria.blacklist', ['fields', 'populate', 'limit', 'skip', 'page', 'sort']);
const fields = req.param('fields') ? req.param('fields').replace(/ /g, '').split(',') : [];
const populate = req.param('populate') ? req.param('populate').replace(/ /g, '').split(',') : [];
const Model = actionUtil.parseModel(req);
const where = actionUtil.parseCriteria(req);
const limit = actionUtil.parseLimit(req);
const skip = req.param('page') * limit || actionUtil.parseSkip(req);
const sort = actionUtil.parseSort(req);
const query = Model.find(null, fields.length > 0 ? {select: fields} : null).where(where).limit(limit).skip(skip).sort(sort);
const findQuery = _.reduce(_.intersection(populate, takeAlias(Model.associations)), populateAlias, query);
findQuery
.then(records => [records, {
root: {
criteria: where,
limit: limit,
start: skip,
end: skip + limit,
page: Math.floor(skip / limit)
}
}])
.spread(res.ok)
.catch(res.negotiate);
module.exports = function findRecords(req, res) {
let Model = actionUtil.parseModel(req);
let criteria = actionUtil.parseCriteria(req);
if (!Model) {
return res.badRequest('invalid_parameter');
}
let options = {
limit : actionUtil.parseLimit(req),
offset : actionUtil.parseSkip(req),
orderBy: actionUtil.parseSort(req)
};
count(req.getRepository(Model.Entity), criteria, options)
.then(count => res.ok({count}))
.catch(error => res.serverError('database_error', error));
};
let countInResponse = sails.config.blueprints.countInResponse;
if (!Model.mapping) {
return fallback(req, res);
}
if (actionUtil.parsePk(req)) {
return require('./findOne')(req, res);
}
let populate = actionUtil.populateRequest(null, req);
let repository = req.getRepository(Model.Entity);
let criteria = actionUtil.parseCriteria(req);
let options = {
limit : actionUtil.parseLimit(req),
offset : actionUtil.parseSkip(req),
orderBy: actionUtil.parseSort(req)
};
if (populate && populate.length && populate[0] !== undefined) {
options.populate = populate;
}
let promises = [repository.find(criteria, options)];
if (countInResponse) {
promises.push(count(repository, criteria, options));
}
Promise.all(promises)
.then(results => {
if (countInResponse) {
exports.sortAndPaginate = function(items, request) {
var limit = actionUtil.parseLimit(request);
var skip = actionUtil.parseSkip(request);
var sort = !_.isUndefined(actionUtil.parseSort(request)) ? actionUtil.parseSort(request).split(' ') : ['id'];
items = _.sortBy(items, sort[0]);
if (sort[1] === 'DESC') {
items.reverse();
}
return items.slice(skip, (skip + limit));
};