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