NestJS 和 TypeORM 自定义多对多关系仅返回 1 个结果
我有 3 个实体:
order.entity.ts
pod.entity.ts
order-pod.entity.ts
- 自定义多对多 -许多具有我需要的额外属性的实体。
对于数据库中的每个订单,我在使用 order-pod.entity.ts 创建的 order_pod 表中都有超过 1 个与之关联的 pod,但是当我从存储库中获取数据时,我有 2 个问题:
- 我总是在
Order.pods
数组中得到 1 个 pod 对象(尽管我拥有的远不止这些) - 数据以一种奇怪的方式嵌套,我更喜欢每个pod 对象是扁平的
这是我得到的结果:
[
{
"id": 1,
"customerFirstName": "Erez",
"customerLastName": "H",
"created_at": "2022-02-23",
"totalQty": 1,
"price": "12.99",
"shippingCost": "9.99",
"pods": [
{
"orderId": 1,
"podId": 1,
"quantity": 1,
"pod": {
"id": 1,
"name": "Green Smoothie Pod",
"description": "A very nice description about the Green Smoothie Pod",
"weightInG": 23,
"color": "green",
"brand": "Tolive",
"imageURL": "https://i.ibb.co/0KR56HX/green-smoothie-pod.png",
"price": "84.00",
"calories": "0.00",
"protein": "12.00",
"sugar": "33.00",
"carbohydrates": "60.00",
"sodium": "24.00",
"cholesterol": "0.00",
"totalFats": "1.00"
}
}
]
}
]
这是我正在使用的 getOrders 方法: order.service.ts
:
async getOrders: Promise<Order[]> {
const allOrders = await this.ordersRepository.find({
relations: ['pods'],
});
return allOrders;
}
这是我的 order.entity.ts
@Entity()
export class Order {
@PrimaryGeneratedColumn('increment')
id: number;
@Column()
customerFirstName: string;
@Column()
customerLastName: string;
@Column('date')
created_at: Date;
@Column()
totalQty: number;
@Column('decimal', { precision: 5, scale: 2 })
price: number;
@Column('decimal', { precision: 5, scale: 2 })
shippingCost: number;
@OneToMany(() => OrderPod, (orderPod) => orderPod.pod)
pods: Pod[];
}
pod.entity.ts
:
@Entity()
export class Pod {
@PrimaryGeneratedColumn('increment')
id: number;
@Column()
name: string;
@Column()
description: string;
@Column()
weightInG: number;
@Column()
color: string;
@Column()
brand: string;
@Column()
imageURL: string;
@Column('decimal', { precision: 5, scale: 2 })
price: number;
@Column('decimal', { precision: 5, scale: 2 })
calories: number;
@Column('decimal', { precision: 5, scale: 2 })
protein: number;
@Column('decimal', { precision: 5, scale: 2 })
sugar: number;
@Column('decimal', { precision: 5, scale: 2 })
carbohydrates: number;
@Column('decimal', { precision: 5, scale: 2 })
sodium: number;
@Column('decimal', { precision: 5, scale: 2 })
cholesterol: number;
@Column('decimal', { precision: 5, scale: 2 })
totalFats: number;
@OneToMany(() => OrderPod, (orderPod) => orderPod.order)
orders: Order[];
}
和我的 order-pod.entity.ts
@Entity()
export class OrderPod {
@PrimaryColumn()
orderId: number;
@PrimaryColumn()
podId: number;
@ManyToOne(() => Order, (order) => order.pods)
order: Order;
@ManyToOne(() => Pod, (pod) => pod.orders, { eager: true })
pod: Pod;
@Column({ type: 'int' })
quantity: number;
}
I have 3 entities:
order.entity.ts
pod.entity.ts
order-pod.entity.ts
- A custom many-to-many entity with extra properties that I need.
For each order in the database I have more than 1 pod associated with it in the order_pod table that has been created using the order-pod.entity.ts
, but when I fetch the data from the repository, I am having 2 issues:
- I am always getting only 1 pod object in the
Order.pods
array (even though I have much more than that) - The data is nested in a weird way and I would prefer each pod object to be flat
This is the result I am getting:
[
{
"id": 1,
"customerFirstName": "Erez",
"customerLastName": "H",
"created_at": "2022-02-23",
"totalQty": 1,
"price": "12.99",
"shippingCost": "9.99",
"pods": [
{
"orderId": 1,
"podId": 1,
"quantity": 1,
"pod": {
"id": 1,
"name": "Green Smoothie Pod",
"description": "A very nice description about the Green Smoothie Pod",
"weightInG": 23,
"color": "green",
"brand": "Tolive",
"imageURL": "https://i.ibb.co/0KR56HX/green-smoothie-pod.png",
"price": "84.00",
"calories": "0.00",
"protein": "12.00",
"sugar": "33.00",
"carbohydrates": "60.00",
"sodium": "24.00",
"cholesterol": "0.00",
"totalFats": "1.00"
}
}
]
}
]
This is the getOrders
method I am using:order.service.ts
:
async getOrders: Promise<Order[]> {
const allOrders = await this.ordersRepository.find({
relations: ['pods'],
});
return allOrders;
}
This is my order.entity.ts
@Entity()
export class Order {
@PrimaryGeneratedColumn('increment')
id: number;
@Column()
customerFirstName: string;
@Column()
customerLastName: string;
@Column('date')
created_at: Date;
@Column()
totalQty: number;
@Column('decimal', { precision: 5, scale: 2 })
price: number;
@Column('decimal', { precision: 5, scale: 2 })
shippingCost: number;
@OneToMany(() => OrderPod, (orderPod) => orderPod.pod)
pods: Pod[];
}
pod.entity.ts
:
@Entity()
export class Pod {
@PrimaryGeneratedColumn('increment')
id: number;
@Column()
name: string;
@Column()
description: string;
@Column()
weightInG: number;
@Column()
color: string;
@Column()
brand: string;
@Column()
imageURL: string;
@Column('decimal', { precision: 5, scale: 2 })
price: number;
@Column('decimal', { precision: 5, scale: 2 })
calories: number;
@Column('decimal', { precision: 5, scale: 2 })
protein: number;
@Column('decimal', { precision: 5, scale: 2 })
sugar: number;
@Column('decimal', { precision: 5, scale: 2 })
carbohydrates: number;
@Column('decimal', { precision: 5, scale: 2 })
sodium: number;
@Column('decimal', { precision: 5, scale: 2 })
cholesterol: number;
@Column('decimal', { precision: 5, scale: 2 })
totalFats: number;
@OneToMany(() => OrderPod, (orderPod) => orderPod.order)
orders: Order[];
}
and my order-pod.entity.ts
@Entity()
export class OrderPod {
@PrimaryColumn()
orderId: number;
@PrimaryColumn()
podId: number;
@ManyToOne(() => Order, (order) => order.pods)
order: Order;
@ManyToOne(() => Pod, (pod) => pod.orders, { eager: true })
pod: Pod;
@Column({ type: 'int' })
quantity: number;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论