自定义仓储
你可以创建一个自定义仓储,其中包含操作数据库的方法。例如,假设我们想要一个名为 findByName(firstName: string, lastName: string) 的方法,它按给定的名和姓搜索用户。这个方法最适合放在 Repository 中,这样我们就可以像 userRepository.findByName(...) 这样调用它。你可以用自定义仓储来实现这一点。
创建自定义仓储有几种方式。
如何创建自定义仓储?
常见的做法是把 repository 实例赋值给一个全局导出的变量,并在整个应用中使用这个变量,例如:
// user.repository.ts
export const UserRepository = dataSource.getRepository(User)
// user.controller.ts
export class UserController {
users() {
return UserRepository.find()
}
}要扩展 UserRepository 的功能,你可以使用 Repository 类的 .extend 方法:
// user.repository.ts
export const UserRepository = dataSource.getRepository(User).extend({
findByName(firstName: string, lastName: string) {
return this.createQueryBuilder("user")
.where("user.firstName = :firstName", { firstName })
.andWhere("user.lastName = :lastName", { lastName })
.getMany()
},
})
// user.controller.ts
export class UserController {
users() {
return UserRepository.findByName("Timber", "Saw")
}
}在事务中使用自定义仓储
事务有自己独立的执行作用域:它们有自己的 query runner、entity manager 和 repository 实例。这就是在事务中不能使用全局的(data source 的)entity manager 和 repository 的原因。要在事务作用域内正确执行查询,你 必须 使用事务提供的 entity manager 及其 getRepository 方法。要在事务中使用自定义仓储,你必须使用所提供 entity manager 实例的 withRepository 方法:
await dataSource.transaction(async (manager) => {
// 在事务中你必须使用事务提供的 manager 实例,
// 不能使用全局的 entity manager 或 repository,
// 因为这个 manager 是专属于该事务的
const userRepository = manager.withRepository(UserRepository)
await userRepository.createAndSave("Timber", "Saw")
const timber = await userRepository.findByName("Timber", "Saw")
})You can create a custom repository which should contain methods to work with your database. For example, let's say we want to have a method called findByName(firstName: string, lastName: string) which will search for users by a given first and last names. The best place for this method is a Repository, so we could call it like userRepository.findByName(...). You can achieve this using custom repositories.
There are several ways how custom repositories can be created.
How to create custom repository?
It's common practice assigning a repository instance to a globally exported variable, and use this variable across your app, for example:
// user.repository.ts
export const UserRepository = dataSource.getRepository(User)
// user.controller.ts
export class UserController {
users() {
return UserRepository.find()
}
}In order to extend UserRepository functionality you can use .extend method of Repository class:
// user.repository.ts
export const UserRepository = dataSource.getRepository(User).extend({
findByName(firstName: string, lastName: string) {
return this.createQueryBuilder("user")
.where("user.firstName = :firstName", { firstName })
.andWhere("user.lastName = :lastName", { lastName })
.getMany()
},
})
// user.controller.ts
export class UserController {
users() {
return UserRepository.findByName("Timber", "Saw")
}
}Using custom repositories in transactions
Transactions have their own scope of execution: they have their own query runner, entity manager and repository instances. That's why using global (data source's) entity manager and repositories won't work in transactions. In order to execute queries properly in scope of transaction you must use provided entity manager and its getRepository method. In order to use custom repositories within transaction, you must use withRepository method of the provided entity manager instance:
await dataSource.transaction(async (manager) => {
// in transactions you MUST use manager instance provided by a transaction,
// you cannot use global entity managers or repositories,
// because this manager is exclusive and transactional
const userRepository = manager.withRepository(UserRepository)
await userRepository.createAndSave("Timber", "Saw")
const timber = await userRepository.findByName("Timber", "Saw")
})