Saving a list of Entity using TypeORM

Kha Nguyễn picture Kha Nguyễn · Oct 30, 2018 · Viewed 10.8k times · Source

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.

Answer

zenbeni picture zenbeni · Oct 31, 2018

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.