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 5 years ago
  • Comments: 20 (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 😄

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.