collection_create.go•33.5 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/account"
"github.com/Southclaws/storyden/internal/ent/asset"
"github.com/Southclaws/storyden/internal/ent/collection"
"github.com/Southclaws/storyden/internal/ent/node"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/rs/xid"
)
// CollectionCreate is the builder for creating a Collection entity.
type CollectionCreate struct {
config
mutation *CollectionMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *CollectionCreate) SetCreatedAt(v time.Time) *CollectionCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *CollectionCreate) SetNillableCreatedAt(v *time.Time) *CollectionCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetUpdatedAt sets the "updated_at" field.
func (_c *CollectionCreate) SetUpdatedAt(v time.Time) *CollectionCreate {
_c.mutation.SetUpdatedAt(v)
return _c
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *CollectionCreate) SetNillableUpdatedAt(v *time.Time) *CollectionCreate {
if v != nil {
_c.SetUpdatedAt(*v)
}
return _c
}
// SetIndexedAt sets the "indexed_at" field.
func (_c *CollectionCreate) SetIndexedAt(v time.Time) *CollectionCreate {
_c.mutation.SetIndexedAt(v)
return _c
}
// SetNillableIndexedAt sets the "indexed_at" field if the given value is not nil.
func (_c *CollectionCreate) SetNillableIndexedAt(v *time.Time) *CollectionCreate {
if v != nil {
_c.SetIndexedAt(*v)
}
return _c
}
// SetName sets the "name" field.
func (_c *CollectionCreate) SetName(v string) *CollectionCreate {
_c.mutation.SetName(v)
return _c
}
// SetSlug sets the "slug" field.
func (_c *CollectionCreate) SetSlug(v string) *CollectionCreate {
_c.mutation.SetSlug(v)
return _c
}
// SetDescription sets the "description" field.
func (_c *CollectionCreate) SetDescription(v string) *CollectionCreate {
_c.mutation.SetDescription(v)
return _c
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (_c *CollectionCreate) SetNillableDescription(v *string) *CollectionCreate {
if v != nil {
_c.SetDescription(*v)
}
return _c
}
// SetCoverAssetID sets the "cover_asset_id" field.
func (_c *CollectionCreate) SetCoverAssetID(v xid.ID) *CollectionCreate {
_c.mutation.SetCoverAssetID(v)
return _c
}
// SetNillableCoverAssetID sets the "cover_asset_id" field if the given value is not nil.
func (_c *CollectionCreate) SetNillableCoverAssetID(v *xid.ID) *CollectionCreate {
if v != nil {
_c.SetCoverAssetID(*v)
}
return _c
}
// SetVisibility sets the "visibility" field.
func (_c *CollectionCreate) SetVisibility(v collection.Visibility) *CollectionCreate {
_c.mutation.SetVisibility(v)
return _c
}
// SetNillableVisibility sets the "visibility" field if the given value is not nil.
func (_c *CollectionCreate) SetNillableVisibility(v *collection.Visibility) *CollectionCreate {
if v != nil {
_c.SetVisibility(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *CollectionCreate) SetID(v xid.ID) *CollectionCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *CollectionCreate) SetNillableID(v *xid.ID) *CollectionCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetOwnerID sets the "owner" edge to the Account entity by ID.
func (_c *CollectionCreate) SetOwnerID(id xid.ID) *CollectionCreate {
_c.mutation.SetOwnerID(id)
return _c
}
// SetNillableOwnerID sets the "owner" edge to the Account entity by ID if the given value is not nil.
func (_c *CollectionCreate) SetNillableOwnerID(id *xid.ID) *CollectionCreate {
if id != nil {
_c = _c.SetOwnerID(*id)
}
return _c
}
// SetOwner sets the "owner" edge to the Account entity.
func (_c *CollectionCreate) SetOwner(v *Account) *CollectionCreate {
return _c.SetOwnerID(v.ID)
}
// SetCoverImageID sets the "cover_image" edge to the Asset entity by ID.
func (_c *CollectionCreate) SetCoverImageID(id xid.ID) *CollectionCreate {
_c.mutation.SetCoverImageID(id)
return _c
}
// SetNillableCoverImageID sets the "cover_image" edge to the Asset entity by ID if the given value is not nil.
func (_c *CollectionCreate) SetNillableCoverImageID(id *xid.ID) *CollectionCreate {
if id != nil {
_c = _c.SetCoverImageID(*id)
}
return _c
}
// SetCoverImage sets the "cover_image" edge to the Asset entity.
func (_c *CollectionCreate) SetCoverImage(v *Asset) *CollectionCreate {
return _c.SetCoverImageID(v.ID)
}
// AddPostIDs adds the "posts" edge to the Post entity by IDs.
func (_c *CollectionCreate) AddPostIDs(ids ...xid.ID) *CollectionCreate {
_c.mutation.AddPostIDs(ids...)
return _c
}
// AddPosts adds the "posts" edges to the Post entity.
func (_c *CollectionCreate) AddPosts(v ...*Post) *CollectionCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddPostIDs(ids...)
}
// AddNodeIDs adds the "nodes" edge to the Node entity by IDs.
func (_c *CollectionCreate) AddNodeIDs(ids ...xid.ID) *CollectionCreate {
_c.mutation.AddNodeIDs(ids...)
return _c
}
// AddNodes adds the "nodes" edges to the Node entity.
func (_c *CollectionCreate) AddNodes(v ...*Node) *CollectionCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddNodeIDs(ids...)
}
// Mutation returns the CollectionMutation object of the builder.
func (_c *CollectionCreate) Mutation() *CollectionMutation {
return _c.mutation
}
// Save creates the Collection in the database.
func (_c *CollectionCreate) Save(ctx context.Context) (*Collection, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *CollectionCreate) SaveX(ctx context.Context) *Collection {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CollectionCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CollectionCreate) 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 *CollectionCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := collection.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
v := collection.DefaultUpdatedAt()
_c.mutation.SetUpdatedAt(v)
}
if _, ok := _c.mutation.Visibility(); !ok {
v := collection.DefaultVisibility
_c.mutation.SetVisibility(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := collection.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *CollectionCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Collection.created_at"`)}
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Collection.updated_at"`)}
}
if _, ok := _c.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Collection.name"`)}
}
if _, ok := _c.mutation.Slug(); !ok {
return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "Collection.slug"`)}
}
if _, ok := _c.mutation.Visibility(); !ok {
return &ValidationError{Name: "visibility", err: errors.New(`ent: missing required field "Collection.visibility"`)}
}
if v, ok := _c.mutation.Visibility(); ok {
if err := collection.VisibilityValidator(v); err != nil {
return &ValidationError{Name: "visibility", err: fmt.Errorf(`ent: validator failed for field "Collection.visibility": %w`, err)}
}
}
if v, ok := _c.mutation.ID(); ok {
if err := collection.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Collection.id": %w`, err)}
}
}
return nil
}
func (_c *CollectionCreate) sqlSave(ctx context.Context) (*Collection, 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 *CollectionCreate) createSpec() (*Collection, *sqlgraph.CreateSpec) {
var (
_node = &Collection{config: _c.config}
_spec = sqlgraph.NewCreateSpec(collection.Table, sqlgraph.NewFieldSpec(collection.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(collection.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.UpdatedAt(); ok {
_spec.SetField(collection.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := _c.mutation.IndexedAt(); ok {
_spec.SetField(collection.FieldIndexedAt, field.TypeTime, value)
_node.IndexedAt = &value
}
if value, ok := _c.mutation.Name(); ok {
_spec.SetField(collection.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := _c.mutation.Slug(); ok {
_spec.SetField(collection.FieldSlug, field.TypeString, value)
_node.Slug = value
}
if value, ok := _c.mutation.Description(); ok {
_spec.SetField(collection.FieldDescription, field.TypeString, value)
_node.Description = &value
}
if value, ok := _c.mutation.Visibility(); ok {
_spec.SetField(collection.FieldVisibility, field.TypeEnum, value)
_node.Visibility = value
}
if nodes := _c.mutation.OwnerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: collection.OwnerTable,
Columns: []string{collection.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.account_collections = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.CoverImageIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collection.CoverImageTable,
Columns: []string{collection.CoverImageColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(asset.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.CoverAssetID = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.PostsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.PostsTable,
Columns: collection.PostsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionPostCreate{config: _c.config, mutation: newCollectionPostMutation(_c.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.NodesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.NodesTable,
Columns: collection.NodesPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionNodeCreate{config: _c.config, mutation: newCollectionNodeMutation(_c.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_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.Collection.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.CollectionUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CollectionCreate) OnConflict(opts ...sql.ConflictOption) *CollectionUpsertOne {
_c.conflict = opts
return &CollectionUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Collection.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CollectionCreate) OnConflictColumns(columns ...string) *CollectionUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CollectionUpsertOne{
create: _c,
}
}
type (
// CollectionUpsertOne is the builder for "upsert"-ing
// one Collection node.
CollectionUpsertOne struct {
create *CollectionCreate
}
// CollectionUpsert is the "OnConflict" setter.
CollectionUpsert struct {
*sql.UpdateSet
}
)
// SetUpdatedAt sets the "updated_at" field.
func (u *CollectionUpsert) SetUpdatedAt(v time.Time) *CollectionUpsert {
u.Set(collection.FieldUpdatedAt, v)
return u
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *CollectionUpsert) UpdateUpdatedAt() *CollectionUpsert {
u.SetExcluded(collection.FieldUpdatedAt)
return u
}
// SetIndexedAt sets the "indexed_at" field.
func (u *CollectionUpsert) SetIndexedAt(v time.Time) *CollectionUpsert {
u.Set(collection.FieldIndexedAt, v)
return u
}
// UpdateIndexedAt sets the "indexed_at" field to the value that was provided on create.
func (u *CollectionUpsert) UpdateIndexedAt() *CollectionUpsert {
u.SetExcluded(collection.FieldIndexedAt)
return u
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (u *CollectionUpsert) ClearIndexedAt() *CollectionUpsert {
u.SetNull(collection.FieldIndexedAt)
return u
}
// SetName sets the "name" field.
func (u *CollectionUpsert) SetName(v string) *CollectionUpsert {
u.Set(collection.FieldName, v)
return u
}
// UpdateName sets the "name" field to the value that was provided on create.
func (u *CollectionUpsert) UpdateName() *CollectionUpsert {
u.SetExcluded(collection.FieldName)
return u
}
// SetSlug sets the "slug" field.
func (u *CollectionUpsert) SetSlug(v string) *CollectionUpsert {
u.Set(collection.FieldSlug, v)
return u
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *CollectionUpsert) UpdateSlug() *CollectionUpsert {
u.SetExcluded(collection.FieldSlug)
return u
}
// SetDescription sets the "description" field.
func (u *CollectionUpsert) SetDescription(v string) *CollectionUpsert {
u.Set(collection.FieldDescription, v)
return u
}
// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *CollectionUpsert) UpdateDescription() *CollectionUpsert {
u.SetExcluded(collection.FieldDescription)
return u
}
// ClearDescription clears the value of the "description" field.
func (u *CollectionUpsert) ClearDescription() *CollectionUpsert {
u.SetNull(collection.FieldDescription)
return u
}
// SetCoverAssetID sets the "cover_asset_id" field.
func (u *CollectionUpsert) SetCoverAssetID(v xid.ID) *CollectionUpsert {
u.Set(collection.FieldCoverAssetID, v)
return u
}
// UpdateCoverAssetID sets the "cover_asset_id" field to the value that was provided on create.
func (u *CollectionUpsert) UpdateCoverAssetID() *CollectionUpsert {
u.SetExcluded(collection.FieldCoverAssetID)
return u
}
// ClearCoverAssetID clears the value of the "cover_asset_id" field.
func (u *CollectionUpsert) ClearCoverAssetID() *CollectionUpsert {
u.SetNull(collection.FieldCoverAssetID)
return u
}
// SetVisibility sets the "visibility" field.
func (u *CollectionUpsert) SetVisibility(v collection.Visibility) *CollectionUpsert {
u.Set(collection.FieldVisibility, v)
return u
}
// UpdateVisibility sets the "visibility" field to the value that was provided on create.
func (u *CollectionUpsert) UpdateVisibility() *CollectionUpsert {
u.SetExcluded(collection.FieldVisibility)
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.Collection.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(collection.FieldID)
// }),
// ).
// Exec(ctx)
func (u *CollectionUpsertOne) UpdateNewValues() *CollectionUpsertOne {
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(collection.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(collection.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Collection.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CollectionUpsertOne) Ignore() *CollectionUpsertOne {
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 *CollectionUpsertOne) DoNothing() *CollectionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CollectionCreate.OnConflict
// documentation for more info.
func (u *CollectionUpsertOne) Update(set func(*CollectionUpsert)) *CollectionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CollectionUpsert{UpdateSet: update})
}))
return u
}
// SetUpdatedAt sets the "updated_at" field.
func (u *CollectionUpsertOne) SetUpdatedAt(v time.Time) *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.SetUpdatedAt(v)
})
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *CollectionUpsertOne) UpdateUpdatedAt() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.UpdateUpdatedAt()
})
}
// SetIndexedAt sets the "indexed_at" field.
func (u *CollectionUpsertOne) SetIndexedAt(v time.Time) *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.SetIndexedAt(v)
})
}
// UpdateIndexedAt sets the "indexed_at" field to the value that was provided on create.
func (u *CollectionUpsertOne) UpdateIndexedAt() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.UpdateIndexedAt()
})
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (u *CollectionUpsertOne) ClearIndexedAt() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.ClearIndexedAt()
})
}
// SetName sets the "name" field.
func (u *CollectionUpsertOne) SetName(v string) *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.SetName(v)
})
}
// UpdateName sets the "name" field to the value that was provided on create.
func (u *CollectionUpsertOne) UpdateName() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.UpdateName()
})
}
// SetSlug sets the "slug" field.
func (u *CollectionUpsertOne) SetSlug(v string) *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.SetSlug(v)
})
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *CollectionUpsertOne) UpdateSlug() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.UpdateSlug()
})
}
// SetDescription sets the "description" field.
func (u *CollectionUpsertOne) SetDescription(v string) *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.SetDescription(v)
})
}
// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *CollectionUpsertOne) UpdateDescription() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.UpdateDescription()
})
}
// ClearDescription clears the value of the "description" field.
func (u *CollectionUpsertOne) ClearDescription() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.ClearDescription()
})
}
// SetCoverAssetID sets the "cover_asset_id" field.
func (u *CollectionUpsertOne) SetCoverAssetID(v xid.ID) *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.SetCoverAssetID(v)
})
}
// UpdateCoverAssetID sets the "cover_asset_id" field to the value that was provided on create.
func (u *CollectionUpsertOne) UpdateCoverAssetID() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.UpdateCoverAssetID()
})
}
// ClearCoverAssetID clears the value of the "cover_asset_id" field.
func (u *CollectionUpsertOne) ClearCoverAssetID() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.ClearCoverAssetID()
})
}
// SetVisibility sets the "visibility" field.
func (u *CollectionUpsertOne) SetVisibility(v collection.Visibility) *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.SetVisibility(v)
})
}
// UpdateVisibility sets the "visibility" field to the value that was provided on create.
func (u *CollectionUpsertOne) UpdateVisibility() *CollectionUpsertOne {
return u.Update(func(s *CollectionUpsert) {
s.UpdateVisibility()
})
}
// Exec executes the query.
func (u *CollectionUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CollectionCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CollectionUpsertOne) 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 *CollectionUpsertOne) 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: CollectionUpsertOne.ID is not supported by MySQL driver. Use CollectionUpsertOne.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 *CollectionUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// CollectionCreateBulk is the builder for creating many Collection entities in bulk.
type CollectionCreateBulk struct {
config
err error
builders []*CollectionCreate
conflict []sql.ConflictOption
}
// Save creates the Collection entities in the database.
func (_c *CollectionCreateBulk) Save(ctx context.Context) ([]*Collection, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Collection, 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.(*CollectionMutation)
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 *CollectionCreateBulk) SaveX(ctx context.Context) []*Collection {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CollectionCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CollectionCreateBulk) 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.Collection.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.CollectionUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CollectionCreateBulk) OnConflict(opts ...sql.ConflictOption) *CollectionUpsertBulk {
_c.conflict = opts
return &CollectionUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Collection.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CollectionCreateBulk) OnConflictColumns(columns ...string) *CollectionUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CollectionUpsertBulk{
create: _c,
}
}
// CollectionUpsertBulk is the builder for "upsert"-ing
// a bulk of Collection nodes.
type CollectionUpsertBulk struct {
create *CollectionCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Collection.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(collection.FieldID)
// }),
// ).
// Exec(ctx)
func (u *CollectionUpsertBulk) UpdateNewValues() *CollectionUpsertBulk {
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(collection.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(collection.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Collection.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CollectionUpsertBulk) Ignore() *CollectionUpsertBulk {
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 *CollectionUpsertBulk) DoNothing() *CollectionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CollectionCreateBulk.OnConflict
// documentation for more info.
func (u *CollectionUpsertBulk) Update(set func(*CollectionUpsert)) *CollectionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CollectionUpsert{UpdateSet: update})
}))
return u
}
// SetUpdatedAt sets the "updated_at" field.
func (u *CollectionUpsertBulk) SetUpdatedAt(v time.Time) *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.SetUpdatedAt(v)
})
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *CollectionUpsertBulk) UpdateUpdatedAt() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.UpdateUpdatedAt()
})
}
// SetIndexedAt sets the "indexed_at" field.
func (u *CollectionUpsertBulk) SetIndexedAt(v time.Time) *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.SetIndexedAt(v)
})
}
// UpdateIndexedAt sets the "indexed_at" field to the value that was provided on create.
func (u *CollectionUpsertBulk) UpdateIndexedAt() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.UpdateIndexedAt()
})
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (u *CollectionUpsertBulk) ClearIndexedAt() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.ClearIndexedAt()
})
}
// SetName sets the "name" field.
func (u *CollectionUpsertBulk) SetName(v string) *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.SetName(v)
})
}
// UpdateName sets the "name" field to the value that was provided on create.
func (u *CollectionUpsertBulk) UpdateName() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.UpdateName()
})
}
// SetSlug sets the "slug" field.
func (u *CollectionUpsertBulk) SetSlug(v string) *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.SetSlug(v)
})
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *CollectionUpsertBulk) UpdateSlug() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.UpdateSlug()
})
}
// SetDescription sets the "description" field.
func (u *CollectionUpsertBulk) SetDescription(v string) *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.SetDescription(v)
})
}
// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *CollectionUpsertBulk) UpdateDescription() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.UpdateDescription()
})
}
// ClearDescription clears the value of the "description" field.
func (u *CollectionUpsertBulk) ClearDescription() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.ClearDescription()
})
}
// SetCoverAssetID sets the "cover_asset_id" field.
func (u *CollectionUpsertBulk) SetCoverAssetID(v xid.ID) *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.SetCoverAssetID(v)
})
}
// UpdateCoverAssetID sets the "cover_asset_id" field to the value that was provided on create.
func (u *CollectionUpsertBulk) UpdateCoverAssetID() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.UpdateCoverAssetID()
})
}
// ClearCoverAssetID clears the value of the "cover_asset_id" field.
func (u *CollectionUpsertBulk) ClearCoverAssetID() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.ClearCoverAssetID()
})
}
// SetVisibility sets the "visibility" field.
func (u *CollectionUpsertBulk) SetVisibility(v collection.Visibility) *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.SetVisibility(v)
})
}
// UpdateVisibility sets the "visibility" field to the value that was provided on create.
func (u *CollectionUpsertBulk) UpdateVisibility() *CollectionUpsertBulk {
return u.Update(func(s *CollectionUpsert) {
s.UpdateVisibility()
})
}
// Exec executes the query.
func (u *CollectionUpsertBulk) 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 CollectionCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CollectionCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CollectionUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}