How to use the sails/lib/hooks/blueprints/actionUtil.parseLimit function in sails

To help you get started, we’ve selected a few sails examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ghaiklor / generator-sails-rest-api / generators / blueprint / templates / api / blueprints / find.js View on Github external
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)
github SpoonX / sails-hook-wetland / blueprints / find.js View on Github external
let Model           = actionUtil.parseModel(req);
  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 => {
github tarlepp / Taskboard / backend / api / services / Data.js View on Github external
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));
};
github SpoonX / sails-hook-wetland / blueprints / count.js View on Github external
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));
};