Sequelize:如何绑定多个模型进行连接查询并创建自定义列。

编程入门 行业动态 更新时间:2024-10-09 23:16:16

Sequelize:如何绑定<a href=https://www.elefans.com/category/jswz/34/1771377.html style=多个模型进行连接查询并创建自定义列。"/>

Sequelize:如何绑定多个模型进行连接查询并创建自定义列。

我想在这里申请一个join和groupBy。Sequelize v5 将从五个模型中获取记录 桌子 并返回以下格式的记录。

{
  "data": [
    {
      "products": {
        "id": "01",
        "name": "lorium",
        "description": "ipsum",
        "product_images": [
          {
            "url": "", // From images tbl
            "image_type": "Front" // From imge_types tbl
          },
          {
            "url": "",
            "image_type": "Back"
          }
        ]
      },
      "vendor": {
        "first_name": "john",
        "last_name": "doe"
      }
    }
  ]
}

我已经创建了单独的五个模型,并为它们分配了关联。

产品型号::

const Product = SQLize.define('product', {
    id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, }
    product_title: { type: new DataTypes.STRING(255) },
    vendor_id: { type: DataTypes.INTEGER.UNSIGNED }
}); 
Product.hasMany(ProductImages, {foreignKey: 'product_id', targetKey: 'id', as :'product_img_refs'})

export { Product };

供应商模型:::

const Vendor = SQLize.define('vendor', {
  id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
  first_name: { type: DataTypes.STRING(100) },
  last_name: { type: DataTypes.STRING(100) }
});
Product.belongsTo(Vendor, {foreignKey: 'id'})
Vendor.hasOne(Product, {foreignKey: 'id'})

export { Vendor }

产品图片模型:::

const ProductImages = SQLize.define('product_images', {
  id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
  product_id: { type: DataTypes.INTEGER },
  product_image_id: { type: DataTypes.INTEGER }
  img_type_id: { type: DataTypes.INTEGER }
});

export {ProductImages}

Images Model::产品图片模型::。

const ImagesModel = SQLize.define('images', {
  id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
  img_url: { type: DataTypes.STRING }
});
export { ImagesModel }

图片类型模型::图片类型模型::。

const ImageTypes = SQLize.define('image_types', {
  id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
  image_type: { type: DataTypes.STRING }
});
export { ImageTypes }

下面是我进行SQLize操作的仓库文件。更新::

public async getProductData() {
  var prodData = Product.findAll({
    include: [
      { model: Vendor, as: 'vendor' }, 
      { model: ProductImages, as: 'product_img_refs' }
    ]
  });
  return prodData;
}

我没有得到正确的方法来绑定所有模型,从而返回上述json格式中描述的结果。

回答如下:

为了得到如题中所示的嵌套输出,你需要在以下内容之间建立关联。

  • ProductImagesImagesModel
  • ProductImagesImageTypes

完成后,您可以将模型嵌套在 findAll 选项,如下图所示。

// Create associations (depends on your data, may be different)
ProductImages.hasOne(ImagesModel);
ProductImages.hasOne(ImageTypes);

// Return product data with nested models
let prodData = Product.findAll({
    include: [
        { model: Vendor, as: 'vendor' },
        {
            model: ProductImages, as: 'product_img_refs',
            include: [
                { model: ImagesModel }, // Join ImagesModel onto ProductImages (add aliases as needed)
                { model: ImageTypes } // Join ImageTypes onto ProductImages (add aliases as needed)
            ]
        }
    ]
});

更多推荐

Sequelize:如何绑定多个模型进行连接查询并创建自定义列。

本文发布于:2024-05-13 11:18:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   自定义   绑定   模型   Sequelize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!