Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!