// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "leafdev.top/leaf/rag/ent/document" ) // DocumentCreate is the builder for creating a Document entity. type DocumentCreate struct { config mutation *DocumentMutation hooks []Hook } // SetName sets the "name" field. func (dc *DocumentCreate) SetName(s string) *DocumentCreate { dc.mutation.SetName(s) return dc } // SetUserID sets the "user_id" field. func (dc *DocumentCreate) SetUserID(s string) *DocumentCreate { dc.mutation.SetUserID(s) return dc } // SetLibraryID sets the "library_id" field. func (dc *DocumentCreate) SetLibraryID(i int64) *DocumentCreate { dc.mutation.SetLibraryID(i) return dc } // SetCreatedAt sets the "created_at" field. func (dc *DocumentCreate) SetCreatedAt(t time.Time) *DocumentCreate { dc.mutation.SetCreatedAt(t) return dc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (dc *DocumentCreate) SetNillableCreatedAt(t *time.Time) *DocumentCreate { if t != nil { dc.SetCreatedAt(*t) } return dc } // SetUpdatedAt sets the "updated_at" field. func (dc *DocumentCreate) SetUpdatedAt(t time.Time) *DocumentCreate { dc.mutation.SetUpdatedAt(t) return dc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (dc *DocumentCreate) SetNillableUpdatedAt(t *time.Time) *DocumentCreate { if t != nil { dc.SetUpdatedAt(*t) } return dc } // Mutation returns the DocumentMutation object of the builder. func (dc *DocumentCreate) Mutation() *DocumentMutation { return dc.mutation } // Save creates the Document in the database. func (dc *DocumentCreate) Save(ctx context.Context) (*Document, error) { dc.defaults() return withHooks(ctx, dc.sqlSave, dc.mutation, dc.hooks) } // SaveX calls Save and panics if Save returns an error. func (dc *DocumentCreate) SaveX(ctx context.Context) *Document { v, err := dc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (dc *DocumentCreate) Exec(ctx context.Context) error { _, err := dc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (dc *DocumentCreate) ExecX(ctx context.Context) { if err := dc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (dc *DocumentCreate) defaults() { if _, ok := dc.mutation.CreatedAt(); !ok { v := document.DefaultCreatedAt() dc.mutation.SetCreatedAt(v) } if _, ok := dc.mutation.UpdatedAt(); !ok { v := document.DefaultUpdatedAt() dc.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (dc *DocumentCreate) check() error { if _, ok := dc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Document.name"`)} } if _, ok := dc.mutation.UserID(); !ok { return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "Document.user_id"`)} } if _, ok := dc.mutation.LibraryID(); !ok { return &ValidationError{Name: "library_id", err: errors.New(`ent: missing required field "Document.library_id"`)} } if _, ok := dc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Document.created_at"`)} } if _, ok := dc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Document.updated_at"`)} } return nil } func (dc *DocumentCreate) sqlSave(ctx context.Context) (*Document, error) { if err := dc.check(); err != nil { return nil, err } _node, _spec := dc.createSpec() if err := sqlgraph.CreateNode(ctx, dc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } id := _spec.ID.Value.(int64) _node.ID = int(id) dc.mutation.id = &_node.ID dc.mutation.done = true return _node, nil } func (dc *DocumentCreate) createSpec() (*Document, *sqlgraph.CreateSpec) { var ( _node = &Document{config: dc.config} _spec = sqlgraph.NewCreateSpec(document.Table, sqlgraph.NewFieldSpec(document.FieldID, field.TypeInt)) ) if value, ok := dc.mutation.Name(); ok { _spec.SetField(document.FieldName, field.TypeString, value) _node.Name = value } if value, ok := dc.mutation.UserID(); ok { _spec.SetField(document.FieldUserID, field.TypeString, value) _node.UserID = value } if value, ok := dc.mutation.LibraryID(); ok { _spec.SetField(document.FieldLibraryID, field.TypeInt64, value) _node.LibraryID = value } if value, ok := dc.mutation.CreatedAt(); ok { _spec.SetField(document.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := dc.mutation.UpdatedAt(); ok { _spec.SetField(document.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } return _node, _spec } // DocumentCreateBulk is the builder for creating many Document entities in bulk. type DocumentCreateBulk struct { config err error builders []*DocumentCreate } // Save creates the Document entities in the database. func (dcb *DocumentCreateBulk) Save(ctx context.Context) ([]*Document, error) { if dcb.err != nil { return nil, dcb.err } specs := make([]*sqlgraph.CreateSpec, len(dcb.builders)) nodes := make([]*Document, len(dcb.builders)) mutators := make([]Mutator, len(dcb.builders)) for i := range dcb.builders { func(i int, root context.Context) { builder := dcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*DocumentMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation var err error nodes[i], specs[i] = builder.createSpec() if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, dcb.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, dcb.driver, spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID if specs[i].ID.Value != nil { id := specs[i].ID.Value.(int64) nodes[i].ID = int(id) } mutation.done = true return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { mut = builder.hooks[i](mut) } mutators[i] = mut }(i, ctx) } if len(mutators) > 0 { if _, err := mutators[0].Mutate(ctx, dcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (dcb *DocumentCreateBulk) SaveX(ctx context.Context) []*Document { v, err := dcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (dcb *DocumentCreateBulk) Exec(ctx context.Context) error { _, err := dcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (dcb *DocumentCreateBulk) ExecX(ctx context.Context) { if err := dcb.Exec(ctx); err != nil { panic(err) } }