'How to add conditions in Typeorm for conflict when use onUpdate

In my project I use NestJS, TypeOrm and Postgresql.

I have entity:

@Entity('books', { schema: 'public' })
export class Book  {
  @PrimaryColumn('character varying', { name: 'id', length: 64 })
  public id: string;

  @Column('character varying', { name: 'name', length: 64 })
  public name: string;

  @Column('boolean', { name: 'is_locked', default: false })
  public isLocked: boolean;
}


I need to insert new rows and, in case of conflict, update only those rows where is_locked = false. Now I use this query:

await this.booksRepository
          .createQueryBuilder()
          .insert()
          .values(books)
          .onConflict(
            `("id") DO UPDATE SET "name" = CASE WHEN books.is_locked THEN books.name ELSE excluded.name END`,
          )
          .execute();

Use of onConflict syntax is deprecated. I want delete deprecated syntax. But I don't understand how I should implement orUpdate for my case. Maybe someone has ideas? Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source