Skip to content

Commit 8a68d05

Browse files
committed
Merge pull request #76 from digitalsadhu/update/dynamic_primary_keys
Update/dynamic primary keys
2 parents 0a5622e + 0382e08 commit 8a68d05

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

lib/serialize.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module.exports = function (app, defaults) {
2424
modelNamePlural,
2525
relatedModelPlural,
2626
relations,
27-
res;
27+
res,
28+
model;
2829

2930
var matches = regexs.filter(function (regex) {
3031
return ctx.methodString.match(regex);
@@ -48,7 +49,8 @@ module.exports = function (app, defaults) {
4849
}
4950

5051
data = utils.clone(ctx.result);
51-
modelNamePlural = utils.pluralForModel(utils.getModelFromContext(ctx, app));
52+
model = utils.getModelFromContext(ctx, app);
53+
modelNamePlural = utils.pluralForModel(model);
5254
type = modelNamePlural;
5355

5456
/**
@@ -77,7 +79,10 @@ module.exports = function (app, defaults) {
7779
}
7880
}
7981

82+
var primaryKeyField = utils.primaryKeyForModel(model);
83+
8084
options = {
85+
primaryKeyField: primaryKeyField,
8186
host: utils.hostFromContext(ctx),
8287
topLevelLinks: {
8388
self: utils.urlFromContext(ctx)

lib/serializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function parseResource (type, data, relations, options) {
5151
}
5252

5353
_.each(data, function (value, property) {
54-
if (property === 'id') {
54+
if (property === options.primaryKeyField) {
5555
resource.id = _(value).toString();
5656
} else {
5757
attributes[property] = value;

lib/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ module.exports = {
1212
hostFromContext: hostFromContext,
1313
modelNameFromContext: modelNameFromContext,
1414
pluralForModel: pluralForModel,
15-
urlFromContext: urlFromContext
15+
urlFromContext: urlFromContext,
16+
primaryKeyForModel: primaryKeyForModel
1617
};
1718

19+
function primaryKeyForModel (model) {
20+
return model.getIdName();
21+
}
22+
1823
/**
1924
* Returns the plural for a model.
2025
* @public

test/find.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,31 @@ describe('loopback json api component find methods', function () {
255255
});
256256
});
257257
});
258+
259+
describe('non standard primary key naming', function () {
260+
beforeEach(function (done) {
261+
app = loopback();
262+
app.set('legacyExplorer', false);
263+
var ds = loopback.createDataSource('memory');
264+
Post = ds.createModel('post', {
265+
customId: {type: Number, id: true, generated: true},
266+
title: String
267+
});
268+
app.model(Post);
269+
app.use(loopback.rest());
270+
JSONAPIComponent(app);
271+
Post.create({title: 'my post'}, done);
272+
});
273+
274+
it('should dynamically handle primary key', function (done) {
275+
request(app)
276+
.get('/posts')
277+
.expect(200)
278+
.end(function (err, res) {
279+
expect(err).to.equal(null);
280+
expect(res.body.data.length).to.equal(1);
281+
expect(res.body.data[0].id).to.equal('1');
282+
done();
283+
});
284+
});
285+
});

test/update.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,34 @@ describe('loopback json api component update method', function () {
262262
});
263263
});
264264
});
265+
266+
describe('non standard primary key naming', function () {
267+
beforeEach(function (done) {
268+
app = loopback();
269+
app.set('legacyExplorer', false);
270+
var ds = loopback.createDataSource('memory');
271+
Post = ds.createModel('post', {
272+
customId: {type: Number, id: true, generated: true},
273+
title: String
274+
});
275+
app.model(Post);
276+
app.use(loopback.rest());
277+
JSONAPIComponent(app);
278+
Post.create({title: 'my post'}, done);
279+
});
280+
281+
it('should dynamically handle primary key', function (done) {
282+
request(app).patch('/posts/1').send({
283+
data: {id: 1, type: 'posts',
284+
attributes: {title: 'my post 2'}
285+
}})
286+
.expect(200)
287+
.end(function (err, res) {
288+
expect(err).to.equal(null);
289+
expect(res.body.data.id).to.equal('1');
290+
expect(res.body.data.attributes.title).to.equal('my post 2');
291+
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/1/);
292+
done();
293+
});
294+
});
295+
});

0 commit comments

Comments
 (0)