英文:
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 "../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();
};
<!-- 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 "auto-bind";
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 "./../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;
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// 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();
<!-- 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 "../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();
};
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论