参考 TypeORM 官网 RelationBuilder
RelationQueryBudiler
顾名思义, RelationQuberyBuilder 是特殊的 QueryBuilder,typeorm 提供来简化 Entity Relations 的操作, 今天以更新为例
one-to-many/many-to-one
先从最简单的 one-to-many/many-to-one, 更新使用者所属平台为例,
在 user.service 新增
- async updateUserPlatById(userId, platId){ // 传入 userId 及 platId
- await this.userRepo.createQueryBuilder()
- .relation(User, 'plat') // 指定载入 relation
- .of(userId) // 找对应的 entity, 可以是 id 或是 queryed entity
- .set(platId); // 更新 (或是新增)platId
- // .then(result => result); 回传 void
- return await this.userRepo.findOne(userId, {relations: ['plat', 'roles']}); // 回传结果
- }
在 user.controller 新增
- @Put(':userId/:platId')
- updateUserPlatById(@Param('userId') userId, @Param('platId') platId) {
- return this.userService.updateUserPlatById(userId, platId);
- }
使用 postman 测试
2018111501.PNG
many-to-many
与 one-to-many 差异不大, 差别在于 many-to-many 用的是 add 或是 addAndRemove
在 user.service 下新增如下代码,
- async updateUserRolesByIds(userId, data: UserDTO){
- // 更新 roles, 必需先取的数据库现有的资料
- const user = await this.userRepo.findOne(userId, {relations: ['roles']});
- Logger.log(user);
- await this.userRepo.createQueryBuilder()
- .relation(User, 'roles') // 指定载入 relation
- .of(userId) // 找对应的 entity, 可以是 id 或是 queryed entity
- // 第一个参数是要新增的 roles, 第二個参数是要移除的 roles
- // 当然可以用 lodash 对阵列做操作, 得到新增的阵列跟移除的阵列
- // 但 builder API 一次只能有 add 或是 remove, 所以用 addAndRemove 是最快的
- .addAndRemove(data.roleIds, user.roles.map(role => role.id));
- // .then(result => return result); 回传 void
- return await this.userRepo.findOne(userId, {relations: ['dep', 'roles']});
- }
为了测试方便, 先修改原本 user.controller 下的 @Put(':userId')
- @Put(':userId')
- updateUserRolesByIds(@Param('userId') userId, @Body() userDTO: UserDTO) {
- return this.userService.updateUserRolesByIds(userId, userDTO);
- }
使用 postman 测试
2018111502.PNG
Refactor user.service 里面的更新使用者方法
把前面两个合起来, 就可以完整更新 User 资料
修改 user.service 下 updateUserById 方法
- async updateUserById(userId, data: UserDTO){
- const user = await this.userRepo.findOne(userId, {relations: ['roles']});
- await this.userRepo.createQueryBuilder()
- .relation(User, 'roles')
- .of(userId)
- .addAndRemove(data.roleIds, user.roles.map(role => role.id))
- .then(async () => { // 要串
- await this.userRepo.createQueryBuilder()
- .relation(User, 'plat')
- .of(userId)
- .set(data.platId);
- });
- await this.userRepo.createQueryBuilder() // 更新非 relation 相关资料
- .update(User) // 指定 update 哪一个 entity
- .set({ // 更新资料
- name: data.name,
- age: data.age,
- })
- // whereInIds 是 helper method
- // 原本应为
- // .where('id = :id', {id: userId})
- .whereInIds(userId)
- // .printSql() 可以用來除錯
- .execute(); // 執行
- return await this.userRepo.findOne(userId, {relations: ['plat', 'roles']});
- }
修改 user.controller
- @Put(':userId')
- updateUserRolesByIds(@Param('userId') userId, @Body() userDTO: UserDTO) {
- return this.userService.updateUserById(userId, userDTO);
- // return this.userService.updateUserRolesByIds(userId, userDTO);
- }
使用 postman 测试
2018111503.PNG
其余 user service 下的新增, 删除, 查询用 querybuilder refactor, 下一章继续
推荐一下我的公众号: [ geekjc ] , 微信号: [ c8706288 ] 一起学习交流编程知识, 分享经验, 各种有趣的事.
tuiguang.PNG
来源: http://www.jianshu.com/p/c28dc38b4132