Files
MonkeyCode/backend/db/modelapikey_create.go

1015 lines
30 KiB
Go

// Code generated by ent, DO NOT EDIT.
package db
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/chaitin/MonkeyCode/backend/db/model"
"github.com/chaitin/MonkeyCode/backend/db/modelapikey"
"github.com/google/uuid"
)
// ModelApiKeyCreate is the builder for creating a ModelApiKey entity.
type ModelApiKeyCreate struct {
config
mutation *ModelApiKeyMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetDeletedAt sets the "deleted_at" field.
func (_c *ModelApiKeyCreate) SetDeletedAt(v time.Time) *ModelApiKeyCreate {
_c.mutation.SetDeletedAt(v)
return _c
}
// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_c *ModelApiKeyCreate) SetNillableDeletedAt(v *time.Time) *ModelApiKeyCreate {
if v != nil {
_c.SetDeletedAt(*v)
}
return _c
}
// SetModelID sets the "model_id" field.
func (_c *ModelApiKeyCreate) SetModelID(v uuid.UUID) *ModelApiKeyCreate {
_c.mutation.SetModelID(v)
return _c
}
// SetNillableModelID sets the "model_id" field if the given value is not nil.
func (_c *ModelApiKeyCreate) SetNillableModelID(v *uuid.UUID) *ModelApiKeyCreate {
if v != nil {
_c.SetModelID(*v)
}
return _c
}
// SetUserID sets the "user_id" field.
func (_c *ModelApiKeyCreate) SetUserID(v uuid.UUID) *ModelApiKeyCreate {
_c.mutation.SetUserID(v)
return _c
}
// SetKind sets the "kind" field.
func (_c *ModelApiKeyCreate) SetKind(v modelapikey.Kind) *ModelApiKeyCreate {
_c.mutation.SetKind(v)
return _c
}
// SetNillableKind sets the "kind" field if the given value is not nil.
func (_c *ModelApiKeyCreate) SetNillableKind(v *modelapikey.Kind) *ModelApiKeyCreate {
if v != nil {
_c.SetKind(*v)
}
return _c
}
// SetVirtualmachineID sets the "virtualmachine_id" field.
func (_c *ModelApiKeyCreate) SetVirtualmachineID(v string) *ModelApiKeyCreate {
_c.mutation.SetVirtualmachineID(v)
return _c
}
// SetNillableVirtualmachineID sets the "virtualmachine_id" field if the given value is not nil.
func (_c *ModelApiKeyCreate) SetNillableVirtualmachineID(v *string) *ModelApiKeyCreate {
if v != nil {
_c.SetVirtualmachineID(*v)
}
return _c
}
// SetAPIKey sets the "api_key" field.
func (_c *ModelApiKeyCreate) SetAPIKey(v string) *ModelApiKeyCreate {
_c.mutation.SetAPIKey(v)
return _c
}
// SetSigningSecret sets the "signing_secret" field.
func (_c *ModelApiKeyCreate) SetSigningSecret(v string) *ModelApiKeyCreate {
_c.mutation.SetSigningSecret(v)
return _c
}
// SetNillableSigningSecret sets the "signing_secret" field if the given value is not nil.
func (_c *ModelApiKeyCreate) SetNillableSigningSecret(v *string) *ModelApiKeyCreate {
if v != nil {
_c.SetSigningSecret(*v)
}
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *ModelApiKeyCreate) SetCreatedAt(v time.Time) *ModelApiKeyCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *ModelApiKeyCreate) SetNillableCreatedAt(v *time.Time) *ModelApiKeyCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *ModelApiKeyCreate) SetID(v uuid.UUID) *ModelApiKeyCreate {
_c.mutation.SetID(v)
return _c
}
// SetModel sets the "model" edge to the Model entity.
func (_c *ModelApiKeyCreate) SetModel(v *Model) *ModelApiKeyCreate {
return _c.SetModelID(v.ID)
}
// Mutation returns the ModelApiKeyMutation object of the builder.
func (_c *ModelApiKeyCreate) Mutation() *ModelApiKeyMutation {
return _c.mutation
}
// Save creates the ModelApiKey in the database.
func (_c *ModelApiKeyCreate) Save(ctx context.Context) (*ModelApiKey, error) {
if err := _c.defaults(); err != nil {
return nil, err
}
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *ModelApiKeyCreate) SaveX(ctx context.Context) *ModelApiKey {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *ModelApiKeyCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *ModelApiKeyCreate) 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 *ModelApiKeyCreate) defaults() error {
if _, ok := _c.mutation.Kind(); !ok {
v := modelapikey.DefaultKind
_c.mutation.SetKind(v)
}
if _, ok := _c.mutation.SigningSecret(); !ok {
v := modelapikey.DefaultSigningSecret
_c.mutation.SetSigningSecret(v)
}
if _, ok := _c.mutation.CreatedAt(); !ok {
if modelapikey.DefaultCreatedAt == nil {
return fmt.Errorf("db: uninitialized modelapikey.DefaultCreatedAt (forgotten import db/runtime?)")
}
v := modelapikey.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
return nil
}
// check runs all checks and user-defined validators on the builder.
func (_c *ModelApiKeyCreate) check() error {
if _, ok := _c.mutation.UserID(); !ok {
return &ValidationError{Name: "user_id", err: errors.New(`db: missing required field "ModelApiKey.user_id"`)}
}
if _, ok := _c.mutation.Kind(); !ok {
return &ValidationError{Name: "kind", err: errors.New(`db: missing required field "ModelApiKey.kind"`)}
}
if v, ok := _c.mutation.Kind(); ok {
if err := modelapikey.KindValidator(v); err != nil {
return &ValidationError{Name: "kind", err: fmt.Errorf(`db: validator failed for field "ModelApiKey.kind": %w`, err)}
}
}
if _, ok := _c.mutation.APIKey(); !ok {
return &ValidationError{Name: "api_key", err: errors.New(`db: missing required field "ModelApiKey.api_key"`)}
}
if v, ok := _c.mutation.APIKey(); ok {
if err := modelapikey.APIKeyValidator(v); err != nil {
return &ValidationError{Name: "api_key", err: fmt.Errorf(`db: validator failed for field "ModelApiKey.api_key": %w`, err)}
}
}
if _, ok := _c.mutation.SigningSecret(); !ok {
return &ValidationError{Name: "signing_secret", err: errors.New(`db: missing required field "ModelApiKey.signing_secret"`)}
}
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "ModelApiKey.created_at"`)}
}
return nil
}
func (_c *ModelApiKeyCreate) sqlSave(ctx context.Context) (*ModelApiKey, 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.(*uuid.UUID); 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 *ModelApiKeyCreate) createSpec() (*ModelApiKey, *sqlgraph.CreateSpec) {
var (
_node = &ModelApiKey{config: _c.config}
_spec = sqlgraph.NewCreateSpec(modelapikey.Table, sqlgraph.NewFieldSpec(modelapikey.FieldID, field.TypeUUID))
)
_spec.OnConflict = _c.conflict
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.DeletedAt(); ok {
_spec.SetField(modelapikey.FieldDeletedAt, field.TypeTime, value)
_node.DeletedAt = value
}
if value, ok := _c.mutation.UserID(); ok {
_spec.SetField(modelapikey.FieldUserID, field.TypeUUID, value)
_node.UserID = value
}
if value, ok := _c.mutation.Kind(); ok {
_spec.SetField(modelapikey.FieldKind, field.TypeEnum, value)
_node.Kind = value
}
if value, ok := _c.mutation.VirtualmachineID(); ok {
_spec.SetField(modelapikey.FieldVirtualmachineID, field.TypeString, value)
_node.VirtualmachineID = value
}
if value, ok := _c.mutation.APIKey(); ok {
_spec.SetField(modelapikey.FieldAPIKey, field.TypeString, value)
_node.APIKey = value
}
if value, ok := _c.mutation.SigningSecret(); ok {
_spec.SetField(modelapikey.FieldSigningSecret, field.TypeString, value)
_node.SigningSecret = value
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(modelapikey.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if nodes := _c.mutation.ModelIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: modelapikey.ModelTable,
Columns: []string{modelapikey.ModelColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(model.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.ModelID = 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.ModelApiKey.Create().
// SetDeletedAt(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.ModelApiKeyUpsert) {
// SetDeletedAt(v+v).
// }).
// Exec(ctx)
func (_c *ModelApiKeyCreate) OnConflict(opts ...sql.ConflictOption) *ModelApiKeyUpsertOne {
_c.conflict = opts
return &ModelApiKeyUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.ModelApiKey.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *ModelApiKeyCreate) OnConflictColumns(columns ...string) *ModelApiKeyUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &ModelApiKeyUpsertOne{
create: _c,
}
}
type (
// ModelApiKeyUpsertOne is the builder for "upsert"-ing
// one ModelApiKey node.
ModelApiKeyUpsertOne struct {
create *ModelApiKeyCreate
}
// ModelApiKeyUpsert is the "OnConflict" setter.
ModelApiKeyUpsert struct {
*sql.UpdateSet
}
)
// SetDeletedAt sets the "deleted_at" field.
func (u *ModelApiKeyUpsert) SetDeletedAt(v time.Time) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldDeletedAt, v)
return u
}
// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateDeletedAt() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldDeletedAt)
return u
}
// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ModelApiKeyUpsert) ClearDeletedAt() *ModelApiKeyUpsert {
u.SetNull(modelapikey.FieldDeletedAt)
return u
}
// SetModelID sets the "model_id" field.
func (u *ModelApiKeyUpsert) SetModelID(v uuid.UUID) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldModelID, v)
return u
}
// UpdateModelID sets the "model_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateModelID() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldModelID)
return u
}
// ClearModelID clears the value of the "model_id" field.
func (u *ModelApiKeyUpsert) ClearModelID() *ModelApiKeyUpsert {
u.SetNull(modelapikey.FieldModelID)
return u
}
// SetUserID sets the "user_id" field.
func (u *ModelApiKeyUpsert) SetUserID(v uuid.UUID) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldUserID, v)
return u
}
// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateUserID() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldUserID)
return u
}
// SetKind sets the "kind" field.
func (u *ModelApiKeyUpsert) SetKind(v modelapikey.Kind) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldKind, v)
return u
}
// UpdateKind sets the "kind" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateKind() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldKind)
return u
}
// SetVirtualmachineID sets the "virtualmachine_id" field.
func (u *ModelApiKeyUpsert) SetVirtualmachineID(v string) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldVirtualmachineID, v)
return u
}
// UpdateVirtualmachineID sets the "virtualmachine_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateVirtualmachineID() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldVirtualmachineID)
return u
}
// ClearVirtualmachineID clears the value of the "virtualmachine_id" field.
func (u *ModelApiKeyUpsert) ClearVirtualmachineID() *ModelApiKeyUpsert {
u.SetNull(modelapikey.FieldVirtualmachineID)
return u
}
// SetAPIKey sets the "api_key" field.
func (u *ModelApiKeyUpsert) SetAPIKey(v string) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldAPIKey, v)
return u
}
// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateAPIKey() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldAPIKey)
return u
}
// SetSigningSecret sets the "signing_secret" field.
func (u *ModelApiKeyUpsert) SetSigningSecret(v string) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldSigningSecret, v)
return u
}
// UpdateSigningSecret sets the "signing_secret" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateSigningSecret() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldSigningSecret)
return u
}
// SetCreatedAt sets the "created_at" field.
func (u *ModelApiKeyUpsert) SetCreatedAt(v time.Time) *ModelApiKeyUpsert {
u.Set(modelapikey.FieldCreatedAt, v)
return u
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *ModelApiKeyUpsert) UpdateCreatedAt() *ModelApiKeyUpsert {
u.SetExcluded(modelapikey.FieldCreatedAt)
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.ModelApiKey.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(modelapikey.FieldID)
// }),
// ).
// Exec(ctx)
func (u *ModelApiKeyUpsertOne) UpdateNewValues() *ModelApiKeyUpsertOne {
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(modelapikey.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.ModelApiKey.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *ModelApiKeyUpsertOne) Ignore() *ModelApiKeyUpsertOne {
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 *ModelApiKeyUpsertOne) DoNothing() *ModelApiKeyUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the ModelApiKeyCreate.OnConflict
// documentation for more info.
func (u *ModelApiKeyUpsertOne) Update(set func(*ModelApiKeyUpsert)) *ModelApiKeyUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&ModelApiKeyUpsert{UpdateSet: update})
}))
return u
}
// SetDeletedAt sets the "deleted_at" field.
func (u *ModelApiKeyUpsertOne) SetDeletedAt(v time.Time) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetDeletedAt(v)
})
}
// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateDeletedAt() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateDeletedAt()
})
}
// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ModelApiKeyUpsertOne) ClearDeletedAt() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.ClearDeletedAt()
})
}
// SetModelID sets the "model_id" field.
func (u *ModelApiKeyUpsertOne) SetModelID(v uuid.UUID) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetModelID(v)
})
}
// UpdateModelID sets the "model_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateModelID() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateModelID()
})
}
// ClearModelID clears the value of the "model_id" field.
func (u *ModelApiKeyUpsertOne) ClearModelID() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.ClearModelID()
})
}
// SetUserID sets the "user_id" field.
func (u *ModelApiKeyUpsertOne) SetUserID(v uuid.UUID) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetUserID(v)
})
}
// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateUserID() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateUserID()
})
}
// SetKind sets the "kind" field.
func (u *ModelApiKeyUpsertOne) SetKind(v modelapikey.Kind) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetKind(v)
})
}
// UpdateKind sets the "kind" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateKind() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateKind()
})
}
// SetVirtualmachineID sets the "virtualmachine_id" field.
func (u *ModelApiKeyUpsertOne) SetVirtualmachineID(v string) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetVirtualmachineID(v)
})
}
// UpdateVirtualmachineID sets the "virtualmachine_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateVirtualmachineID() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateVirtualmachineID()
})
}
// ClearVirtualmachineID clears the value of the "virtualmachine_id" field.
func (u *ModelApiKeyUpsertOne) ClearVirtualmachineID() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.ClearVirtualmachineID()
})
}
// SetAPIKey sets the "api_key" field.
func (u *ModelApiKeyUpsertOne) SetAPIKey(v string) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetAPIKey(v)
})
}
// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateAPIKey() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateAPIKey()
})
}
// SetSigningSecret sets the "signing_secret" field.
func (u *ModelApiKeyUpsertOne) SetSigningSecret(v string) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetSigningSecret(v)
})
}
// UpdateSigningSecret sets the "signing_secret" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateSigningSecret() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateSigningSecret()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *ModelApiKeyUpsertOne) SetCreatedAt(v time.Time) *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *ModelApiKeyUpsertOne) UpdateCreatedAt() *ModelApiKeyUpsertOne {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *ModelApiKeyUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for ModelApiKeyCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *ModelApiKeyUpsertOne) 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 *ModelApiKeyUpsertOne) ID(ctx context.Context) (id uuid.UUID, 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("db: ModelApiKeyUpsertOne.ID is not supported by MySQL driver. Use ModelApiKeyUpsertOne.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 *ModelApiKeyUpsertOne) IDX(ctx context.Context) uuid.UUID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// ModelApiKeyCreateBulk is the builder for creating many ModelApiKey entities in bulk.
type ModelApiKeyCreateBulk struct {
config
err error
builders []*ModelApiKeyCreate
conflict []sql.ConflictOption
}
// Save creates the ModelApiKey entities in the database.
func (_c *ModelApiKeyCreateBulk) Save(ctx context.Context) ([]*ModelApiKey, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*ModelApiKey, 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.(*ModelApiKeyMutation)
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 *ModelApiKeyCreateBulk) SaveX(ctx context.Context) []*ModelApiKey {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *ModelApiKeyCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *ModelApiKeyCreateBulk) 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.ModelApiKey.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.ModelApiKeyUpsert) {
// SetDeletedAt(v+v).
// }).
// Exec(ctx)
func (_c *ModelApiKeyCreateBulk) OnConflict(opts ...sql.ConflictOption) *ModelApiKeyUpsertBulk {
_c.conflict = opts
return &ModelApiKeyUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.ModelApiKey.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *ModelApiKeyCreateBulk) OnConflictColumns(columns ...string) *ModelApiKeyUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &ModelApiKeyUpsertBulk{
create: _c,
}
}
// ModelApiKeyUpsertBulk is the builder for "upsert"-ing
// a bulk of ModelApiKey nodes.
type ModelApiKeyUpsertBulk struct {
create *ModelApiKeyCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.ModelApiKey.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(modelapikey.FieldID)
// }),
// ).
// Exec(ctx)
func (u *ModelApiKeyUpsertBulk) UpdateNewValues() *ModelApiKeyUpsertBulk {
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(modelapikey.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.ModelApiKey.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *ModelApiKeyUpsertBulk) Ignore() *ModelApiKeyUpsertBulk {
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 *ModelApiKeyUpsertBulk) DoNothing() *ModelApiKeyUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the ModelApiKeyCreateBulk.OnConflict
// documentation for more info.
func (u *ModelApiKeyUpsertBulk) Update(set func(*ModelApiKeyUpsert)) *ModelApiKeyUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&ModelApiKeyUpsert{UpdateSet: update})
}))
return u
}
// SetDeletedAt sets the "deleted_at" field.
func (u *ModelApiKeyUpsertBulk) SetDeletedAt(v time.Time) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetDeletedAt(v)
})
}
// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateDeletedAt() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateDeletedAt()
})
}
// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *ModelApiKeyUpsertBulk) ClearDeletedAt() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.ClearDeletedAt()
})
}
// SetModelID sets the "model_id" field.
func (u *ModelApiKeyUpsertBulk) SetModelID(v uuid.UUID) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetModelID(v)
})
}
// UpdateModelID sets the "model_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateModelID() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateModelID()
})
}
// ClearModelID clears the value of the "model_id" field.
func (u *ModelApiKeyUpsertBulk) ClearModelID() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.ClearModelID()
})
}
// SetUserID sets the "user_id" field.
func (u *ModelApiKeyUpsertBulk) SetUserID(v uuid.UUID) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetUserID(v)
})
}
// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateUserID() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateUserID()
})
}
// SetKind sets the "kind" field.
func (u *ModelApiKeyUpsertBulk) SetKind(v modelapikey.Kind) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetKind(v)
})
}
// UpdateKind sets the "kind" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateKind() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateKind()
})
}
// SetVirtualmachineID sets the "virtualmachine_id" field.
func (u *ModelApiKeyUpsertBulk) SetVirtualmachineID(v string) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetVirtualmachineID(v)
})
}
// UpdateVirtualmachineID sets the "virtualmachine_id" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateVirtualmachineID() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateVirtualmachineID()
})
}
// ClearVirtualmachineID clears the value of the "virtualmachine_id" field.
func (u *ModelApiKeyUpsertBulk) ClearVirtualmachineID() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.ClearVirtualmachineID()
})
}
// SetAPIKey sets the "api_key" field.
func (u *ModelApiKeyUpsertBulk) SetAPIKey(v string) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetAPIKey(v)
})
}
// UpdateAPIKey sets the "api_key" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateAPIKey() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateAPIKey()
})
}
// SetSigningSecret sets the "signing_secret" field.
func (u *ModelApiKeyUpsertBulk) SetSigningSecret(v string) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetSigningSecret(v)
})
}
// UpdateSigningSecret sets the "signing_secret" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateSigningSecret() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateSigningSecret()
})
}
// SetCreatedAt sets the "created_at" field.
func (u *ModelApiKeyUpsertBulk) SetCreatedAt(v time.Time) *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *ModelApiKeyUpsertBulk) UpdateCreatedAt() *ModelApiKeyUpsertBulk {
return u.Update(func(s *ModelApiKeyUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *ModelApiKeyUpsertBulk) 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("db: OnConflict was set for builder %d. Set it on the ModelApiKeyCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("db: missing options for ModelApiKeyCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *ModelApiKeyUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}