admin管理员组文章数量:1221774
I created to TypeORM Entities Category and Subcategory
Category.ts
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@OneToMany(() => Subcategory, (subcategory) => subcategory.category)
public Subcategories: Subcategory[];
@OneToMany(() => Item, (item) => item.category)
public Items: Item[];
}
Subcategory.ts
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToOne,
OneToMany,
CreateDateColumn,
UpdateDateColumn
} from "typeorm";
import {Category} from "./Category";
import {Item} from "./Item";
@Entity()
export class Subcategory {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@ManyToOne(() => Category, (category) => category.Subcategories)
public category: Category;
@OneToMany(() => Item, (item) => item.subcategory)
public Items: Item[];
}
Get Category Query on TypeScript :
await getRepository(Category).find();
Issue:
I couldn't find a way online to include the Subcategories into the getCategory query, I just one to perform a simple join that will get me All the categories and their subcategories. In entity framework I used to something like Category.Includes(Subcategories) and it does the job.
Is there any easy/simple way to join tables in TypeORM / NodeJS ?
Thanks a lot for taking the time to read my question, I appreciate it!
I created to TypeORM Entities Category and Subcategory
Category.ts
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@OneToMany(() => Subcategory, (subcategory) => subcategory.category)
public Subcategories: Subcategory[];
@OneToMany(() => Item, (item) => item.category)
public Items: Item[];
}
Subcategory.ts
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToOne,
OneToMany,
CreateDateColumn,
UpdateDateColumn
} from "typeorm";
import {Category} from "./Category";
import {Item} from "./Item";
@Entity()
export class Subcategory {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@ManyToOne(() => Category, (category) => category.Subcategories)
public category: Category;
@OneToMany(() => Item, (item) => item.subcategory)
public Items: Item[];
}
Get Category Query on TypeScript :
await getRepository(Category).find();
Issue:
I couldn't find a way online to include the Subcategories into the getCategory query, I just one to perform a simple join that will get me All the categories and their subcategories. In entity framework I used to something like Category.Includes(Subcategories) and it does the job.
Is there any easy/simple way to join tables in TypeORM / NodeJS ?
Thanks a lot for taking the time to read my question, I appreciate it!
Share Improve this question asked Jan 19, 2020 at 23:48 DevMachineDevMachine 5633 gold badges6 silver badges15 bronze badges2 Answers
Reset to default 14After looking up different tutorials I found that this way worked perfectly for my need:
const categories = await getRepository(Category).find({relations: ['Subcategories']});
const categories = await getRepository(Category).createQueryBuilder("ct").
innerJoinAndSelect("ct.Subcategories", "sb").getMany()
Refer to the following documentation
https://typeorm.io/#/select-query-builder
本文标签: javascriptJoin Tables in TypeORM amp NodeJSStack Overflow
版权声明:本文标题:javascript - Join Tables in TypeORM & NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739300705a2157124.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论