{ 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!

One to One Association

In this tutorial we will be covering One-to-One associations in SailsJs Models

1. Creating SailsJs Project

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

2. MySQL Database Setup

We will be using MySQL DB for this tutorial.
You can follow the below tutorial about setting up MySQL Db
Setting up MySQL in SailsJs

Import the SQL File in your MySQL database, you can find the SQL file at Github URL below

3. Creating Models

We will create two models People and FoodItems same the tables created in previous step
Use the below commands to generate models

sails generate model People
sails generate model FoodItems

Add the below model definition in respective Models

api/models/People.js

module.exports = {

    tableName: "people",
    attributes: {
        id: {
            type: "integer",
            autoIncrement: true,
            primaryKey: true
        },
        name: {
            type: "string"
        },
        age: {
            type: "integer"
        },
        country: {
            type: "integer"
        },
        food: {
            model: "FoodItems",
            columnName: "food_id"
        }
    }
};

 

api/models/FoodItems.js

module.exports = {

    tableName: "food_items",
    attributes: {
        id: {
            type: "integer",
            autoIncrement: true,
            primaryKey: true
        },
        food_name: {
            type: "string"
        }
    }
};

To create relation between two models, we can define model as an attribute in the parent model as in above example, we have defined food key for the food_items model.
Now we can use populate function of Sails which will create the association query and returns the data

4. Creating Controller

Let’s generate a controller using below command

sails generate controller People

 

5. Writing Code

Let’s write a function which takes country as input and returns all the people of that country with food items using one to one association

Callback

getPeopleByCountryCallback: function (req, res) {
    People.find({country: req.param("country")}).populate("food").exec(function (err, people) {
       if(err) {
           sails.log.debug(err);
           res.serverError();
       }
       res.send(people);
    });
}

Promise

getPeopleByCountry: function (req, res) {
    People.find({country: req.param("country")}).populate("food")
        .then(function (people) {
            res.send(people);
        })
        .catch(function (err) {
            sails.log.debug(err);
            res.serverError();
        });
}

 

6. Creating Routes

Let’s create routes for these two functions
Copy the below routes and paste in your config/routes.js

'/api/people/:country': 'PeopleController.getPeopleByCountry',
'/api/people/callback/:country': 'PeopleController.getPeopleByCountryCallback',

 

7. Running application

Now you can run your sails application by running the below command

sails lift

 

You can get the complete source code of this tutorial in below URL
SailsOneToOneAssociation