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
@@ -55,14 +55,8 @@ func (cc *CardCreate) Mutation() *CardMutation {
|
||||
|
||||
// Save creates the Card in the database.
|
||||
func (cc *CardCreate) Save(ctx context.Context) (*Card, error) {
|
||||
if _, ok := cc.mutation.Expired(); !ok {
|
||||
return nil, &ValidationError{Name: "expired", err: errors.New("ent: missing required field \"expired\"")}
|
||||
}
|
||||
if _, ok := cc.mutation.Number(); !ok {
|
||||
return nil, &ValidationError{Name: "number", err: errors.New("ent: missing required field \"number\"")}
|
||||
}
|
||||
if _, ok := cc.mutation.OwnerID(); !ok {
|
||||
return nil, &ValidationError{Name: "owner", err: errors.New("ent: missing required edge \"owner\"")}
|
||||
if err := cc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
@@ -100,6 +94,19 @@ func (cc *CardCreate) SaveX(ctx context.Context) *Card {
|
||||
return v
|
||||
}
|
||||
|
||||
func (cc *CardCreate) preSave() error {
|
||||
if _, ok := cc.mutation.Expired(); !ok {
|
||||
return &ValidationError{Name: "expired", err: errors.New("ent: missing required field \"expired\"")}
|
||||
}
|
||||
if _, ok := cc.mutation.Number(); !ok {
|
||||
return &ValidationError{Name: "number", err: errors.New("ent: missing required field \"number\"")}
|
||||
}
|
||||
if _, ok := cc.mutation.OwnerID(); !ok {
|
||||
return &ValidationError{Name: "owner", err: errors.New("ent: missing required edge \"owner\"")}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) {
|
||||
c, _spec := cc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
|
||||
@@ -161,3 +168,67 @@ func (cc *CardCreate) createSpec() (*Card, *sqlgraph.CreateSpec) {
|
||||
}
|
||||
return c, _spec
|
||||
}
|
||||
|
||||
// CardCreateBulk is the builder for creating a bulk of Card entities.
|
||||
type CardCreateBulk struct {
|
||||
config
|
||||
builders []*CardCreate
|
||||
}
|
||||
|
||||
// Save creates the Card entities in the database.
|
||||
func (ccb *CardCreateBulk) Save(ctx context.Context) ([]*Card, error) {
|
||||
specs := make([]*sqlgraph.CreateSpec, len(ccb.builders))
|
||||
nodes := make([]*Card, 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.(*CardMutation)
|
||||
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 *CardCreateBulk) SaveX(ctx context.Context) []*Card {
|
||||
v, err := ccb.Save(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -150,6 +150,11 @@ func (c *CardClient) Create() *CardCreate {
|
||||
return &CardCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// BulkCreate returns a builder for creating a bulk of Card entities.
|
||||
func (c *CardClient) CreateBulk(builders ...*CardCreate) *CardCreateBulk {
|
||||
return &CardCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for Card.
|
||||
func (c *CardClient) Update() *CardUpdate {
|
||||
mutation := newCardMutation(c.config, OpUpdate)
|
||||
@@ -249,6 +254,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)
|
||||
|
||||
@@ -62,11 +62,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 _, ok := uc.mutation.Name(); !ok {
|
||||
return nil, &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
if err := uc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
@@ -104,6 +101,16 @@ 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 _, ok := uc.mutation.Name(); !ok {
|
||||
return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
}
|
||||
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 {
|
||||
@@ -165,3 +172,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