norgepaul
norgepaul

Reputation: 6053

What is the correct way to execute a custom query with NestJS?

I'm just getting started with NestJS. I have followed the sequelize database example here https://docs.nestjs.com/recipes/sql-sequelize. This is great when using models, but what is the NestJS method of executing custom queries similar to this one.

res = await this.sequelize.query(
      `
     select * from my_table where id=$id
      `,
      {
        bind: {
          id: id,
        },
        type: QueryTypes.SELECT,
        transaction,
      },

Upvotes: 0

Views: 2705

Answers (1)

Anatoly
Anatoly

Reputation: 22758

You have Sequelize connection instance as a provider so you can try something like this:

return this.catsRepository.query('select * from my_table where id=$id',
   {
     bind: {
       id: id,
     },
     type: QueryTypes.SELECT,
     transaction,
  });

Upvotes: 1

Related Questions