Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
NodeJS - TypeORM cascade insert using QueryBuilder

I'm trying to upsert an object array with a many-to-many relation to a MySQL table(s) using TypeORM query builder. Had to use the builder because repository save doesn't support upserts. Problem is, while save cascades nested objects, QueryBuilder doesn't seem to (and RelationalQueryBuilder doesn't support upserts on MySQL).

Anyone know how I can do both? I want to both update and cascade, on an array.

First object:

@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: 'bigint', unique: true })
shopifyId: number;

@Column('int')
shopId: number | null;

@Column('varchar')
name: string | null;

@Column('varchar')
productType: string | null;

@Column('timestamp', { default: () => 'CURRENT_TIMESTAMP' })
updated: Date | null;

@ManyToMany(type => ProductTag, productTag => productTag.products, {
    cascade: ['insert', 'update']
})
@JoinTable()
productTags: ProductTag[] | null;

}

Second object:

@Entity()
export class ProductTag {
@PrimaryGeneratedColumn()
id: number;

@Column('varchar')
name: string | null;

@ManyToMany(type => Product, product => product.productTags)
products: Product[] | null;

}

Current upsert:

    getConnection()
    .createQueryBuilder()
    .relation(Product, 'productTags')
    .insert()
    .into(Product)
    .values(products)
    .orUpdate({
        conflict_target: ['shopifyId'],
        overwrite: ['name', 'shopId', 'productType', 'updated']
    })
    .execute();

Thank you

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!