简体   繁体   English

Sails js:如何在Sails js中的单个模型中为两个不同的数据库定义两个MySQL连接?

[英]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. 我创建了一个模型Employee.js和EmployeeController.js。 I have defined two connections in Employee.js file: 我在Employee.js文件中定义了两个连接:

    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. 下面是我的EmployeeController.js文件,其中包含两个视图:CreateEmp和CreateUsr,与此模型和控制器相关联。 Also, I have defined two functions to handle the post requests from these views. 此外,我已经定义了两个函数来处理来自这些视图的post请求。 Here, I want to insert the data from CreateEmp to a different database and from CreateUsr to a different database. 在这里,我想将CreateEmp中的数据插入到不同的数据库中,并将CreateUsr中的数据插入到不同的数据库中。

    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: 我在这里包含了我的config / connections.js:

    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: 这里我在下面包含了我的model.js:

    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. 如果数据库上的负载增加,请将其水平扩展(添加更多服务器以分配负载) - mySQL适用于这些事情。 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). 此外,在对象中两次声明相同的密钥并不是真正有效的Javascript(就像在第一个代码块中使用connection ,并在最后一个代码块中进行migrate )。 The second definition will just override the first if they're different. 如果它们不同,第二个定义将覆盖第一个定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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