typeTo => discriminator
This commit is contained in:
parent
5a5aa3f14d
commit
4e76c2a77f
|
@ -65,8 +65,8 @@ function RelationDefinition(definition) {
|
|||
this.keyFrom = definition.keyFrom;
|
||||
this.modelTo = definition.modelTo;
|
||||
this.keyTo = definition.keyTo;
|
||||
this.typeTo = definition.typeTo;
|
||||
if (!this.typeTo) {
|
||||
this.discriminator = definition.discriminator;
|
||||
if (!this.discriminator) {
|
||||
assert(this.modelTo, 'Target model is required');
|
||||
}
|
||||
this.modelThrough = definition.modelThrough;
|
||||
|
@ -102,8 +102,8 @@ RelationDefinition.prototype.toJSON = function () {
|
|||
RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
|
||||
filter.where = filter.where || {};
|
||||
if ((this.type !== 'belongsTo' || this.type === 'hasOne')
|
||||
&& typeof this.typeTo === 'string') { // polymorphic
|
||||
filter.where[this.typeTo] = this.modelFrom.modelName;
|
||||
&& typeof this.discriminator === 'string') { // polymorphic
|
||||
filter.where[this.discriminator] = this.modelFrom.modelName;
|
||||
}
|
||||
if (typeof this.scope === 'function') {
|
||||
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')
|
||||
&& typeof this.typeTo === 'string') { // polymorphic
|
||||
target[this.typeTo] = this.modelFrom.modelName;
|
||||
&& typeof this.discriminator === 'string') { // polymorphic
|
||||
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 idName = modelFrom.dataSource.idName(modelFrom.modelName) || 'id';
|
||||
var typeTo;
|
||||
var discriminator;
|
||||
|
||||
if (typeof params.polymorphic === 'string') {
|
||||
var polymorphic = params.polymorphic;
|
||||
typeTo = i8n.camelize(polymorphic + '_type', true);
|
||||
discriminator = i8n.camelize(polymorphic + '_type', true);
|
||||
if (!params.invert) {
|
||||
fk = i8n.camelize(polymorphic + '_id', true);
|
||||
}
|
||||
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,
|
||||
keyFrom: idName,
|
||||
keyTo: fk,
|
||||
typeTo: typeTo,
|
||||
discriminator: discriminator,
|
||||
modelTo: modelTo,
|
||||
multiple: true,
|
||||
properties: params.properties,
|
||||
|
@ -448,7 +448,7 @@ RelationDefinition.hasMany = function hasMany(modelFrom, modelTo, params) {
|
|||
definition.applyScope(this, filter);
|
||||
|
||||
if (params.through && params.polymorphic && params.invert) {
|
||||
filter.where[typeTo] = modelTo.modelName; // overwrite
|
||||
filter.where[discriminator] = modelTo.modelName; // overwrite
|
||||
filter.collect = params.polymorphic;
|
||||
filter.include = filter.collect;
|
||||
} else if (params.through) {
|
||||
|
@ -584,7 +584,7 @@ var throughKeys = function(definition) {
|
|||
var modelThrough = definition.modelThrough;
|
||||
var pk2 = definition.modelTo.definition.idName();
|
||||
|
||||
if (definition.typeTo) { // polymorphic
|
||||
if (definition.discriminator) { // polymorphic
|
||||
var fk1 = definition.keyTo;
|
||||
var fk2 = definition.keyThrough;
|
||||
} else {
|
||||
|
@ -845,7 +845,7 @@ HasManyThrough.prototype.remove = function (acInst, done) {
|
|||
*
|
||||
*/
|
||||
RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
|
||||
var typeTo, params = params || {};
|
||||
var discriminator, params = params || {};
|
||||
if ('string' === typeof modelTo && !params.polymorphic) {
|
||||
params.as = modelTo;
|
||||
if (params.model) {
|
||||
|
@ -863,7 +863,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
|
|||
var idName = params.idName || 'id';
|
||||
var relationName = params.as || i8n.camelize(polymorphic, 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
|
||||
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.defineProperty(modelFrom.modelName, typeTo, { type: 'string', index: true });
|
||||
modelFrom.dataSource.defineProperty(modelFrom.modelName, discriminator, { type: 'string', index: true });
|
||||
} else {
|
||||
var idName = modelFrom.dataSource.idName(modelTo.modelName) || 'id';
|
||||
var relationName = params.as || i8n.camelize(modelTo.modelName, true);
|
||||
|
@ -886,7 +886,7 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) {
|
|||
modelFrom: modelFrom,
|
||||
keyFrom: fk,
|
||||
keyTo: idName,
|
||||
typeTo: typeTo,
|
||||
discriminator: discriminator,
|
||||
modelTo: modelTo,
|
||||
properties: params.properties,
|
||||
scope: params.scope,
|
||||
|
@ -965,7 +965,7 @@ BelongsTo.prototype.related = function (refresh, params) {
|
|||
var self = this;
|
||||
var modelFrom = this.definition.modelFrom;
|
||||
var modelTo = this.definition.modelTo;
|
||||
var typeTo = this.definition.typeTo;
|
||||
var discriminator = this.definition.discriminator;
|
||||
var pk = this.definition.keyTo;
|
||||
var fk = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
|
@ -984,14 +984,14 @@ BelongsTo.prototype.related = function (refresh, params) {
|
|||
if (params instanceof ModelBaseClass) { // acts as setter
|
||||
modelTo = params.constructor;
|
||||
modelInstance[fk] = params[pk];
|
||||
if (typeTo) modelInstance[typeTo] = params.constructor.modelName;
|
||||
if (discriminator) modelInstance[discriminator] = params.constructor.modelName;
|
||||
self.resetCache(params);
|
||||
} else if (typeof params === 'function') { // acts as async getter
|
||||
|
||||
if (typeTo && !modelTo) {
|
||||
var modelToName = modelInstance[typeTo];
|
||||
if (discriminator && !modelTo) {
|
||||
var modelToName = modelInstance[discriminator];
|
||||
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();
|
||||
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 fk = params.foreignKey || i8n.camelize(modelFrom.modelName + '_id', true);
|
||||
var typeTo;
|
||||
var discriminator;
|
||||
|
||||
if (typeof params.polymorphic === 'string') {
|
||||
var polymorphic = params.polymorphic;
|
||||
fk = i8n.camelize(polymorphic + '_id', true);
|
||||
typeTo = i8n.camelize(polymorphic + '_type', true);
|
||||
discriminator = i8n.camelize(polymorphic + '_type', true);
|
||||
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,
|
||||
keyFrom: pk,
|
||||
keyTo: fk,
|
||||
typeTo: typeTo,
|
||||
discriminator: discriminator,
|
||||
modelTo: modelTo,
|
||||
properties: params.properties,
|
||||
options: params.options
|
||||
|
|
Loading…
Reference in New Issue