正确的架构的Node.js模块与数据库的连接? [英] Proper architecture for Node.js modules and database connectivity?

查看:151
本文介绍了正确的架构的Node.js模块与数据库的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个接受的TCP连接节点的应用程序。我有几个单独的节点模块(利用出口功能)。每个需要一个数据库连接来处理他们的一些功能。

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-池是很普通的,轻松适应任何DB客户端

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:节点池是一个通用的DB-无关池

upd: node-pool is a generic db-agnostic pool

这篇关于正确的架构的Node.js模块与数据库的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆