From 1b9edbcfcb5a797063f417d8a3ce3ce74d98b960 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Sun, 15 Dec 2013 22:51:47 -0800 Subject: [PATCH] Fix a regression in mongodb connector --- lib/dao.js | 13 ++++++++++++- test/loopback-dl.test.js | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index 5207d4af..22f9cff2 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -392,8 +392,19 @@ DataAccessObject._coerce = function (where) { return Boolean(val); } }; + } else if(DataType === Number) { + // This fixes a regression in mongodb connector + // For numbers, only convert it produces a valid number + // LoopBack by default injects a number id. We should fix it based + // on the connector's input, for example, mongoddb should use string + // while RDBs typically use number + DataType = function(val) { + var num = Number(val); + return !isNaN(num) ? num : val; + }; } - if (!DataType) { + + if (!DataType) { continue; } diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index bd24a623..2cebc11d 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -662,6 +662,14 @@ describe('DataAccessObject', function () { assert.deepEqual(where, {date: undefined}); }); + it('should skip conversion if it produces NaN for numbers', function () { + where = model._coerce({age: 'xyz'}); + assert.deepEqual(where, {age: 'xyz'}); + + where = model._coerce({age: {inq: ['xyz', '12']}}); + assert.deepEqual(where, {age: {inq: ['xyz', 12]}}); + }); + }); describe('Load models from json', function () {