如何在 Nodejs 和 Express 中重用数据库连接
How to reuse Db Connections in Nodejs and Express
我想知道在我的案例中重用 db 连接的最佳方法是什么,它在 NodeJs 和 express 中重用 couchebase 连接。
对于 Express 部分,我创建了一个这样的中间件
var couchbase = require('couchbase')
var config = require('../config/config')
module.exports = (req,res,next)=>{
var cluster = new couchbase.Cluster(config.cluster)
cluster.authenticate(config.userid, config.password)
let bucket = cluster.openBucket(config.bucket);
bucket.manager().createPrimaryIndex(function() {});
req.bucket = bucket;
req.N1qlQuery = couchbase.N1qlQuery;
next();
}
在 Express 应用程序中运行良好,因为我告诉它
const dbSessionMiddleware = require('../middleware/couch')
app.use(dbSessionMiddleware)
这允许我通过 req.bucket 访问它。我的问题是我的应用程序中有控制器,以防万一可能会调用辅助函数,它们可能会调用另一个函数来取回一些数据。我想避免必须继续将请求对象向下传递 5 个级别左右才能使用中间件。有没有更好的方法可以将连接/存储桶暴露给正常功能?
您可以创建一个专门的模块(例如 db.js
),您可以在其中为您的连接池实现单例。
// pseudo-code
export const getDb = () => {
let db
if (!db) {
const connection = createConnectionPool()
db = connection.db
}
return db
}
此函数可以导入到您的中间件和代码的其他部分。
您是否尝试过将初始化代码从中间件函数中取出? Couchbase Documentation 并没有显示它是以这种方式使用的。尽管这个例子是在 vanilla Node.js 中。通过将它放在中间件函数中,每次服务器收到请求时,您都将重新连接到数据库。
我在顶级 app.js 正文中连接到我的 Mongo 服务器,这允许连接持续存在。然后我可以在我的模型和控制器中导入我需要的猫鼬参考来概述如何获取某些数据,然后在相关路由端点内调用控制器的方法。
已编辑以显示将存储桶分配为控制器的示例 class 字段
在你的app.js
const couchbase = require("couchbase");
const config = require("../config/config");
// ...app.js
const CouchController = require("../controllers/CouchController")(couchbase, config);
// app.js...
在您的控制器中
class CouchController {
constructor(couchbase, config) {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
doSomeQuery(queryString, callback) {
// Use your query however its meant to be used. I'm not familiar with couchbase queries.
this.bucket.manager().createPrimaryIndex(function() {
this.bucket.query(
this.N1qlQuery.fromString("SELECT * FROM bucketname WHERE in interests LIMIT 1"),
[queryString],
callback(err, result)
)
});
}
}
然后从路由内部调用控制器方法
router.get("/", function(req, res, next) {
let searchParam = req.query.someParam;
CouchController.doSomeQuery(searchParam)
.then(result => {
res.json(result);
});
});
我想知道在我的案例中重用 db 连接的最佳方法是什么,它在 NodeJs 和 express 中重用 couchebase 连接。 对于 Express 部分,我创建了一个这样的中间件
var couchbase = require('couchbase')
var config = require('../config/config')
module.exports = (req,res,next)=>{
var cluster = new couchbase.Cluster(config.cluster)
cluster.authenticate(config.userid, config.password)
let bucket = cluster.openBucket(config.bucket);
bucket.manager().createPrimaryIndex(function() {});
req.bucket = bucket;
req.N1qlQuery = couchbase.N1qlQuery;
next();
}
在 Express 应用程序中运行良好,因为我告诉它
const dbSessionMiddleware = require('../middleware/couch')
app.use(dbSessionMiddleware)
这允许我通过 req.bucket 访问它。我的问题是我的应用程序中有控制器,以防万一可能会调用辅助函数,它们可能会调用另一个函数来取回一些数据。我想避免必须继续将请求对象向下传递 5 个级别左右才能使用中间件。有没有更好的方法可以将连接/存储桶暴露给正常功能?
您可以创建一个专门的模块(例如 db.js
),您可以在其中为您的连接池实现单例。
// pseudo-code
export const getDb = () => {
let db
if (!db) {
const connection = createConnectionPool()
db = connection.db
}
return db
}
此函数可以导入到您的中间件和代码的其他部分。
您是否尝试过将初始化代码从中间件函数中取出? Couchbase Documentation 并没有显示它是以这种方式使用的。尽管这个例子是在 vanilla Node.js 中。通过将它放在中间件函数中,每次服务器收到请求时,您都将重新连接到数据库。
我在顶级 app.js 正文中连接到我的 Mongo 服务器,这允许连接持续存在。然后我可以在我的模型和控制器中导入我需要的猫鼬参考来概述如何获取某些数据,然后在相关路由端点内调用控制器的方法。
已编辑以显示将存储桶分配为控制器的示例 class 字段
在你的app.js
const couchbase = require("couchbase");
const config = require("../config/config");
// ...app.js
const CouchController = require("../controllers/CouchController")(couchbase, config);
// app.js...
在您的控制器中
class CouchController {
constructor(couchbase, config) {
// You may either pass couchbase and config as params, or import directly into the controller
this.cluster = new couchbase.Cluster(config.cluster);
this.cluster.authenticate(config.userid, config.password);
this.bucket = cluster.openBucket(config.bucket);
this.N1qlQuery = couchbase.N1qlQuery;
}
doSomeQuery(queryString, callback) {
// Use your query however its meant to be used. I'm not familiar with couchbase queries.
this.bucket.manager().createPrimaryIndex(function() {
this.bucket.query(
this.N1qlQuery.fromString("SELECT * FROM bucketname WHERE in interests LIMIT 1"),
[queryString],
callback(err, result)
)
});
}
}
然后从路由内部调用控制器方法
router.get("/", function(req, res, next) {
let searchParam = req.query.someParam;
CouchController.doSomeQuery(searchParam)
.then(result => {
res.json(result);
});
});