You can use fully typeorm to save all required data, but make sure you have created entities with proper connection between them :
// Promo.ts
@Index()
@OneToOne(() => User, user => user.promo)
@JoinColumn()
user: User
I have added @JoinColumn to Promo expecting, that Promo Entity will have userId in table.
// User.ts
@OneToOne(() => Promo, promo => promo.user)
promo: Promo
In Repository, for example, you can use Transaction operation and save User and right after save this user with other required Promo details:
saveUser(user: User) {
return getManager().transaction(async (manager) => {
const result = await manager
.getRepository(User)
.createQueryBuilder()
.insert()
.values(user)
.execute()
// check on result emptiness
const createdUser = await manager
.getRepository(User)
.createQueryBuilder()
.where('id=:id',{id: result.identifiers[0].id})
.getOne()
await manager
.getRepository(Promo)
.createQueryBuilder()
.insert()
.values({user: createdUser})
.execute()
})
}
It will save in one transaction first User and after Promo with this user.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…