Renamed mapping to properties
This commit is contained in:
parent
6aaa3f216e
commit
48c4f25b09
|
@ -69,7 +69,7 @@ function RelationDefinition(definition) {
|
||||||
this.modelThrough = definition.modelThrough;
|
this.modelThrough = definition.modelThrough;
|
||||||
this.keyThrough = definition.keyThrough;
|
this.keyThrough = definition.keyThrough;
|
||||||
this.multiple = (this.type !== 'belongsTo' && this.type !== 'hasOne');
|
this.multiple = (this.type !== 'belongsTo' && this.type !== 'hasOne');
|
||||||
this.mapping = definition.mapping || {};
|
this.properties = definition.properties || {};
|
||||||
this.scope = definition.scope;
|
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} modelInstance
|
||||||
* @param {Object} target
|
* @param {Object} target
|
||||||
*/
|
*/
|
||||||
RelationDefinition.prototype.applyMapping = function(modelInstance, target) {
|
RelationDefinition.prototype.applyProperties = function(modelInstance, target) {
|
||||||
if (typeof this.mapping === 'function') {
|
if (typeof this.properties === 'function') {
|
||||||
var data = this.mapping.call(this, modelInstance);
|
var data = this.properties.call(this, modelInstance);
|
||||||
for(var k in data) {
|
for(var k in data) {
|
||||||
target[k] = data[k];
|
target[k] = data[k];
|
||||||
}
|
}
|
||||||
} else if (typeof this.mapping === 'object') {
|
} else if (typeof this.properties === 'object') {
|
||||||
for(var k in this.mapping) {
|
for(var k in this.properties) {
|
||||||
var key = this.mapping[k];
|
var key = this.properties[k];
|
||||||
target[key] = modelInstance[k];
|
target[key] = modelInstance[k];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -362,7 +362,7 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
|
||||||
keyTo: fk,
|
keyTo: fk,
|
||||||
modelTo: modelTo,
|
modelTo: modelTo,
|
||||||
multiple: true,
|
multiple: true,
|
||||||
mapping: params.mapping,
|
properties: params.properties,
|
||||||
scope: params.scope
|
scope: params.scope
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -486,7 +486,7 @@ HasManyThrough.prototype.create = function create(data, done) {
|
||||||
d[fk1] = modelInstance[definition.keyFrom];
|
d[fk1] = modelInstance[definition.keyFrom];
|
||||||
d[fk2] = to[pk2];
|
d[fk2] = to[pk2];
|
||||||
|
|
||||||
definition.applyMapping(modelInstance, d);
|
definition.applyProperties(modelInstance, d);
|
||||||
|
|
||||||
// Then create the through model
|
// Then create the through model
|
||||||
modelThrough.create(d, function (e, through) {
|
modelThrough.create(d, function (e, through) {
|
||||||
|
@ -533,7 +533,7 @@ HasManyThrough.prototype.add = function (acInst, done) {
|
||||||
|
|
||||||
data[fk1] = this.modelInstance[pk1];
|
data[fk1] = this.modelInstance[pk1];
|
||||||
data[fk2] = acInst[pk2] || acInst;
|
data[fk2] = acInst[pk2] || acInst;
|
||||||
definition.applyMapping(this.modelInstance, data);
|
definition.applyProperties(this.modelInstance, data);
|
||||||
|
|
||||||
// Create an instance of the through model
|
// Create an instance of the through model
|
||||||
modelThrough.findOrCreate(filter, data, function(err, ac) {
|
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};
|
fn.returns = {arg: relationName, type: 'object', root: true};
|
||||||
|
|
||||||
modelFrom.prototype['__get__' + relationName] = fn;
|
modelFrom.prototype['__get__' + relationName] = fn;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
BelongsTo.prototype.create = function(targetModelData, cb) {
|
BelongsTo.prototype.create = function(targetModelData, cb) {
|
||||||
|
@ -710,7 +709,7 @@ BelongsTo.prototype.related = function (refresh, params) {
|
||||||
var pk = this.definition.keyTo;
|
var pk = this.definition.keyTo;
|
||||||
var fk = this.definition.keyFrom;
|
var fk = this.definition.keyFrom;
|
||||||
var modelInstance = this.modelInstance;
|
var modelInstance = this.modelInstance;
|
||||||
|
|
||||||
if (arguments.length === 1) {
|
if (arguments.length === 1) {
|
||||||
params = refresh;
|
params = refresh;
|
||||||
refresh = false;
|
refresh = false;
|
||||||
|
@ -842,7 +841,7 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
|
||||||
keyFrom: pk,
|
keyFrom: pk,
|
||||||
keyTo: fk,
|
keyTo: fk,
|
||||||
modelTo: modelTo,
|
modelTo: modelTo,
|
||||||
mapping: params.mapping
|
properties: params.properties
|
||||||
});
|
});
|
||||||
|
|
||||||
modelFrom.dataSource.defineForeignKey(modelTo.modelName, fk, modelFrom.modelName);
|
modelFrom.dataSource.defineForeignKey(modelTo.modelName, fk, modelFrom.modelName);
|
||||||
|
@ -885,7 +884,7 @@ HasOne.prototype.create = function (targetModelData, cb) {
|
||||||
query.where[fk] = targetModelData[fk];
|
query.where[fk] = targetModelData[fk];
|
||||||
|
|
||||||
this.definition.applyScope(modelInstance, query);
|
this.definition.applyScope(modelInstance, query);
|
||||||
this.definition.applyMapping(modelInstance, targetModelData);
|
this.definition.applyProperties(modelInstance, targetModelData);
|
||||||
|
|
||||||
modelTo.findOne(query, function(err, result) {
|
modelTo.findOne(query, function(err, result) {
|
||||||
if(err) {
|
if(err) {
|
||||||
|
@ -928,7 +927,7 @@ HasMany.prototype.create = function (targetModelData, cb) {
|
||||||
targetModelData = targetModelData || {};
|
targetModelData = targetModelData || {};
|
||||||
targetModelData[fk] = modelInstance[pk];
|
targetModelData[fk] = modelInstance[pk];
|
||||||
|
|
||||||
this.definition.applyMapping(modelInstance, targetModelData);
|
this.definition.applyProperties(modelInstance, targetModelData);
|
||||||
|
|
||||||
modelTo.create(targetModelData, function(err, targetModel) {
|
modelTo.create(targetModelData, function(err, targetModel) {
|
||||||
if(!err) {
|
if(!err) {
|
||||||
|
@ -953,7 +952,7 @@ HasMany.prototype.build = HasOne.prototype.build = function(targetModelData) {
|
||||||
targetModelData = targetModelData || {};
|
targetModelData = targetModelData || {};
|
||||||
targetModelData[fk] = this.modelInstance[pk];
|
targetModelData[fk] = this.modelInstance[pk];
|
||||||
|
|
||||||
this.definition.applyMapping(this.modelInstance, targetModelData);
|
this.definition.applyProperties(this.modelInstance, targetModelData);
|
||||||
|
|
||||||
return new modelTo(targetModelData);
|
return new modelTo(targetModelData);
|
||||||
};
|
};
|
||||||
|
|
|
@ -126,9 +126,9 @@ describe('relations', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasMany with mapping', function () {
|
describe('hasMany with properties', function () {
|
||||||
it('can be declared with mapping', function (done) {
|
it('can be declared with properties', function (done) {
|
||||||
Book.hasMany(Chapter, { mapping: { type: 'bookType' } });
|
Book.hasMany(Chapter, { properties: { type: 'bookType' } });
|
||||||
db.automigrate(done);
|
db.automigrate(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -146,14 +146,14 @@ describe('relations', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('hasMany with scope', 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, {
|
Category.hasMany(Product, {
|
||||||
mapping: function(inst) {
|
properties: function(inst) {
|
||||||
if (!inst.productType) return; // skip
|
if (!inst.productType) return; // skip
|
||||||
return { type: inst.productType };
|
return { type: inst.productType };
|
||||||
},
|
},
|
||||||
scope: function(inst, filter) {
|
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 };
|
if (m) return { where: m };
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -198,13 +198,13 @@ describe('relations', function () {
|
||||||
// a reference to the parent scope/instance: ctx.instance
|
// a reference to the parent scope/instance: ctx.instance
|
||||||
// in order to enforce a (dynamic scope) at runtime
|
// in order to enforce a (dynamic scope) at runtime
|
||||||
// a temporary property can be set in the beforeRemoting
|
// 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.
|
// 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) {
|
Category.findOne(function (err, c) {
|
||||||
c.productType = 'tool'; // temporary, for mapping
|
c.productType = 'tool'; // temporary
|
||||||
c.products.create(function(err, p) {
|
c.products.create(function(err, p) {
|
||||||
p.categoryId.should.equal(c.id);
|
p.categoryId.should.equal(c.id);
|
||||||
p.type.should.equal('tool');
|
p.type.should.equal('tool');
|
||||||
|
@ -304,7 +304,7 @@ describe('relations', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can be declared using hasOne method', 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');
|
Object.keys((new Account()).toObject()).should.include('supplierId');
|
||||||
(new Supplier()).account.should.be.an.instanceOf(Function);
|
(new Supplier()).account.should.be.an.instanceOf(Function);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue