Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Running raw complicated queries with sequelize

I have a peculiar problem. I have a joined query due to the distribution of data in various table. I want to run this as raw sql query using node module for sequelize as alot existing code is associated using sequelize orm. I do not want to use orm here but just a plain raw query.

SELECT testmodel.bacode, testmodel2.paopkey from (SELECT ptr.paopkey as paopkey, psgkey.pskey from (SELECT * FROM   pts where paopkey = 4 and ptr_active = 1) ptr,
pt_sg psgkey where ptr.pkey = psgkey.pkey) testmodel2,
pt_act testmodel where testmodel.pskey =  testmodel2.pskey;

Any advise if the above query can be run as raw query and the data can be captured as result. Also when I run the below code with below sequelize code, I get the error "_sequelize.default.query is not a function"

import Sequelize from 'sequelize';
async function getTestDataList(opcoKey) {
  const testdata = await Sequelize.query(`SELECT testmodel.bacode, testmodel2.paopkey from (SELECT ptr.paopkey as paopkey, psgkey.pskey from (SELECT * FROM pts where paopkey = 4 and ptr_active = 1) ptr,
  pt_sg psgkey where ptr.pkey = psgkey.pkey) testmodel2,
  pt_act testmodel where testmodel.pskey =  testmodel2.pskey;`, 
  { type: Sequelize.QueryTypes.SELECT }).then(function (results) {
     // SELECT query - use then
  })
  return toPlain(testdata);
}

Dbclient code using sequelize instance is as follows

import config from 'config';
import Sequelize from 'sequelize';
class DataSource { constructor() {this.DBclient = null; } 
initializeDB(dbCredentials) { const dataSource = config.datasource; 
const options = { host: "127.0.0.1", dialect: dataSource.dialect, port: dataSource.port, logging: false, }; 
const { password } = dbCredentials || dataSource; 
this.DBclient = new Sequelize( dataSource.database, "root", "root", options, ); 
} getDBClient() { return this.DBclient; }}export default new DataSource();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should store a created Sequelize connection and use it to call query method:

const sequelize = new Sequilize(connection_options_here)

const testdata = await sequelize.query(`SELECT testmodel.bacode, testmodel2.paopkey from (SELECT ptr.paopkey as paopkey, psgkey.pskey from (SELECT * FROM pts where paopkey = 4 and ptr_active = 1) ptr,
  pt_sg psgkey where ptr.pkey = psgkey.pkey) testmodel2,
  pt_act testmodel where testmodel.pskey =  testmodel2.pskey;`, 
  { type: Sequelize.QueryTypes.SELECT })

I removed then in the above code because there is no need in it thus you use await.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda