mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed abstract table implementation and added sample
This commit is contained in:
parent
447c145589
commit
3cd0f830e9
72
sample/sample6-abstract-table/app.ts
Normal file
72
sample/sample6-abstract-table/app.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import {createMysqlConnection} from "../../src/typeorm";
|
||||
import {Post} from "./entity/Post";
|
||||
import {PostCategory} from "./entity/PostCategory";
|
||||
import {PostAuthor} from "./entity/PostAuthor";
|
||||
import {Blog} from "./entity/Blog";
|
||||
|
||||
// first create a connection
|
||||
let options = {
|
||||
host: "192.168.99.100",
|
||||
port: 3306,
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaCreate: true
|
||||
};
|
||||
|
||||
createMysqlConnection(options, [__dirname + "/entity"]).then(connection => {
|
||||
|
||||
let category1 = new PostCategory();
|
||||
category1.name = "post category #1";
|
||||
|
||||
let category2 = new PostCategory();
|
||||
category2.name = "post category #2";
|
||||
|
||||
let author = new PostAuthor();
|
||||
author.name = "Umed";
|
||||
|
||||
let post = new Post();
|
||||
post.text = "Hello how are you?";
|
||||
post.title = "hello";
|
||||
post.author = author;
|
||||
post.categories.push(category1, category2);
|
||||
|
||||
category1 = new PostCategory();
|
||||
category1.name = "post category #1";
|
||||
|
||||
category2 = new PostCategory();
|
||||
category2.name = "post category #2";
|
||||
|
||||
author = new PostAuthor();
|
||||
author.name = "Umed";
|
||||
|
||||
let blog = new Blog();
|
||||
blog.text = "Hello how are you?";
|
||||
blog.title = "hello";
|
||||
blog.author = author;
|
||||
blog.categories.push(category1, category2);
|
||||
|
||||
let postRepository = connection.getRepository(Post);
|
||||
let blogRepository = connection.getRepository(Blog);
|
||||
|
||||
postRepository
|
||||
.persist(post)
|
||||
.then(post => {
|
||||
console.log("Post has been saved");
|
||||
return postRepository.findById(post.id);
|
||||
})
|
||||
.then(loadedPost => {
|
||||
console.log("post is loaded: ", loadedPost);
|
||||
return blogRepository.persist(blog);
|
||||
})
|
||||
.then(blog => {
|
||||
console.log("Blog has been saved");
|
||||
return blogRepository.findById(blog.id);
|
||||
})
|
||||
.then(loadedBlog => {
|
||||
console.log("blog is loaded: ", loadedBlog);
|
||||
return blogRepository.persist(blog);
|
||||
})
|
||||
.catch(error => console.log("Cannot save. Error: ", error.stack ? error.stack : error));
|
||||
|
||||
}, error => console.log("Cannot connect: ", error.stack ? error.stack : error));
|
||||
18
sample/sample6-abstract-table/entity/BasePost.ts
Normal file
18
sample/sample6-abstract-table/entity/BasePost.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import {PrimaryColumn, Column} from "../../../src/decorator/Columns";
|
||||
import {Table} from "../../../src/decorator/Tables";
|
||||
import {ManyToMany} from "../../../src/decorator/Relations";
|
||||
import {PostCategory} from "./PostCategory";
|
||||
import {PostAuthor} from "./PostAuthor";
|
||||
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
|
||||
import {AbstractTable} from "../../../src/decorator/tables/AbstractTable";
|
||||
|
||||
@AbstractTable()
|
||||
export class BasePost {
|
||||
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
}
|
||||
29
sample/sample6-abstract-table/entity/Blog.ts
Normal file
29
sample/sample6-abstract-table/entity/Blog.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import {Column} from "../../../src/decorator/Columns";
|
||||
import {Table} from "../../../src/decorator/Tables";
|
||||
import {BasePost} from "./BasePost";
|
||||
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
|
||||
import {PostAuthor} from "./PostAuthor";
|
||||
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
|
||||
import {PostCategory} from "./PostCategory";
|
||||
|
||||
@Table("sample6_blog")
|
||||
export class Blog extends BasePost {
|
||||
|
||||
@Column()
|
||||
text: string;
|
||||
|
||||
@ManyToOne(type => PostAuthor, post => post.posts, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
author: PostAuthor;
|
||||
|
||||
@ManyToMany(type => PostCategory, category => category.posts, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
categories: PostCategory[] = [];
|
||||
|
||||
}
|
||||
29
sample/sample6-abstract-table/entity/Post.ts
Normal file
29
sample/sample6-abstract-table/entity/Post.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import {Column} from "../../../src/decorator/Columns";
|
||||
import {Table} from "../../../src/decorator/Tables";
|
||||
import {BasePost} from "./BasePost";
|
||||
import {PostCategory} from "./PostCategory";
|
||||
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
|
||||
import {PostAuthor} from "./PostAuthor";
|
||||
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
|
||||
|
||||
@Table("sample6_post")
|
||||
export class Post extends BasePost {
|
||||
|
||||
@Column()
|
||||
text: string;
|
||||
|
||||
@ManyToOne(type => PostAuthor, post => post.posts, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
author: PostAuthor;
|
||||
|
||||
@ManyToMany(type => PostCategory, category => category.posts, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
categories: PostCategory[] = [];
|
||||
|
||||
}
|
||||
18
sample/sample6-abstract-table/entity/PostAuthor.ts
Normal file
18
sample/sample6-abstract-table/entity/PostAuthor.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import {PrimaryColumn, Column} from "../../../src/decorator/Columns";
|
||||
import {Table} from "../../../src/decorator/Tables";
|
||||
import {Post} from "./Post";
|
||||
import {OneToMany} from "../../../src/decorator/relations/OneToMany";
|
||||
|
||||
@Table("sample6_post_author")
|
||||
export class PostAuthor {
|
||||
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@OneToMany(type => Post, post => post.author)
|
||||
posts: Post[];
|
||||
|
||||
}
|
||||
22
sample/sample6-abstract-table/entity/PostCategory.ts
Normal file
22
sample/sample6-abstract-table/entity/PostCategory.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import {PrimaryColumn, Column} from "../../../src/decorator/Columns";
|
||||
import {Table} from "../../../src/decorator/Tables";
|
||||
import {Post} from "./Post";
|
||||
import {ManyToManyInverse} from "../../../src/decorator/relations/ManyToManyInverse";
|
||||
|
||||
@Table("sample6_post_category")
|
||||
export class PostCategory {
|
||||
|
||||
@PrimaryColumn("int", { autoIncrement: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToManyInverse(type => Post, post => post.categories, {
|
||||
cascadeInsert: true,
|
||||
cascadeUpdate: true,
|
||||
cascadeRemove: true
|
||||
})
|
||||
posts: Post[] = [];
|
||||
|
||||
}
|
||||
@ -6,6 +6,6 @@ import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
|
||||
*/
|
||||
export function AbstractTable() {
|
||||
return function (cls: Function) {
|
||||
defaultMetadataStorage.addTableMetadata(new TableMetadata(cls, name, true));
|
||||
defaultMetadataStorage.addTableMetadata(new TableMetadata(cls, undefined, true));
|
||||
};
|
||||
}
|
||||
@ -119,7 +119,7 @@ export class MetadataStorage {
|
||||
}
|
||||
|
||||
findTableMetadatasForClasses(classes: Function[]): TableMetadata[] {
|
||||
return this.tableMetadatas.filter(metadata => classes.indexOf(metadata.target) !== -1);
|
||||
return this.tableMetadatas.filter(metadata => classes.indexOf(metadata.target) !== -1 && !metadata.isAbstract);
|
||||
}
|
||||
|
||||
findCompoundIndexMetadatasForClasses(classes: Function[]): CompoundIndexMetadata[] {
|
||||
|
||||
@ -340,7 +340,7 @@ export class EntityPersistOperationBuilder {
|
||||
id: entity[metadata.primaryColumn.name],
|
||||
entity: entity
|
||||
}])
|
||||
.filter((entity: any, index: number, allEntities: any[]) => allEntities.indexOf(entity) === index); // unique
|
||||
.filter((entity: any, index: number, allEntities: any[]) => allEntities.indexOf(entity) === index); // unique
|
||||
}
|
||||
|
||||
private diffColumns(metadata: EntityMetadata, newEntity: any, dbEntity: any) {
|
||||
|
||||
@ -31,7 +31,7 @@ export class PersistOperation {
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(this.dbEntity);
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log("NEW ENTITY");
|
||||
console.log("PERSISTENT ENTITY");
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(this.persistedEntity);
|
||||
console.log("---------------------------------------------------------");
|
||||
@ -39,7 +39,7 @@ export class PersistOperation {
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(this.allDbEntities);
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log("ALL NEW ENTITIES");
|
||||
console.log("ALL PERSISTENT ENTITIES");
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(this.allPersistedEntities);
|
||||
console.log("---------------------------------------------------------");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user