How to use the sails/lib/hooks/blueprints/actionUtil.parseModel 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 / update.js View on Github external
module.exports = function updateOneRecord(req, res, base, recursive) {

  // Look up the model
  let Model = actionUtil.parseModel(req);

  if (!Model.mapping) {
    return fallback(req, res);
  }

  // Locate and validate the required `id` parameter.
  let pk = actionUtil.requirePk(req);

  // Default the value blacklist to just "id", so that models that have an
  // "id" field that is _not_ the primary key don't have the id field
  // updated mistakenly.  See https://github.com/balderdashy/sails/issues/3625
  req.options.values           = req.options.values || {};
  req.options.values.blacklist = req.options.values.blacklist || ['id'];

  // Create `values` object (monolithic combination of all parameters)
  // But omit the blacklisted params (like JSONP callback param, etc.)
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 / findone.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 pk = actionUtil.requirePk(req);
  const query = Model.find(pk, fields.length > 0 ? {select: fields} : null);
  const findQuery = _.reduce(_.intersection(populate, takeAliases(Model.associations)), populateAliases, query);

  findQuery
    .then(record => record[0] ? res.ok(record[0]) : res.notFound())
    .catch(res.negotiate);
};
github ghaiklor / generator-sails-rest-api / generators / blueprint / templates / api / blueprints / create.js View on Github external
module.exports = (req, res) => {
  const Model = actionUtil.parseModel(req);
  const values = actionUtil.parseValues(req);

  Model
    .create(_.omit(values, 'id'))
    .then(res.created)
    .catch(res.negotiate);
};
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 / index.js View on Github external
req.getRepository = (Entity) => {
            if (!Entity) {
              Entity = actionUtil.parseModel(req).Entity;
            }

            return getManager().getRepository(Entity);
          };
github tarlepp / Taskboard / backend / api / base / Controller.js View on Github external
schema: function(request, response) {
        var model = actionUtil.parseModel(request);

        var data = {
            attributes: model.schema,
            associations: model.associations
        };

        response.json(200, data);
    }
};
github ghaiklor / generator-sails-rest-api / generators / hook / templates / api / hooks / CountHook.js View on Github external
const defaultCountBlueprint = (req, res) => {
  const Model = actionUtil.parseModel(req);
  const countQuery = Model.count();

  countQuery.then(count => res.ok({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});
        }
      })
    ;
  }
};
github ghaiklor / generator-sails-rest-api / generators / blueprint / templates / Blueprint.js View on Github external
module.exports = (req, res) => {
  const Model = actionUtil.parseModel(req);

  res.ok();
};