I have the following entities:
@Entity('user')
class User {
...
@ManyToMany(() => Poll, poll => poll.votedUsers)
votedPolls!: Poll[]
}
@Entity('poll')
class Poll {
...
@ManyToMany(() => User, user => user.votedPolls)
@JoinTable()
votedUsers!: User[]
}
Now i want to load a poll, add a new user that voted and store it. There are two problems however:
- Its not atomic. If two users vote at the same time, one of them would not be stored as voted (or at least as i understand the way TypeORM works)
- I need to load ALL the users with the poll, add one to them, and then store all of them, which is not efficient
I searched the documentation of TypeORM and found nothing. The only thing I found was loading not all users, but all user ids. But Its also not the best way as number of voted users grows