KSDaemon/sails-hook-sequelize

How to do raw SQL queries to database?

mrded opened this issue · 6 comments

mrded commented

I'm looking for a way to access sequelize.query to make a native sql query.

As I understand sails-hook-sequelize initialises a pool connection with database, and it should be possible to reuse it for own needs.

Can you please point me how to do that?

Thank you.

Well, actually there are a more than one points in your question :)

  1. Right now only Sequelize module itself is exposed to global
  2. What you are willing to do — is to make a native query on top of already initialized connection (one of used in application) — that is not possible right now. But i think i'll implement it quickly
  3. About connection pool: it depends on what options have you defined for connection

Check current master branch. If you have exposeToGlobal in config set to true, than you have a Global SequelizeConnections object with keys named as connections in runtime. And you can run SequelizeConnections.myLuckyConnection.query()

mrded commented

I do have exposeToGlobal enabled, however I still cannot access SequelizeConnections object.

// config/sequelize.js
module.exports = {
  sequelize: {
    exposeToGlobal: true
  }
}; 
// api/services/MysqlService.js
module.exports.query = function(sql) {
  console.log(SequelizeConnections); // ReferenceError: SequelizeConnections is not defined
  //...
};

I'll check it

Hm... I see SequelizeConnections global variable during tests. So it works.
Can you create a test repo with this situation?

well, i've just tested it. SequelizeConnections are available within services.
Closing for now. Feel free to reopen and provide more info...