当前位置:   article > 正文

typeorm 更新,使用typeORM进行批量插入/更新的有效方法

typeorm 批量更新

How it's possible to insert pre-created objects into relation with an object using TypeORM?

As I know the non-efficient is this(many to many relations):

const tags = await Tag.findByIds(tagIds);

const todo = await Todo.findOne(todoId);

todo.tags = tags;

return await Todo.save(todo)

Also .add() property of RelationalQueryBuilder takes single object or Id as is mentioned in docs

await getConnection()

.createQueryBuilder()

.relation(Post, "categories")

.of(1)

.add(3);

So what is the efficient way to bulk insert?

解决方案

I had error while using @Arg imported from typeGraphql for passing an array of string so I resolved it so add() from typeOrm takes the Id array

so the fast and performant example of relational query is:

import { Resolver, Arg, Mutation } from "type-graphql";

import { Todo } from "../../entity/Todo";

import { createQueryBuilder } from "typeorm";

@Resolver()

export class BulkAssignTagsResolver {

@Mutation(() => Todo)

async bulkAssignTodoTag(

@Arg("tagIds", () => [String]) tagIds: string[],

@Arg("todoId") todoId: string

): Promise {

await createQueryBuilder("todo")

.relation(Todo, "tags")

.of(todoId)

.add(tagIds);

return Todo.findOne(todoId, { relations: ["tags"] });

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/661359
推荐阅读
相关标签
  

闽ICP备14008679号