Sto costruendo una nestjs applicazione che utilizza typeorm per comunicare con postgres.
Le tabelle vengono create dinamicamente, e i dati vengono inseriti anche in modo dinamico. Ecco perché io uso il raw query invece di entità.
Il problema è che alcuni dei dati nelle tabelle sono relative e non riesco ad inserire nuovi dati, a meno che la precedente query di inserimento è finito.
Come faccio a controllare se l'esecuzione di query finito? Qui è un esempio di flusso di lavoro che uso io. Funziona con dati di piccole dimensioni, ma non riesce con i big data (10 000 000 voci e di più)
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();
}
}
}
Il risultato desiderato è quello di avere alcuni di callback per queryRunner.query
come questo queryRunner.query('raw_sql', (err, res) => {})
E ' possibile con typeorm?
Grazie