Closed RiyaAggarwal123 closed 2 years ago
@stevehanson This is an excellent feature addition and looks really well developed. What are the chances we can see this merged?
Thank you for this contribution. I am still considering if this is something we would include in Fishery.
In the meantime if anyone else is needing this functionality, you could always define your own factory that supports this and use it in your project, something like:
abstract class BulkFactory<T, I = any> extends Factory<T, I, T> {
abstract onBulkCreate(_list: T[]): Promise<T[]>;
bulkCreate(
number: number,
params: DeepPartial<T> = {},
options: BuildOptions<T, I> = {},
): Promise<T[]> {
const list = this.buildList(number, params, options);
return this.onBulkCreate(list);
}
}
class UserFactory extends BulkFactory<User> {
onBulkCreate(list: User[]) {
return Promise.resolve(list);
}
}
const users = await userFactory.bulkCreate(2, { name: 'susan' });
expect(users.length).toEqual(2);
expect(users[0].name).toEqual('susan');
I'm going to close this as it is not something we are interested in adding to the library at this time. I recommend using the supplied code from my comment above to create your own implementation if needed.
We are using fishery to create test data on remote servers via APIs, and we are using
createList()
, which iterates on each object and callsonCreate()
method (which hits API) for each object. Making an API call for each object is inefficient, so we added a bulk-create hook to perform bulk-operations.Now we are providing support for bulk create, in which it calls the
onBulkCreate()
method (which hits create api only one time for all the objects) ifbulkCreate
flag passes true tocreateList()
.For example:
bulkUserFactory.createList(2, { name: 'susan' }, { bulkCreate: true });