Sails JS, Modelless Query Database
I need to run a query on the association table (generated by sails), so I can't use "Model.query" because I don't have a model for this table. And I couldn't find a solution for this. Thanks to
If you don't have any model, you must use the appropriate adapter for this. I did it with mysql
. Since you didn't say what database you are using, I cannot help you. But I can guide you. Below is an example with an adapter mysql
.
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 any model, you can run any raw request with Model.query(queryStrign, callback)
.
you can use another model object for this, eg User.query(sql,function(err,result){});
.
Late, but I managed to find a way to request 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 admit it's a little long, but it works.