阅读文档后:https://github.com/brianc/node-pg-pool ,我有点担心重用 new Pool()
方法。
文档建议我需要在 exports
之前放置 new Pool()
并像这样return
// db.js
const pool = new Pool();
module.exports = () => { return pool; }
这样我可以重用 Pool
直到 idleTimeoutMillis
或 client.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/