如何使用 Transaction 插入 2 个或更多表
How to insert in 2 or more tables using Transaction
我需要做一些操作,如果全部通过则提交或全部失败。
我知道事务可以做到这一点,但我在处理多个 table 插入时遇到了问题。
nestJs的documentation说我只需要导入Connect方法,创建一个特定的结构,但是,超过1个就没有覆盖了table。
NestJs 文档代码:
async createMany(users: User[]) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await queryRunner.manager.save(users[0]);
await queryRunner.manager.save(users[1]);
await queryRunner.commitTransaction();
} catch (err) {
// since we have errors lets rollback the changes we made
await queryRunner.rollbackTransaction();
} finally {
// you need to release a queryRunner which was manually instantiated
await queryRunner.release();
}
}
如您所见,所有引用用户实体的内容,他在哪里指定?
我的代码:
const queryR = this.connection.createQueryRunner();
await queryR.connect();
await queryR.startTransaction();
try {
//## SHIPMENT ENTITY ##
const shipmentToInsert = Transformador.prepareShipment(ShipConfirmDetails, ShipConfirmSummary)
//const shipmentInserted = await this.shipmentRepositoryService.insert(shipmentToInsert)//normal insert of entity
const shipmentInserted = await queryR.manager.save(shipmentToInsert)
//## ORDER ENTITY ##
const ordersToInsert = Transformador.prepareOrder(ShipConfirmDetails, shipmentInserted.shipment_id)
const orderedInserted = await queryR.manager.save(ordersToInsert)
//## LINE ENTITY ##
const linesToInsert = Transformador.prepareLines(ShipConfirmDetails, orderedInserted)
const linesInserted = await queryR.manager.save(linesToInsert)
} catch (e) {
console.log(">>Error, se procede a hacer Rollback>>");
await queryR.rollbackTransaction();
} finally {
console.log(">>Se libera Transaction>>");
await queryR.release();
}
您可以使用 entityManager
创建交易,如下所示:
export class ExampleTransaction {
constructor(
@InjectEntityManager()
private readonly entityManager: EntityManager,
) { }
public async transactionExampleMethod(): Promise<void> {
await this.entityManager.transaction(async (transactionalManager: EntityManager) => {
//## SHIPMENT ENTITY ##
const shipmentToInsert = Transformador.prepareShipment(ShipConfirmDetails, ShipConfirmSummary);
const shipmentInserted = await transactionalManager.save(shipmentToInsert);
//## ORDER ENTITY ##
const ordersToInsert = Transformador.prepareOrder(ShipConfirmDetails, shipmentInserted.shipment_id)
const orderedInserted = await transactionalManager.save(ordersToInsert)
//## LINE ENTITY ##
const linesToInsert = Transformador.prepareLines(ShipConfirmDetails, orderedInserted)
const linesInserted = await transactionalManager.save(linesToInsert)
}
}
}
我需要做一些操作,如果全部通过则提交或全部失败。
我知道事务可以做到这一点,但我在处理多个 table 插入时遇到了问题。
nestJs的documentation说我只需要导入Connect方法,创建一个特定的结构,但是,超过1个就没有覆盖了table。
NestJs 文档代码:
async createMany(users: User[]) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
await queryRunner.manager.save(users[0]);
await queryRunner.manager.save(users[1]);
await queryRunner.commitTransaction();
} catch (err) {
// since we have errors lets rollback the changes we made
await queryRunner.rollbackTransaction();
} finally {
// you need to release a queryRunner which was manually instantiated
await queryRunner.release();
}
}
如您所见,所有引用用户实体的内容,他在哪里指定?
我的代码:
const queryR = this.connection.createQueryRunner();
await queryR.connect();
await queryR.startTransaction();
try {
//## SHIPMENT ENTITY ##
const shipmentToInsert = Transformador.prepareShipment(ShipConfirmDetails, ShipConfirmSummary)
//const shipmentInserted = await this.shipmentRepositoryService.insert(shipmentToInsert)//normal insert of entity
const shipmentInserted = await queryR.manager.save(shipmentToInsert)
//## ORDER ENTITY ##
const ordersToInsert = Transformador.prepareOrder(ShipConfirmDetails, shipmentInserted.shipment_id)
const orderedInserted = await queryR.manager.save(ordersToInsert)
//## LINE ENTITY ##
const linesToInsert = Transformador.prepareLines(ShipConfirmDetails, orderedInserted)
const linesInserted = await queryR.manager.save(linesToInsert)
} catch (e) {
console.log(">>Error, se procede a hacer Rollback>>");
await queryR.rollbackTransaction();
} finally {
console.log(">>Se libera Transaction>>");
await queryR.release();
}
您可以使用 entityManager
创建交易,如下所示:
export class ExampleTransaction {
constructor(
@InjectEntityManager()
private readonly entityManager: EntityManager,
) { }
public async transactionExampleMethod(): Promise<void> {
await this.entityManager.transaction(async (transactionalManager: EntityManager) => {
//## SHIPMENT ENTITY ##
const shipmentToInsert = Transformador.prepareShipment(ShipConfirmDetails, ShipConfirmSummary);
const shipmentInserted = await transactionalManager.save(shipmentToInsert);
//## ORDER ENTITY ##
const ordersToInsert = Transformador.prepareOrder(ShipConfirmDetails, shipmentInserted.shipment_id)
const orderedInserted = await transactionalManager.save(ordersToInsert)
//## LINE ENTITY ##
const linesToInsert = Transformador.prepareLines(ShipConfirmDetails, orderedInserted)
const linesInserted = await transactionalManager.save(linesToInsert)
}
}
}