【问题标题】:Sequelize Association getting data like JSONSequelize Association 获取 JSON 等数据
【发布时间】:2019-10-12 20:06:15
【问题描述】:

我用椰子和椰子列表这两个表简化了我的问题。椰子表存储了椰子的规格,椰子列表存储了卖家有椰子的位置。每次都会在卖家和椰子之间创建一个关系,并将其插入到椰子列表表中。

我尝试过使用带属性的关键字,设置不同的关系HasMany、HasOne、BelongsToMany。

椰子列表

import { Column, DataType, Model, Table, ForeignKey, BelongsTo } from 'sequelize-typescript';
import Coconut from './coconut';


@Table({
  modelName: 'cononuts_list',
  timestamps: true,
  paranoid: true,
})
export default class CoconutList extends Model<CoconutList> {
  @Column({
    allowNull: false,
    primaryKey: true,
    autoIncrement: true,
    type: DataType.INTEGER,
  })
  public id: number;

  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  public sellerId: string;

  @ForeignKey(() => coconut)
  @Column({
    type: DataType.INTEGER,
    allowNull: false,
  })
  public coconutId: number;

  @BelongsTo(() => Coconut, 'coconutId')
  public coconut: Coconut;


}

椰子

import { Column, DataType, Model, Table, HasMany } from 'sequelize-typescript';
import CoconutList from './coconutList';


@Table({
  modelName: 'coconuts',
  timestamps: true,
  paranoid: true,
})
export default class Coconut extends Model<Coconut> {
  @Column({
    allowNull: false,
    primaryKey: true,
    autoIncrement: true,
    type: DataType.INTEGER,
  })
  public id: number;

  @HasMany(() => CoconutList, 'id')
  public coconutList: CoconutList[];

  @Column({
    type: DataType.INTEGER,
    allowNull: false,
  })
  public weight: number;

  @Column({
    type: DataType.INTEGER,
    allowNull: false,
  })
  public count: number;

  @Column({
    type: DataType.FLOAT,
    allowNull: false,
  })
  public size: number;
}

查询:

const coconutsBySeller = await CoconutList.findAll({
    include: [
      {
        model: Coconut,
        attributes: ['weight', 'size', 'count'],
      },
    ],
    attributes: ['sellerId'],
  });

代码输出如下:

[
  {
    "sellerId": "f3ff90d8-ed8e-42c9-9c7b-d607111a359e",
    "coconut": {
      "weight": 100,
      "count": 6,
      "size": 11,
    }
  },
  {
    "sellerId": "f3ff90d8-ed8e-42c9-9c7b-d607111a359e",
    "coconut": {
      "weight": 200,
      "count": 12,
      "size": 20,
    }
  },
  {
    "sellerId": "ffffaaaa-ed8e-42c9-9c7b-d607111a359e",
    "coconut": {
      "weight": 300,
      "count": 18,
      "size": 50,
    }
  },
  {
    "sellerId": "ffffaaaa-ed8e-42c9-9c7b-d607111a359e",
    "coconut": {
      "weight": 100,
      "count": 6,
      "size": 11,
    }
  }
]

相反,我想得到

[
  {
    "sellerId": "f3ff90d8-ed8e-42c9-9c7b-d607111a359e",
    [{
      "weight": 100,
      "count": 6,
      "size": 11,
    },
    {
      "weight": 200,
      "count": 12,
      "size": 20,
    }]
  },
  {
    "sellerId": "ffffaaaa-ed8e-42c9-9c7b-d607111a359e",
    [{
      "weight": 300,
      "count": 18,
      "size": 50,
},   
    {
      "weight": 100,
      "count": 6,
      "size": 11,
    }
  ]
]

除了数据的格式之外,我还想知道是否可以在上述查询中使用 Sequelize 获取分配给卖家的椰子的重量、计数、大小字段的 min_max 值。

【问题讨论】:

    标签: node.js orm sequelize.js sequelize-typescript


    【解决方案1】:

    你可以试试这个按卖家分组:

    const coconutsBySeller = await CoconutList.findAll({
        include: [
          {
            model: Coconut,
            attributes: ['weight', 'size', 'count'],
          },
        ],
        attributes: ['sellerId'],
        group: ['sellerId']
      });
    

    【讨论】:

    • 它只返回一个对象而不是多个。
    猜你喜欢
    • 2020-06-27
    • 2022-11-08
    • 2018-11-28
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    相关资源
    最近更新 更多