Typeorm update entity base on other entiry with DataMapper

44 views Asked by At

I'm new in TypeORM and now I face with this issue.

I have 2 table user and login_method - have One-to-One relation. I need update the user info base on login_method info but it seems TypeORM doesn't support inner join with update. I dont know how to approach using Update QueryBuilder.

Here is my entity:

user.entity

@Entity('user')
export class User extends BaseEntity{
  @Field()
  @Column({ unique: true })
  name: string;

  @Column({ nullable: true, length: 32 })
  activeCode: string;

  @Column({ default: false })
  isActivated: boolean;

  @OneToMany(() => LoginMethod, (method) => method.user)
  loginMethod: LoginMethod[];
}

login_method.entity

export class LoginMethod extends BaseEntity {
  @Column({ nullable: true })
  email: string;

  @Column({ nullable: true })
  password: string;

  @Column()
  userId: number;

  @ManyToOne(() => User, (user) => user.loginMethod)
  user: User;

My raw SQL like this

update user
inner join login_method on login_method.userId = user.id
set user.isActivated = 0
where login_method.email = "some random string here"

Please let me know how to use Update QueryBuilder with DataMapper or we have the only way like get the user and then edit this user instance and call save() method?

0

There are 0 answers