How to use the sails/lib/hooks/blueprints/actionUtil.parseCriteria 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 SpoonX / sails-hook-wetland / blueprints / find.js View on Github external
// Look up the model
  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));
  }
github pantsel / konga / api / policies / activeNodeData.js View on Github external
module.exports = function activeNodeData(request, response, next) {
  sails.log.verbose(__filename + ':' + __line + ' [Policy.activeNodeData() called]');


  // sails.config.kong_admin_url = request.headers['kong-admin-url'] || sails.config.kong_admin_url

  var c = actionUtil.parseCriteria(request)

  if(c.hasOwnProperty('or')){
    c['and'] = [{node_id : request.node_id}]
  }else{
    c['node_id'] = request.node_id
  }

  request.query.where = JSON.stringify(c)


  return  next()


};
github tarlepp / Taskboard / backend / api / services / RightsGet.js View on Github external
exports.makeConditionValidProperty = function(validIds, property, request) {
    // Parse where criteria
    var where = actionUtil.parseCriteria(request);

    // Normalize valid id array
    validIds = _.map(validIds, function(id) {
        return parseInt(id, 10);
    });

    // Specified property is not yet in where query
    if (!where[property]) {
        where[property] = validIds;
    } else { // We have id condition set so we need to check if that / those are allowed
        // Normalize current ids
        var currentIds = _.map((!_.isArray(where[property])) ? [where[property]] : where[property], function (id) {
            return parseInt(id, 10);
        });

        // Remove not valid ids
github tarlepp / Taskboard / backend / api / services / Data.js View on Github external
exports.getCollection = function(request, extraCriteria, next) {
    // Determine model name and used default criteria object
    var model = actionUtil.parseModel(request);
    var criteria = actionUtil.parseCriteria(request);

    // Merge extra criteria to main criteria object
    if (extraCriteria) {
        criteria = _.merge(criteria, extraCriteria);
    }

    // Fetch data from database
    model
        .find(criteria)
        .exec(function found(error, items) {
            next(error, items);
        });
};
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)
      }
    }])
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));
};
github tarlepp / Taskboard / backend / api / base / Controller.js View on Github external
count: function(request, response) {
        var model = actionUtil.parseModel(request);

        model
            .count(actionUtil.parseCriteria(request))
            .exec(function found(error, count) {
                if (error) {
                    response.json(500, error);
                } else {
                    response.json(200, {count: count});
                }
            });
    },
github pantsel / konga / api / base / Controller.js View on Github external
count: function count(request, response) {
    var Model = actionUtil.parseModel(request);

    Model
      .count(actionUtil.parseCriteria(request))
      .exec(function found(error, count) {
        if (error) {
          response.negotiate(error);
        } else {
          response.ok({count: count});
        }
      })
    ;
  }
};