From a24b222a725c942c4fe0add2af141b7dcd798091 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Thu, 8 Jan 2015 15:34:04 +0100 Subject: [PATCH 1/2] Don't coerce nested objects into Model instances For queries, this is undesirable. It also affects loopback-connector-mongodb's ability to perform nested queries (it expects plain objects to work correctly, and to allow $elemMatch for example). --- lib/dao.js | 5 +++++ test/datatype.test.js | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index f800d79c..3444fc5f 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -21,6 +21,7 @@ var setScopeValuesFromWhere = utils.setScopeValuesFromWhere; var mergeQuery = utils.mergeQuery; var util = require('util'); var assert = require('assert'); +var Model = require('./model'); /** * Base class for all persistent objects. @@ -593,6 +594,10 @@ DataAccessObject._coerce = function (where) { continue; } + if (DataType.prototype instanceof Model) { + continue; + } + if (DataType === geo.GeoPoint) { // Skip the GeoPoint as the near operator breaks the assumption that // an operation has only one property diff --git a/test/datatype.test.js b/test/datatype.test.js index 13f83a20..b4761ffa 100644 --- a/test/datatype.test.js +++ b/test/datatype.test.js @@ -7,13 +7,16 @@ describe('datatypes', function () { before(function (done) { db = getSchema(); + Nested = db.define('Nested', {}); + Model = db.define('Model', { str: String, date: Date, num: Number, bool: Boolean, list: {type: [String]}, - arr: Array + arr: Array, + nested: Nested }); db.automigrate(function () { Model.destroyAll(done); @@ -114,4 +117,10 @@ describe('datatypes', function () { }); } }); + + it('should not coerce nested objects into ModelConstructor types', function() { + var coerced = Model._coerce({ nested: { foo: 'bar' } }); + coerced.nested.constructor.name.should.equal('Object'); + }); + }); From a19b778cbdef07fab6daac8dedec68b19dc034f6 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Thu, 8 Jan 2015 15:44:28 +0100 Subject: [PATCH 2/2] Change Model to BaseModel for clarity --- lib/dao.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 3444fc5f..a4d12867 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -21,7 +21,7 @@ var setScopeValuesFromWhere = utils.setScopeValuesFromWhere; var mergeQuery = utils.mergeQuery; var util = require('util'); var assert = require('assert'); -var Model = require('./model'); +var BaseModel = require('./model'); /** * Base class for all persistent objects. @@ -594,7 +594,7 @@ DataAccessObject._coerce = function (where) { continue; } - if (DataType.prototype instanceof Model) { + if (DataType.prototype instanceof BaseModel) { continue; }