如何通过 node.js 连接到多个 orientdb?

问题描述

我正在使用 orientdb 和 node.js 对多租户应用程序进行原型设计。我能够连接到主数据库,但无法连接到辅助数据库。我将 orientdb 信息存储在 config.json 上(即 { "0": {"name": "orient0","host": "localhost","port": 2424,"username": "orient0","password": "orient0"},"1": {"name": "orient1","username": "orient1","password": "orient1"} } ) 下面是我的 nodejs 代码。这是如何处理与多个orientdb的连接?

const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const config = require("./config/config");
const OrientDBClient = require("orientjs").OrientDBClient;

const userRoutes = require("./routes/user");
const postsRoutes = require("./routes/posts");

const app = express();
let dbId = "0";

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/images",express.static(path.join("backend/images")));

app.use((req,res,next) => {
  
  console.log('url:'+ req.url);  
  let url = req.url.split("/");
  dbId = url[2];
  console.log('dbId:'+dbId);
  res.setHeader("Access-Control-Allow-Origin","*");
  res.setHeader(
    "Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept,Authorization"
  );
  res.setHeader(
    "Access-Control-Allow-Methods","GET,POST,PATCH,PUT,DELETE,OPTIONS"
  );
  next();
});

const client = new OrientDBClient({
  host: config.storageConfig[dbId].host,port: config.storageConfig[dbId].port,pool: {
    max: 10
  }
});

const boostrap = pool => {
  app.use((req,next) => {
    pool
      .acquire()
      .then(session => {
        console.log('boostrap/pool...');
        res.locals.db = session;
        res.on("finish",() => {
          session.close();
        });
        next();
      })
      .catch(err => {
        res.status(500).send(err);
      });
  });
  
  app.use("/api/*/user",userRoutes);
  app.use("/api/posts",postsRoutes);
};

client
  .connect()
  .then(() => {
    console.log('client...');
    return client.sessions({
    name: config.storageConfig[dbId].name,username: config.storageConfig[dbId].username,password: config.storageConfig[dbId].password,pool: {
        max: 25
      }
    });
  })
  .then(pool => {    
    boostrap(pool);
  })
  .catch(err => {
    console.log(err);
  });

module.exports = app

;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)