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
-
Why you don't create a model for your association table ? If you really need to make a request on it just create a model. I don't know if you can make a query without model.jaumard– jaumard2015-05-19 12:05:12 +00:00Commented May 19, 2015 at 12:05
-
Which database are you using?taufique– taufique2015-05-19 14:20:45 +00:00Commented May 19, 2015 at 14:20
-
@jaumard when the (mongodb) database is already there with data (from another app, not sails app) in it, and I want to use that database with a new sails.js app, can I just create a Model which has the same structure like the Collection in my database? and the data will not be deleted or modified at all? (!important!)Suisse– Suisse2017-06-18 17:21:24 +00:00Commented Jun 18, 2017 at 17:21
Add a comment
|
3 Answers
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).
3 Comments
Crusader
can I use default model methods to generate sql for some parts? I mean where(criteria) - Or do I need to parse it by myself?
taufique
@Crusader, can you be more specific?
Crusader
I want to make sql from string + parsed criteria from request, something like this:
'select ... from table join table 2' + Model.where(request) + Model.order(request) + Model.limit(request)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.
1 Comment
jlanza
Could you please tell me how you got this? @karmic