简体   繁体   English

使用sails js版本1提升后的动态数据库连接

[英]Dynamic database connection after lift using sails js version 1

i want solution for MYsql only 我只想要MYsql的解决方案

I want to switch database on per request bases i have achieved it in previous versions of sails but with this new version (1.0.0) i am not able to do sails.js - I want to add DB connection dynamically after sails lift 我想在每个请求的基础上切换数据库我已经在以前版本的风帆中实现了它但是使用这个新版本(1.0.0)我无法做sails.js - 我希望在风帆提升后动态添加数据库连接

there is no documentation for it 它没有文档

i have primary database lets say DB_A which i use as default datastore to authenticate users after it users have their own database to interact and after lift it can be any database present in system with which user can connect and fetch 我有主数据库让我说DB_A,我用它作为默认数据存储区来验证用户后,用户有自己的数据库进行交互,升级后,它可以是系统中存在的任何数据库,用户可以连接和获取

a few option i discovered 我发现了一些选择

1) reload orm 1)重新加载orm

2) create-manager 2)创建经理

but dont know how to work with it as none of them are suggested 但不知道如何使用它,因为没有人建议

can any one help me with it 任何人都可以帮助我

I was facing the similar issue while I was using PostgreSQL in sails app. 我在sails app中使用PostgreSQL时遇到了类似的问题。 I found a workaround below. 我在下面找到了一个解决方法。 ie npm install pg. 即npm install pg。 in your case install mysql plugin ie npm i mysql 在你的情况下安装mysql插件即npm我mysql

I have created dbconnections.js in the sails app controllers folder 我在sails app controllers文件夹中创建了dbconnections.js

const a = {
  host: 'abc.com',
  port: 'xxxx',
  database: 'xxxx',
  user: 'xxxx',
  password: 'xxxx'
}

const b = {
  host: 'ecd.com',
 port: 'xxxx',
  database: 'xxxx',
  user: 'xxxx',
  password: 'xxxx'
}


module.exports = { a: a, b: b};

then in my other controllers I have used pg plugin to match with the user passed db env through api 然后在我的其他控制器中,我使用pg插件来匹配通过api传递db env的用户

ie in my other controller 即在我的另一个控制器中

var sourceFile = require('./dbconnections.js');

const {
      Client
    } = require('pg');

for your case you could follow this https://www.npmjs.com/package/mysql to create connection object and follow the below logic to dynamically connect to different dbs 对于您的情况,您可以按照此https://www.npmjs.com/package/mysql创建连接对象并按照以下逻辑动态连接到不同的dbs

    var match_env;

    for (x in sourceFile) {

      if (x === env) {
        match_env = sourceFile[x];

      }
    }

    const client = new Client(match_env)

now client object will have all postgresql transactions which one could execute, ie client.connect() const qry = "select * from person where city= "newyork"; client.query(qry, function (err, resp) { 现在客户端对象将具有可以执行的所有postgresql事务,即client.connect()const qry =“select * from person where city =”newyork“; client.query(qry,function(err,resp){

    console.log("before", client)

    client.end();
    console.log("after", client)
    //user = JSON.parse(user);
    if (err) {
      return res.json({
        error: err
      });
    }
    if (resp === undefined) {
      return res.notFound();
    } else
      console.log(resp);
    return res.json({
      userData: resp.rows
    });

  }

);

Based on the use case we could move the above code to other js file and call in as many controller as we can and this would work with multiple for multiple db connections of postgres sql in sails app. 基于用例,我们可以将上面的代码移动到其他js文件并尽可能多地调用控制器,这将适用于sails app中postgres sql的多个数据库连接的多个。

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

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