Skip to content

Commit 54be7fe

Browse files
committed
Fix create() not working if primaryKey was different from id
Use Model.primaryKey to refer to the Model's primary key instead of hard-coded `id`
1 parent 053240c commit 54be7fe

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

lib/api/blueprints/_util/actionUtil.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ module.exports = {
3636
parseValues: function(req, model) {
3737

3838
var values = req.body.data.attributes || {};
39-
values.id = req.allParams()['id'];
39+
40+
if (req.allParams()['id']) {
41+
values[model.primaryKey] = req.allParams()['id'];
42+
}
4043

4144
return values;
4245
},

lib/api/blueprints/create.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ module.exports = function createRecord(req, res) {
2323

2424
if (err) return res.negotiate(err);
2525

26-
var Q = Model.findOne(newInstance.id);
26+
var Q = Model.findOne(newInstance[Model.primaryKey]);
2727
Q.exec( (err, newRecord) => {
2828

29+
if (err) {
30+
return res.negotiate(err);
31+
}
32+
2933
return res.created(newRecord);
3034
});
3135
});

lib/api/services/JsonApiService.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require("lodash");
2+
const pluralize = require('pluralize');
23
const JSONAPISerializer = require('json-api-serializer');
34
const jsonApiValidator = require('../../context-aware-jsonapi-validator/validator');
45
const Serializer = new JSONAPISerializer();
@@ -39,6 +40,22 @@ module.exports = {
3940
return caseSetting;
4041
},
4142

43+
/*
44+
* Method to get an sails Model from a model's name
45+
*
46+
* @method _getModelObjectFromModelName
47+
* @param {string} model name
48+
* @return {object} sails model
49+
*/
50+
_getModelObjectFromModelName: function(modelName) {
51+
52+
modelName = _.camelCase(modelName); // Model variable name are always in one word with no seprator
53+
modelName = pluralize.singular(modelName); // Model variable name are always singular
54+
modelName = _.upperFirst(modelName); // Model variable name always have their first letter capitalize
55+
56+
return global[modelName];
57+
},
58+
4259
// Code inspired from the MIT licenced module https://github.com/danivek/json-api-serializer
4360
_convertCase: function(data, convertCaseOptions) {
4461

@@ -84,9 +101,18 @@ module.exports = {
84101

85102
serialize: function(modelName, data) {
86103

104+
let Model = this._getModelObjectFromModelName(modelName);
105+
let id = 'id';
106+
107+
if (Model) {
108+
id = Model.primaryKey;
109+
} else {
110+
sails.log.warn('Model ' + modelName + ' could not be found by sails-json-api-blueprints');
111+
}
112+
87113
Serializer.register(modelName, {
88114
convertCase: this.getAttributesSerializedCaseSetting(),
89-
id: 'id'
115+
id: id
90116
});
91117

92118
var returnedValue = Serializer.serialize(modelName, data);

0 commit comments

Comments
 (0)