How to use the sails/lib/hooks/blueprints/actionUtil.requirePk 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 ArekSredzki / electron-release-server / api / controllers / AssetController.js View on Github external
destroy: function(req, res) {
    var pk = actionUtil.requirePk(req);

    var query = Asset.findOne(pk);
    query.populate('version');
    query
      .then(function foundRecord(record) {
        if (!record) return res.notFound(
          'No record found with the specified `name`.'
        );

        // Delete the file & remove from db
        return Promise.join(
            AssetService.destroy(record, req),
            AssetService.deleteFile(record),
            function() {})
          .then(function success() {
            res.ok(record);
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.)
  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 + '`');
  }
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 / 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 / destroy.js View on Github external
module.exports = (req, res) => {
  const Model = actionUtil.parseModel(req);
  const pk = actionUtil.requirePk(req);

  Model
    .destroy(pk)
    .then(records => records[0] ? res.ok(records[0]) : res.notFound())
    .catch(res.negotiate);
};
github ArekSredzki / electron-release-server / api / controllers / VersionController.js View on Github external
destroy: function(req, res) {
    var pk = actionUtil.requirePk(req);

    var query = Version.findOne(pk);
    query.populate('assets');
    query.exec(function foundRecord(err, record) {
      if (err) return res.serverError(err);
      if (!record) return res.notFound(
        'No record found with the specified `name`.'
      );

      var deletePromises = _.map(record.assets, function(asset) {
        return Promise.join(
          AssetService.destroy(asset, req),
          AssetService.deleteFile(asset),
          function() {
            sails.log.info('Destroyed asset: ', asset);
          });