Search code examples
node.jsexpressprisma

change database based on request origin using expressjs & prisma


I want to change database dynamically based on request origin.

I create a globalMiddleware which is called on every routes.

// middlwares/global.middleware.js

import DBController from "../controllers/db.controller.js";
import { db1, db2 } from "../prisma/prismaClient.js";
export default (req, res, next) => {
  const dbcontroller = DBController();
  const domain = req.get("origin");
  switch (domain) {
    case "http://localhost:3000":
      dbcontroller.setDB(db1);
      break;
    case "http://localhost:3001":
      dbcontroller.setDB(db2);
      break;
  }
  next();
};

but when i set the db inside DBController by calling dbcontroller.setDB() method and finally calling this.DB it is undefined.

// controller/db.controller.js

import autoBind from "auto-bind";
class DBController {
  constructor() {
    this.DB;
    autoBind(this);
  }
  setDB(prismaClient) {
    this.DB = prismaClient;
  }
}
export default DBController;

// conrtoller/controller.js

import { generateResponse } from "./../util/public.util.js";
import DBController from "./db.controller.js";
import autoBind from "auto-bind";
import createError from "http-errors";
class Controller extends DBController {
  constructor() {
    super();
    this.generateResponse = generateResponse;
    this.createError = createError;
    autoBind(this);
  }
}
export default Controller;

// controller/article.controller.js

import Controller from "./controller.js";
class ArticleController extends Controller {
  async get(req, res, next) {
    try {
      const articles = await this.DB.article.findMany(); //this.DB is undefined
      const response = this.generateResponse("success", articles);
      res.send(response);
    } catch (error) {
      next(error);
    }
  }
}
export default new ArticleController();

I don't know how should i set a global DB inside a top-level controller which can be used every where.

I also try js global.db vars and express app.set("db",db1) but i think these are not a good solution for this work.


Solution

  • finally I modify global.middleware.js file and modify request instead of setting database in a high-level controller :

    import {
      prisma_aramgostar,
      prisma_karen
    } from "../prisma/prismaClient.js";
    export default async(req, res, next) => {
      const domain = await req.get("x-forwarded-host");
      switch (domain) {
        case "localhost:3000":
          req.DB = prisma_aramgostar;
          console.log("db: aramgostar");
          break;
        case "127.0.0.1:3001":
          req.DB = prisma_karen;
          console.log("db: karen");
          break;
      }
      next();
    };