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

MongoDB and MySQL Configuration

1. Create new project

You can follow this tutorial for setting up sails project
Setting up Sails project

2. Installing Packages

To use MySQL/MongoDB, first you need to install adapter for it

    • Install sails-mongo adapter using npm.

      npm install sails-mongo --save
      

 

  • Install sails-mysql adapter using npm

    npm install sails-mysql --save
    

 

3. Configure DB

You can configure DB in config/connections.js

    • Configure MonogDB in connections using below code

      mongoDB: {
                          adapter: 'sails-mongo',
                          host: 'localhost',
                          port: 27017,
                          user: '', //optional
                          password: '', //optional
                          database: 'my_mongo' //optional
                      }
      

 

  • Configure MySQL in connections using below code

    mysqlDB: {
           adapter: 'sails-mysql',
           host: 'localhost',
           user: 'root', //optional
           password: 'root', //optional
           database: 'sailsit' //optional
        }
    

 

4. Model Configuration

You can configure database to be used by model at global level (for all models) or at specific model level

    • Configuring DB at global level
      Define DB connection name in config/models.js

      connection: 'mongoDB' //This name should be exactly same as defined in config/connections.js
      

 

  • Configuring DB at model level
    Define DB connection at specific model in api/models/YourModel.js

    module.exports = {
        connection: 'mongoDB', 
        attributes:{
    
        }
    };
    

You can get the sample project setup with Mongo/MySQL configuration at below URL
SailsMongoMySQLConfig

One Comment

  1. Pingback: Sails Callback to Promises - SailsIt

Comments are closed.