我必须创建一个动态的sequelize查询,它可以有1到3个条件,第一个是类似查询,第二个是完全匹配 . 用户可以选择任何组合 . 我似乎无法格式化where条件,以使其正常工作 .
在玩完它后,这很有用 .
我使用打字稿,所以如果不是,请调整 .
let whereClause = {}; if (searchCriteria.documentName) { whereClause['documentName'] = { $like: '%' + searchCriteria.documentName + '%' }; } if (searchCriteria.language) { whereClause['targetLanguage'] = searchCriteria.language; } if (searchCriteria.department) { whereClause['departmentName'] = searchCriteria.department; } let documents = await this._model.DocumentTranslation.findAll({ include: [{ model: this._model.Document }], order: [ ['createdAt', 'ASC'] ], where: whereClause });
1 回答
在玩完它后,这很有用 .
我使用打字稿,所以如果不是,请调整 .