我要建一个nestjs应用程序,使用typeorm与postgres。
我的表是创造的动态,并插入数据也动态。 这就是为什么我使用原始的查询,而不是实体。
问题是,有些表中的数据是相关的,我不能插入新数据,除非之前插入的查询已经完成。
我如何检查,如果查询执行完成了吗? 这里是一个例子工作流程,我使用。 它适用的小数据,但没有与大数据(10 000 000个条目,并更多)
export class Test {
constructor(
private readonly connection: Connection;
) {}
public async insertData(table1, table2, arr1, arr2) {
await insertInto(table1, arr1);
//I want second insertInto() to be executed after I get confirmation from database that insertInto() from above is finished
await insertInto(table2, arr2);
}
private async insertInto(table, data) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
const preparedData = [];
//prepare data to be inserted as raw query
//...
try {
await queryRunner.query(`INSERT IGNORE INTO "${table}" VALUES ${preparedData}`);
await queryRunner.commitTransaction();
} catch (e) {
await queryRunner.rollbackTransaction();
throw new InternalServerErrorException(e, Error while executing custom query. Rollback transaction.)
} finally {
await queryRunner.release();
}
}
}
所希望的结果是有一些回调 queryRunner.query
像这样的 queryRunner.query('raw_sql', (err, res) => {})
是它可能与typeorm?
感谢