UNPKG

generator-sails-rest-api

Version:

Yeoman generator that provides already configured and optimized Sails REST API with bundle of predefined features

25 lines (21 loc) 689 B
"use strict"; const _ = require('lodash'); const actionUtil = require('sails/lib/hooks/blueprints/actionUtil'); /** * Update One Record * PUT /:model/:id * * An API call to update a model instance with the specified `id`, treating the other unbound parameters as attributes. */ 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); };