property_create.go•20.4 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/Southclaws/storyden/internal/ent/node"
"github.com/Southclaws/storyden/internal/ent/property"
"github.com/Southclaws/storyden/internal/ent/propertyschemafield"
"github.com/rs/xid"
)
// PropertyCreate is the builder for creating a Property entity.
type PropertyCreate struct {
config
mutation *PropertyMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *PropertyCreate) SetCreatedAt(v time.Time) *PropertyCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *PropertyCreate) SetNillableCreatedAt(v *time.Time) *PropertyCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetNodeID sets the "node_id" field.
func (_c *PropertyCreate) SetNodeID(v xid.ID) *PropertyCreate {
_c.mutation.SetNodeID(v)
return _c
}
// SetFieldID sets the "field_id" field.
func (_c *PropertyCreate) SetFieldID(v xid.ID) *PropertyCreate {
_c.mutation.SetFieldID(v)
return _c
}
// SetValue sets the "value" field.
func (_c *PropertyCreate) SetValue(v string) *PropertyCreate {
_c.mutation.SetValue(v)
return _c
}
// SetID sets the "id" field.
func (_c *PropertyCreate) SetID(v xid.ID) *PropertyCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *PropertyCreate) SetNillableID(v *xid.ID) *PropertyCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetNode sets the "node" edge to the Node entity.
func (_c *PropertyCreate) SetNode(v *Node) *PropertyCreate {
return _c.SetNodeID(v.ID)
}
// SetSchemaID sets the "schema" edge to the PropertySchemaField entity by ID.
func (_c *PropertyCreate) SetSchemaID(id xid.ID) *PropertyCreate {
_c.mutation.SetSchemaID(id)
return _c
}
// SetSchema sets the "schema" edge to the PropertySchemaField entity.
func (_c *PropertyCreate) SetSchema(v *PropertySchemaField) *PropertyCreate {
return _c.SetSchemaID(v.ID)
}
// Mutation returns the PropertyMutation object of the builder.
func (_c *PropertyCreate) Mutation() *PropertyMutation {
return _c.mutation
}
// Save creates the Property in the database.
func (_c *PropertyCreate) Save(ctx context.Context) (*Property, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *PropertyCreate) SaveX(ctx context.Context) *Property {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *PropertyCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *PropertyCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_c *PropertyCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := property.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := property.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *PropertyCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Property.created_at"`)}
}
if _, ok := _c.mutation.NodeID(); !ok {
return &ValidationError{Name: "node_id", err: errors.New(`ent: missing required field "Property.node_id"`)}
}
if _, ok := _c.mutation.FieldID(); !ok {
return &ValidationError{Name: "field_id", err: errors.New(`ent: missing required field "Property.field_id"`)}
}
if _, ok := _c.mutation.Value(); !ok {
return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "Property.value"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := property.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Property.id": %w`, err)}
}
}
if len(_c.mutation.NodeIDs()) == 0 {
return &ValidationError{Name: "node", err: errors.New(`ent: missing required edge "Property.node"`)}
}
if len(_c.mutation.SchemaIDs()) == 0 {
return &ValidationError{Name: "schema", err: errors.New(`ent: missing required edge "Property.schema"`)}
}
return nil
}
func (_c *PropertyCreate) sqlSave(ctx context.Context) (*Property, error) {
if err := _c.check(); err != nil {
return nil, err
}
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*xid.ID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *PropertyCreate) createSpec() (*Property, *sqlgraph.CreateSpec) {
var (
_node = &Property{config: _c.config}
_spec = sqlgraph.NewCreateSpec(property.Table, sqlgraph.NewFieldSpec(property.FieldID, field.TypeString))
)
_spec.OnConflict = _c.conflict
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(property.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.Value(); ok {
_spec.SetField(property.FieldValue, field.TypeString, value)
_node.Value = value
}
if nodes := _c.mutation.NodeIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.NodeTable,
Columns: []string{property.NodeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.NodeID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.SchemaIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.SchemaTable,
Columns: []string{property.SchemaColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(propertyschemafield.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.FieldID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.Property.Create().
// SetCreatedAt(v).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.PropertyUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *PropertyCreate) OnConflict(opts ...sql.ConflictOption) *PropertyUpsertOne {
_c.conflict = opts
return &PropertyUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Property.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *PropertyCreate) OnConflictColumns(columns ...string) *PropertyUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &PropertyUpsertOne{
create: _c,
}
}
type (
// PropertyUpsertOne is the builder for "upsert"-ing
// one Property node.
PropertyUpsertOne struct {
create *PropertyCreate
}
// PropertyUpsert is the "OnConflict" setter.
PropertyUpsert struct {
*sql.UpdateSet
}
)
// SetNodeID sets the "node_id" field.
func (u *PropertyUpsert) SetNodeID(v xid.ID) *PropertyUpsert {
u.Set(property.FieldNodeID, v)
return u
}
// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *PropertyUpsert) UpdateNodeID() *PropertyUpsert {
u.SetExcluded(property.FieldNodeID)
return u
}
// SetFieldID sets the "field_id" field.
func (u *PropertyUpsert) SetFieldID(v xid.ID) *PropertyUpsert {
u.Set(property.FieldFieldID, v)
return u
}
// UpdateFieldID sets the "field_id" field to the value that was provided on create.
func (u *PropertyUpsert) UpdateFieldID() *PropertyUpsert {
u.SetExcluded(property.FieldFieldID)
return u
}
// SetValue sets the "value" field.
func (u *PropertyUpsert) SetValue(v string) *PropertyUpsert {
u.Set(property.FieldValue, v)
return u
}
// UpdateValue sets the "value" field to the value that was provided on create.
func (u *PropertyUpsert) UpdateValue() *PropertyUpsert {
u.SetExcluded(property.FieldValue)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
// client.Property.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(property.FieldID)
// }),
// ).
// Exec(ctx)
func (u *PropertyUpsertOne) UpdateNewValues() *PropertyUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
if _, exists := u.create.mutation.ID(); exists {
s.SetIgnore(property.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(property.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Property.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *PropertyUpsertOne) Ignore() *PropertyUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PropertyUpsertOne) DoNothing() *PropertyUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the PropertyCreate.OnConflict
// documentation for more info.
func (u *PropertyUpsertOne) Update(set func(*PropertyUpsert)) *PropertyUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&PropertyUpsert{UpdateSet: update})
}))
return u
}
// SetNodeID sets the "node_id" field.
func (u *PropertyUpsertOne) SetNodeID(v xid.ID) *PropertyUpsertOne {
return u.Update(func(s *PropertyUpsert) {
s.SetNodeID(v)
})
}
// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *PropertyUpsertOne) UpdateNodeID() *PropertyUpsertOne {
return u.Update(func(s *PropertyUpsert) {
s.UpdateNodeID()
})
}
// SetFieldID sets the "field_id" field.
func (u *PropertyUpsertOne) SetFieldID(v xid.ID) *PropertyUpsertOne {
return u.Update(func(s *PropertyUpsert) {
s.SetFieldID(v)
})
}
// UpdateFieldID sets the "field_id" field to the value that was provided on create.
func (u *PropertyUpsertOne) UpdateFieldID() *PropertyUpsertOne {
return u.Update(func(s *PropertyUpsert) {
s.UpdateFieldID()
})
}
// SetValue sets the "value" field.
func (u *PropertyUpsertOne) SetValue(v string) *PropertyUpsertOne {
return u.Update(func(s *PropertyUpsert) {
s.SetValue(v)
})
}
// UpdateValue sets the "value" field to the value that was provided on create.
func (u *PropertyUpsertOne) UpdateValue() *PropertyUpsertOne {
return u.Update(func(s *PropertyUpsert) {
s.UpdateValue()
})
}
// Exec executes the query.
func (u *PropertyUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for PropertyCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *PropertyUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}
// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *PropertyUpsertOne) ID(ctx context.Context) (id xid.ID, err error) {
if u.create.driver.Dialect() == dialect.MySQL {
// In case of "ON CONFLICT", there is no way to get back non-numeric ID
// fields from the database since MySQL does not support the RETURNING clause.
return id, errors.New("ent: PropertyUpsertOne.ID is not supported by MySQL driver. Use PropertyUpsertOne.Exec instead")
}
node, err := u.create.Save(ctx)
if err != nil {
return id, err
}
return node.ID, nil
}
// IDX is like ID, but panics if an error occurs.
func (u *PropertyUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// PropertyCreateBulk is the builder for creating many Property entities in bulk.
type PropertyCreateBulk struct {
config
err error
builders []*PropertyCreate
conflict []sql.ConflictOption
}
// Save creates the Property entities in the database.
func (_c *PropertyCreateBulk) Save(ctx context.Context) ([]*Property, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Property, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
func(i int, root context.Context) {
builder := _c.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*PropertyMutation)
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, _c.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
spec.OnConflict = _c.conflict
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, _c.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
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, _c.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (_c *PropertyCreateBulk) SaveX(ctx context.Context) []*Property {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *PropertyCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *PropertyCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.Property.CreateBulk(builders...).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.PropertyUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *PropertyCreateBulk) OnConflict(opts ...sql.ConflictOption) *PropertyUpsertBulk {
_c.conflict = opts
return &PropertyUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Property.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *PropertyCreateBulk) OnConflictColumns(columns ...string) *PropertyUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &PropertyUpsertBulk{
create: _c,
}
}
// PropertyUpsertBulk is the builder for "upsert"-ing
// a bulk of Property nodes.
type PropertyUpsertBulk struct {
create *PropertyCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Property.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(property.FieldID)
// }),
// ).
// Exec(ctx)
func (u *PropertyUpsertBulk) UpdateNewValues() *PropertyUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
for _, b := range u.create.builders {
if _, exists := b.mutation.ID(); exists {
s.SetIgnore(property.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(property.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Property.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *PropertyUpsertBulk) Ignore() *PropertyUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *PropertyUpsertBulk) DoNothing() *PropertyUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the PropertyCreateBulk.OnConflict
// documentation for more info.
func (u *PropertyUpsertBulk) Update(set func(*PropertyUpsert)) *PropertyUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&PropertyUpsert{UpdateSet: update})
}))
return u
}
// SetNodeID sets the "node_id" field.
func (u *PropertyUpsertBulk) SetNodeID(v xid.ID) *PropertyUpsertBulk {
return u.Update(func(s *PropertyUpsert) {
s.SetNodeID(v)
})
}
// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *PropertyUpsertBulk) UpdateNodeID() *PropertyUpsertBulk {
return u.Update(func(s *PropertyUpsert) {
s.UpdateNodeID()
})
}
// SetFieldID sets the "field_id" field.
func (u *PropertyUpsertBulk) SetFieldID(v xid.ID) *PropertyUpsertBulk {
return u.Update(func(s *PropertyUpsert) {
s.SetFieldID(v)
})
}
// UpdateFieldID sets the "field_id" field to the value that was provided on create.
func (u *PropertyUpsertBulk) UpdateFieldID() *PropertyUpsertBulk {
return u.Update(func(s *PropertyUpsert) {
s.UpdateFieldID()
})
}
// SetValue sets the "value" field.
func (u *PropertyUpsertBulk) SetValue(v string) *PropertyUpsertBulk {
return u.Update(func(s *PropertyUpsert) {
s.SetValue(v)
})
}
// UpdateValue sets the "value" field to the value that was provided on create.
func (u *PropertyUpsertBulk) UpdateValue() *PropertyUpsertBulk {
return u.Update(func(s *PropertyUpsert) {
s.UpdateValue()
})
}
// Exec executes the query.
func (u *PropertyUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {
return u.create.err
}
for i, b := range u.create.builders {
if len(b.conflict) != 0 {
return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PropertyCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for PropertyCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *PropertyUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}