Sequelize.js - build SQL query without executing it

时间:2018-12-19 11:11:53

标签: node.js sequelize.js

Is there a way to get Sequelize to build a query with the replacements (so I'll be able to use their SQL injection cleanup) and just get the raw SQL query, without executing it?

3 个答案:

答案 0 :(得分:1)

该功能似乎尚未实现,但是有些用户试图将问题继续进行。

请参见github issue

答案 1 :(得分:0)

它没有记录,但是我使用了下一种方法(Sequelize版本5.2.13):

let tableName = myModel.getTableName(options);
let qi = myModel.QueryInterface;
options.type = 'SELECT';
options.model = myModel;
var sql = qi.QueryGenerator.selectQuery(tableName, options, myModel);

答案 2 :(得分:0)

您可以使用您要生成的查询类型调用QueryGenerator。 例如selectQuery

    const sql = MyModel.QueryGenerator.selectQuery(
        MyModel.getTableName(), {
        where: {
            someAttribute: "value"
        },
        attributes: ["other", "attributes"]
    },
        MyModel);

还有insertQueryupdateQuerydeleteQuery