如何在 sequelize 中为查询中的所有模型设置公共 where 参数

How to set common where param to all models in query, in sequelize

我正在尝试实现查询,在那里我可以找到所有记录,其中至少一个属性包括文本,用户随请求发送,因为我在哪里使用,但它可以搜索,因为我理解,只在自己的模型中,但我需要它在父模型和相关联的字段中一起搜索它,而不是分开。

有我的模特:

const Picture = sequelize.define<IPictureInstance>('picture', {
   id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
   img: { type: DataTypes.STRING, allowNull: false },
   mainTitle: { type: DataTypes.STRING, allowNull: false },
   description: { type: DataTypes.TEXT }
});

const PictureInfo = sequelize.define<IPictureInfoInstance>('pictureInfo', {
   id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
   title: { type: DataTypes.STRING, allowNull: false },
   description: { type: DataTypes.STRING, allowNull: false }
});

const PictureTag = sequelize.define<IPictureTagInstance>('pictureTag', {
   id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
   text: { type: DataTypes.TEXT, allowNull: false }
});

const PicturesTags = sequelize.define<IPicturesTagsInstance>('picturesTags', {
   id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }
});

及其关联:

Picture.belongsToMany(PictureTag, { through: PicturesTags, as: "tags", onDelete: 'cascade' });
PictureTag.belongsToMany(Picture, { through: PicturesTags, as: "pictures", onDelete: 'cascade' 
});

Picture.hasMany(PictureInfo, { onDelete: "cascade" });
PictureInfo.belongsTo(Picture);

这就是我尝试做的事情:

  static async getPictures(query: string | undefined) {
const pictures = await models.Picture.findAll({
  where: {
    [Op.or]: {
      mainTitle: { [Op.iRegexp]: `${query}` },
      description: { [Op.iRegexp]: `${query}` },
    },
  },
  include: [
    {
      model: models.PictureInfo,
      as: "pictureInfos",
      where: {
        [Op.or]: {
          title: { [Op.iRegexp]: `${query}` },
          description: { [Op.iRegexp]: `${query}` }
        }
      },
      required: false
    },
    {
      model: models.PictureTag,
      as: "tags",
      attributes: ["id", "text"],
      where: { text: { [Op.iRegexp]: `${query}` } },
      through: {
        attributes: [],
      },
      required: false
    }

  ],

});
  return pictures;
}

但在这种情况下,当它在 first where param it returns 一个空数组中找不到记录时,我理解,但这不是我需要的行为。

我需要一起检查每个属性。

因此,如果用户发送query=cat,它将检查mainTitle 和description,如果没有,它将检查关联的pictureInfos 字段,如果没有,则检查pictureTags 关联的字段,这就是我需要的,将不胜感激。

我的解决方案:

static async getPictures(query: string | undefined) {

const whereStatement = {
  [Op.or]: {
    mainTitle: { [Op.iRegexp]: `${query}` },
    description: { [Op.iRegexp]: `${query}` },
    "$tags.text$": { [Op.iRegexp]: `${query}` },
    "$pictureInfos.title$": { [Op.iRegexp]: `${query}` },
    "$pictureInfos.description$": { [Op.iRegexp]: `${query}` },
  }
};

const pictures = await models.Picture.findAll({
  where: whereStatement,
  include: [
    {
      model: models.PictureInfo,
      as: "pictureInfos",
    },
    {
      model: models.PictureTag,
      as: "tags",
    }

  ],
});

return pictures;
}

但我仍然对那些 $ 符号感到困惑,它们的用途是什么,我在文档中找不到答案?

Link to the Sequelize documentation, to confirm my answer