Tzahi Ben Artzi
Tzahi Ben Artzi

Reputation: 61

Sequelize.js - build SQL query without executing it

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?

Upvotes: 6

Views: 5105

Answers (3)

Chalkers
Chalkers

Reputation: 1953

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.

Upvotes: 4

Andre Pan
Andre Pan

Reputation: 21

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);

Upvotes: 2

Errorname
Errorname

Reputation: 2459

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

See github issue.

Upvotes: 3

Related Questions