From 48c4f25b09ba75c607a7d0dc5ee642d8e233be64 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Sat, 12 Jul 2014 00:02:16 +0200 Subject: [PATCH] Renamed mapping to properties --- lib/relation-definition.js | 33 ++++++++++++++++----------------- test/relations.test.js | 20 ++++++++++---------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 18993ff8..704bcfaf 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -69,7 +69,7 @@ function RelationDefinition(definition) { this.modelThrough = definition.modelThrough; this.keyThrough = definition.keyThrough; this.multiple = (this.type !== 'belongsTo' && this.type !== 'hasOne'); - this.mapping = definition.mapping || {}; + this.properties = definition.properties || {}; this.scope = definition.scope; } @@ -107,19 +107,19 @@ RelationDefinition.prototype.applyScope = function(modelInstance, filter) { }; /** - * Apply the configured mapping to the target object. + * Apply the configured properties to the target object. * @param {Object} modelInstance * @param {Object} target */ -RelationDefinition.prototype.applyMapping = function(modelInstance, target) { - if (typeof this.mapping === 'function') { - var data = this.mapping.call(this, modelInstance); +RelationDefinition.prototype.applyProperties = function(modelInstance, target) { + if (typeof this.properties === 'function') { + var data = this.properties.call(this, modelInstance); for(var k in data) { target[k] = data[k]; } - } else if (typeof this.mapping === 'object') { - for(var k in this.mapping) { - var key = this.mapping[k]; + } else if (typeof this.properties === 'object') { + for(var k in this.properties) { + var key = this.properties[k]; target[key] = modelInstance[k]; } } @@ -362,7 +362,7 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) { keyTo: fk, modelTo: modelTo, multiple: true, - mapping: params.mapping, + properties: params.properties, scope: params.scope }); @@ -486,7 +486,7 @@ HasManyThrough.prototype.create = function create(data, done) { d[fk1] = modelInstance[definition.keyFrom]; d[fk2] = to[pk2]; - definition.applyMapping(modelInstance, d); + definition.applyProperties(modelInstance, d); // Then create the through model modelThrough.create(d, function (e, through) { @@ -533,7 +533,7 @@ HasManyThrough.prototype.add = function (acInst, done) { data[fk1] = this.modelInstance[pk1]; data[fk2] = acInst[pk2] || acInst; - definition.applyMapping(this.modelInstance, data); + definition.applyProperties(this.modelInstance, data); // Create an instance of the through model modelThrough.findOrCreate(filter, data, function(err, ac) { @@ -662,7 +662,6 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) { fn.returns = {arg: relationName, type: 'object', root: true}; modelFrom.prototype['__get__' + relationName] = fn; - }; BelongsTo.prototype.create = function(targetModelData, cb) { @@ -710,7 +709,7 @@ BelongsTo.prototype.related = function (refresh, params) { var pk = this.definition.keyTo; var fk = this.definition.keyFrom; var modelInstance = this.modelInstance; - + if (arguments.length === 1) { params = refresh; refresh = false; @@ -842,7 +841,7 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) { keyFrom: pk, keyTo: fk, modelTo: modelTo, - mapping: params.mapping + properties: params.properties }); modelFrom.dataSource.defineForeignKey(modelTo.modelName, fk, modelFrom.modelName); @@ -885,7 +884,7 @@ HasOne.prototype.create = function (targetModelData, cb) { query.where[fk] = targetModelData[fk]; this.definition.applyScope(modelInstance, query); - this.definition.applyMapping(modelInstance, targetModelData); + this.definition.applyProperties(modelInstance, targetModelData); modelTo.findOne(query, function(err, result) { if(err) { @@ -928,7 +927,7 @@ HasMany.prototype.create = function (targetModelData, cb) { targetModelData = targetModelData || {}; targetModelData[fk] = modelInstance[pk]; - this.definition.applyMapping(modelInstance, targetModelData); + this.definition.applyProperties(modelInstance, targetModelData); modelTo.create(targetModelData, function(err, targetModel) { if(!err) { @@ -953,7 +952,7 @@ HasMany.prototype.build = HasOne.prototype.build = function(targetModelData) { targetModelData = targetModelData || {}; targetModelData[fk] = this.modelInstance[pk]; - this.definition.applyMapping(this.modelInstance, targetModelData); + this.definition.applyProperties(this.modelInstance, targetModelData); return new modelTo(targetModelData); }; diff --git a/test/relations.test.js b/test/relations.test.js index 0ba08a3c..2fae2b9b 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -126,9 +126,9 @@ describe('relations', function () { }); }); - describe('hasMany with mapping', function () { - it('can be declared with mapping', function (done) { - Book.hasMany(Chapter, { mapping: { type: 'bookType' } }); + describe('hasMany with properties', function () { + it('can be declared with properties', function (done) { + Book.hasMany(Chapter, { properties: { type: 'bookType' } }); db.automigrate(done); }); @@ -146,14 +146,14 @@ describe('relations', function () { }); describe('hasMany with scope', function () { - it('can be declared with mapping', function (done) { + it('can be declared with properties', function (done) { Category.hasMany(Product, { - mapping: function(inst) { + properties: function(inst) { if (!inst.productType) return; // skip return { type: inst.productType }; }, scope: function(inst, filter) { - var m = this.mapping(inst); // re-use mapping + var m = this.properties(inst); // re-use properties if (m) return { where: m }; } }); @@ -198,13 +198,13 @@ describe('relations', function () { // a reference to the parent scope/instance: ctx.instance // in order to enforce a (dynamic scope) at runtime // a temporary property can be set in the beforeRemoting - // handler. Optionally, a dynamic mapping can be declared. + // handler. Optionally,properties dynamic properties can be declared. // // The code below simulates this. - it('should create record on scope - mapped', function (done) { + it('should create record on scope - properties', function (done) { Category.findOne(function (err, c) { - c.productType = 'tool'; // temporary, for mapping + c.productType = 'tool'; // temporary c.products.create(function(err, p) { p.categoryId.should.equal(c.id); p.type.should.equal('tool'); @@ -304,7 +304,7 @@ describe('relations', function () { }); it('can be declared using hasOne method', function () { - Supplier.hasOne(Account, { mapping: { name: 'supplierName' } }); + Supplier.hasOne(Account, { properties: { name: 'supplierName' } }); Object.keys((new Account()).toObject()).should.include('supplierId'); (new Supplier()).account.should.be.an.instanceOf(Function); });