It would be nice to have some way to create / insert multiple entities of the same type at once.
We had a few conversations about it a few weeks ago, and I'm in favor of adding this functionality if it reduces the number of roundtrips to the database.
I'll leave it open to get suggestions for the API design.
Hey, a few months passed and we got to it. This is something I posted internally and I'd like users in the OSS will give their feedback, share thoughts or suggestions for the next API (the generic one).
I want to introduce an API for applying updates (change-sets) in batches. The main reason for it, is performance improvement.
In many cases, the number of round-trips to the database can be reduces from multiple (or tens) to 1 (or a few) in GraphQL mutations.
In the first phase, I want to add an API for bulk inserting entities from the same type, and in the second phase I'll add a generic API for applying any list of change-sets - I'm not sure about the return type yet.
The the first API goes as follows:
users, err := client.User.
BulkCreate(builders...).
Save(ctx)
err := client.User.
BulkCreate(builders...).
Exec(ctx)
In many cases, we iterate over a list of GraphQL/REST objects and create entities from them. For this case, I want to add the following API:
users, err := client.User.
MapBulkCreate(slice, func(b *ent.UserCreate, i int) error {
// Configure the builder.
b.SetName(slice[i].Name)
return nil
}).
Save(ctx)
// Open Q: should we add the context to the callback function?
I'm not sure the second method (MapBulkCreate) is really necessary. We can add it to our projects using custom templates, and add it to the official codegen if we'll see that it's useful.
When we use BulkCreate, we usually need on conflict feature.
But there are a lot of differences between DBMSs(pgsql mysql sqlite). Maybe we can pass a custom string into BulkCreate function?
extra := "ON DUPLICATE KEY UPDATE `retention` = VALUES(`retention`), `updated_at` = VALUES(`upda
ted_at`)"
client.User.
BulkCreate([]builders, extra).
Exec(ctx)
Just like https://github.com/shanbay/gobay/blob/master/cmd/gobay/templates/spec/enttmpl/client.tmpl#L209
Thanks for the feedback @sljeff. This will be addressed with #139.
CreateBulk API was added to master, and we'll be released with v0.2.8.
See https://entgo.io/docs/crud/#create-many
Most helpful comment
We had a few conversations about it a few weeks ago, and I'm in favor of adding this functionality if it reduces the number of roundtrips to the database.
I'll leave it open to get suggestions for the API design.