3

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

3
  • 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. Commented May 19, 2015 at 12:05
  • Which database are you using? Commented 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!) Commented Jun 18, 2017 at 17:21

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

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?
@Crusader, can you be more specific?
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)
1

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

Could you please tell me how you got this? @karmic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.