collectionnode_create.go•21.2 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/Southclaws/storyden/internal/ent/collection"
"github.com/Southclaws/storyden/internal/ent/collectionnode"
"github.com/Southclaws/storyden/internal/ent/node"
"github.com/rs/xid"
)
// CollectionNodeCreate is the builder for creating a CollectionNode entity.
type CollectionNodeCreate struct {
config
mutation *CollectionNodeMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *CollectionNodeCreate) SetCreatedAt(v time.Time) *CollectionNodeCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *CollectionNodeCreate) SetNillableCreatedAt(v *time.Time) *CollectionNodeCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetCollectionID sets the "collection_id" field.
func (_c *CollectionNodeCreate) SetCollectionID(v xid.ID) *CollectionNodeCreate {
_c.mutation.SetCollectionID(v)
return _c
}
// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
func (_c *CollectionNodeCreate) SetNillableCollectionID(v *xid.ID) *CollectionNodeCreate {
if v != nil {
_c.SetCollectionID(*v)
}
return _c
}
// SetNodeID sets the "node_id" field.
func (_c *CollectionNodeCreate) SetNodeID(v xid.ID) *CollectionNodeCreate {
_c.mutation.SetNodeID(v)
return _c
}
// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (_c *CollectionNodeCreate) SetNillableNodeID(v *xid.ID) *CollectionNodeCreate {
if v != nil {
_c.SetNodeID(*v)
}
return _c
}
// SetMembershipType sets the "membership_type" field.
func (_c *CollectionNodeCreate) SetMembershipType(v string) *CollectionNodeCreate {
_c.mutation.SetMembershipType(v)
return _c
}
// SetNillableMembershipType sets the "membership_type" field if the given value is not nil.
func (_c *CollectionNodeCreate) SetNillableMembershipType(v *string) *CollectionNodeCreate {
if v != nil {
_c.SetMembershipType(*v)
}
return _c
}
// SetCollection sets the "collection" edge to the Collection entity.
func (_c *CollectionNodeCreate) SetCollection(v *Collection) *CollectionNodeCreate {
return _c.SetCollectionID(v.ID)
}
// SetNode sets the "node" edge to the Node entity.
func (_c *CollectionNodeCreate) SetNode(v *Node) *CollectionNodeCreate {
return _c.SetNodeID(v.ID)
}
// Mutation returns the CollectionNodeMutation object of the builder.
func (_c *CollectionNodeCreate) Mutation() *CollectionNodeMutation {
return _c.mutation
}
// Save creates the CollectionNode in the database.
func (_c *CollectionNodeCreate) Save(ctx context.Context) (*CollectionNode, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *CollectionNodeCreate) SaveX(ctx context.Context) *CollectionNode {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CollectionNodeCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CollectionNodeCreate) 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 *CollectionNodeCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := collectionnode.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.CollectionID(); !ok {
v := collectionnode.DefaultCollectionID()
_c.mutation.SetCollectionID(v)
}
if _, ok := _c.mutation.NodeID(); !ok {
v := collectionnode.DefaultNodeID()
_c.mutation.SetNodeID(v)
}
if _, ok := _c.mutation.MembershipType(); !ok {
v := collectionnode.DefaultMembershipType
_c.mutation.SetMembershipType(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *CollectionNodeCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "CollectionNode.created_at"`)}
}
if _, ok := _c.mutation.CollectionID(); !ok {
return &ValidationError{Name: "collection_id", err: errors.New(`ent: missing required field "CollectionNode.collection_id"`)}
}
if v, ok := _c.mutation.CollectionID(); ok {
if err := collectionnode.CollectionIDValidator(v.String()); err != nil {
return &ValidationError{Name: "collection_id", err: fmt.Errorf(`ent: validator failed for field "CollectionNode.collection_id": %w`, err)}
}
}
if _, ok := _c.mutation.NodeID(); !ok {
return &ValidationError{Name: "node_id", err: errors.New(`ent: missing required field "CollectionNode.node_id"`)}
}
if v, ok := _c.mutation.NodeID(); ok {
if err := collectionnode.NodeIDValidator(v.String()); err != nil {
return &ValidationError{Name: "node_id", err: fmt.Errorf(`ent: validator failed for field "CollectionNode.node_id": %w`, err)}
}
}
if _, ok := _c.mutation.MembershipType(); !ok {
return &ValidationError{Name: "membership_type", err: errors.New(`ent: missing required field "CollectionNode.membership_type"`)}
}
if len(_c.mutation.CollectionIDs()) == 0 {
return &ValidationError{Name: "collection", err: errors.New(`ent: missing required edge "CollectionNode.collection"`)}
}
if len(_c.mutation.NodeIDs()) == 0 {
return &ValidationError{Name: "node", err: errors.New(`ent: missing required edge "CollectionNode.node"`)}
}
return nil
}
func (_c *CollectionNodeCreate) sqlSave(ctx context.Context) (*CollectionNode, 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
}
return _node, nil
}
func (_c *CollectionNodeCreate) createSpec() (*CollectionNode, *sqlgraph.CreateSpec) {
var (
_node = &CollectionNode{config: _c.config}
_spec = sqlgraph.NewCreateSpec(collectionnode.Table, nil)
)
_spec.OnConflict = _c.conflict
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(collectionnode.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.MembershipType(); ok {
_spec.SetField(collectionnode.FieldMembershipType, field.TypeString, value)
_node.MembershipType = value
}
if nodes := _c.mutation.CollectionIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collectionnode.CollectionTable,
Columns: []string{collectionnode.CollectionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(collection.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.CollectionID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.NodeIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collectionnode.NodeTable,
Columns: []string{collectionnode.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)
}
return _node, _spec
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.CollectionNode.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.CollectionNodeUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CollectionNodeCreate) OnConflict(opts ...sql.ConflictOption) *CollectionNodeUpsertOne {
_c.conflict = opts
return &CollectionNodeUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.CollectionNode.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CollectionNodeCreate) OnConflictColumns(columns ...string) *CollectionNodeUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CollectionNodeUpsertOne{
create: _c,
}
}
type (
// CollectionNodeUpsertOne is the builder for "upsert"-ing
// one CollectionNode node.
CollectionNodeUpsertOne struct {
create *CollectionNodeCreate
}
// CollectionNodeUpsert is the "OnConflict" setter.
CollectionNodeUpsert struct {
*sql.UpdateSet
}
)
// SetCollectionID sets the "collection_id" field.
func (u *CollectionNodeUpsert) SetCollectionID(v xid.ID) *CollectionNodeUpsert {
u.Set(collectionnode.FieldCollectionID, v)
return u
}
// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *CollectionNodeUpsert) UpdateCollectionID() *CollectionNodeUpsert {
u.SetExcluded(collectionnode.FieldCollectionID)
return u
}
// SetNodeID sets the "node_id" field.
func (u *CollectionNodeUpsert) SetNodeID(v xid.ID) *CollectionNodeUpsert {
u.Set(collectionnode.FieldNodeID, v)
return u
}
// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *CollectionNodeUpsert) UpdateNodeID() *CollectionNodeUpsert {
u.SetExcluded(collectionnode.FieldNodeID)
return u
}
// SetMembershipType sets the "membership_type" field.
func (u *CollectionNodeUpsert) SetMembershipType(v string) *CollectionNodeUpsert {
u.Set(collectionnode.FieldMembershipType, v)
return u
}
// UpdateMembershipType sets the "membership_type" field to the value that was provided on create.
func (u *CollectionNodeUpsert) UpdateMembershipType() *CollectionNodeUpsert {
u.SetExcluded(collectionnode.FieldMembershipType)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
// client.CollectionNode.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (u *CollectionNodeUpsertOne) UpdateNewValues() *CollectionNodeUpsertOne {
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.CreatedAt(); exists {
s.SetIgnore(collectionnode.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.CollectionNode.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CollectionNodeUpsertOne) Ignore() *CollectionNodeUpsertOne {
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 *CollectionNodeUpsertOne) DoNothing() *CollectionNodeUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CollectionNodeCreate.OnConflict
// documentation for more info.
func (u *CollectionNodeUpsertOne) Update(set func(*CollectionNodeUpsert)) *CollectionNodeUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CollectionNodeUpsert{UpdateSet: update})
}))
return u
}
// SetCollectionID sets the "collection_id" field.
func (u *CollectionNodeUpsertOne) SetCollectionID(v xid.ID) *CollectionNodeUpsertOne {
return u.Update(func(s *CollectionNodeUpsert) {
s.SetCollectionID(v)
})
}
// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *CollectionNodeUpsertOne) UpdateCollectionID() *CollectionNodeUpsertOne {
return u.Update(func(s *CollectionNodeUpsert) {
s.UpdateCollectionID()
})
}
// SetNodeID sets the "node_id" field.
func (u *CollectionNodeUpsertOne) SetNodeID(v xid.ID) *CollectionNodeUpsertOne {
return u.Update(func(s *CollectionNodeUpsert) {
s.SetNodeID(v)
})
}
// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *CollectionNodeUpsertOne) UpdateNodeID() *CollectionNodeUpsertOne {
return u.Update(func(s *CollectionNodeUpsert) {
s.UpdateNodeID()
})
}
// SetMembershipType sets the "membership_type" field.
func (u *CollectionNodeUpsertOne) SetMembershipType(v string) *CollectionNodeUpsertOne {
return u.Update(func(s *CollectionNodeUpsert) {
s.SetMembershipType(v)
})
}
// UpdateMembershipType sets the "membership_type" field to the value that was provided on create.
func (u *CollectionNodeUpsertOne) UpdateMembershipType() *CollectionNodeUpsertOne {
return u.Update(func(s *CollectionNodeUpsert) {
s.UpdateMembershipType()
})
}
// Exec executes the query.
func (u *CollectionNodeUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CollectionNodeCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CollectionNodeUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}
// CollectionNodeCreateBulk is the builder for creating many CollectionNode entities in bulk.
type CollectionNodeCreateBulk struct {
config
err error
builders []*CollectionNodeCreate
conflict []sql.ConflictOption
}
// Save creates the CollectionNode entities in the database.
func (_c *CollectionNodeCreateBulk) Save(ctx context.Context) ([]*CollectionNode, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*CollectionNode, 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.(*CollectionNodeMutation)
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.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 *CollectionNodeCreateBulk) SaveX(ctx context.Context) []*CollectionNode {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CollectionNodeCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CollectionNodeCreateBulk) 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.CollectionNode.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.CollectionNodeUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CollectionNodeCreateBulk) OnConflict(opts ...sql.ConflictOption) *CollectionNodeUpsertBulk {
_c.conflict = opts
return &CollectionNodeUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.CollectionNode.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CollectionNodeCreateBulk) OnConflictColumns(columns ...string) *CollectionNodeUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CollectionNodeUpsertBulk{
create: _c,
}
}
// CollectionNodeUpsertBulk is the builder for "upsert"-ing
// a bulk of CollectionNode nodes.
type CollectionNodeUpsertBulk struct {
create *CollectionNodeCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.CollectionNode.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (u *CollectionNodeUpsertBulk) UpdateNewValues() *CollectionNodeUpsertBulk {
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.CreatedAt(); exists {
s.SetIgnore(collectionnode.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.CollectionNode.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CollectionNodeUpsertBulk) Ignore() *CollectionNodeUpsertBulk {
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 *CollectionNodeUpsertBulk) DoNothing() *CollectionNodeUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CollectionNodeCreateBulk.OnConflict
// documentation for more info.
func (u *CollectionNodeUpsertBulk) Update(set func(*CollectionNodeUpsert)) *CollectionNodeUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CollectionNodeUpsert{UpdateSet: update})
}))
return u
}
// SetCollectionID sets the "collection_id" field.
func (u *CollectionNodeUpsertBulk) SetCollectionID(v xid.ID) *CollectionNodeUpsertBulk {
return u.Update(func(s *CollectionNodeUpsert) {
s.SetCollectionID(v)
})
}
// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *CollectionNodeUpsertBulk) UpdateCollectionID() *CollectionNodeUpsertBulk {
return u.Update(func(s *CollectionNodeUpsert) {
s.UpdateCollectionID()
})
}
// SetNodeID sets the "node_id" field.
func (u *CollectionNodeUpsertBulk) SetNodeID(v xid.ID) *CollectionNodeUpsertBulk {
return u.Update(func(s *CollectionNodeUpsert) {
s.SetNodeID(v)
})
}
// UpdateNodeID sets the "node_id" field to the value that was provided on create.
func (u *CollectionNodeUpsertBulk) UpdateNodeID() *CollectionNodeUpsertBulk {
return u.Update(func(s *CollectionNodeUpsert) {
s.UpdateNodeID()
})
}
// SetMembershipType sets the "membership_type" field.
func (u *CollectionNodeUpsertBulk) SetMembershipType(v string) *CollectionNodeUpsertBulk {
return u.Update(func(s *CollectionNodeUpsert) {
s.SetMembershipType(v)
})
}
// UpdateMembershipType sets the "membership_type" field to the value that was provided on create.
func (u *CollectionNodeUpsertBulk) UpdateMembershipType() *CollectionNodeUpsertBulk {
return u.Update(func(s *CollectionNodeUpsert) {
s.UpdateMembershipType()
})
}
// Exec executes the query.
func (u *CollectionNodeUpsertBulk) 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 CollectionNodeCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CollectionNodeCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CollectionNodeUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}