sails.js - I want to add DB connection dynamically after sails lift

前端 未结 3 1947
庸人自扰
庸人自扰 2020-12-06 07:36

During sails lift I don\'t yet have all the connection information for my DB.

Is there a way to either have config values dependent on promises or dynamically creat

3条回答
  •  有刺的猬
    2020-12-06 08:09

    I have found a workaround for MySql DB

    Important: In my case, I will be changing database but all database would have the same schema only difference is in their name and data they contain and make sure to add any error handling you need

    In config/connection.js -------- disable Pooling

     mysql_database: {
       adapter: 'sails-mysql',
       host: 'localhost',
       user: 'root', //optional
       password: '12345', //optional
       database: 'db1', //optional
       pool: false
     },
    

    Now navigate to

    node_modules/sails-mysql/lib/connections/spawn.js

    Add connectionObject.config = sails.SwitchDbConfig

    connectionObject.config = sails.SwitchDbConfig
    
    var conn = mysql.createConnection(connectionObject.config);
      conn.connect(function (err) {
        afterwards(err, conn);
      });
    

    Now Finally Set sails.SwitchDbConfig form anywhere (service , controller ,etc) as

    sails.SwitchDbConfig = { 
                pool: false,
                connectionLimit: 5,
                waitForConnections: true,
                adapter: 'sails-mysql',
                host: 'localhost',
                user: 'root',
                password: '12345',
                database: sails.DB_NAME,
                identity: 'mysql_database' 
              }
    

    And at last if you find something wrong for needs to be updated .... please ping

提交回复
热议问题