Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sails.js

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 * ".

like image 360
bigradish Avatar asked Dec 02 '22 18:12

bigradish


1 Answers

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);
    });
like image 87
jlonganecker Avatar answered Mar 15 '23 17:03

jlonganecker