基于请求来源使用 Express.js 和 Prisma 更改数据库

huangapple go评论74阅读模式
英文:

change database based on request origin using expressjs & prisma

问题

我想根据请求来源动态更改数据库。

我创建了一个全局中间件,它在每个路由上都被调用。

// 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();
};

但是,当我通过调用 dbcontroller.setDB() 方法在 DBController 中设置 db,最后调用 this.DB 时它是未定义的。

// controller/db.controller.js

import autoBind from "auto-bind";
class DBController {
  constructor() {
    this.DB;
    autoBind(this);
  }
  setDB(prismaClient) {
    this.DB = prismaClient;
  }
}
export default DBController;
// controller/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();

我不知道应该如何在顶层控制器中设置一个全局的 DB,可以在任何地方使用。

我也尝试过使用全局的 JavaScript 变量和 Express 的 app.set("db", db1),但我认为这些不是解决这个问题的好方法。

英文:

I want to change database dynamically based on request origin.

I create a globalMiddleware which is called on every routes.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// middlwares/global.middleware.js

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

<!-- end snippet -->

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// controller/db.controller.js

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

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// conrtoller/controller.js

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

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// controller/article.controller.js

import Controller from &quot;./controller.js&quot;;
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(&quot;success&quot;, articles);
      res.send(response);
    } catch (error) {
      next(error);
    }
  }
}
export default new ArticleController();

<!-- end snippet -->

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.

答案1

得分: 0

最后,我修改了global.middleware.js文件,并修改了request,而不是在高级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();
};
英文:

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 23:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定