From c37de7f00844199ac0868c7dae1b4e59319d856b Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Wed, 21 Jan 2015 20:16:34 +0800 Subject: [PATCH] fix id properties should sort by its index Signed-off-by: Clark Wang --- lib/model-definition.js | 2 +- test/model-definition.test.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/model-definition.js b/lib/model-definition.js index 32bbcd1c..21094812 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -141,7 +141,7 @@ ModelDefinition.prototype.ids = function () { ids.push({name: key, id: id, property: props[key]}); } ids.sort(function (a, b) { - return a.key - b.key; + return a.id - b.id; }); this._ids = ids; return ids; diff --git a/test/model-definition.test.js b/test/model-definition.test.js index 342dfde3..b44fd971 100644 --- a/test/model-definition.test.js +++ b/test/model-definition.test.js @@ -215,6 +215,28 @@ describe('ModelDefinition class', function () { done(); }); + it('should sort id properties by its index', function () { + var modelBuilder = new ModelBuilder(); + + var User = new ModelDefinition(modelBuilder, 'User', { + userId: {type: String, id: 2}, + userType: {type: String, id: 1}, + name: "string", + bio: ModelBuilder.Text, + approved: Boolean, + joinedAt: Date, + age: "number" + }); + + var ids = User.ids(); + assert.ok(Array.isArray(ids)); + assert.equal(ids.length, 2); + assert.equal(ids[0].id, 1); + assert.equal(ids[0].name, 'userType'); + assert.equal(ids[1].id, 2); + assert.equal(ids[1].name, 'userId'); + }); + it('should report correct table/column names', function (done) { var modelBuilder = new ModelBuilder();