mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
committed by
Ariel Mashraki
parent
04859c6fb4
commit
875e6e516b
@@ -63,11 +63,8 @@ func (cc *CarCreate) Mutation() *CarMutation {
|
||||
|
||||
// Save creates the Car in the database.
|
||||
func (cc *CarCreate) Save(ctx context.Context) (*Car, error) {
|
||||
if _, ok := cc.mutation.Model(); !ok {
|
||||
return nil, &ValidationError{Name: "model", err: errors.New("ent: missing required field \"model\"")}
|
||||
}
|
||||
if _, ok := cc.mutation.RegisteredAt(); !ok {
|
||||
return nil, &ValidationError{Name: "registered_at", err: errors.New("ent: missing required field \"registered_at\"")}
|
||||
if err := cc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
@@ -105,6 +102,16 @@ func (cc *CarCreate) SaveX(ctx context.Context) *Car {
|
||||
return v
|
||||
}
|
||||
|
||||
func (cc *CarCreate) preSave() error {
|
||||
if _, ok := cc.mutation.Model(); !ok {
|
||||
return &ValidationError{Name: "model", err: errors.New("ent: missing required field \"model\"")}
|
||||
}
|
||||
if _, ok := cc.mutation.RegisteredAt(); !ok {
|
||||
return &ValidationError{Name: "registered_at", err: errors.New("ent: missing required field \"registered_at\"")}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) {
|
||||
c, _spec := cc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
|
||||
@@ -166,3 +173,67 @@ func (cc *CarCreate) createSpec() (*Car, *sqlgraph.CreateSpec) {
|
||||
}
|
||||
return c, _spec
|
||||
}
|
||||
|
||||
// CarCreateBulk is the builder for creating a bulk of Car entities.
|
||||
type CarCreateBulk struct {
|
||||
config
|
||||
builders []*CarCreate
|
||||
}
|
||||
|
||||
// Save creates the Car entities in the database.
|
||||
func (ccb *CarCreateBulk) Save(ctx context.Context) ([]*Car, error) {
|
||||
specs := make([]*sqlgraph.CreateSpec, len(ccb.builders))
|
||||
nodes := make([]*Car, len(ccb.builders))
|
||||
mutators := make([]Mutator, len(ccb.builders))
|
||||
for i := range ccb.builders {
|
||||
func(i int, root context.Context) {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
builder := ccb.builders[i]
|
||||
if err := builder.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation, ok := m.(*CarMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
builder.mutation = mutation
|
||||
nodes[i], specs[i] = builder.createSpec()
|
||||
var err error
|
||||
if i < len(mutators)-1 {
|
||||
_, err = mutators[i+1].Mutate(root, ccb.builders[i+1].mutation)
|
||||
} else {
|
||||
// Invoke the actual operation on the latest mutation in the chain.
|
||||
if err = sqlgraph.BatchCreate(ctx, ccb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
}
|
||||
}
|
||||
}
|
||||
mutation.done = true
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := specs[i].ID.Value.(int64)
|
||||
nodes[i].ID = int(id)
|
||||
return nodes[i], nil
|
||||
})
|
||||
for i := len(ccb.builders[i].hooks) - 1; i >= 0; i-- {
|
||||
mut = ccb.builders[i].hooks[i](mut)
|
||||
}
|
||||
mutators[i] = mut
|
||||
}(i, ctx)
|
||||
}
|
||||
if _, err := mutators[0].Mutate(ctx, ccb.builders[0].mutation); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
func (ccb *CarCreateBulk) SaveX(ctx context.Context) []*Car {
|
||||
v, err := ccb.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -157,6 +157,11 @@ func (c *CarClient) Create() *CarCreate {
|
||||
return &CarCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// BulkCreate returns a builder for creating a bulk of Car entities.
|
||||
func (c *CarClient) CreateBulk(builders ...*CarCreate) *CarCreateBulk {
|
||||
return &CarCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for Car.
|
||||
func (c *CarClient) Update() *CarUpdate {
|
||||
mutation := newCarMutation(c.config, OpUpdate)
|
||||
@@ -256,6 +261,11 @@ func (c *GroupClient) Create() *GroupCreate {
|
||||
return &GroupCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// BulkCreate returns a builder for creating a bulk of Group entities.
|
||||
func (c *GroupClient) CreateBulk(builders ...*GroupCreate) *GroupCreateBulk {
|
||||
return &GroupCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for Group.
|
||||
func (c *GroupClient) Update() *GroupUpdate {
|
||||
mutation := newGroupMutation(c.config, OpUpdate)
|
||||
@@ -355,6 +365,11 @@ func (c *UserClient) Create() *UserCreate {
|
||||
return &UserCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// BulkCreate returns a builder for creating a bulk of User entities.
|
||||
func (c *UserClient) CreateBulk(builders ...*UserCreate) *UserCreateBulk {
|
||||
return &UserCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for User.
|
||||
func (c *UserClient) Update() *UserUpdate {
|
||||
mutation := newUserMutation(c.config, OpUpdate)
|
||||
|
||||
@@ -52,13 +52,8 @@ func (gc *GroupCreate) Mutation() *GroupMutation {
|
||||
|
||||
// Save creates the Group in the database.
|
||||
func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
|
||||
if _, ok := gc.mutation.Name(); !ok {
|
||||
return nil, &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
}
|
||||
if v, ok := gc.mutation.Name(); ok {
|
||||
if err := group.NameValidator(v); err != nil {
|
||||
return nil, &ValidationError{Name: "name", err: fmt.Errorf("ent: validator failed for field \"name\": %w", err)}
|
||||
}
|
||||
if err := gc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
@@ -96,6 +91,18 @@ func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
|
||||
return v
|
||||
}
|
||||
|
||||
func (gc *GroupCreate) preSave() error {
|
||||
if _, ok := gc.mutation.Name(); !ok {
|
||||
return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
}
|
||||
if v, ok := gc.mutation.Name(); ok {
|
||||
if err := group.NameValidator(v); err != nil {
|
||||
return &ValidationError{Name: "name", err: fmt.Errorf("ent: validator failed for field \"name\": %w", err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
|
||||
gr, _spec := gc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil {
|
||||
@@ -149,3 +156,67 @@ func (gc *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) {
|
||||
}
|
||||
return gr, _spec
|
||||
}
|
||||
|
||||
// GroupCreateBulk is the builder for creating a bulk of Group entities.
|
||||
type GroupCreateBulk struct {
|
||||
config
|
||||
builders []*GroupCreate
|
||||
}
|
||||
|
||||
// Save creates the Group entities in the database.
|
||||
func (gcb *GroupCreateBulk) Save(ctx context.Context) ([]*Group, error) {
|
||||
specs := make([]*sqlgraph.CreateSpec, len(gcb.builders))
|
||||
nodes := make([]*Group, len(gcb.builders))
|
||||
mutators := make([]Mutator, len(gcb.builders))
|
||||
for i := range gcb.builders {
|
||||
func(i int, root context.Context) {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
builder := gcb.builders[i]
|
||||
if err := builder.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation, ok := m.(*GroupMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
builder.mutation = mutation
|
||||
nodes[i], specs[i] = builder.createSpec()
|
||||
var err error
|
||||
if i < len(mutators)-1 {
|
||||
_, err = mutators[i+1].Mutate(root, gcb.builders[i+1].mutation)
|
||||
} else {
|
||||
// Invoke the actual operation on the latest mutation in the chain.
|
||||
if err = sqlgraph.BatchCreate(ctx, gcb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
}
|
||||
}
|
||||
}
|
||||
mutation.done = true
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := specs[i].ID.Value.(int64)
|
||||
nodes[i].ID = int(id)
|
||||
return nodes[i], nil
|
||||
})
|
||||
for i := len(gcb.builders[i].hooks) - 1; i >= 0; i-- {
|
||||
mut = gcb.builders[i].hooks[i](mut)
|
||||
}
|
||||
mutators[i] = mut
|
||||
}(i, ctx)
|
||||
}
|
||||
if _, err := mutators[0].Mutate(ctx, gcb.builders[0].mutation); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
func (gcb *GroupCreateBulk) SaveX(ctx context.Context) []*Group {
|
||||
v, err := gcb.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -82,17 +82,8 @@ func (uc *UserCreate) Mutation() *UserMutation {
|
||||
|
||||
// Save creates the User in the database.
|
||||
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
if _, ok := uc.mutation.Age(); !ok {
|
||||
return nil, &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
|
||||
}
|
||||
if v, ok := uc.mutation.Age(); ok {
|
||||
if err := user.AgeValidator(v); err != nil {
|
||||
return nil, &ValidationError{Name: "age", err: fmt.Errorf("ent: validator failed for field \"age\": %w", err)}
|
||||
}
|
||||
}
|
||||
if _, ok := uc.mutation.Name(); !ok {
|
||||
v := user.DefaultName
|
||||
uc.mutation.SetName(v)
|
||||
if err := uc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
@@ -130,6 +121,22 @@ func (uc *UserCreate) SaveX(ctx context.Context) *User {
|
||||
return v
|
||||
}
|
||||
|
||||
func (uc *UserCreate) preSave() error {
|
||||
if _, ok := uc.mutation.Age(); !ok {
|
||||
return &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
|
||||
}
|
||||
if v, ok := uc.mutation.Age(); ok {
|
||||
if err := user.AgeValidator(v); err != nil {
|
||||
return &ValidationError{Name: "age", err: fmt.Errorf("ent: validator failed for field \"age\": %w", err)}
|
||||
}
|
||||
}
|
||||
if _, ok := uc.mutation.Name(); !ok {
|
||||
v := user.DefaultName
|
||||
uc.mutation.SetName(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
||||
u, _spec := uc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
|
||||
@@ -210,3 +217,67 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
|
||||
}
|
||||
return u, _spec
|
||||
}
|
||||
|
||||
// UserCreateBulk is the builder for creating a bulk of User entities.
|
||||
type UserCreateBulk struct {
|
||||
config
|
||||
builders []*UserCreate
|
||||
}
|
||||
|
||||
// Save creates the User entities in the database.
|
||||
func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
|
||||
specs := make([]*sqlgraph.CreateSpec, len(ucb.builders))
|
||||
nodes := make([]*User, len(ucb.builders))
|
||||
mutators := make([]Mutator, len(ucb.builders))
|
||||
for i := range ucb.builders {
|
||||
func(i int, root context.Context) {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
builder := ucb.builders[i]
|
||||
if err := builder.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation, ok := m.(*UserMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
builder.mutation = mutation
|
||||
nodes[i], specs[i] = builder.createSpec()
|
||||
var err error
|
||||
if i < len(mutators)-1 {
|
||||
_, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation)
|
||||
} else {
|
||||
// Invoke the actual operation on the latest mutation in the chain.
|
||||
if err = sqlgraph.BatchCreate(ctx, ucb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
|
||||
if cerr, ok := isSQLConstraintError(err); ok {
|
||||
err = cerr
|
||||
}
|
||||
}
|
||||
}
|
||||
mutation.done = true
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := specs[i].ID.Value.(int64)
|
||||
nodes[i].ID = int(id)
|
||||
return nodes[i], nil
|
||||
})
|
||||
for i := len(ucb.builders[i].hooks) - 1; i >= 0; i-- {
|
||||
mut = ucb.builders[i].hooks[i](mut)
|
||||
}
|
||||
mutators[i] = mut
|
||||
}(i, ctx)
|
||||
}
|
||||
if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User {
|
||||
v, err := ucb.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user