我应该在 Knex.js 中释放客户端吗?
Should I release client in Knex.js?
我在 pg
中使用 PostgreSQL。配置如下:
const { Pool } = require("pg");
const { dbUser, dbHost, database, dbHmac, dbPort } = require("../config");
const dbConfig = {
user: dbUser,
host: dbHost,
database,
password: dbHmac,
port: dbPort,
max: 10,
idleTimeoutMillis: 30000
};
var pool = new Pool(dbConfig);
module.exports = pool;
目前池子使用情况如下:
app.post("/deleteCognitoUserBasedOnEmail", async function (req, res) {
let isUserAdminRes, email = req.body.email;
try {
isUserAdminRes = await client.query(
`select is_admin from end_user where email = `,[email]
);
}
catch (e) {
client.release();
console.error("Error occurred : " + e);
return res.status(500).send({ errorMessage: "Internal Server Error. " + e.message });
}
client.release();
return res
.status(status)
.send({ responseMessage });
}
正如大家所见,我必须在所有地方发布客户端。我的项目中有一些复杂的函数将客户端作为参数传递。这种方式有时很难跟踪客户。我决定切换到 knex.js。
Knex.js有没有这样的限制?我在 knex.js 中阅读了有关 .destroy() 方法的信息,但它是在您不需要连接时使用的。但是在我的项目中,API 会在一天中每天被多次访问。
在knex.js的情况下,众多的例子都省略了连接管理;它在用户代码中不存在。它只是没有在文档中提到,因为你不这样做。因此,它 记录在案 。
如果您更喜欢确凿的证据,这里是 knex.js 中实际执行查询的代码 - 请注意 ensureConnection
如何在查询之前建立连接并在查询之后释放它:https://github.com/knex/knex/blob/8cfad286f246aff7455784b51751fdd17ba24c4c/lib/execution/runner.js#L27
我在 pg
中使用 PostgreSQL。配置如下:
const { Pool } = require("pg");
const { dbUser, dbHost, database, dbHmac, dbPort } = require("../config");
const dbConfig = {
user: dbUser,
host: dbHost,
database,
password: dbHmac,
port: dbPort,
max: 10,
idleTimeoutMillis: 30000
};
var pool = new Pool(dbConfig);
module.exports = pool;
目前池子使用情况如下:
app.post("/deleteCognitoUserBasedOnEmail", async function (req, res) {
let isUserAdminRes, email = req.body.email;
try {
isUserAdminRes = await client.query(
`select is_admin from end_user where email = `,[email]
);
}
catch (e) {
client.release();
console.error("Error occurred : " + e);
return res.status(500).send({ errorMessage: "Internal Server Error. " + e.message });
}
client.release();
return res
.status(status)
.send({ responseMessage });
}
正如大家所见,我必须在所有地方发布客户端。我的项目中有一些复杂的函数将客户端作为参数传递。这种方式有时很难跟踪客户。我决定切换到 knex.js。 Knex.js有没有这样的限制?我在 knex.js 中阅读了有关 .destroy() 方法的信息,但它是在您不需要连接时使用的。但是在我的项目中,API 会在一天中每天被多次访问。
在knex.js的情况下,众多的例子都省略了连接管理;它在用户代码中不存在。它只是没有在文档中提到,因为你不这样做。因此,它 记录在案 。
如果您更喜欢确凿的证据,这里是 knex.js 中实际执行查询的代码 - 请注意 ensureConnection
如何在查询之前建立连接并在查询之后释放它:https://github.com/knex/knex/blob/8cfad286f246aff7455784b51751fdd17ba24c4c/lib/execution/runner.js#L27