cascade: boolean | ("insert" | "update")[]
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(type => Question, question => question.categories)
questions: Question[];
}
@Entity()
export class Question {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category, category => category.questions, {
cascade: true // -> cascade option 설정
})
@JoinTable()
categories: Category[];
}
const category1 = new Category();
category1.name = "ORMs";
const category2 = new Category();
category2.name = "Programming";
const question = new Question();
question.title = "How to ask questions?";
question.text = "Where can I ask TypeORM-related questions?";
question.categories = [category1, category2];
await connection.manager.save(question);
casecade: true
category1
, category2
에 대해 save
메소드를 호출하지 않아도 자동으로 insert 수행insert
, update
, remove
, soft-remove
, recover
, etc.save
메소드를 호출할 때 자동으로 수행 됨casecade: ['insert', 'update']
@OneToOne
에는 필수propertyName + referencedColumnName
로 자동 생성@ManyToOne(type => Category)
@JoinColumn() // -> 'categoryId' 컬럼 생성됨
category: Category;
@ManyToOne(type => Category)
@JoinColumn({ name : 'cat_id' })
category: Category;
referencedColumnName
이용@ManyToOne(type => Category)
@JoinColumn({ referencedColumnName: "name" }) // Category의 PK인'id'가 아닌 'name' 컬럼을 참조
category: Category;
referencedColumnName
설정해야 함@ManyToOne(type => Category)
@JoinColumn([
{ name: "category_id", referencedColumnName: "id" },
{ name: "locale_id", referencedColumnName: "locale_id" }
])
category: Category;
@ManyToMany
에서 사용@ManyToMany(type => Category)
@JoinTable({
name: "question_categories", // table name for the junction table of this relation
joinColumn: {
name: "question",
referencedColumnName: "id"
},
inverseJoinColumn: {
name: "category",
referencedColumnName: "id"
}
})
categories: Category[];
@OneToOne
, @JoinColumn
한 쪽에만 선언 (uni-directional)@OneToOne
선언@JoinColumn
한 쪽에만@JoinColumn
생략 가능@OneToMany
사용 시, @ManyToOne
필수@ManyToOne
사용 시, @OneToMany
필수 X (@ManyToOne
단독 사용 가능)@JoinTable
사용find
할 경우, 연관된 관계가 자동으로 로드되어 짐(자동 조인의 개념)find
메소드에서만 작동 @ManyToMany(type => Category, category => category.questions, {
eager: true
})
@JoinTable()
categories: Category[];
@ManyToMany(type => Question, question => question.categories)
questions: Promise<Question[]>;
@ManyToMany(type => Category, category => category.questions)
@JoinTable()
categories: Promise<Category[]>;
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToOne(type => Category, category => category.childCategories)
parentCategory: Category;
@OneToMany(type => Category, category => category.parentCategory)
childCategories: Category[];
}
Find
1. FindOptions
- where절 relations option 사용
const userRepository = connection.getRepository(User); const users = await userRepository.find({ relations: ['photos']});
2. QueryBuilder
- join 사용
const users = await connection .getRepository(User) .createQueryBuilder('user') .leftJoinAndSelect('user.photos', 'photo') .getMany();
This article is simple to read and enjoyable, without omitting any of the important details. word finder
This post is easy to read and appreciate without leaving out any details. Excellent work!
@geometry dash