NestJS 和 TypeORM 自定义多对多关系仅返回 1 个结果

发布于 2025-01-09 01:56:43 字数 3660 浏览 0 评论 0原文

我有 3 个实体:

  1. order.entity.ts
  2. pod.entity.ts
  3. order-pod.entity.ts - 自定义多对多 -许多具有我需要的额外属性的实体。

对于数据库中的每个订单,我在使用 order-pod.entity.ts 创建的 order_pod 表中都有超过 1 个与之关联的 pod,但是当我从存储库中获取数据时,我有 2 个问题:

  1. 我总是在 Order.pods 数组中得到 1 个 pod 对象(尽管我拥有的远不止这些)
  2. 数据以一种奇怪的方式嵌套,我更喜欢每个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:

  1. order.entity.ts
  2. pod.entity.ts
  3. 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:

  1. I am always getting only 1 pod object in the Order.pods array (even though I have much more than that)
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文