mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: add support for MapBulkCreate (#3696)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/examples/o2orecur/ent/migrate"
|
||||
@@ -228,6 +229,21 @@ func (c *NodeClient) CreateBulk(builders ...*NodeCreate) *NodeCreateBulk {
|
||||
return &NodeCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
|
||||
// a builder and applies setFunc on it.
|
||||
func (c *NodeClient) MapCreateBulk(slice any, setFunc func(*NodeCreate, int)) *NodeCreateBulk {
|
||||
rv := reflect.ValueOf(slice)
|
||||
if rv.Kind() != reflect.Slice {
|
||||
return &NodeCreateBulk{err: fmt.Errorf("calling to NodeClient.MapCreateBulk with wrong type %T, need slice", slice)}
|
||||
}
|
||||
builders := make([]*NodeCreate, rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
builders[i] = c.Create()
|
||||
setFunc(builders[i], i)
|
||||
}
|
||||
return &NodeCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for Node.
|
||||
func (c *NodeClient) Update() *NodeUpdate {
|
||||
mutation := newNodeMutation(c.config, OpUpdate)
|
||||
|
||||
@@ -173,11 +173,15 @@ func (nc *NodeCreate) createSpec() (*Node, *sqlgraph.CreateSpec) {
|
||||
// NodeCreateBulk is the builder for creating many Node entities in bulk.
|
||||
type NodeCreateBulk struct {
|
||||
config
|
||||
err error
|
||||
builders []*NodeCreate
|
||||
}
|
||||
|
||||
// Save creates the Node entities in the database.
|
||||
func (ncb *NodeCreateBulk) Save(ctx context.Context) ([]*Node, error) {
|
||||
if ncb.err != nil {
|
||||
return nil, ncb.err
|
||||
}
|
||||
specs := make([]*sqlgraph.CreateSpec, len(ncb.builders))
|
||||
nodes := make([]*Node, len(ncb.builders))
|
||||
mutators := make([]Mutator, len(ncb.builders))
|
||||
|
||||
Reference in New Issue
Block a user