category_create.go•38.1 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/asset"
"github.com/Southclaws/storyden/internal/ent/category"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/rs/xid"
)
// CategoryCreate is the builder for creating a Category entity.
type CategoryCreate struct {
config
mutation *CategoryMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *CategoryCreate) SetCreatedAt(v time.Time) *CategoryCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableCreatedAt(v *time.Time) *CategoryCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetUpdatedAt sets the "updated_at" field.
func (_c *CategoryCreate) SetUpdatedAt(v time.Time) *CategoryCreate {
_c.mutation.SetUpdatedAt(v)
return _c
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableUpdatedAt(v *time.Time) *CategoryCreate {
if v != nil {
_c.SetUpdatedAt(*v)
}
return _c
}
// SetName sets the "name" field.
func (_c *CategoryCreate) SetName(v string) *CategoryCreate {
_c.mutation.SetName(v)
return _c
}
// SetSlug sets the "slug" field.
func (_c *CategoryCreate) SetSlug(v string) *CategoryCreate {
_c.mutation.SetSlug(v)
return _c
}
// SetDescription sets the "description" field.
func (_c *CategoryCreate) SetDescription(v string) *CategoryCreate {
_c.mutation.SetDescription(v)
return _c
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableDescription(v *string) *CategoryCreate {
if v != nil {
_c.SetDescription(*v)
}
return _c
}
// SetColour sets the "colour" field.
func (_c *CategoryCreate) SetColour(v string) *CategoryCreate {
_c.mutation.SetColour(v)
return _c
}
// SetNillableColour sets the "colour" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableColour(v *string) *CategoryCreate {
if v != nil {
_c.SetColour(*v)
}
return _c
}
// SetSort sets the "sort" field.
func (_c *CategoryCreate) SetSort(v int) *CategoryCreate {
_c.mutation.SetSort(v)
return _c
}
// SetNillableSort sets the "sort" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableSort(v *int) *CategoryCreate {
if v != nil {
_c.SetSort(*v)
}
return _c
}
// SetAdmin sets the "admin" field.
func (_c *CategoryCreate) SetAdmin(v bool) *CategoryCreate {
_c.mutation.SetAdmin(v)
return _c
}
// SetNillableAdmin sets the "admin" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableAdmin(v *bool) *CategoryCreate {
if v != nil {
_c.SetAdmin(*v)
}
return _c
}
// SetParentCategoryID sets the "parent_category_id" field.
func (_c *CategoryCreate) SetParentCategoryID(v xid.ID) *CategoryCreate {
_c.mutation.SetParentCategoryID(v)
return _c
}
// SetNillableParentCategoryID sets the "parent_category_id" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableParentCategoryID(v *xid.ID) *CategoryCreate {
if v != nil {
_c.SetParentCategoryID(*v)
}
return _c
}
// SetCoverImageAssetID sets the "cover_image_asset_id" field.
func (_c *CategoryCreate) SetCoverImageAssetID(v xid.ID) *CategoryCreate {
_c.mutation.SetCoverImageAssetID(v)
return _c
}
// SetNillableCoverImageAssetID sets the "cover_image_asset_id" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableCoverImageAssetID(v *xid.ID) *CategoryCreate {
if v != nil {
_c.SetCoverImageAssetID(*v)
}
return _c
}
// SetMetadata sets the "metadata" field.
func (_c *CategoryCreate) SetMetadata(v map[string]interface{}) *CategoryCreate {
_c.mutation.SetMetadata(v)
return _c
}
// SetID sets the "id" field.
func (_c *CategoryCreate) SetID(v xid.ID) *CategoryCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *CategoryCreate) SetNillableID(v *xid.ID) *CategoryCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// AddPostIDs adds the "posts" edge to the Post entity by IDs.
func (_c *CategoryCreate) AddPostIDs(ids ...xid.ID) *CategoryCreate {
_c.mutation.AddPostIDs(ids...)
return _c
}
// AddPosts adds the "posts" edges to the Post entity.
func (_c *CategoryCreate) AddPosts(v ...*Post) *CategoryCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddPostIDs(ids...)
}
// SetParentID sets the "parent" edge to the Category entity by ID.
func (_c *CategoryCreate) SetParentID(id xid.ID) *CategoryCreate {
_c.mutation.SetParentID(id)
return _c
}
// SetNillableParentID sets the "parent" edge to the Category entity by ID if the given value is not nil.
func (_c *CategoryCreate) SetNillableParentID(id *xid.ID) *CategoryCreate {
if id != nil {
_c = _c.SetParentID(*id)
}
return _c
}
// SetParent sets the "parent" edge to the Category entity.
func (_c *CategoryCreate) SetParent(v *Category) *CategoryCreate {
return _c.SetParentID(v.ID)
}
// AddChildIDs adds the "children" edge to the Category entity by IDs.
func (_c *CategoryCreate) AddChildIDs(ids ...xid.ID) *CategoryCreate {
_c.mutation.AddChildIDs(ids...)
return _c
}
// AddChildren adds the "children" edges to the Category entity.
func (_c *CategoryCreate) AddChildren(v ...*Category) *CategoryCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddChildIDs(ids...)
}
// SetCoverImageID sets the "cover_image" edge to the Asset entity by ID.
func (_c *CategoryCreate) SetCoverImageID(id xid.ID) *CategoryCreate {
_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 *CategoryCreate) SetNillableCoverImageID(id *xid.ID) *CategoryCreate {
if id != nil {
_c = _c.SetCoverImageID(*id)
}
return _c
}
// SetCoverImage sets the "cover_image" edge to the Asset entity.
func (_c *CategoryCreate) SetCoverImage(v *Asset) *CategoryCreate {
return _c.SetCoverImageID(v.ID)
}
// Mutation returns the CategoryMutation object of the builder.
func (_c *CategoryCreate) Mutation() *CategoryMutation {
return _c.mutation
}
// Save creates the Category in the database.
func (_c *CategoryCreate) Save(ctx context.Context) (*Category, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *CategoryCreate) SaveX(ctx context.Context) *Category {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CategoryCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CategoryCreate) 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 *CategoryCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := category.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
v := category.DefaultUpdatedAt()
_c.mutation.SetUpdatedAt(v)
}
if _, ok := _c.mutation.Description(); !ok {
v := category.DefaultDescription
_c.mutation.SetDescription(v)
}
if _, ok := _c.mutation.Colour(); !ok {
v := category.DefaultColour
_c.mutation.SetColour(v)
}
if _, ok := _c.mutation.Sort(); !ok {
v := category.DefaultSort
_c.mutation.SetSort(v)
}
if _, ok := _c.mutation.Admin(); !ok {
v := category.DefaultAdmin
_c.mutation.SetAdmin(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := category.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *CategoryCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Category.created_at"`)}
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Category.updated_at"`)}
}
if _, ok := _c.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Category.name"`)}
}
if _, ok := _c.mutation.Slug(); !ok {
return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "Category.slug"`)}
}
if _, ok := _c.mutation.Description(); !ok {
return &ValidationError{Name: "description", err: errors.New(`ent: missing required field "Category.description"`)}
}
if _, ok := _c.mutation.Colour(); !ok {
return &ValidationError{Name: "colour", err: errors.New(`ent: missing required field "Category.colour"`)}
}
if _, ok := _c.mutation.Sort(); !ok {
return &ValidationError{Name: "sort", err: errors.New(`ent: missing required field "Category.sort"`)}
}
if _, ok := _c.mutation.Admin(); !ok {
return &ValidationError{Name: "admin", err: errors.New(`ent: missing required field "Category.admin"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := category.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Category.id": %w`, err)}
}
}
return nil
}
func (_c *CategoryCreate) sqlSave(ctx context.Context) (*Category, 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 *CategoryCreate) createSpec() (*Category, *sqlgraph.CreateSpec) {
var (
_node = &Category{config: _c.config}
_spec = sqlgraph.NewCreateSpec(category.Table, sqlgraph.NewFieldSpec(category.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(category.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.UpdatedAt(); ok {
_spec.SetField(category.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := _c.mutation.Name(); ok {
_spec.SetField(category.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := _c.mutation.Slug(); ok {
_spec.SetField(category.FieldSlug, field.TypeString, value)
_node.Slug = value
}
if value, ok := _c.mutation.Description(); ok {
_spec.SetField(category.FieldDescription, field.TypeString, value)
_node.Description = value
}
if value, ok := _c.mutation.Colour(); ok {
_spec.SetField(category.FieldColour, field.TypeString, value)
_node.Colour = value
}
if value, ok := _c.mutation.Sort(); ok {
_spec.SetField(category.FieldSort, field.TypeInt, value)
_node.Sort = value
}
if value, ok := _c.mutation.Admin(); ok {
_spec.SetField(category.FieldAdmin, field.TypeBool, value)
_node.Admin = value
}
if value, ok := _c.mutation.Metadata(); ok {
_spec.SetField(category.FieldMetadata, field.TypeJSON, value)
_node.Metadata = value
}
if nodes := _c.mutation.PostsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: category.PostsTable,
Columns: []string{category.PostsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.ParentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: category.ParentTable,
Columns: []string{category.ParentColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.ParentCategoryID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.ChildrenIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: category.ChildrenTable,
Columns: []string{category.ChildrenColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.CoverImageIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: category.CoverImageTable,
Columns: []string{category.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.CoverImageAssetID = &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.Category.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.CategoryUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CategoryCreate) OnConflict(opts ...sql.ConflictOption) *CategoryUpsertOne {
_c.conflict = opts
return &CategoryUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Category.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CategoryCreate) OnConflictColumns(columns ...string) *CategoryUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CategoryUpsertOne{
create: _c,
}
}
type (
// CategoryUpsertOne is the builder for "upsert"-ing
// one Category node.
CategoryUpsertOne struct {
create *CategoryCreate
}
// CategoryUpsert is the "OnConflict" setter.
CategoryUpsert struct {
*sql.UpdateSet
}
)
// SetUpdatedAt sets the "updated_at" field.
func (u *CategoryUpsert) SetUpdatedAt(v time.Time) *CategoryUpsert {
u.Set(category.FieldUpdatedAt, v)
return u
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateUpdatedAt() *CategoryUpsert {
u.SetExcluded(category.FieldUpdatedAt)
return u
}
// SetName sets the "name" field.
func (u *CategoryUpsert) SetName(v string) *CategoryUpsert {
u.Set(category.FieldName, v)
return u
}
// UpdateName sets the "name" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateName() *CategoryUpsert {
u.SetExcluded(category.FieldName)
return u
}
// SetSlug sets the "slug" field.
func (u *CategoryUpsert) SetSlug(v string) *CategoryUpsert {
u.Set(category.FieldSlug, v)
return u
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateSlug() *CategoryUpsert {
u.SetExcluded(category.FieldSlug)
return u
}
// SetDescription sets the "description" field.
func (u *CategoryUpsert) SetDescription(v string) *CategoryUpsert {
u.Set(category.FieldDescription, v)
return u
}
// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateDescription() *CategoryUpsert {
u.SetExcluded(category.FieldDescription)
return u
}
// SetColour sets the "colour" field.
func (u *CategoryUpsert) SetColour(v string) *CategoryUpsert {
u.Set(category.FieldColour, v)
return u
}
// UpdateColour sets the "colour" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateColour() *CategoryUpsert {
u.SetExcluded(category.FieldColour)
return u
}
// SetSort sets the "sort" field.
func (u *CategoryUpsert) SetSort(v int) *CategoryUpsert {
u.Set(category.FieldSort, v)
return u
}
// UpdateSort sets the "sort" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateSort() *CategoryUpsert {
u.SetExcluded(category.FieldSort)
return u
}
// AddSort adds v to the "sort" field.
func (u *CategoryUpsert) AddSort(v int) *CategoryUpsert {
u.Add(category.FieldSort, v)
return u
}
// SetAdmin sets the "admin" field.
func (u *CategoryUpsert) SetAdmin(v bool) *CategoryUpsert {
u.Set(category.FieldAdmin, v)
return u
}
// UpdateAdmin sets the "admin" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateAdmin() *CategoryUpsert {
u.SetExcluded(category.FieldAdmin)
return u
}
// SetParentCategoryID sets the "parent_category_id" field.
func (u *CategoryUpsert) SetParentCategoryID(v xid.ID) *CategoryUpsert {
u.Set(category.FieldParentCategoryID, v)
return u
}
// UpdateParentCategoryID sets the "parent_category_id" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateParentCategoryID() *CategoryUpsert {
u.SetExcluded(category.FieldParentCategoryID)
return u
}
// ClearParentCategoryID clears the value of the "parent_category_id" field.
func (u *CategoryUpsert) ClearParentCategoryID() *CategoryUpsert {
u.SetNull(category.FieldParentCategoryID)
return u
}
// SetCoverImageAssetID sets the "cover_image_asset_id" field.
func (u *CategoryUpsert) SetCoverImageAssetID(v xid.ID) *CategoryUpsert {
u.Set(category.FieldCoverImageAssetID, v)
return u
}
// UpdateCoverImageAssetID sets the "cover_image_asset_id" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateCoverImageAssetID() *CategoryUpsert {
u.SetExcluded(category.FieldCoverImageAssetID)
return u
}
// ClearCoverImageAssetID clears the value of the "cover_image_asset_id" field.
func (u *CategoryUpsert) ClearCoverImageAssetID() *CategoryUpsert {
u.SetNull(category.FieldCoverImageAssetID)
return u
}
// SetMetadata sets the "metadata" field.
func (u *CategoryUpsert) SetMetadata(v map[string]interface{}) *CategoryUpsert {
u.Set(category.FieldMetadata, v)
return u
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *CategoryUpsert) UpdateMetadata() *CategoryUpsert {
u.SetExcluded(category.FieldMetadata)
return u
}
// ClearMetadata clears the value of the "metadata" field.
func (u *CategoryUpsert) ClearMetadata() *CategoryUpsert {
u.SetNull(category.FieldMetadata)
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.Category.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(category.FieldID)
// }),
// ).
// Exec(ctx)
func (u *CategoryUpsertOne) UpdateNewValues() *CategoryUpsertOne {
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(category.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(category.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Category.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CategoryUpsertOne) Ignore() *CategoryUpsertOne {
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 *CategoryUpsertOne) DoNothing() *CategoryUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CategoryCreate.OnConflict
// documentation for more info.
func (u *CategoryUpsertOne) Update(set func(*CategoryUpsert)) *CategoryUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CategoryUpsert{UpdateSet: update})
}))
return u
}
// SetUpdatedAt sets the "updated_at" field.
func (u *CategoryUpsertOne) SetUpdatedAt(v time.Time) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetUpdatedAt(v)
})
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateUpdatedAt() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateUpdatedAt()
})
}
// SetName sets the "name" field.
func (u *CategoryUpsertOne) SetName(v string) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetName(v)
})
}
// UpdateName sets the "name" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateName() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateName()
})
}
// SetSlug sets the "slug" field.
func (u *CategoryUpsertOne) SetSlug(v string) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetSlug(v)
})
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateSlug() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateSlug()
})
}
// SetDescription sets the "description" field.
func (u *CategoryUpsertOne) SetDescription(v string) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetDescription(v)
})
}
// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateDescription() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateDescription()
})
}
// SetColour sets the "colour" field.
func (u *CategoryUpsertOne) SetColour(v string) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetColour(v)
})
}
// UpdateColour sets the "colour" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateColour() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateColour()
})
}
// SetSort sets the "sort" field.
func (u *CategoryUpsertOne) SetSort(v int) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetSort(v)
})
}
// AddSort adds v to the "sort" field.
func (u *CategoryUpsertOne) AddSort(v int) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.AddSort(v)
})
}
// UpdateSort sets the "sort" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateSort() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateSort()
})
}
// SetAdmin sets the "admin" field.
func (u *CategoryUpsertOne) SetAdmin(v bool) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetAdmin(v)
})
}
// UpdateAdmin sets the "admin" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateAdmin() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateAdmin()
})
}
// SetParentCategoryID sets the "parent_category_id" field.
func (u *CategoryUpsertOne) SetParentCategoryID(v xid.ID) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetParentCategoryID(v)
})
}
// UpdateParentCategoryID sets the "parent_category_id" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateParentCategoryID() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateParentCategoryID()
})
}
// ClearParentCategoryID clears the value of the "parent_category_id" field.
func (u *CategoryUpsertOne) ClearParentCategoryID() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.ClearParentCategoryID()
})
}
// SetCoverImageAssetID sets the "cover_image_asset_id" field.
func (u *CategoryUpsertOne) SetCoverImageAssetID(v xid.ID) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetCoverImageAssetID(v)
})
}
// UpdateCoverImageAssetID sets the "cover_image_asset_id" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateCoverImageAssetID() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateCoverImageAssetID()
})
}
// ClearCoverImageAssetID clears the value of the "cover_image_asset_id" field.
func (u *CategoryUpsertOne) ClearCoverImageAssetID() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.ClearCoverImageAssetID()
})
}
// SetMetadata sets the "metadata" field.
func (u *CategoryUpsertOne) SetMetadata(v map[string]interface{}) *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.SetMetadata(v)
})
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *CategoryUpsertOne) UpdateMetadata() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.UpdateMetadata()
})
}
// ClearMetadata clears the value of the "metadata" field.
func (u *CategoryUpsertOne) ClearMetadata() *CategoryUpsertOne {
return u.Update(func(s *CategoryUpsert) {
s.ClearMetadata()
})
}
// Exec executes the query.
func (u *CategoryUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CategoryCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CategoryUpsertOne) 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 *CategoryUpsertOne) 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: CategoryUpsertOne.ID is not supported by MySQL driver. Use CategoryUpsertOne.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 *CategoryUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// CategoryCreateBulk is the builder for creating many Category entities in bulk.
type CategoryCreateBulk struct {
config
err error
builders []*CategoryCreate
conflict []sql.ConflictOption
}
// Save creates the Category entities in the database.
func (_c *CategoryCreateBulk) Save(ctx context.Context) ([]*Category, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Category, 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.(*CategoryMutation)
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 *CategoryCreateBulk) SaveX(ctx context.Context) []*Category {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CategoryCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CategoryCreateBulk) 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.Category.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.CategoryUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CategoryCreateBulk) OnConflict(opts ...sql.ConflictOption) *CategoryUpsertBulk {
_c.conflict = opts
return &CategoryUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Category.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CategoryCreateBulk) OnConflictColumns(columns ...string) *CategoryUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CategoryUpsertBulk{
create: _c,
}
}
// CategoryUpsertBulk is the builder for "upsert"-ing
// a bulk of Category nodes.
type CategoryUpsertBulk struct {
create *CategoryCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Category.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(category.FieldID)
// }),
// ).
// Exec(ctx)
func (u *CategoryUpsertBulk) UpdateNewValues() *CategoryUpsertBulk {
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(category.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(category.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Category.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CategoryUpsertBulk) Ignore() *CategoryUpsertBulk {
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 *CategoryUpsertBulk) DoNothing() *CategoryUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CategoryCreateBulk.OnConflict
// documentation for more info.
func (u *CategoryUpsertBulk) Update(set func(*CategoryUpsert)) *CategoryUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CategoryUpsert{UpdateSet: update})
}))
return u
}
// SetUpdatedAt sets the "updated_at" field.
func (u *CategoryUpsertBulk) SetUpdatedAt(v time.Time) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetUpdatedAt(v)
})
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateUpdatedAt() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateUpdatedAt()
})
}
// SetName sets the "name" field.
func (u *CategoryUpsertBulk) SetName(v string) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetName(v)
})
}
// UpdateName sets the "name" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateName() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateName()
})
}
// SetSlug sets the "slug" field.
func (u *CategoryUpsertBulk) SetSlug(v string) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetSlug(v)
})
}
// UpdateSlug sets the "slug" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateSlug() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateSlug()
})
}
// SetDescription sets the "description" field.
func (u *CategoryUpsertBulk) SetDescription(v string) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetDescription(v)
})
}
// UpdateDescription sets the "description" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateDescription() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateDescription()
})
}
// SetColour sets the "colour" field.
func (u *CategoryUpsertBulk) SetColour(v string) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetColour(v)
})
}
// UpdateColour sets the "colour" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateColour() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateColour()
})
}
// SetSort sets the "sort" field.
func (u *CategoryUpsertBulk) SetSort(v int) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetSort(v)
})
}
// AddSort adds v to the "sort" field.
func (u *CategoryUpsertBulk) AddSort(v int) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.AddSort(v)
})
}
// UpdateSort sets the "sort" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateSort() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateSort()
})
}
// SetAdmin sets the "admin" field.
func (u *CategoryUpsertBulk) SetAdmin(v bool) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetAdmin(v)
})
}
// UpdateAdmin sets the "admin" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateAdmin() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateAdmin()
})
}
// SetParentCategoryID sets the "parent_category_id" field.
func (u *CategoryUpsertBulk) SetParentCategoryID(v xid.ID) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetParentCategoryID(v)
})
}
// UpdateParentCategoryID sets the "parent_category_id" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateParentCategoryID() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateParentCategoryID()
})
}
// ClearParentCategoryID clears the value of the "parent_category_id" field.
func (u *CategoryUpsertBulk) ClearParentCategoryID() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.ClearParentCategoryID()
})
}
// SetCoverImageAssetID sets the "cover_image_asset_id" field.
func (u *CategoryUpsertBulk) SetCoverImageAssetID(v xid.ID) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetCoverImageAssetID(v)
})
}
// UpdateCoverImageAssetID sets the "cover_image_asset_id" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateCoverImageAssetID() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateCoverImageAssetID()
})
}
// ClearCoverImageAssetID clears the value of the "cover_image_asset_id" field.
func (u *CategoryUpsertBulk) ClearCoverImageAssetID() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.ClearCoverImageAssetID()
})
}
// SetMetadata sets the "metadata" field.
func (u *CategoryUpsertBulk) SetMetadata(v map[string]interface{}) *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.SetMetadata(v)
})
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *CategoryUpsertBulk) UpdateMetadata() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.UpdateMetadata()
})
}
// ClearMetadata clears the value of the "metadata" field.
func (u *CategoryUpsertBulk) ClearMetadata() *CategoryUpsertBulk {
return u.Update(func(s *CategoryUpsert) {
s.ClearMetadata()
})
}
// Exec executes the query.
func (u *CategoryUpsertBulk) 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 CategoryCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CategoryCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CategoryUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}