mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added support for custom table names
This commit is contained in:
parent
a13ee96ba2
commit
886212c873
@ -104,7 +104,7 @@ export class EntityToDbObjectTransformer {
|
||||
columnName: any,
|
||||
cascadeOption?: CascadeOption) {
|
||||
|
||||
const relation = metadata.findRelationWithOneByPropertyName(columnName);
|
||||
const relation = metadata.findRelationWithOneWithPropertyName(columnName);
|
||||
const addFunction = (id: any) => dbObject[relation.name] = id;
|
||||
this.parseRelation(deepness, metadata, entity, relation, entity[columnName], addFunction, cascadeOption);
|
||||
}
|
||||
@ -117,7 +117,7 @@ export class EntityToDbObjectTransformer {
|
||||
columnName: any,
|
||||
cascadeOption?: CascadeOption) {
|
||||
|
||||
const relation = metadata.findRelationWithManyByPropertyName(columnName);
|
||||
const relation = metadata.findRelationWithManyWithPropertyName(columnName);
|
||||
const addFunction = (id: any) => dbObject[relation.name].push(id);
|
||||
|
||||
dbObject[relation.name] = [];
|
||||
@ -150,7 +150,7 @@ export class EntityToDbObjectTransformer {
|
||||
let afterExecution = (insertedRelationEntity: any) => {
|
||||
let id = relationTypeMetadata.getEntityId(insertedRelationEntity);
|
||||
addFunction(this.createObjectId(id, relationTypeMetadata));
|
||||
const inverseSideRelationMetadata = relationTypeMetadata.findRelationByPropertyName(relation.inverseSideProperty);
|
||||
const inverseSideRelationMetadata = relationTypeMetadata.findRelationWithPropertyName(relation.inverseSideProperty);
|
||||
return <InverseSideUpdateOperation> {
|
||||
inverseSideEntityId: id,
|
||||
inverseSideEntityMetadata: relationTypeMetadata,
|
||||
|
||||
@ -201,7 +201,7 @@ export class DocumentRemover<Document> {
|
||||
|
||||
// add new inverse side update operation
|
||||
if (relation.inverseSideProperty) {
|
||||
const inverseSideRelationSchema = relatedSchema.findRelationByPropertyName(relation.inverseSideProperty);
|
||||
const inverseSideRelationSchema = relatedSchema.findRelationWithPropertyName(relation.inverseSideProperty);
|
||||
this.inverseSideUpdateOperations.push({
|
||||
inverseSideDocumentId: id,
|
||||
inverseSideDocumentSchema: relatedSchema,
|
||||
|
||||
@ -4,7 +4,7 @@ import {Table} from "../../../src/decorator/Tables";
|
||||
@Table("sample1_post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -40,7 +40,7 @@ TypeORM.createMysqlConnection(options, [Post, PostDetails, Image, ImageDetails,
|
||||
.leftJoin("post.images", "image")
|
||||
.leftJoin("post.secondaryImages", "secondaryImage")
|
||||
.leftJoin("image.details", "imageDetails", "on", "imageDetails.meta=:meta")
|
||||
.innerJoin("post.cover", "cover")
|
||||
.innerJoin("post.coverId", "cover")
|
||||
.leftJoin("post.categories", "category", "on", "category.description=:description")
|
||||
//.leftJoin(Image, "image", "on", "image.post=post.id")
|
||||
//.where("post.id=:id")
|
||||
@ -50,7 +50,8 @@ TypeORM.createMysqlConnection(options, [Post, PostDetails, Image, ImageDetails,
|
||||
|
||||
return qb
|
||||
.getSingleResult()
|
||||
.then(result => console.log(JSON.stringify(result, null, 4)))
|
||||
.then(result => console.log(result))
|
||||
// .then(result => console.log(JSON.stringify(result, null, 4)))
|
||||
.catch(error => console.log(error.stack ? error.stack : error));
|
||||
|
||||
/*let details = new PostDetails();
|
||||
|
||||
@ -6,7 +6,7 @@ import {Post} from "./Post";
|
||||
@Table("sample2_category")
|
||||
export class Category {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -6,7 +6,7 @@ import {Post} from "./Post";
|
||||
@Table("sample2_cover")
|
||||
export class Cover {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -7,7 +7,7 @@ import {ImageDetails} from "./ImageDetails";
|
||||
@Table("sample2_image")
|
||||
export class Image {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -6,7 +6,7 @@ import {Image} from "./Image";
|
||||
@Table("sample2_image_details")
|
||||
export class ImageDetails {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -9,16 +9,16 @@ import {PostDetails} from "./PostDetails";
|
||||
@Table("sample2_post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column({
|
||||
isNullable: false
|
||||
nullable: false
|
||||
})
|
||||
title: string;
|
||||
|
||||
@Column({
|
||||
isNullable: false
|
||||
nullable: false
|
||||
})
|
||||
text: string;
|
||||
|
||||
@ -31,9 +31,17 @@ export class Post {
|
||||
@OneToMany<Image>(type => Image, image => image.secondaryPost)
|
||||
secondaryImages: Image[];
|
||||
|
||||
@ManyToOne<Cover>(type => Cover, cover => cover.posts)
|
||||
@ManyToOne<Cover>(type => Cover, cover => cover.posts, {
|
||||
name: "coverId"
|
||||
})
|
||||
cover: Cover;
|
||||
|
||||
/*@Column({
|
||||
nullable: true,
|
||||
type: "int"
|
||||
})
|
||||
coverId: number;*/
|
||||
|
||||
@ManyToMany<Category>(true, type => Category, category => category.posts)
|
||||
categories: Category;
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import {Post} from "./Post";
|
||||
@Table("sample2_post_details")
|
||||
export class PostDetails {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -6,7 +6,7 @@ import {Post} from "./Post";
|
||||
@Table("sample3-comment")
|
||||
export class Comment {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -6,7 +6,7 @@ import {Comment} from "./Comment";
|
||||
@Table("sample3-post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int", { isAutoIncrement: true })
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
|
||||
@ -27,7 +27,7 @@ export function Column(typeOrOptions?: string|ColumnOptions, options?: ColumnOpt
|
||||
if (!options.type)
|
||||
options.type = type;
|
||||
|
||||
if (options.isAutoIncrement)
|
||||
if (options.autoIncrement)
|
||||
throw new Error(`Column for property ${propertyName} in ${(<any>object.constructor).name} cannot have auto increment. To have this ability you need to use @PrimaryColumn decorator.`);
|
||||
|
||||
// todo: need proper type validation here
|
||||
@ -61,7 +61,7 @@ export function PrimaryColumn(typeOrOptions?: string|ColumnOptions, options?: Co
|
||||
if (!options.type)
|
||||
options.type = type;
|
||||
|
||||
if (options.isNullable)
|
||||
if (options.nullable)
|
||||
throw new Error(`Primary column for property ${propertyName} in ${(<any>object.constructor).name} cannot be nullable. Its not allowed for primary keys. Please remove isNullable option.`);
|
||||
|
||||
// todo: need proper type validation here
|
||||
|
||||
@ -106,7 +106,7 @@ export class EntityMetadataBuilder {
|
||||
const options: ColumnOptions = {
|
||||
type: inverseSideMetadata.primaryColumn.type,
|
||||
oldColumnName: relation.oldColumnName,
|
||||
isNullable: relation.isNullable
|
||||
nullable: relation.isNullable
|
||||
};
|
||||
relationalColumn = new ColumnMetadata(metadata.target, relation.name, false, false, false, options);
|
||||
metadata.columns.push(relationalColumn);
|
||||
|
||||
@ -97,12 +97,12 @@ export class ColumnMetadata extends PropertyMetadata {
|
||||
|
||||
if (options.length)
|
||||
this._length = options.length;
|
||||
if (options.isAutoIncrement)
|
||||
this._isAutoIncrement = options.isAutoIncrement;
|
||||
if (options.isUnique)
|
||||
this._isUnique = options.isUnique;
|
||||
if (options.isNullable)
|
||||
this._isNullable = options.isNullable;
|
||||
if (options.autoIncrement)
|
||||
this._isAutoIncrement = options.autoIncrement;
|
||||
if (options.unique)
|
||||
this._isUnique = options.unique;
|
||||
if (options.nullable)
|
||||
this._isNullable = options.nullable;
|
||||
if (options.columnDefinition)
|
||||
this._columnDefinition = options.columnDefinition;
|
||||
if (options.comment)
|
||||
|
||||
@ -166,40 +166,36 @@ export class EntityMetadata {
|
||||
return this._relations.find(relation => relation.name === propertyName);
|
||||
}
|
||||
|
||||
findRelationWithOneByPropertyName(propertyName: string): RelationMetadata {
|
||||
findRelationWithOneWithPropertyName(propertyName: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.propertyName === propertyName && (relation.isOneToMany || relation.isOneToOne));
|
||||
}
|
||||
|
||||
findRelationWithOneByDbName(name: string): RelationMetadata {
|
||||
findRelationWithOneWithDbName(name: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.name === name && (relation.isOneToMany || relation.isOneToOne));
|
||||
}
|
||||
|
||||
findRelationWithManyByPropertyName(propertyName: string): RelationMetadata {
|
||||
findRelationWithManyWithPropertyName(propertyName: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.propertyName === propertyName && (relation.isManyToOne || relation.isManyToMany));
|
||||
}
|
||||
|
||||
findRelationWithManyByDbName(name: string): RelationMetadata {
|
||||
findRelationWithManyWithDbName(name: string): RelationMetadata {
|
||||
return this._relations.find(relation => relation.name === name && (relation.isManyToOne || relation.isManyToMany));
|
||||
}
|
||||
|
||||
findRelationByPropertyName(name: string): RelationMetadata {
|
||||
return this.findRelationWithOneByPropertyName(name) || this.findRelationWithManyByPropertyName(name);
|
||||
}
|
||||
|
||||
hasRelationWithOneWithPropertyName(propertyName: string): boolean {
|
||||
return !!this.findRelationWithOneByPropertyName(propertyName);
|
||||
return !!this.findRelationWithOneWithPropertyName(propertyName);
|
||||
}
|
||||
|
||||
hasRelationWithManyWithPropertyName(propertyName: string): boolean {
|
||||
return !!this.findRelationWithManyByPropertyName(propertyName);
|
||||
return !!this.findRelationWithManyWithPropertyName(propertyName);
|
||||
}
|
||||
|
||||
hasRelationWithOneWithName(name: string): boolean {
|
||||
return !!this.findRelationWithOneByDbName(name);
|
||||
return !!this.findRelationWithOneWithDbName(name);
|
||||
}
|
||||
|
||||
hasRelationWithManyWithName(name: string): boolean {
|
||||
return !!this.findRelationWithManyByDbName(name);
|
||||
return !!this.findRelationWithManyWithDbName(name);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -118,8 +118,8 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
this._isCascadeRemove = options.isCascadeRemove;
|
||||
if (options.oldColumnName)
|
||||
this._oldColumnName = options.oldColumnName;
|
||||
if (options.isNullable)
|
||||
this._isNullable = options.isNullable;
|
||||
if (options.nullable)
|
||||
this._isNullable = options.nullable;
|
||||
|
||||
if (!this._name)
|
||||
this._name = propertyName;
|
||||
@ -162,7 +162,7 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
}
|
||||
|
||||
get inverseRelation(): RelationMetadata {
|
||||
return this._relatedEntityMetadata.findRelationByPropertyName(this.computeInverseSide(this._inverseSideProperty));
|
||||
return this._relatedEntityMetadata.findRelationWithPropertyName(this.computeInverseSide(this._inverseSideProperty));
|
||||
}
|
||||
|
||||
get isOwning(): boolean {
|
||||
|
||||
@ -2,9 +2,9 @@ export interface ColumnOptions {
|
||||
name?: string;
|
||||
type?: string;
|
||||
length?: string;
|
||||
isAutoIncrement?: boolean;
|
||||
isUnique?: boolean;
|
||||
isNullable?: boolean;
|
||||
autoIncrement?: boolean;
|
||||
unique?: boolean;
|
||||
nullable?: boolean;
|
||||
columnDefinition?: string;
|
||||
comment?: string;
|
||||
oldColumnName?: string;
|
||||
|
||||
@ -28,6 +28,6 @@ export interface RelationOptions {
|
||||
/**
|
||||
* Indicates if relation column value can be nullable or not.
|
||||
*/
|
||||
isNullable?: boolean;
|
||||
nullable?: boolean;
|
||||
|
||||
}
|
||||
@ -204,12 +204,12 @@ export class QueryBuilder<Entity> {
|
||||
return sql;
|
||||
}
|
||||
|
||||
execute(): Promise<any[]> {
|
||||
return this.connection.driver.query<any[]>(this.getSql())
|
||||
execute<T>(): Promise<T> {
|
||||
return this.connection.driver.query<T>(this.getSql());
|
||||
}
|
||||
|
||||
getScalarResults(): Promise<any[]> {
|
||||
return this.execute().then(results => this.rawResultsToObjects(results));
|
||||
return this.execute<any[]>().then(results => this.rawResultsToObjects(results));
|
||||
|
||||
}
|
||||
|
||||
@ -257,7 +257,7 @@ export class QueryBuilder<Entity> {
|
||||
.forEach(join => {
|
||||
const joinMetadata = this.aliasMap.getEntityMetadataByAlias(join.alias);
|
||||
joinMetadata.columns.forEach(column => {
|
||||
allSelects.push(join.alias.name + "." + column.name + " AS " + join.alias.name + "_" + column.name);
|
||||
allSelects.push(join.alias.name + "." + column.name + " AS " + join.alias.name + "_" + column.propertyName);
|
||||
});
|
||||
});
|
||||
|
||||
@ -300,7 +300,7 @@ export class QueryBuilder<Entity> {
|
||||
const parentMetadata = this.aliasMap.getEntityMetadataByAlias(this.aliasMap.findAliasByName(parentAlias));
|
||||
const parentTable = parentMetadata.table.name;
|
||||
const parentTableColumn = parentMetadata.primaryColumn.name;
|
||||
const relation = parentMetadata.findRelationByPropertyName(join.alias.parentPropertyName);
|
||||
const relation = parentMetadata.findRelationWithDbName(join.alias.parentPropertyName);
|
||||
const junctionMetadata = relation.junctionEntityMetadata;
|
||||
const joinMetadata = this.aliasMap.getEntityMetadataByAlias(join.alias);
|
||||
const joinTable = joinMetadata.table.name;
|
||||
|
||||
@ -54,13 +54,13 @@ export class AliasMap {
|
||||
} else if (alias.parentAliasName && alias.parentPropertyName) {
|
||||
const parentAlias = this.findAliasByName(alias.parentAliasName); // todo: throw exceptions everywhere
|
||||
const parentEntityMetadata = this.getEntityMetadataByAlias(parentAlias);
|
||||
const relation = parentEntityMetadata.findRelationByPropertyName(alias.parentPropertyName);
|
||||
const relation = parentEntityMetadata.findRelationWithDbName(alias.parentPropertyName);
|
||||
return relation.relatedEntityMetadata;
|
||||
}
|
||||
|
||||
throw new Error("Cannot get entity metadata for the given alias " + alias.name);
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Private Methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user