typeTo => discriminator

This commit is contained in:
Fabien Franzen 2014-07-28 10:18:42 +02:00
parent 5a5aa3f14d
commit 4e76c2a77f
1 changed files with 25 additions and 25 deletions

View File

@ -65,8 +65,8 @@ function RelationDefinition(definition) {
this.keyFrom = definition.keyFrom; this.keyFrom = definition.keyFrom;
this.modelTo = definition.modelTo; this.modelTo = definition.modelTo;
this.keyTo = definition.keyTo; this.keyTo = definition.keyTo;
this.typeTo = definition.typeTo; this.discriminator = definition.discriminator;
if (!this.typeTo) { if (!this.discriminator) {
assert(this.modelTo, 'Target model is required'); assert(this.modelTo, 'Target model is required');
} }
this.modelThrough = definition.modelThrough; this.modelThrough = definition.modelThrough;
@ -102,8 +102,8 @@ RelationDefinition.prototype.toJSON = function () {
RelationDefinition.prototype.applyScope = function(modelInstance, filter) { RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
filter.where = filter.where || {}; filter.where = filter.where || {};
if ((this.type !== 'belongsTo' || this.type === 'hasOne') if ((this.type !== 'belongsTo' || this.type === 'hasOne')
&& typeof this.typeTo === 'string') { // polymorphic && typeof this.discriminator === 'string') { // polymorphic
filter.where[this.typeTo] = this.modelFrom.modelName; filter.where[this.discriminator] = this.modelFrom.modelName;
} }
if (typeof this.scope === 'function') { if (typeof this.scope === 'function') {
var scope = this.scope.call(this, modelInstance, filter); var scope = this.scope.call(this, modelInstance, filter);
@ -133,8 +133,8 @@ RelationDefinition.prototype.applyProperties = function(modelInstance, target) {
} }
} }
if ((this.type !== 'belongsTo' || this.type === 'hasOne') if ((this.type !== 'belongsTo' || this.type === 'hasOne')
&& typeof this.typeTo === 'string') { // polymorphic && typeof this.discriminator === 'string') { // polymorphic
target[this.typeTo] = this.modelFrom.modelName; target[this.discriminator] = this.modelFrom.modelName;
} }
}; };
@ -367,16 +367,16 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
var fk = params.foreignKey || i8n.camelize(thisClassName + '_id', true); var fk = params.foreignKey || i8n.camelize(thisClassName + '_id', true);
var idName = modelFrom.dataSource.idName(modelFrom.modelName) || 'id'; var idName = modelFrom.dataSource.idName(modelFrom.modelName) || 'id';
var typeTo; var discriminator;
if (typeof params.polymorphic === 'string') { if (typeof params.polymorphic === 'string') {
var polymorphic = params.polymorphic; var polymorphic = params.polymorphic;
typeTo = i8n.camelize(polymorphic + '_type', true); discriminator = i8n.camelize(polymorphic + '_type', true);
if (!params.invert) { if (!params.invert) {
fk = i8n.camelize(polymorphic + '_id', true); fk = i8n.camelize(polymorphic + '_id', true);
} }
if (!params.through) { if (!params.through) {
modelTo.dataSource.defineProperty(modelTo.modelName, typeTo, { type: 'string', index: true }); modelTo.dataSource.defineProperty(modelTo.modelName, discriminator, { type: 'string', index: true });
} }
} }
@ -386,7 +386,7 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
modelFrom: modelFrom, modelFrom: modelFrom,
keyFrom: idName, keyFrom: idName,
keyTo: fk, keyTo: fk,
typeTo: typeTo, discriminator: discriminator,
modelTo: modelTo, modelTo: modelTo,
multiple: true, multiple: true,
properties: params.properties, properties: params.properties,
@ -448,7 +448,7 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
definition.applyScope(this, filter); definition.applyScope(this, filter);
if (params.through && params.polymorphic && params.invert) { if (params.through && params.polymorphic && params.invert) {
filter.where[typeTo] = modelTo.modelName; // overwrite filter.where[discriminator] = modelTo.modelName; // overwrite
filter.collect = params.polymorphic; filter.collect = params.polymorphic;
filter.include = filter.collect; filter.include = filter.collect;
} else if (params.through) { } else if (params.through) {
@ -584,7 +584,7 @@ var throughKeys = function(definition) {
var modelThrough = definition.modelThrough; var modelThrough = definition.modelThrough;
var pk2 = definition.modelTo.definition.idName(); var pk2 = definition.modelTo.definition.idName();
if (definition.typeTo) { // polymorphic if (definition.discriminator) { // polymorphic
var fk1 = definition.keyTo; var fk1 = definition.keyTo;
var fk2 = definition.keyThrough; var fk2 = definition.keyThrough;
} else { } else {
@ -845,7 +845,7 @@ HasManyThrough.prototype.remove = function (acInst, done) {
* *
*/ */
RelationDefinition.belongsTo = function (modelFrom, modelTo, params) { RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
var typeTo, params = params || {}; var discriminator, params = params || {};
if ('string' === typeof modelTo && !params.polymorphic) { if ('string' === typeof modelTo && !params.polymorphic) {
params.as = modelTo; params.as = modelTo;
if (params.model) { if (params.model) {
@ -863,7 +863,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
var idName = params.idName || 'id'; var idName = params.idName || 'id';
var relationName = params.as || i8n.camelize(polymorphic, true); var relationName = params.as || i8n.camelize(polymorphic, true);
var fk = i8n.camelize(polymorphic + '_id', true); var fk = i8n.camelize(polymorphic + '_id', true);
var typeTo = i8n.camelize(polymorphic + '_type', true); var discriminator = i8n.camelize(polymorphic + '_type', true);
if (typeof params.idType === 'string') { // explicit key type if (typeof params.idType === 'string') { // explicit key type
modelFrom.dataSource.defineProperty(modelFrom.modelName, fk, { type: params.idType, index: true }); modelFrom.dataSource.defineProperty(modelFrom.modelName, fk, { type: params.idType, index: true });
@ -871,7 +871,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
modelFrom.dataSource.defineForeignKey(modelFrom.modelName, fk, modelFrom.modelName); modelFrom.dataSource.defineForeignKey(modelFrom.modelName, fk, modelFrom.modelName);
} }
modelFrom.dataSource.defineProperty(modelFrom.modelName, typeTo, { type: 'string', index: true }); modelFrom.dataSource.defineProperty(modelFrom.modelName, discriminator, { type: 'string', index: true });
} else { } else {
var idName = modelFrom.dataSource.idName(modelTo.modelName) || 'id'; var idName = modelFrom.dataSource.idName(modelTo.modelName) || 'id';
var relationName = params.as || i8n.camelize(modelTo.modelName, true); var relationName = params.as || i8n.camelize(modelTo.modelName, true);
@ -886,7 +886,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
modelFrom: modelFrom, modelFrom: modelFrom,
keyFrom: fk, keyFrom: fk,
keyTo: idName, keyTo: idName,
typeTo: typeTo, discriminator: discriminator,
modelTo: modelTo, modelTo: modelTo,
properties: params.properties, properties: params.properties,
scope: params.scope, scope: params.scope,
@ -965,7 +965,7 @@ BelongsTo.prototype.related = function (refresh, params) {
var self = this; var self = this;
var modelFrom = this.definition.modelFrom; var modelFrom = this.definition.modelFrom;
var modelTo = this.definition.modelTo; var modelTo = this.definition.modelTo;
var typeTo = this.definition.typeTo; var discriminator = this.definition.discriminator;
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;
@ -984,14 +984,14 @@ BelongsTo.prototype.related = function (refresh, params) {
if (params instanceof ModelBaseClass) { // acts as setter if (params instanceof ModelBaseClass) { // acts as setter
modelTo = params.constructor; modelTo = params.constructor;
modelInstance[fk] = params[pk]; modelInstance[fk] = params[pk];
if (typeTo) modelInstance[typeTo] = params.constructor.modelName; if (discriminator) modelInstance[discriminator] = params.constructor.modelName;
self.resetCache(params); self.resetCache(params);
} else if (typeof params === 'function') { // acts as async getter } else if (typeof params === 'function') { // acts as async getter
if (typeTo && !modelTo) { if (discriminator && !modelTo) {
var modelToName = modelInstance[typeTo]; var modelToName = modelInstance[discriminator];
if (typeof modelToName !== 'string') { if (typeof modelToName !== 'string') {
throw new Error('Polymorphic model not found: `' + typeTo + '` not set'); throw new Error('Polymorphic model not found: `' + discriminator + '` not set');
} }
modelToName = modelToName.toLowerCase(); modelToName = modelToName.toLowerCase();
modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName); modelTo = lookupModel(modelFrom.dataSource.modelBuilder.models, modelToName);
@ -1134,14 +1134,14 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
var relationName = params.as || i8n.camelize(modelTo.modelName, true); var relationName = params.as || i8n.camelize(modelTo.modelName, true);
var fk = params.foreignKey || i8n.camelize(modelFrom.modelName + '_id', true); var fk = params.foreignKey || i8n.camelize(modelFrom.modelName + '_id', true);
var typeTo; var discriminator;
if (typeof params.polymorphic === 'string') { if (typeof params.polymorphic === 'string') {
var polymorphic = params.polymorphic; var polymorphic = params.polymorphic;
fk = i8n.camelize(polymorphic + '_id', true); fk = i8n.camelize(polymorphic + '_id', true);
typeTo = i8n.camelize(polymorphic + '_type', true); discriminator = i8n.camelize(polymorphic + '_type', true);
if (!params.through) { if (!params.through) {
modelTo.dataSource.defineProperty(modelTo.modelName, typeTo, { type: 'string', index: true }); modelTo.dataSource.defineProperty(modelTo.modelName, discriminator, { type: 'string', index: true });
} }
} }
@ -1151,7 +1151,7 @@ RelationDefinition.hasOne = function (modelFrom, modelTo, params) {
modelFrom: modelFrom, modelFrom: modelFrom,
keyFrom: pk, keyFrom: pk,
keyTo: fk, keyTo: fk,
typeTo: typeTo, discriminator: discriminator,
modelTo: modelTo, modelTo: modelTo,
properties: params.properties, properties: params.properties,
options: params.options options: params.options