Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT WITH CAST OR CONVERT IN iLike USING SEQUELIZE

I need to do a full-text search with Sequelize, but when i have to use an iLike with a INTEGER or a DATE column i can't send a String in the where clause, how can i do the cast off an column?

Example with a Postgresql query what I want to do:

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 this what i tried to do with 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",
                },
            ]
        })
like image 543
Zilmar Avatar asked Sep 10 '25 16:09

Zilmar


1 Answers

Some notes:

1- Cast function dont works with node reference, so the $produtor1.codigo$ wouldn't work, you have to use the postgres reference, in that case would be produtor1.codigo

2- You have to call the function by the sequelize that is defined in the models/index.js

So:

call the sequelize like that:

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

And call the cast function like that:

sequelize.where(
    sequelize.cast(sequelize.col("produtor1.codigo"),"varchar"),
    { [Op.iLike]: `%${search}%` }
)
like image 138
Bruno Santi Avatar answered Sep 13 '25 05:09

Bruno Santi