user12929912
user12929912

Reputation:

Sequelize: Find All That Match the query (Case Insensitive)

If I have a context.data.filename ='DOC.doc' and the database record contains record for example DOC.doc and DOC-1.doc then it should return both data with the employeeId, but right now it only returns DOC.doc that matches which is wrong , it should also return DOC-1 since it matches the patter.

Any idea guys ? would be much apprecaited , thank you.

#Code

  const file = await context.service.Model.findAll({
    where: {
      filename: {
        [Op.like]: `%${context.data.filename}%`,
      },
      employeeId: record.id,
    },
    paranoid: false,
  });

Upvotes: 0

Views: 499

Answers (1)

vicbyte
vicbyte

Reputation: 3790

This is not a problem with sequelize. If you are searching for DOC.doc and want to find DOC-1.doc, you need to put the % sign before the dot. % means "any number of any character". So your search query should look like DOC%.doc.

For example:

const filename = '%' + context.data.filename.replace('.', '%.')  + '%';
const file = await context.service.Model.findAll({
    where: {
      filename: {
        [Op.like]: filename,
      },
      employeeId: record.id,
    },
    paranoid: false,
});

Upvotes: 1

Related Questions