How to use the sails/lib/hooks/blueprints/actionUtil.parseValues 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
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.)
  let values = actionUtil.parseValues(req);

  // No matter what, don't allow changing the PK via the update blueprint
  // (you should just drop and re-add the record if that's what you really want)
  if (typeof values[Model.primaryKey] !== 'undefined' && values[Model.primaryKey] != pk) {
    req._sails.log.warn('Cannot change primary key via update blueprint; ignoring value sent for `' + Model.primaryKey + '`');
  }

  // Make sure the primary key is unchanged
  values[Model.primaryKey] = pk;
  let manager              = req.getManager();
  let populator            = req.wetland.getPopulator(manager);
  base                     = typeof base === 'object' ? base : populator.findDataForUpdate(pk, Model.Entity, values);

  return Promise.resolve(base).then(base => {
    if (!base) {
      res.notFound();
github ArekSredzki / electron-release-server / api / controllers / AssetController.js View on Github external
create: function(req, res) {
    // Create data object (monolithic combination of all parameters)
    // Omit the blacklisted params (like JSONP callback param, etc.)
    var data = actionUtil.parseValues(req);

    if (!data.version) {
      return res.badRequest('A version is required.');
    }

    if (_.isString(data.version)) {
      // Only a id was provided, normalize
      data.version = {
        id: data.version
      };
    } else if (data.version && data.version.id) {
      // Valid request, but we only want the id
      data.version = {
        id: data.version.id
      };
    } else {
github ghaiklor / generator-sails-rest-api / generators / blueprint / templates / api / blueprints / update.js View on Github external
module.exports = (req, res) => {
  const Model = actionUtil.parseModel(req);
  const pk = actionUtil.requirePk(req);
  const values = actionUtil.parseValues(req);
  const pkName = Model.primaryKey;
  const criteria = {};
  criteria[pkName] = pk;
  
  Model
    .update(criteria, _.omit(values, pkName))
    .then(records => records[0] ? res.ok(records[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 SpoonX / sails-hook-wetland / blueprints / create.js View on Github external
module.exports = function createRecord(req, res, recursive) {
  let Model = actionUtil.parseModel(req);

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

  let data      = actionUtil.parseValues(req);
  let manager   = req.getManager();
  let newRecord = req.wetland.getPopulator(manager).assign(Model.Entity, data, null, recursive);

  return manager.persist(newRecord).flush().then(() => {
    res.created(newRecord);

    return newRecord;
  }).catch(res.serverError);
};