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