{ sailsit }

Sails Js Tutorials for Beginners and Forum

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Raw Native Queries

1. Creating SailsJs Project

Follow the below tutorial for setting up SailsJs
Setting up sails project

2. Database Setup

You can follow the below tutorial about setting up MySQL & MongoDb
Setting up MySQL & MongoDB in SailsJs

3. Creating Models

Use the below command to generate People model

sails generate model People
sails generate model Movies

add the below code in api/model/People.js

module.exports = {
    connection: "mysqlDB",
    tableName: "people",
    attributes: {
        id: {
            type: "integer",
            autoIncrement: true,
            primaryKey: true
        },
        name: {
            type: "string"
        },
        age: {
            type: "integer"
        },
        country: {
            type: "integer"
        }
    },
    getPeople: function (cb) {
        People.query("SELECT * FROM people", [], function (err, results) {
            if(err || !results || results.length == 0) {
                cb({msg: "Nothing found"}, null);
                return;
            }
            cb(null, results);
        });
    }
};

This model is connected to a mysqlDB and uses raw sql query to fetch data

add the below code in api/model/Movies.js

module.exports = {

    connection: "mongoDB",
    tableName: "movies",
    autoPK: true,
    attributes: {
        movie_name: {
            type: "string"
        },
        image: {
            type: "string"
        },
        rating: {
            type: "float"
        },
        release_year: {
            type: "integer"
        },
        genre: {
            type: "string"
        },
        running_time: {
            type: 'integer'
        }
    },

    /**
     * Native find query for mongo
     * Collection will support all the mongo native queries
     * @param cb
     */
    getMovies: function (cb) {
        Movies.native(function(err, collection){
            if(err || !collection){
                cb({msg: "nothing found"}, null);
                return;
            }

            collection.find().toArray(function (err, results) {
                if(err || !results || results.length == 0) {
                    cb({msg: "Nothing found"}, null);
                    return;
                }
                cb(null, results);
            });

        });
    },
};

This model is connected to a mongoDB and uses native query of mongo to fetch data

4. Creating Controller

Use the below command to generate a controller

sails generate controller RawQueries

paste the below code in api/controllers/RawQueriesController.js

module.exports = {

    getPeople: function (req, res) {
        People.getPeople(function (err, results) {
            if(err) {
                return res.send({
                    "success": false,
                    "message": (err.msg) ? err.msg : "Nothing found"
                });
            }

            return res.send({
                "success": true,
                "data": results
            });
        });
    },

    getMovies: function (req, res) {
        Movies.getMovies(function (err, results) {
            if(err) {
                return res.send({
                    "success": false,
                    "message": (err.msg) ? err.msg : "Nothing found"
                });
            }

            return res.send({
                "success": true,
                "data": results
            });
        });
    }

};

 

5. Creating Route

Let’s create a route for triggering the controller function
Paste the below route in your config/routes.js

'GET /api/raw/people': 'RawQueriesController.getPeople',
'GET /api/native/movies': 'RawQueriesController.getMovies',

 

6. Running Sails

Now you can start your sails project using below command

sails lift

MySQL & MongoDB export will also be available with the code in github
You can find the source code for the tutorial at below URL
RawNativeQueriesSourceCode