Skip to content

Commit 14f3784

Browse files
authored
Merge pull request #28 from dynamiccast/use-primary-key
Use Model's primary key instead of hard coded `id`
2 parents dc052a8 + 54be7fe commit 14f3784

File tree

6 files changed

+108
-3
lines changed

6 files changed

+108
-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);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* CustomController
3+
*
4+
* @description :: Server-side logic for managing customs
5+
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
6+
*/
7+
8+
module.exports = {
9+
10+
};
11+

tests/dummy/api/models/Custom.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Custom.js
3+
*
4+
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
5+
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
6+
*/
7+
8+
module.exports = {
9+
10+
autoPK: false,
11+
12+
attributes: {
13+
key: {
14+
type: 'integer',
15+
autoIncrement: true,
16+
primaryKey: true,
17+
unique: true,
18+
index: true
19+
}
20+
},
21+
22+
autoCreatedAt: false,
23+
autoUpdatedAt: false
24+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var clone = require('clone');
2+
var request = require('supertest');
3+
var JSONAPIValidator = require('jsonapi-validator').Validator;
4+
5+
validateJSONapi = function(res) {
6+
var validator = new JSONAPIValidator();
7+
8+
validator.validate(res.body);
9+
};
10+
11+
describe("Custom primary key name", function() {
12+
13+
describe("POST /customs", function() {
14+
15+
it('Should return a unique identifier from custom sails primaryKey', function (done) {
16+
17+
var categoryToCreate = {
18+
'data': {
19+
'attributes': {
20+
},
21+
'type':'customs'
22+
}
23+
};
24+
25+
categoryCreated = clone(categoryToCreate);
26+
categoryCreated.data.id = "1";
27+
28+
request(sails.hooks.http.app)
29+
.post('/customs')
30+
.send(categoryToCreate)
31+
.expect(201)
32+
.expect(validateJSONapi)
33+
.expect(categoryCreated)
34+
.end(done);
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)