Query Runner
什么是 QueryRunner?
如果 RDBMS 支持连接池,每个新的 QueryRunner 实例都会从连接池中取走一个连接。对不支持连接池的数据库,它在整个数据源范围内使用同一个连接。
完整的 QueryRunner API 记录在迁移章节中。
创建新的 QueryRunner 实例
使用 createQueryRunner 方法创建一个新的 QueryRunner:
const queryRunner = dataSource.createQueryRunner()使用 QueryRunner
创建 QueryRunner 的新实例后,当你发出第一条查询时就会从池中获取一个连接:
const queryRunner = dataSource.createQueryRunner()
await queryRunner.query("SELECT 1")
await queryRunner.release()你也可以用 connect 方法直接从连接池中获取一个连接:
const queryRunner = dataSource.createQueryRunner()
const clientConnection = await queryRunner.connect()
await queryRunner.release()重要:当 QueryRunner 不再需要时,请务必释放它,把连接归还给连接池:
await queryRunner.release()QueryRunner 被释放之后,就不能再使用它的方法了。
QueryRunner 也有自己的 EntityManager 实例,你可以通过 manager 属性使用它,在该 QueryRunner 实例所使用的特定数据库连接上执行 EntityManager 查询:
let queryRunner: QueryRunner
try {
queryRunner = dataSource.createQueryRunner()
// 使用单个数据库连接执行多条查询
await queryRunner.manager.update(
Employee,
{ level: "junior" },
{ bonus: 0.2 },
)
await queryRunner.manager.update(
Employee,
{ level: "senior" },
{ bonus: 0.1 },
)
} catch (error) {
console.error(error)
} finally {
// 用完之后记得释放连接
await queryRunner.release()
}显式资源管理
QueryRunner 也支持显式资源管理:
async function updateSalaries() {
await using queryRunner = dataSource.createQueryRunner()
await queryRunner.manager.update(
Employee,
{ level: "junior" },
{ bonus: 0.2 },
)
await queryRunner.manager.update(
Employee,
{ level: "senior" },
{ bonus: 0.1 },
)
// 不再需要手动释放 QueryRunner
}
try {
await updateSalaries()
} catch (error) {
console.error(error)
}以这种方式声明 query runner 时,它会在所在作用域的最后一条语句执行完毕后被自动释放。
What is a QueryRunner?
Each new QueryRunner instance takes a single connection from the connection pool, if the RDBMS supports connection pooling. For databases that do not support connection pools, it uses the same connection across the entire data source.
The full QueryRunner API is documented in the migrations section.
Creating a new QueryRunner instance
Use the createQueryRunner method to create a new QueryRunner:
const queryRunner = dataSource.createQueryRunner()Using QueryRunner
After creating a new instance of QueryRunner, a connection will be acquired from the pool when you issue the first query:
const queryRunner = dataSource.createQueryRunner()
await queryRunner.query("SELECT 1")
await queryRunner.release()You can also use connect method to directly get a connection from the connection pool:
const queryRunner = dataSource.createQueryRunner()
const clientConnection = await queryRunner.connect()
await queryRunner.release()Important: make sure to release the QueryRunner when it is no longer necessary to return the connection back to the connection pool:
await queryRunner.release()After the QueryRunner is released, it is no longer possible to use the query runner methods.
QueryRunner also has its own EntityManager instance, which you can use through the manager property to run EntityManager queries on a particular database connection used by the QueryRunner instance:
let queryRunner: QueryRunner
try {
queryRunner = dataSource.createQueryRunner()
// use a single database connection to execute multiple queries
await queryRunner.manager.update(
Employee,
{ level: "junior" },
{ bonus: 0.2 },
)
await queryRunner.manager.update(
Employee,
{ level: "senior" },
{ bonus: 0.1 },
)
} catch (error) {
console.error(error)
} finally {
// remember to release connection after you are done using it
await queryRunner.release()
}Explicit Resource Management
QueryRunner also supports explicit resource management:
async function updateSalaries() {
await using queryRunner = dataSource.createQueryRunner()
await queryRunner.manager.update(
Employee,
{ level: "junior" },
{ bonus: 0.2 },
)
await queryRunner.manager.update(
Employee,
{ level: "senior" },
{ bonus: 0.1 },
)
// no need anymore to manually release the QueryRunner
}
try {
await updateSalaries()
} catch (error) {
console.error(error)
}When declaring a query runner like this, it will be automatically released after the last statement in the containing scope was executed.