Is there a way to access `last_inserted_id` in TypeORM? Is there a way to access `last_inserted_id` in TypeORM? express express

Is there a way to access `last_inserted_id` in TypeORM?


NOTE: Please note that I have not used TypeORM since roughly the time of original answer, so there may be better ways to do this now.

Figured it out. Use returning method...

const inserts = await getConnection()            .createQueryBuilder()            .insert()            .into(Company)            .values([                { Name: "abcdef", Address: "456 denny lane" },                 { Name: "ghijkil", Address: "899 ihop lane" }            ])            .returning("INSERTED.*")            .printSql()            .execute();// `inserts` holds array of inserted objects


OUTPUT or RETURNING clause only supported by Microsoft SQL Server or PostgreSQL databases.

For MySql, you can get the last_insert_id from the result. it looks like the following.

InsertResult {  identifiers: [ { id: 1 } ],  generatedMaps:   [ { id: 1,       creationTime: 2019-09-03T10:09:03.000Z,       lastUpdate: 2019-09-03T10:09:03.000Z } ],  raw:   OkPacket {     fieldCount: 0,     affectedRows: 1,     insertId: 1,     serverStatus: 2,     warningCount: 0,     message: '',     protocol41: true,     changedRows: 0 } }