6

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 Answers 3

4

You can just call the QueryGenerator with the type of query you want to generate. For example a selectQuery:

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

There's also insertQuery, updateQuery, deleteQuery too.

Sign up to request clarification or add additional context in comments.

2 Comments

Might also be MyModel.queryGenerator.
I had to use Utils.mapFinderOptions(queryOptions, MyModel) for Sequelize to transform field names to db column names.
3

It looks like this feature isn't implemented yet, but there are some users trying to push the issue forward.

See github issue.

Comments

2

it's undocumented, but I use next way (Sequelize ver. 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.