typeorm: [bug] Respository:insert() does not respect SaveOptions

Issue type:

[ ] question [ x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [x ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x ] 0.2.11 (or put your version here)

I found two potential bug with insert: chunk options are skipped:

  • if I use connection.getRepository(MyEntity)insert(listOfEntities, {chunk:2}), all of the entities are saved at once, they are not chunked.

sorry for being brief, I can add an example if its not enough

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 21 (15 by maintainers)

Most upvoted comments

For anyone coming to this in 2020, I solved this using the lodash chunk method (obviously refactor for your needs, a loop with an await might not work for you but it does for me):

for (const chunkItem of chunk(bigArray, 1000)) {
  await this.repo.insert(chunkItem);
}

This is not a small issue. After almost 3 years this should be fixed. If nothing else you should remove the options argument from the BaseEntity.insert method since it is ignored by Repository.insert method. A better solution would be to implement at least some SaveOptions like reload.

For example insert 1M records to database in migration script. So I need it fast and I would probably use primitive insert method. I think it is only one reasonable case why I want to see chunk option in insert method 😄

Hello here,

@vlapo @pleerock @Kononnable

There is a huge problem for postgres if we are not able to use chunk on insert method : https://github.com/brianc/node-postgres/issues/1463#issuecomment-333313948

So yes, if we want to insert 1M rows (for pure example) using insert on a postgres database an exception will be thrown for not be able to have more than 65535 prepared parameters in the query.

This can have a huge impact i think for developers who dont want to use the save method and want to insert lot of datas really fast without some checking.

I thought about it a bit and I agree that we should remove them(repository, entity manager, base entity). It’s really not nice for the users to remove such methods if they use them, but most of the time they’re confusing for newcomers. In general they’ll have to pay the price on migration(we should write a really good guide on it), but their code will became more explicit, so no new user will be mistaken anymore. If they want they still can make such helper themselves.