SELECT WITH CAST OR CONVERT TO AN ILIKE USING SEQUELIZE

Asked

Viewed 239 times

-1

I need to do a full-text search using sequelize, but when I place an iLike to search for an element of an integer or date type column, I can’t send a String, as I would a cast to search for that element?

An example of a query with Postgresql to illustrate what I want to do using Sequelize:

SELECT  "Proposta".id,  "Proposta".id_segurado, "Proposta".data_implantacao, "Proposta".data_assinatura, "Proposta".status, "Proposta".numero_proposta,
            "Segurado".documento, "Segurado".nome,
            p1.id, p1.nome, p1.codigo, p1.documento, p2.id,
            p2.nome, p2.codigo, p2.documento
FROM "Proposta"
LEFT JOIN "Segurado" ON "Proposta".id_segurado = "Segurado".id
LEFT JOIN "Produtor" p1 ON p1.id = "Proposta".id_produtor1
LEFT JOIN "Produtor" p2 ON p2.id = "Proposta".id_produtor2
WHERE ((p1.codigo = '8002866' OR p2.codigo = '8002866') 
AND ("Proposta".status ILIKE '%108297494%' OR "Proposta".numero_proposta ILIKE '%108297494%'
     OR CAST("Proposta".data_implantacao AS VARCHAR) ILIKE '%108297494%'
     OR CAST("Proposta".data_assinatura AS VARCHAR) ILIKE '%108297494%'))

And that’s what I tried to do using Sequelize:

const propostas = await Proposta.findAll({
            where: {
                [Op.or]: [
                    { "$produtor1.codigo$": documento_produtor },
                    { "$produtor2.codigo$": documento_produtor },
                ],
                [Op.or]: [
                    {
                        status: {
                            [Op.iLike]: `%${search}%`,
                        },
                    },
                    {
                        numero_proposta: {
                            [Op.iLike]: `%${search}%`,
                        },
                    },
                    db.Sequelize.where(
                        db.Sequelize.cast(
                            db.Sequelize.col("$produtor1.codigo$", "VARCHAR"),
                            { [Op.iLike]: `%${search}%` }
                        )
                    ),
                    db.Sequelize.where(
                        db.Sequelize.cast(
                            db.Sequelize.col("$produtor2.codigo$", "VARCHAR"),
                            { [Op.iLike]: `%${search}%` }
                        )
                    ),
                ],
            },
            include: [
                {
                    model: Segurado,
                    as: "segurado",
                },
                {
                    model: Produtor,
                    as: "produtor1",
                },
                {
                    model: Produtor,
                    as: "produtor2",
                },
            ]
        })

1 answer

1


Hello,

1- The cast function should receive the column and the field of the bank and not the reference within the Node, so $productor1.code$ would not work.

2-The cast function should be called by the sequelize of index.js inside the models folder that is automatically created by sequelize-cli

then matter

const {sequelize} = require("{path}/models/index.js")

and call the cast function this way:

sequelize.where(
    sequelize.cast(sequelize.col("produtor1.codigo"),"varchar"),
    { [Op.iLike]: `%${search}%` }
)
  • It worked, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.