gduh
gduh

Reputation: 1182

NodeJS Sequelize - delete with nested select query

I have a SQL query like :

delete from myTable where versionId in (select id from version where code='TEST') 

A select query nested inside a delete query. I would like to build the same query using NodeJS Sequelize, so the code could be something like this :

db.myTable.destroy({
        where: {
          versionId: { $in: [12, 34, 56] }
        }
    }).then(nbDeleted => {
        ...
    }

In that example, [12, 34, 56] is hard coded, but instead of it, I would like to use the select query.

I tried to googled in that way, but for now I'm not able to find the solution.

Any ideas ?

Upvotes: 1

Views: 1595

Answers (1)

joshua.paling
joshua.paling

Reputation: 13952

What you're after is a literal. Try this:

db.myTable.destroy({
    where: {
      versionId: { 
        $in: [
          sequelize.literal("select id from version where code='TEST'")
        ] 
      }
    }
}).then(nbDeleted => {
    ...
}

Sequelize will just put that literal straight into your where-in clause without any processing.

Upvotes: 2

Related Questions