简体   繁体   中英

Sails js: How can I define two MySQL connections to two different databases in a single model in Sails js?

I have created a model Employee.js and EmployeeController.js. I have defined two connections in Employee.js file:

    module.exports = {

    connection: 'LocalhostMysqlServer',
    attributes: {    
      name:{
        type:"string", 
        required:true,
      },
      empnum:{
        type:"integer",
        required:true,
        unique: true
      },
      email:{
        type:"string",
        required:true,
        unique: true
      }  
    },

    connection: 'LocalhostMysqlServer1',
      attributes: {
        username:{
        type:"string", 
        required:true,
      },
      usrnum:{
        type:"integer",
        required:true,
        unique: true
      },
      email:{
        type:"string",
        required:true,
        unique: true
      }
    },
 };

Below is my EmployeeController.js file where I have included two views: CreateEmp & CreateUsr, associated with this Model and controller. Also, I have defined two functions to handle the post requests from these views. Here, I want to insert the data from CreateEmp to a different database and from CreateUsr to a different database.

    module.exports = {

      createEmp: function(req,res){
        'use strict';
        res.view('new.ejs');
      },

      createUsr: function(req,res){
        'use strict';
        res.view('newUser.ejs');
      },

      createEmployee: function(req, res){
        if(req.method=="POST"){
          var name= req.param("name");
          var empnum= req.param("empnum");
          var email= req.param("email");

          var insert= "INSERT INTO employee(name, empnum, email) VALUES ('"+name+"', "+empnum+", '"+email+"')";
          Employee.query(insert, function(err, record){
            if(err)
              console.log(err);
            else{
              console.log(record);
            }
          })
        }
      },

      createUser: function(req, res){
        if(req.method=="POST"){
          var username= req.param("username");
          var usrnum= req.param("usrnum");
          var email= req.param("email");

          var insert= "INSERT INTO user (username, usrnum, email) VALUES ('"+username+"', "+usrnum+", '"+email+"')";
          Employee.query(insert, function(err, record){
            if(err)
              console.log(err);
            else{
              console.log(record);
            }
          })
        }
      },

    };

I have included my config/connections.js here:

    module.exports.connections = {

      localDiskDb: {
        adapter: 'sails-disk'
      },


      LocalhostMysqlServer: {
        adapter: 'sails-mysql',
        //module: 'sails-mysql',
        host: 'localhost',
        user: 'root',
        password: 'disisanshu',
        database: 'sailsTest1',
        tableName: 'employee'
      },

      LocalhostMysqlServer1: {
        adapter: 'sails-mysql',
        host: 'localhost',
        user: 'root',
        password: 'disisanshu',
        database: 'sailsTest2',
        tableName: 'user'
      }

    };

Here I have included my model.js below:

    module.exports.models = {


      // migrate: 'alter'

      connection: 'LocalhostMysqlServer',
      migrate: 'alter',
      connection1: 'LocalhostMysqlServer1',
      migrate: 'alter'

    };

There is no reason why you should do such a thing.

You are probably looking to reduce your load on your database. A master-slave database kind of structure should be implemented instead.

Also, the database should be totally de-coupled from the rest of your app.That is why you should have only one connection. If the load on your database increases, scale it horizontally (add more servers to distribute the load)- mySQL is good for these things. This can and should be done without any change in your app code.

You can't use two different connections in the same model--hopefully there's no documentation that says this is possible! You'll just have to define two different models.

Also, it's not really valid Javascript to declare the same key twice in an object (as you do with connection in your first code block, and with migrate in your last). The second definition will just override the first if they're different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM