How to update a many-to-many relationship with TypeORM?

Viewed 31

I find myself unable to understand how I should do to update a many-to-many relation with TypeORM.

I have a user table, a tag table and a user_tags jointable defined as such:

//User entity

    @ManyToMany(
        type => Tag, 
        tag => tag.users, 
        { 
            cascade: true,
            orphanedRowAction: 'delete'
        }
    )
    @JoinTable(
        { name: "user_tags" }
    )
    tags!: Relation<Tag[]>

//Tag entity

    @ManyToMany(
        type => User, 
        user => user.tags
    )
    users!: Relation<User[]>

I can insert multiple tags in the jointable when doing:

const user = new User()
   user.tags = tags,
   //...

But now I want to, lets say, remove some tags, add some others, and keep some of them for a specific user. How can I achieve that?

Related but couldn't get it to work:

0 Answers
Related