bigradish
bigradish

Reputation: 41

Is there a way for sails.js to select fields in SQL queries?

I'm new to sails.js. It seems that there is no way for sails.js to make a SQL query for select some fields/columns of a table. The queries seem all "select * ".

Upvotes: 4

Views: 4560

Answers (4)

MSi
MSi

Reputation: 107

Im using sails 0.12 and what I figure is: Whenever .populate or .paginate are used(with .find), 'select' should be used. For other cases,'fields'should be used

Upvotes: 0

jemiloii
jemiloii

Reputation: 25719

Fields no longer works, you must use select instead.

Model.find({field: 'value'}, {select: ['id', 'name']})
  .paginate({page: 1}, {limit: 10})
  .exec(function(err, results) {
    if(err) {
      res.badRequest('reason');
    }
    res.json(results);
});

Upvotes: 8

jlonganecker
jlonganecker

Reputation: 1198

I just found a way to make this happen. I am using the 2nd parameter of Model.find

Model.find({field: 'value'}, {fields: ['id', 'name']})

If you set fields to false it simulates a SELECT *

Model.find({field: 'value'}, {fields: false})

A complete example:

Model.find({field: 'value'}, {fields: ['id', 'name']})
    .paginate({page: 1}, {limit: 10)
    .exec(function(err, results) {
        if(err) {
            res.badRequest('reason');
        }
        res.jsonx(results);
    });

Upvotes: 9

user810606
user810606

Reputation:

There is no Waterline SELECT implementation at the moment, but you can use Model.query(sqlQuery, callback) to run a raw SQL query on your database.

For example:

User.query('SELECT email, username FROM users WHERE id = 10;', function (err, users) {
  // stuff
});

Upvotes: 1

Related Questions