asset_create.go•32.8 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/event"
"github.com/Southclaws/storyden/internal/ent/link"
"github.com/Southclaws/storyden/internal/ent/node"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/rs/xid"
)
// AssetCreate is the builder for creating a Asset entity.
type AssetCreate struct {
config
mutation *AssetMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *AssetCreate) SetCreatedAt(v time.Time) *AssetCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *AssetCreate) SetNillableCreatedAt(v *time.Time) *AssetCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetUpdatedAt sets the "updated_at" field.
func (_c *AssetCreate) SetUpdatedAt(v time.Time) *AssetCreate {
_c.mutation.SetUpdatedAt(v)
return _c
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *AssetCreate) SetNillableUpdatedAt(v *time.Time) *AssetCreate {
if v != nil {
_c.SetUpdatedAt(*v)
}
return _c
}
// SetFilename sets the "filename" field.
func (_c *AssetCreate) SetFilename(v string) *AssetCreate {
_c.mutation.SetFilename(v)
return _c
}
// SetSize sets the "size" field.
func (_c *AssetCreate) SetSize(v int) *AssetCreate {
_c.mutation.SetSize(v)
return _c
}
// SetMimeType sets the "mime_type" field.
func (_c *AssetCreate) SetMimeType(v string) *AssetCreate {
_c.mutation.SetMimeType(v)
return _c
}
// SetMetadata sets the "metadata" field.
func (_c *AssetCreate) SetMetadata(v map[string]interface{}) *AssetCreate {
_c.mutation.SetMetadata(v)
return _c
}
// SetAccountID sets the "account_id" field.
func (_c *AssetCreate) SetAccountID(v xid.ID) *AssetCreate {
_c.mutation.SetAccountID(v)
return _c
}
// SetParentAssetID sets the "parent_asset_id" field.
func (_c *AssetCreate) SetParentAssetID(v xid.ID) *AssetCreate {
_c.mutation.SetParentAssetID(v)
return _c
}
// SetNillableParentAssetID sets the "parent_asset_id" field if the given value is not nil.
func (_c *AssetCreate) SetNillableParentAssetID(v *xid.ID) *AssetCreate {
if v != nil {
_c.SetParentAssetID(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *AssetCreate) SetID(v xid.ID) *AssetCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *AssetCreate) SetNillableID(v *xid.ID) *AssetCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// AddPostIDs adds the "posts" edge to the Post entity by IDs.
func (_c *AssetCreate) AddPostIDs(ids ...xid.ID) *AssetCreate {
_c.mutation.AddPostIDs(ids...)
return _c
}
// AddPosts adds the "posts" edges to the Post entity.
func (_c *AssetCreate) AddPosts(v ...*Post) *AssetCreate {
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 *AssetCreate) AddNodeIDs(ids ...xid.ID) *AssetCreate {
_c.mutation.AddNodeIDs(ids...)
return _c
}
// AddNodes adds the "nodes" edges to the Node entity.
func (_c *AssetCreate) AddNodes(v ...*Node) *AssetCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddNodeIDs(ids...)
}
// AddLinkIDs adds the "links" edge to the Link entity by IDs.
func (_c *AssetCreate) AddLinkIDs(ids ...xid.ID) *AssetCreate {
_c.mutation.AddLinkIDs(ids...)
return _c
}
// AddLinks adds the "links" edges to the Link entity.
func (_c *AssetCreate) AddLinks(v ...*Link) *AssetCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddLinkIDs(ids...)
}
// SetOwnerID sets the "owner" edge to the Account entity by ID.
func (_c *AssetCreate) SetOwnerID(id xid.ID) *AssetCreate {
_c.mutation.SetOwnerID(id)
return _c
}
// SetOwner sets the "owner" edge to the Account entity.
func (_c *AssetCreate) SetOwner(v *Account) *AssetCreate {
return _c.SetOwnerID(v.ID)
}
// SetParentID sets the "parent" edge to the Asset entity by ID.
func (_c *AssetCreate) SetParentID(id xid.ID) *AssetCreate {
_c.mutation.SetParentID(id)
return _c
}
// SetNillableParentID sets the "parent" edge to the Asset entity by ID if the given value is not nil.
func (_c *AssetCreate) SetNillableParentID(id *xid.ID) *AssetCreate {
if id != nil {
_c = _c.SetParentID(*id)
}
return _c
}
// SetParent sets the "parent" edge to the Asset entity.
func (_c *AssetCreate) SetParent(v *Asset) *AssetCreate {
return _c.SetParentID(v.ID)
}
// AddAssetIDs adds the "assets" edge to the Asset entity by IDs.
func (_c *AssetCreate) AddAssetIDs(ids ...xid.ID) *AssetCreate {
_c.mutation.AddAssetIDs(ids...)
return _c
}
// AddAssets adds the "assets" edges to the Asset entity.
func (_c *AssetCreate) AddAssets(v ...*Asset) *AssetCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddAssetIDs(ids...)
}
// AddEventIDs adds the "event" edge to the Event entity by IDs.
func (_c *AssetCreate) AddEventIDs(ids ...xid.ID) *AssetCreate {
_c.mutation.AddEventIDs(ids...)
return _c
}
// AddEvent adds the "event" edges to the Event entity.
func (_c *AssetCreate) AddEvent(v ...*Event) *AssetCreate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddEventIDs(ids...)
}
// Mutation returns the AssetMutation object of the builder.
func (_c *AssetCreate) Mutation() *AssetMutation {
return _c.mutation
}
// Save creates the Asset in the database.
func (_c *AssetCreate) Save(ctx context.Context) (*Asset, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *AssetCreate) SaveX(ctx context.Context) *Asset {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *AssetCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *AssetCreate) 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 *AssetCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := asset.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
v := asset.DefaultUpdatedAt()
_c.mutation.SetUpdatedAt(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := asset.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *AssetCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Asset.created_at"`)}
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Asset.updated_at"`)}
}
if _, ok := _c.mutation.Filename(); !ok {
return &ValidationError{Name: "filename", err: errors.New(`ent: missing required field "Asset.filename"`)}
}
if _, ok := _c.mutation.Size(); !ok {
return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "Asset.size"`)}
}
if _, ok := _c.mutation.MimeType(); !ok {
return &ValidationError{Name: "mime_type", err: errors.New(`ent: missing required field "Asset.mime_type"`)}
}
if _, ok := _c.mutation.AccountID(); !ok {
return &ValidationError{Name: "account_id", err: errors.New(`ent: missing required field "Asset.account_id"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := asset.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Asset.id": %w`, err)}
}
}
if len(_c.mutation.OwnerIDs()) == 0 {
return &ValidationError{Name: "owner", err: errors.New(`ent: missing required edge "Asset.owner"`)}
}
return nil
}
func (_c *AssetCreate) sqlSave(ctx context.Context) (*Asset, 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 *AssetCreate) createSpec() (*Asset, *sqlgraph.CreateSpec) {
var (
_node = &Asset{config: _c.config}
_spec = sqlgraph.NewCreateSpec(asset.Table, sqlgraph.NewFieldSpec(asset.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(asset.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.UpdatedAt(); ok {
_spec.SetField(asset.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := _c.mutation.Filename(); ok {
_spec.SetField(asset.FieldFilename, field.TypeString, value)
_node.Filename = value
}
if value, ok := _c.mutation.Size(); ok {
_spec.SetField(asset.FieldSize, field.TypeInt, value)
_node.Size = value
}
if value, ok := _c.mutation.MimeType(); ok {
_spec.SetField(asset.FieldMimeType, field.TypeString, value)
_node.MimeType = value
}
if value, ok := _c.mutation.Metadata(); ok {
_spec.SetField(asset.FieldMetadata, field.TypeJSON, value)
_node.Metadata = value
}
if nodes := _c.mutation.PostsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: asset.PostsTable,
Columns: asset.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)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.NodesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: asset.NodesTable,
Columns: asset.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)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.LinksIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: asset.LinksTable,
Columns: asset.LinksPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(link.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.OwnerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: asset.OwnerTable,
Columns: []string{asset.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.AccountID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.ParentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: asset.ParentTable,
Columns: []string{asset.ParentColumn},
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.ParentAssetID = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.AssetsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: asset.AssetsTable,
Columns: []string{asset.AssetsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(asset.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.EventIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: asset.EventTable,
Columns: []string{asset.EventColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(event.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_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.Asset.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.AssetUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *AssetCreate) OnConflict(opts ...sql.ConflictOption) *AssetUpsertOne {
_c.conflict = opts
return &AssetUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Asset.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *AssetCreate) OnConflictColumns(columns ...string) *AssetUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &AssetUpsertOne{
create: _c,
}
}
type (
// AssetUpsertOne is the builder for "upsert"-ing
// one Asset node.
AssetUpsertOne struct {
create *AssetCreate
}
// AssetUpsert is the "OnConflict" setter.
AssetUpsert struct {
*sql.UpdateSet
}
)
// SetUpdatedAt sets the "updated_at" field.
func (u *AssetUpsert) SetUpdatedAt(v time.Time) *AssetUpsert {
u.Set(asset.FieldUpdatedAt, v)
return u
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *AssetUpsert) UpdateUpdatedAt() *AssetUpsert {
u.SetExcluded(asset.FieldUpdatedAt)
return u
}
// SetFilename sets the "filename" field.
func (u *AssetUpsert) SetFilename(v string) *AssetUpsert {
u.Set(asset.FieldFilename, v)
return u
}
// UpdateFilename sets the "filename" field to the value that was provided on create.
func (u *AssetUpsert) UpdateFilename() *AssetUpsert {
u.SetExcluded(asset.FieldFilename)
return u
}
// SetSize sets the "size" field.
func (u *AssetUpsert) SetSize(v int) *AssetUpsert {
u.Set(asset.FieldSize, v)
return u
}
// UpdateSize sets the "size" field to the value that was provided on create.
func (u *AssetUpsert) UpdateSize() *AssetUpsert {
u.SetExcluded(asset.FieldSize)
return u
}
// AddSize adds v to the "size" field.
func (u *AssetUpsert) AddSize(v int) *AssetUpsert {
u.Add(asset.FieldSize, v)
return u
}
// SetMimeType sets the "mime_type" field.
func (u *AssetUpsert) SetMimeType(v string) *AssetUpsert {
u.Set(asset.FieldMimeType, v)
return u
}
// UpdateMimeType sets the "mime_type" field to the value that was provided on create.
func (u *AssetUpsert) UpdateMimeType() *AssetUpsert {
u.SetExcluded(asset.FieldMimeType)
return u
}
// SetMetadata sets the "metadata" field.
func (u *AssetUpsert) SetMetadata(v map[string]interface{}) *AssetUpsert {
u.Set(asset.FieldMetadata, v)
return u
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *AssetUpsert) UpdateMetadata() *AssetUpsert {
u.SetExcluded(asset.FieldMetadata)
return u
}
// ClearMetadata clears the value of the "metadata" field.
func (u *AssetUpsert) ClearMetadata() *AssetUpsert {
u.SetNull(asset.FieldMetadata)
return u
}
// SetAccountID sets the "account_id" field.
func (u *AssetUpsert) SetAccountID(v xid.ID) *AssetUpsert {
u.Set(asset.FieldAccountID, v)
return u
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *AssetUpsert) UpdateAccountID() *AssetUpsert {
u.SetExcluded(asset.FieldAccountID)
return u
}
// SetParentAssetID sets the "parent_asset_id" field.
func (u *AssetUpsert) SetParentAssetID(v xid.ID) *AssetUpsert {
u.Set(asset.FieldParentAssetID, v)
return u
}
// UpdateParentAssetID sets the "parent_asset_id" field to the value that was provided on create.
func (u *AssetUpsert) UpdateParentAssetID() *AssetUpsert {
u.SetExcluded(asset.FieldParentAssetID)
return u
}
// ClearParentAssetID clears the value of the "parent_asset_id" field.
func (u *AssetUpsert) ClearParentAssetID() *AssetUpsert {
u.SetNull(asset.FieldParentAssetID)
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.Asset.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(asset.FieldID)
// }),
// ).
// Exec(ctx)
func (u *AssetUpsertOne) UpdateNewValues() *AssetUpsertOne {
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(asset.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(asset.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Asset.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *AssetUpsertOne) Ignore() *AssetUpsertOne {
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 *AssetUpsertOne) DoNothing() *AssetUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the AssetCreate.OnConflict
// documentation for more info.
func (u *AssetUpsertOne) Update(set func(*AssetUpsert)) *AssetUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&AssetUpsert{UpdateSet: update})
}))
return u
}
// SetUpdatedAt sets the "updated_at" field.
func (u *AssetUpsertOne) SetUpdatedAt(v time.Time) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.SetUpdatedAt(v)
})
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *AssetUpsertOne) UpdateUpdatedAt() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.UpdateUpdatedAt()
})
}
// SetFilename sets the "filename" field.
func (u *AssetUpsertOne) SetFilename(v string) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.SetFilename(v)
})
}
// UpdateFilename sets the "filename" field to the value that was provided on create.
func (u *AssetUpsertOne) UpdateFilename() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.UpdateFilename()
})
}
// SetSize sets the "size" field.
func (u *AssetUpsertOne) SetSize(v int) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.SetSize(v)
})
}
// AddSize adds v to the "size" field.
func (u *AssetUpsertOne) AddSize(v int) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.AddSize(v)
})
}
// UpdateSize sets the "size" field to the value that was provided on create.
func (u *AssetUpsertOne) UpdateSize() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.UpdateSize()
})
}
// SetMimeType sets the "mime_type" field.
func (u *AssetUpsertOne) SetMimeType(v string) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.SetMimeType(v)
})
}
// UpdateMimeType sets the "mime_type" field to the value that was provided on create.
func (u *AssetUpsertOne) UpdateMimeType() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.UpdateMimeType()
})
}
// SetMetadata sets the "metadata" field.
func (u *AssetUpsertOne) SetMetadata(v map[string]interface{}) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.SetMetadata(v)
})
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *AssetUpsertOne) UpdateMetadata() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.UpdateMetadata()
})
}
// ClearMetadata clears the value of the "metadata" field.
func (u *AssetUpsertOne) ClearMetadata() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.ClearMetadata()
})
}
// SetAccountID sets the "account_id" field.
func (u *AssetUpsertOne) SetAccountID(v xid.ID) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *AssetUpsertOne) UpdateAccountID() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.UpdateAccountID()
})
}
// SetParentAssetID sets the "parent_asset_id" field.
func (u *AssetUpsertOne) SetParentAssetID(v xid.ID) *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.SetParentAssetID(v)
})
}
// UpdateParentAssetID sets the "parent_asset_id" field to the value that was provided on create.
func (u *AssetUpsertOne) UpdateParentAssetID() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.UpdateParentAssetID()
})
}
// ClearParentAssetID clears the value of the "parent_asset_id" field.
func (u *AssetUpsertOne) ClearParentAssetID() *AssetUpsertOne {
return u.Update(func(s *AssetUpsert) {
s.ClearParentAssetID()
})
}
// Exec executes the query.
func (u *AssetUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for AssetCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *AssetUpsertOne) 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 *AssetUpsertOne) 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: AssetUpsertOne.ID is not supported by MySQL driver. Use AssetUpsertOne.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 *AssetUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// AssetCreateBulk is the builder for creating many Asset entities in bulk.
type AssetCreateBulk struct {
config
err error
builders []*AssetCreate
conflict []sql.ConflictOption
}
// Save creates the Asset entities in the database.
func (_c *AssetCreateBulk) Save(ctx context.Context) ([]*Asset, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Asset, 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.(*AssetMutation)
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 *AssetCreateBulk) SaveX(ctx context.Context) []*Asset {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *AssetCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *AssetCreateBulk) 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.Asset.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.AssetUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *AssetCreateBulk) OnConflict(opts ...sql.ConflictOption) *AssetUpsertBulk {
_c.conflict = opts
return &AssetUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Asset.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *AssetCreateBulk) OnConflictColumns(columns ...string) *AssetUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &AssetUpsertBulk{
create: _c,
}
}
// AssetUpsertBulk is the builder for "upsert"-ing
// a bulk of Asset nodes.
type AssetUpsertBulk struct {
create *AssetCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Asset.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(asset.FieldID)
// }),
// ).
// Exec(ctx)
func (u *AssetUpsertBulk) UpdateNewValues() *AssetUpsertBulk {
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(asset.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(asset.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Asset.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *AssetUpsertBulk) Ignore() *AssetUpsertBulk {
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 *AssetUpsertBulk) DoNothing() *AssetUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the AssetCreateBulk.OnConflict
// documentation for more info.
func (u *AssetUpsertBulk) Update(set func(*AssetUpsert)) *AssetUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&AssetUpsert{UpdateSet: update})
}))
return u
}
// SetUpdatedAt sets the "updated_at" field.
func (u *AssetUpsertBulk) SetUpdatedAt(v time.Time) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.SetUpdatedAt(v)
})
}
// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *AssetUpsertBulk) UpdateUpdatedAt() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.UpdateUpdatedAt()
})
}
// SetFilename sets the "filename" field.
func (u *AssetUpsertBulk) SetFilename(v string) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.SetFilename(v)
})
}
// UpdateFilename sets the "filename" field to the value that was provided on create.
func (u *AssetUpsertBulk) UpdateFilename() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.UpdateFilename()
})
}
// SetSize sets the "size" field.
func (u *AssetUpsertBulk) SetSize(v int) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.SetSize(v)
})
}
// AddSize adds v to the "size" field.
func (u *AssetUpsertBulk) AddSize(v int) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.AddSize(v)
})
}
// UpdateSize sets the "size" field to the value that was provided on create.
func (u *AssetUpsertBulk) UpdateSize() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.UpdateSize()
})
}
// SetMimeType sets the "mime_type" field.
func (u *AssetUpsertBulk) SetMimeType(v string) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.SetMimeType(v)
})
}
// UpdateMimeType sets the "mime_type" field to the value that was provided on create.
func (u *AssetUpsertBulk) UpdateMimeType() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.UpdateMimeType()
})
}
// SetMetadata sets the "metadata" field.
func (u *AssetUpsertBulk) SetMetadata(v map[string]interface{}) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.SetMetadata(v)
})
}
// UpdateMetadata sets the "metadata" field to the value that was provided on create.
func (u *AssetUpsertBulk) UpdateMetadata() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.UpdateMetadata()
})
}
// ClearMetadata clears the value of the "metadata" field.
func (u *AssetUpsertBulk) ClearMetadata() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.ClearMetadata()
})
}
// SetAccountID sets the "account_id" field.
func (u *AssetUpsertBulk) SetAccountID(v xid.ID) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *AssetUpsertBulk) UpdateAccountID() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.UpdateAccountID()
})
}
// SetParentAssetID sets the "parent_asset_id" field.
func (u *AssetUpsertBulk) SetParentAssetID(v xid.ID) *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.SetParentAssetID(v)
})
}
// UpdateParentAssetID sets the "parent_asset_id" field to the value that was provided on create.
func (u *AssetUpsertBulk) UpdateParentAssetID() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.UpdateParentAssetID()
})
}
// ClearParentAssetID clears the value of the "parent_asset_id" field.
func (u *AssetUpsertBulk) ClearParentAssetID() *AssetUpsertBulk {
return u.Update(func(s *AssetUpsert) {
s.ClearParentAssetID()
})
}
// Exec executes the query.
func (u *AssetUpsertBulk) 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 AssetCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for AssetCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *AssetUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}