From 6eb18cb2f6e7ca6450b4e1e2238cd1d9082440c7 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 10 Jul 2015 10:03:51 -0700 Subject: [PATCH] Make sure base property definitions are cloned Sub models sometimes need to customize the properties from the base model. This change allows each sub model has its own copy of the base property definition to avoid potential conflicts across multiple sub models of the same base. --- lib/datasource.js | 1 + lib/model-builder.js | 8 +++++++- test/loopback-dl.test.js | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/datasource.js b/lib/datasource.js index 549e9734..2a8b7b35 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -498,6 +498,7 @@ DataSource.prototype.setupDataAccess = function (modelClass, settings) { // Set the default id type from connector's ability var idType = this.connector.getDefaultIdType() || String; idProp.type = idType; + modelClass.definition.rawProperties[idName].type = idType; modelClass.definition.properties[idName].type = idType; if (settings.forceId) { modelClass.validatesAbsenceOf(idName, {if: 'isNewRecord'}); diff --git a/lib/model-builder.js b/lib/model-builder.js index 89b5f765..b36e4d41 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -377,7 +377,13 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett continue; } if (subclassProperties[key] === undefined) { - subclassProperties[key] = properties[key]; + var baseProp = properties[key]; + var basePropCopy = baseProp; + if (baseProp && typeof baseProp === 'object') { + // Deep clone the base prop + basePropCopy = mergeSettings(null, baseProp); + } + subclassProperties[key] = basePropCopy; } } diff --git a/test/loopback-dl.test.js b/test/loopback-dl.test.js index b5d0c658..14d9892b 100644 --- a/test/loopback-dl.test.js +++ b/test/loopback-dl.test.js @@ -691,6 +691,22 @@ describe('Load models with base', function () { Customer.definition.properties.name.should.have.property('type', String); }); + it('should inherit properties by clone from base option', function () { + var ds = new ModelBuilder(); + + var User = ds.define('User', {name: String}); + + var Customer1 = ds.define('Customer1', {vip: Boolean}, {base: 'User'}); + var Customer2 = ds.define('Customer2', {vip: Boolean}, {base: 'User'}); + + Customer1.definition.properties.should.have.property('name'); + Customer2.definition.properties.should.have.property('name'); + Customer1.definition.properties.name.should.not.be.equal( + Customer2.definition.properties.name); + Customer1.definition.properties.name.should.eql( + Customer2.definition.properties.name); + }); + it('should revert properties from base model', function() { var ds = new ModelBuilder();