node.js - 通过模块导出重用 pg-pool

标签 node.js postgresql node-postgres node-pg-pool

阅读文档后:https://github.com/brianc/node-pg-pool ,我有点担心重用 new Pool() 方法。

文档建议我需要在 exports 之前放置 new Pool() 并像这样return

// db.js
const pool = new Pool();
module.exports = () => { return pool; }

这样我可以重用 Pool 直到 idleTimeoutMillisclient.release(),通过使用 require() 来自其他文件 例如:

const connect = require('./db')
connect().query(' .... ');  

如果这是正确的,它是如何工作的? node.js 是否缓存 new Pool(),因为它不在 module.exports 中?

最佳答案

是的,它被有效地缓存了,因为你只创建了一次(并且 Node 缓存模块)并且你总是在你的导出方法中引用同一个实例。

关于node.js - 通过模块导出重用 pg-pool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059990/

相关文章:

node.js - sails lift 导致错误 ENOENT(没有此类文件或目录)

sql - 根据同一列中的数据,每两行计算 SQL 中的百分比?

python - Django raw sql/postgres 时区混淆

sql - 需要一个 SQL 查询来计算字符串的唯一组合

postgresql - 使用 node-postgres 获取函数的返回类型 OID

javascript - 从依赖项中的 node_modules 复制文件

javascript - Discord Bot 未接收交互

node.js - 对文档调用精益会抛出 TypeError : lean is not a function

postgresql - 在 pg 转储后插入到表中

javascript - 正确插入表名