Save creates the User entities in the database.
(ctx context.Context)
| 623 | |
| 624 | // Save creates the User entities in the database. |
| 625 | func (_c *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { |
| 626 | if _c.err != nil { |
| 627 | return nil, _c.err |
| 628 | } |
| 629 | specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) |
| 630 | nodes := make([]*User, len(_c.builders)) |
| 631 | mutators := make([]Mutator, len(_c.builders)) |
| 632 | for i := range _c.builders { |
| 633 | func(i int, root context.Context) { |
| 634 | builder := _c.builders[i] |
| 635 | builder.defaults() |
| 636 | var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 637 | mutation, ok := m.(*UserMutation) |
| 638 | if !ok { |
| 639 | return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 640 | } |
| 641 | if err := builder.check(); err != nil { |
| 642 | return nil, err |
| 643 | } |
| 644 | builder.mutation = mutation |
| 645 | var err error |
| 646 | nodes[i], specs[i] = builder.createSpec() |
| 647 | if i < len(mutators)-1 { |
| 648 | _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) |
| 649 | } else { |
| 650 | spec := &sqlgraph.BatchCreateSpec{Nodes: specs} |
| 651 | spec.OnConflict = _c.conflict |
| 652 | // Invoke the actual operation on the latest mutation in the chain. |
| 653 | if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil { |
| 654 | if sqlgraph.IsConstraintError(err) { |
| 655 | err = &ConstraintError{msg: err.Error(), wrap: err} |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | if err != nil { |
| 660 | return nil, err |
| 661 | } |
| 662 | mutation.id = &nodes[i].ID |
| 663 | mutation.done = true |
| 664 | return nodes[i], nil |
| 665 | }) |
| 666 | for i := len(builder.hooks) - 1; i >= 0; i-- { |
| 667 | mut = builder.hooks[i](mut) |
| 668 | } |
| 669 | mutators[i] = mut |
| 670 | }(i, ctx) |
| 671 | } |
| 672 | if len(mutators) > 0 { |
| 673 | if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil { |
| 674 | return nil, err |
| 675 | } |
| 676 | } |
| 677 | return nodes, nil |
| 678 | } |
| 679 | |
| 680 | // SaveX is like Save, but panics if an error occurs. |
| 681 | func (_c *UserCreateBulk) SaveX(ctx context.Context) []*User { |