Как искать по включенным моделям в одном "where"?

Мой "where" для поиска:

where = {
  [Op.or]: [
    {
      name: {
        [Op.like]: `%${word}%`,
      },
    },
    {
      description: {
        [Op.like]: `%${word}%`,
      },
    },
    {
      '$types.name$': {
        [Op.like]: `%${word}%`,
      },
    },
  ],
};

И метод получения products с пагинацией, который должен включать в себя модели Type и Feature:

const products = await this.productModel.findAndCountAll({
  subQuery: false,
  limit,
  offset,
  distinct: true,
  order: [['name', 'ASC']],
  where,
  include: [
    {
      model: Type,
      include: [
        {
          model: Feature,
        },
      ],
    },
  ],
});

Я должен использовать subQuery: false для работы '$types.name$'. Но subQuery: false не работает корректно с limit и offset (я не получаю все данные).

Есть ли способы исправить это?

Product model:

@Table({ tableName: 'products' })
export class Product extends Model<Product, ProductCreationAttrs> {
  @Column({
    type: DataType.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  })
  id: number;

  @Column({ type: DataType.STRING, unique: true, allowNull: false })
  name: string;

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

  @HasMany(() => Type, { foreignKey: 'productId' })
  types: Type[];
}

Type model:

@Table({ tableName: 'types' })
export class Type extends Model<Type, TypeCreationAttrs> {
  @Column({
    type: DataType.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  })
  id: number;

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

  @BelongsTo(() => Product, { foreignKey: 'productId' })
  product: Product;

  @BelongsToMany(() => Feature, () => TypeFeatures)
  features: Feature[];
}

Dialect: MariaDB


Ответы (0 шт):