简体   繁体   English

Seqelize:检索最新的 hasMany 关系

[英]Seqelize: Retrieve latest of hasMany relationship

Here is my relationship:这是我的关系:

KbDevice.hasMany(KbDeviceEvent, { foreignKey: 'kb_device_id', as: 'events' })

Here is my query:这是我的查询:

  const allKbDevice = await KbDevice.findAll({
    attributes: ['id', 'make', 'model'],
    include: [
      {
        model: KbDeviceEvent,
        as: 'events',
        attributes: ['id', 'event', 'chamber_id','created_at'],
      },
    ],
  });

The issue is, instead of an array, I would like to get one KbDeviceEvent as the latest item based on created_at .问题是,我想要一个KbDeviceEvent作为基于created_at的最新项目,而不是一个数组。 I tried to use an include but it kept throwing an error.我尝试使用include ,但它一直抛出错误。 Can anyone help please?有人可以帮忙吗?

Do you need to see the actual models to answer this question?您需要查看实际模型来回答这个问题吗? If so, please comment.如果是这样,请发表评论。

Thank you,谢谢,

EDIT:编辑:

Here is the result of the above query:以下是上述查询的结果:

{
  "id": "d75f9f96-a468-4c95-8098-a92411f8e73c",
  "make": "Toyota",
  "model": "Lexus",
  "events": [
    {
      "id": "a737bf7b-2b2e-4817-b351-69db330cb5c7",
      "event": "deposit",
      "chamber_id": 2,
      "created_at": "2022-09-01T22:36:19.849Z"
    },
  ]
}

And I'm looking for:我正在寻找:

{
  "id": "d75f9f96-a468-4c95-8098-a92411f8e73c",
  "make": "Toyota",
  "model": "Lexus",
  "event": { // I am the latest event
    "id": "a737bf7b-2b2e-4817-b351-69db330cb5c7",
    "event": "deposit",
    "chamber_id": 2,
    "created_at": "2022-09-01T22:36:19.849Z"
  }
}

Query to fetch the associated model sorted by created_at :查询以获取按created_at排序的关联 model :

const allKbDevice = await KbDevice.findAll({
    attributes: ['id', 'make', 'model'],
    include: [
      {
        model: KbDeviceEvent,
        as: 'events',
        attributes: ['id', 'event', 'chamber_id','created_at'],
      },
    ],
    order: [
      [{ model: KbDeviceEvent, as: 'events' }, 'created_at', 'DESC']
    ],
    limit: 1
});

Please note that this will always return the results in this manner:请注意,这将始终以这种方式返回结果:

{
  "id": "d75f9f96-a468-4c95-8098-a92411f8e73c",
  "make": "Toyota",
  "model": "Lexus",
  "events": [
    {
      "id": "a737bf7b-2b2e-4817-b351-69db330cb5c7",
      "event": "deposit",
      "chamber_id": 2,
      "created_at": "2022-09-01T22:36:19.849Z"
    },
  ]
}

since KbDevice model has hasMany relation with KbDeviceEvent model.由于KbDevice model 与hasMany model 有KbDeviceEvent关系。 You can do allKbDevice['events'][0] in your code to get the single event.您可以在代码中执行allKbDevice['events'][0]来获取单个事件。 If you are open to modifying the query, then you can achieve an output which doesn't have any array.如果您愿意修改查询,则可以实现没有任何数组的 output。 However, device would be embedded inside event object.但是, device将嵌入到event object 中。 The query would be:查询将是:

const kbDeviceEvent = await KbDeviceEvent.findAll({
    attributes: ['id', 'event', 'chamber_id','created_at'],
    order: [
      ['created_at', 'DESC']
    ],
    limit: 1,
    include: [
      {
        model: KbDevice,
        as: 'device',
        attributes: ['id', 'make', 'model']
      },
    ]
});

This will output something like this:这将 output 是这样的:

{
  "id": "a737bf7b-2b2e-4817-b351-69db330cb5c7",
  "event": "deposit",
  "chamber_id": 2,
  "created_at": "2022-09-01T22:36:19.849Z"
  "device": {
    "id": "d75f9f96-a468-4c95-8098-a92411f8e73c",
    "make": "Toyota",
    "model": "Lexus",
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM