javascript - Sequelize条件包含where子句nodejs

标签 javascript node.js sequelize.js

我有这段代码,其中有多个 where 子句:

Time_Sheet_Details.findAll({
    include: [
        {
            model: timesheetNotesSubcon,
            required: false,
            attributes:["note","file_name", "id", "working_hrs", "timestamp", "has_screenshot", "notes_category"]
        },
        {
            model: Timesheet,
            attributes:["id","leads_id","userid"],
            where:  {leads_id: filters.leads_id}, // Client
            include:[
                {
                    model: Lead_Info, attributes:["id","fname","lname","email","hiring_coordinator_id","status"],
                    where:  {hiring_coordinator_id: {$in: filters.sc_id}}, // SC
                    include:[{
                        model: adminInfoSchema,
                        required: false,
                        attributes:["admin_id","admin_fname", "admin_lname", "admin_email", "signature_contact_nos", "signature_company"],      
                    }]

                },
                {
                    model:Personal_Info,attributes:["userid","fname","lname","email"],
                    where: {userid: filters.subcon_id}, // Subcon
                }
            ]
        }],
    where: { 
        reference_date: filters.reference_date
    },
    order:[
        ["id","DESC"]
    ],
    offset:((page-1)*limit),
    limit : limit,
    subQuery:false

}).then(function(foundObject){
    willFulfillDeferred.resolve(foundObject);
});

where 子句是带有注释 Client、SC 和 Subcon 的子句。但是,如果这些 where 子句是可选的,最好的方法是什么?我用它作为搜索过滤器。因此,如果 filters.leads_idnullwhere: {leads_id:filters.leads_id},//Client 不应包含在查询中。与其他人一样。我能想到的唯一解决方案是为每个非空参数的场景重复这些代码块,但这样做过于重复且不切实际。

还有其他方法或解决方案吗?

最佳答案

如果我理解正确,我认为第一步,您应该根据是否设置每个特定搜索条件来定义各自的 where 子句:

const clientWhere = filters.leads_id ? {leads_id: filters.leads_id} : {}
const scWhere = filters.sc_id ? {hiring_coordinator_id: {$in: filters.sc_id}} : {}
const subconWhere = filters.subcon_id ? {userid: filters.subcon_id} : {}

因此,此时如果未设置搜索选项,则只会有一个空对象作为 where 子句。

接下来,在查询中使用那些预定义的 where 子句对象:

Time_Sheet_Details.findAll({
    include: [
        {
            model: timesheetNotesSubcon,
            required: false,
            attributes:["note","file_name", "id", "working_hrs", "timestamp", "has_screenshot", "notes_category"]
        },
        {
            model: Timesheet,
            attributes:["id","leads_id","userid"],
            where: clientWhere, // Client
            include:[
                {
                    model: Lead_Info, attributes:["id","fname","lname","email","hiring_coordinator_id","status"],
                    where: scWhere, // SC
                    include:[{
                        model: adminInfoSchema,
                        required: false,
                        attributes:["admin_id","admin_fname", "admin_lname", "admin_email", "signature_contact_nos", "signature_company"],      
                    }]

                },
                {
                    model:Personal_Info,attributes:["userid","fname","lname","email"],
                    where: subconWhere, // Subcon
                }
            ]
        }],
    where: { 
        reference_date: filters.reference_date
    },
    order:[
        ["id","DESC"]
    ],
    offset:((page-1)*limit),
    limit : limit,
    subQuery:false

}).then(function(foundObject){
    willFulfillDeferred.resolve(foundObject);
});

关于javascript - Sequelize条件包含where子句nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733737/

相关文章:

javascript - 使用 TypeScript 扩展 Breeze 实体

node.js - 使用http和express有什么好处

node.js - 如何在 node.js 和 socket.io 应用程序中组织套接字处理

node.js - Sequelize - 在关联表上选择

javascript - Sequelize.js setter 函数没有按我的预期工作

node.js - 使用正则表达式进行序列化验证

javascript - 使用 jQuery .each() 的递增数字

Javascript 下拉导航 - 缩小未选择的选项

javascript - 如何将此对象中的每个 key 对作为新对象添加到数组中?

javascript - 如何在mongodb数据库上存储setTimeout id?