mo.dhouibi
mo.dhouibi

Reputation: 595

Sails JS, Query database without a Model

I need to run query to the association table (created by sails), So I cannot use "Model.query",because I don't have model for this table. And I couldn't find the solution for this. Thanks

Upvotes: 3

Views: 1978

Answers (3)

karmic
karmic

Reputation: 11

Kind of late to the party but I did manage to find a way to query a connection without defining or using any models:

sails.adapters['sql-server'].query('my-sqlserver',null,'select *...', null, function(err, data){
  if(err)...
  return data;
});

I'll admit, it is a bit long but it works.

Upvotes: 1

taufique
taufique

Reputation: 2751

If you don't have any model defined, you have to use relevant adapter to do it. I have done it with mysql. As you have not said what database you are using, I can't help you to the point. But I can guide you. An example with mysql adapter is following.

var mysql = require('mysql');
var connection = mysql.createConnection({
    host     : sails.config.connections.mysql.host,
    user     : sails.config.connections.mysql.user,
    password : sails.config.connections.mysql.password,
    database:  sails.config.connections.mysql.database
});
connection.query(queryString, function(err, records){
    // Do something
});

Or if you have a any model defined, then you can run any raw query with Model.query(queryStrign, callback).

Upvotes: 1

zieglar
zieglar

Reputation: 830

you can use other model object like User.query(sql,function(err,result){}); to do that.

Upvotes: 3

Related Questions