【问题标题】:How can I use two different databases in one single node app?如何在一个单节点应用程序中使用两个不同的数据库?
【发布时间】:2021-12-13 08:30:01
【问题描述】:

我已经安装了 Hbase 客户端和 PostgreSql 客户端,但是如何在单个节点应用程序中连接两个数据库

import { error } from "console";

const hbase = require('hbase');

export class Db {

    private conn = hbase();
    private config = { host: '0.0.0.0', port: 8080 };
    public client = new hbase.Client(this.config);

    constructor() {
        this.conn = new hbase.Client(this.config);
    }

    public conection() {
        this.conn.table('messages').exists((error: string, succuss: string) => {
            if (!succuss) {
                this.createTable('messages', 'message_data');
            }
        });
    }

    private createTable(TblName: string, CF: string) {
        this.conn.table(TblName).create(CF, function (error: string, success: string) {
            console.log(success);
            return success
        });
    }

}

【问题讨论】:

    标签: node.js postgresql hbase


    【解决方案1】:

    我建议为 Hbase 和 PostgreSql 创建两个不同的类。并在需要时在您的应用程序中使用它们。

    另一件事也在构造函数中使用依赖注入,而不是在类中定义配置。这样您就可以在实例中注入任何数据库配置。

    这是代码示例

    1. 创建类来管理 HBaseDB 连接

    import { error } from "console";
    
    const hbase = require('hbase');
    
    export class HBaseDB {
        
        //Inject this config object in your class constructor
        //private config = { host: '0.0.0.0', port: 8080 };
        
        //Here we have injected config object 
        constructor(config) {        
            this.conn = new hbase.Client(config);
        }
    
        public conection() {
            this.conn.table('messages').exists((error: string, succuss: string) => {
                if (!succuss) {
                    this.createTable('messages', 'message_data');
                }
            });
        }
    
        private createTable(TblName: string, CF: string) {
            this.conn.table(TblName).create(CF, function (error: string, success: string) {
                console.log(success);
                return success
            });
        }
    
    }
    1. 创建类来管理 PostgreSQL 连接

    const pg = require('pg');
    
    export class PostgresqlDB {
    
        constructor(config) {
            //config contains database,username,password etc... configs
            this.pool = new pg.Pool({ config })
            this.conn = undefined
        }
    
        public async conection() {
            //If connection is already connected, no need to connect again
            //This will save time
            if (this.conn === undefined)
                this.conn = await this.pool.connect()
        }
    
    
        public async query(query) {             
            await this.conection()
            const response = await this.conn.query(query)       
            return response
        }
    
    }

    现在你可以在代码中使用它们

    const pgDB = require('./PostgresqlDB')
    const hbaseDB = require('./HBaseDB')
    
    const hbaseDBConn = new hbaseDB({ host: '0.0.0.0', port: 8080 })
    const pgDBConn = new pgDB({ database: 'test', user:'test',password:'test'})

    注意:以上代码仅供理解之用,您可能需要添加验证并更正一些语法以供实际使用

    【讨论】:

    • 给我举个例子?
    • @P.Mondal 添加了一些示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2011-08-07
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多