gpt4 book ai didi

javascript - 动态 where 子句 Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:31:11 25 4
gpt4 key购买 nike

我有以下代码:

const { TN, TITLE, QUEUE, TYPE, SERVICE, OWNER, CUSTOMER_ID, CUSTOMER_USER_ID, CREATE_BY, create_time, ESTADO_DO_SLA } = req.query;

let query = {};
if (TN != null) query.TN = TN;
if (TITLE != null) query.TITLE = TITLE;
if (QUEUE != null) query.QUEUE = QUEUE;
if (TYPE != null) query.TYPE = TYPE;
if (SERVICE != null) query.SERVICE = SERVICE;
if (OWNER != null) query.OWNER = OWNER;
if (CUSTOMER_ID != null) query.CUSTOMER_ID = CUSTOMER_ID;
if (CUSTOMER_USER_ID != null) query.CUSTOMER_USER_ID = CUSTOMER_USER_ID;
if (CREATE_BY != null) query.CREATE_BY = CREATE_BY;

const limit = req.query.limit != null ? Number.parseInt(req.query.limit) : 1000;
const page = req.query.page != null ? (Number.parseInt(req.query.page) * limit) - limit : 0;


const tickets = await ViewSLA.findAll({
raw: true,
offset: page,
limit: limit,
where: query
});
关键是, 部分工作的 ,但仅当值相等时才匹配,但我想在某些参数中执行一些 Op.like 并保留其他参数。
例如,匹配 TN(票号)参数的确切值是有意义的,但我想通过 TITLE 参数进行过滤,如果它被告知,在 where 子句中使用 like 。
那可能吗?

最佳答案

我刚刚找到了一个方法:

const { TN, TITLE, QUEUE, TYPE, SERVICE, OWNER, CUSTOMER_ID, CUSTOMER_USER_ID, CREATE_BY, create_time, ESTADO_DO_SLA } = req.query;

let query = {};
if (TN != null) query.TN = TN;
if (TITLE != null) query.TITLE = {
[Op.like]: `%${TITLE}%`
};
if (QUEUE != null) query.QUEUE = QUEUE;
if (TYPE != null) query.TYPE = TYPE;
if (SERVICE != null) query.SERVICE = SERVICE;
if (OWNER != null) query.OWNER = OWNER;
if (CUSTOMER_ID != null) query.CUSTOMER_ID = CUSTOMER_ID;
if (CUSTOMER_USER_ID != null) query.CUSTOMER_USER_ID = CUSTOMER_USER_ID;
if (CREATE_BY != null) query.CREATE_BY = CREATE_BY;
if (ESTADO_DO_SLA != null) query.ESTADO_DO_SLA = ESTADO_DO_SLA;

const limit = req.query.limit != null ? Number.parseInt(req.query.limit) : countTickets;
const page = req.query.page != null ? (Number.parseInt(req.query.page) * limit) - limit : 0;


const tickets = await ViewSLA.findAll({
raw: true,
offset: page,
limit: limit,
where: query
});
像魅力一样工作。

关于javascript - 动态 where 子句 Sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66748417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com