Reputation: 463
I'm using Repository
class of typeorm to work with my Postgres database.
My problem is when calling feedRepository.save(feeds)
, with feedRepository
declared as feedRepository: Repository<Feed>
, some elements of feeds
may be invalid for insertion. How can I force typeorm to skip invalid entities and continue to insert valid ones?
P/S: I have particular reasons for not using for loop
to insert data one by one.
Upvotes: 12
Views: 40478
Reputation: 1076
You are having issues saving a large number of new rows. You have to break them into chunks.
Typeorm has a save options chunk
/**
* Breaks save execution into chunks of a given size.
* For example, if you want to save 100,000 objects but you have issues with saving them,
* you can break them into 10 groups of 10,000 objects (by setting { chunk: 10000 }) and save each group separately.
* This option is needed to perform very big insertions when you have issues with underlying driver parameter number limitation.
*/
chunk?: number;
Upvotes: 6
Reputation: 7213
You can save multiple entities at once by using a list of entities to save as parameter in the repository as described in the doc :
/**
* Saves all given entities in the database.
* If entities do not exist in the database then inserts, otherwise updates.
*/
save<T extends DeepPartial<Entity>>(entities: T[], options?: SaveOptions): Promise<T[]>;
/**
* Saves a given entity in the database.
* If entity does not exist in the database then inserts, otherwise updates.
*/
save<T extends DeepPartial<Entity>>(entity: T, options?: SaveOptions): Promise<T>;
You should validate your entities first by using validation tools such as class-validator
https://github.com/typestack/class-validator
You can also implement a method isValid(): boolean
in your entity class that you can use to determine if your data is correct and then filtering the list according to this function.
You will only spend compute time to check your entities with validators, so it should be quite quick, a lot better than to deal with exceptions coming from the database or with overtuning typeorm.
Upvotes: 12