refactored metadatas

This commit is contained in:
Umed Khudoiberdiev 2016-05-25 14:14:27 +05:00
parent 67e010e690
commit 697178f3b7
6 changed files with 49 additions and 65 deletions

View File

@ -264,18 +264,30 @@ export class EntityMetadata {
return entity[this.primaryColumn.propertyName];
}
/**
* Checks if column with the given property name exist.
*/
hasColumnWithPropertyName(propertyName: string): boolean {
return !!this.columns.find(column => column.propertyName === propertyName);
}
/**
* Checks if column with the given database name exist.
*/
hasColumnWithDbName(name: string): boolean {
return !!this.columns.find(column => column.name === name);
}
/**
* Checks if relation with the given property name exist.
*/
hasRelationWithPropertyName(propertyName: string): boolean {
return !!this.relations.find(relation => relation.propertyName === propertyName);
}
/**
* Finds relation with the given property name.
*/
findRelationWithPropertyName(propertyName: string): RelationMetadata {
const relation = this.relations.find(relation => relation.propertyName === propertyName);
if (!relation)
@ -284,70 +296,34 @@ export class EntityMetadata {
return relation;
}
/**
* Checks if relation with the given name exist.
*/
hasRelationWithDbName(dbName: string): boolean {
return !!this.relations.find(relation => relation.name === dbName);
return !!this.relationsWithJoinColumns.find(relation => relation.name === dbName);
}
findRelationWithDbName(propertyName: string): RelationMetadata {
const relation = this.relations.find(relation => relation.name === propertyName);
/**
* Finds relation with the given name.
*/
findRelationWithDbName(name: string): RelationMetadata {
const relation = this.relationsWithJoinColumns.find(relation => relation.name === name);
if (!relation)
throw new Error(`Relation with name ${propertyName} in ${this.name} entity was not found.`);
return relation;
}
hasRelationWithOneWithPropertyName(propertyName: string): boolean {
return !!this.relations.find(relation => relation.propertyName === propertyName && (relation.isOneToMany || relation.isOneToOne));
}
findRelationWithOneWithPropertyName(propertyName: string): RelationMetadata {
const relation = this.relations.find(relation => relation.propertyName === propertyName && (relation.isOneToMany || relation.isOneToOne));
if (!relation)
throw new Error(`Relation with one with property name ${propertyName} in ${this.name} entity was not found.`);
return relation;
}
hasRelationWithOneWithDbName(name: string): boolean {
return !!this.relations.find(relation => relation.name === name && (relation.isOneToMany || relation.isOneToOne));
}
findRelationWithOneWithDbName(name: string): RelationMetadata {
const relation = this.relations.find(relation => relation.name === name && (relation.isOneToMany || relation.isOneToOne));
if (!relation)
throw new Error(`Relation with one with name ${name} in ${this.name} entity was not found.`);
return relation;
}
hasRelationWithManyWithPropertyName(name: string): boolean {
return !!this.relations.find(relation => relation.propertyName === name && (relation.isManyToOne || relation.isManyToMany));
}
findRelationWithManyWithPropertyName(name: string): RelationMetadata {
const relation = this.relations.find(relation => relation.propertyName === name && (relation.isManyToOne || relation.isManyToMany));
if (!relation)
throw new Error(`Relation with many with property name ${name} in ${this.name} entity was not found.`);
return relation;
}
hasRelationWithManyWithDbName(name: string): boolean {
return !!this.relations.find(relation => relation.name === name && (relation.isManyToOne || relation.isManyToMany));
}
findRelationWithManyWithDbName(name: string): RelationMetadata {
const relation = this.relations.find(relation => relation.name === name && (relation.isManyToOne || relation.isManyToMany));
if (!relation)
throw new Error(`Relation with many with name ${name} in ${this.name} entity was not found.`);
throw new Error(`Relation with name ${name} in ${this.name} entity was not found.`);
return relation;
}
/**
* Checks if there is a tree parent relation. Used only in tree-tables.
*/
get hasTreeParentRelation() {
return !!this.relations.find(relation => relation.isTreeParent);
}
/**
* Tree parent relation. Used only in tree-tables.
*/
get treeParentRelation() {
const relation = this.relations.find(relation => relation.isTreeParent);
if (!relation)
@ -356,10 +332,16 @@ export class EntityMetadata {
return relation;
}
/**
* Checks if there is a tree children relation. Used only in tree-tables.
*/
get hasTreeChildrenRelation() {
return !!this.relations.find(relation => relation.isTreeChildren);
}
/**
* Tree children relation. Used only in tree-tables.
*/
get treeChildrenRelation() {
const relation = this.relations.find(relation => relation.isTreeChildren);
if (!relation)

View File

@ -85,8 +85,8 @@ export class JoinTableMetadata extends PropertyMetadata {
return this.relation.entityMetadata.namingStrategy.joinTableName(
this.relation.entityMetadata.table.name,
this.relation.inverseEntityMetadata.table.name,
this.relation.name,
this.relation.hasInverseSide ? this.relation.inverseRelation.name : "",
this.relation.propertyName,
this.relation.hasInverseSide ? this.relation.inverseRelation.propertyName : "",
this.referencedColumn.name,
this.inverseReferencedColumn.name
);

View File

@ -163,6 +163,8 @@ export class RelationMetadata extends PropertyMetadata {
* Also only owning sides of the relations have this property.
*/
get name(): string {
if (!this.isOwning || this.isManyToMany)
throw new Error(`Relation name cannot be retrieved for many-to-many relations or not owning relations.`);
// todo: maybe need to throw exception if its a many-to-many relation?
@ -251,10 +253,10 @@ export class RelationMetadata extends PropertyMetadata {
if (this._inverseSideProperty) {
return this.computeInverseSide(this._inverseSideProperty);
} else if (this.isTreeParent) {
} else if (this.isTreeParent && this.entityMetadata.hasTreeChildrenRelation) {
return this.entityMetadata.treeChildrenRelation.propertyName;
} else if (this.isTreeChildren) {
} else if (this.isTreeChildren && this.entityMetadata.hasTreeParentRelation) {
return this.entityMetadata.treeParentRelation.propertyName;
}

View File

@ -46,11 +46,11 @@ export class DefaultNamingStrategy implements NamingStrategyInterface {
joinTableName(firstTableName: string,
secondTableName: string,
firstColumnName: string,
secondColumnName: string,
firstPropertyName: string,
secondPropertyName: string): string {
return _.snakeCase(firstTableName + "_" + firstColumnName + "_" + secondTableName + "_" + secondPropertyName);
secondPropertyName: string,
firstColumnName: string,
secondColumnName: string): string {
return _.snakeCase(firstTableName + "_" + firstPropertyName + "_" + secondTableName + "_" + secondColumnName);
}
joinTableColumnName(tableName: string, columnName: string, secondTableName: string, secondColumnName: string): string {

View File

@ -48,11 +48,11 @@ export interface NamingStrategyInterface {
* Gets the name of the join table used in the many-to-many relations.
*/
joinTableName(firstTableName: string,
secondTableName: string,
firstColumnName: string,
secondColumnName: string,
secondTableName: string,
firstPropertyName: string,
secondPropertyName: string): string;
secondPropertyName: string,
firstColumnName: string,
secondColumnName: string): string;
/**
* Gets the name of the column used for columns in the junction tables.

View File

@ -635,7 +635,7 @@ export class QueryBuilder<Entity> {
metadata.columns.forEach(column => {
statement = statement.replace(new RegExp(alias.name + "." + column.propertyName, "g"), alias.name + "." + column.name);
});
metadata.relations.forEach(relation => {
metadata.relationsWithJoinColumns.forEach(relation => {
statement = statement.replace(new RegExp(alias.name + "." + relation.propertyName, "g"), alias.name + "." + relation.name);
});
});