Node.js 模块和数据库连接的正确架构? [英] Proper architecture for Node.js modules and database connectivity?
问题描述
我正在编写一个接受 TCP 连接的 Node 应用程序.我有几个单独的 Node 模块(利用导出功能).每个都需要一个数据库连接来处理它们的一些功能.
I'm writing a Node app that accepts TCP connections. I have a few separate Node modules (utilizing the exports functionality). Each needs a database connection to handle some of their functionality.
构建/处理数据库连接的最佳方法是什么?每个模块应该创建一个单独的连接还是我的主 JS 文件应该在我需要它们时将数据库连接处理程序传递给模块?
What's the best way to architect/handle database connections? Should each module create a separate connection or should my main JS file pass in a database connection handler to the modules when I require them?
推荐答案
imo 最好的方法是初始化连接池并在作为参数传递的模块中使用它.mysql-pool 非常通用,很容易适应任何数据库客户端
imo best approach is to initialize connections pool and use it in modules passing as parameter. mysql-pool is quite generic and easily adaptable to any db client
upd:node-pool 是一个通用的数据库不可知池
upd: node-pool is a generic db-agnostic pool
这篇关于Node.js 模块和数据库连接的正确架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!