// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/Wei-Shaw/sub2api/ent/apikey"
	"github.com/Wei-Shaw/sub2api/ent/group"
	"github.com/Wei-Shaw/sub2api/ent/usagelog"
	"github.com/Wei-Shaw/sub2api/ent/user"
)

// APIKeyCreate is the builder for creating a APIKey entity.
type APIKeyCreate struct {
	config
	mutation *APIKeyMutation
	hooks    []Hook
	conflict []sql.ConflictOption
}

// SetCreatedAt sets the "created_at" field.
func (_c *APIKeyCreate) SetCreatedAt(v time.Time) *APIKeyCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableCreatedAt(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetCreatedAt(*v)
	}
	return _c
}

// SetUpdatedAt sets the "updated_at" field.
func (_c *APIKeyCreate) SetUpdatedAt(v time.Time) *APIKeyCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableUpdatedAt(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetUpdatedAt(*v)
	}
	return _c
}

// SetDeletedAt sets the "deleted_at" field.
func (_c *APIKeyCreate) SetDeletedAt(v time.Time) *APIKeyCreate {
	_c.mutation.SetDeletedAt(v)
	return _c
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableDeletedAt(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetDeletedAt(*v)
	}
	return _c
}

// SetUserID sets the "user_id" field.
func (_c *APIKeyCreate) SetUserID(v int64) *APIKeyCreate {
	_c.mutation.SetUserID(v)
	return _c
}

// SetKey sets the "key" field.
func (_c *APIKeyCreate) SetKey(v string) *APIKeyCreate {
	_c.mutation.SetKey(v)
	return _c
}

// SetName sets the "name" field.
func (_c *APIKeyCreate) SetName(v string) *APIKeyCreate {
	_c.mutation.SetName(v)
	return _c
}

// SetGroupID sets the "group_id" field.
func (_c *APIKeyCreate) SetGroupID(v int64) *APIKeyCreate {
	_c.mutation.SetGroupID(v)
	return _c
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableGroupID(v *int64) *APIKeyCreate {
	if v != nil {
		_c.SetGroupID(*v)
	}
	return _c
}

// SetStatus sets the "status" field.
func (_c *APIKeyCreate) SetStatus(v string) *APIKeyCreate {
	_c.mutation.SetStatus(v)
	return _c
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableStatus(v *string) *APIKeyCreate {
	if v != nil {
		_c.SetStatus(*v)
	}
	return _c
}

// SetLastUsedAt sets the "last_used_at" field.
func (_c *APIKeyCreate) SetLastUsedAt(v time.Time) *APIKeyCreate {
	_c.mutation.SetLastUsedAt(v)
	return _c
}

// SetNillableLastUsedAt sets the "last_used_at" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableLastUsedAt(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetLastUsedAt(*v)
	}
	return _c
}

// SetIPWhitelist sets the "ip_whitelist" field.
func (_c *APIKeyCreate) SetIPWhitelist(v []string) *APIKeyCreate {
	_c.mutation.SetIPWhitelist(v)
	return _c
}

// SetIPBlacklist sets the "ip_blacklist" field.
func (_c *APIKeyCreate) SetIPBlacklist(v []string) *APIKeyCreate {
	_c.mutation.SetIPBlacklist(v)
	return _c
}

// SetQuota sets the "quota" field.
func (_c *APIKeyCreate) SetQuota(v float64) *APIKeyCreate {
	_c.mutation.SetQuota(v)
	return _c
}

// SetNillableQuota sets the "quota" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableQuota(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetQuota(*v)
	}
	return _c
}

// SetQuotaUsed sets the "quota_used" field.
func (_c *APIKeyCreate) SetQuotaUsed(v float64) *APIKeyCreate {
	_c.mutation.SetQuotaUsed(v)
	return _c
}

// SetNillableQuotaUsed sets the "quota_used" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableQuotaUsed(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetQuotaUsed(*v)
	}
	return _c
}

// SetExpiresAt sets the "expires_at" field.
func (_c *APIKeyCreate) SetExpiresAt(v time.Time) *APIKeyCreate {
	_c.mutation.SetExpiresAt(v)
	return _c
}

// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableExpiresAt(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetExpiresAt(*v)
	}
	return _c
}

// SetRateLimit5h sets the "rate_limit_5h" field.
func (_c *APIKeyCreate) SetRateLimit5h(v float64) *APIKeyCreate {
	_c.mutation.SetRateLimit5h(v)
	return _c
}

// SetNillableRateLimit5h sets the "rate_limit_5h" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableRateLimit5h(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetRateLimit5h(*v)
	}
	return _c
}

// SetRateLimit1d sets the "rate_limit_1d" field.
func (_c *APIKeyCreate) SetRateLimit1d(v float64) *APIKeyCreate {
	_c.mutation.SetRateLimit1d(v)
	return _c
}

// SetNillableRateLimit1d sets the "rate_limit_1d" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableRateLimit1d(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetRateLimit1d(*v)
	}
	return _c
}

// SetRateLimit7d sets the "rate_limit_7d" field.
func (_c *APIKeyCreate) SetRateLimit7d(v float64) *APIKeyCreate {
	_c.mutation.SetRateLimit7d(v)
	return _c
}

// SetNillableRateLimit7d sets the "rate_limit_7d" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableRateLimit7d(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetRateLimit7d(*v)
	}
	return _c
}

// SetUsage5h sets the "usage_5h" field.
func (_c *APIKeyCreate) SetUsage5h(v float64) *APIKeyCreate {
	_c.mutation.SetUsage5h(v)
	return _c
}

// SetNillableUsage5h sets the "usage_5h" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableUsage5h(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetUsage5h(*v)
	}
	return _c
}

// SetUsage1d sets the "usage_1d" field.
func (_c *APIKeyCreate) SetUsage1d(v float64) *APIKeyCreate {
	_c.mutation.SetUsage1d(v)
	return _c
}

// SetNillableUsage1d sets the "usage_1d" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableUsage1d(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetUsage1d(*v)
	}
	return _c
}

// SetUsage7d sets the "usage_7d" field.
func (_c *APIKeyCreate) SetUsage7d(v float64) *APIKeyCreate {
	_c.mutation.SetUsage7d(v)
	return _c
}

// SetNillableUsage7d sets the "usage_7d" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableUsage7d(v *float64) *APIKeyCreate {
	if v != nil {
		_c.SetUsage7d(*v)
	}
	return _c
}

// SetWindow5hStart sets the "window_5h_start" field.
func (_c *APIKeyCreate) SetWindow5hStart(v time.Time) *APIKeyCreate {
	_c.mutation.SetWindow5hStart(v)
	return _c
}

// SetNillableWindow5hStart sets the "window_5h_start" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableWindow5hStart(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetWindow5hStart(*v)
	}
	return _c
}

// SetWindow1dStart sets the "window_1d_start" field.
func (_c *APIKeyCreate) SetWindow1dStart(v time.Time) *APIKeyCreate {
	_c.mutation.SetWindow1dStart(v)
	return _c
}

// SetNillableWindow1dStart sets the "window_1d_start" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableWindow1dStart(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetWindow1dStart(*v)
	}
	return _c
}

// SetWindow7dStart sets the "window_7d_start" field.
func (_c *APIKeyCreate) SetWindow7dStart(v time.Time) *APIKeyCreate {
	_c.mutation.SetWindow7dStart(v)
	return _c
}

// SetNillableWindow7dStart sets the "window_7d_start" field if the given value is not nil.
func (_c *APIKeyCreate) SetNillableWindow7dStart(v *time.Time) *APIKeyCreate {
	if v != nil {
		_c.SetWindow7dStart(*v)
	}
	return _c
}

// SetUser sets the "user" edge to the User entity.
func (_c *APIKeyCreate) SetUser(v *User) *APIKeyCreate {
	return _c.SetUserID(v.ID)
}

// SetGroup sets the "group" edge to the Group entity.
func (_c *APIKeyCreate) SetGroup(v *Group) *APIKeyCreate {
	return _c.SetGroupID(v.ID)
}

// AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
func (_c *APIKeyCreate) AddUsageLogIDs(ids ...int64) *APIKeyCreate {
	_c.mutation.AddUsageLogIDs(ids...)
	return _c
}

// AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
func (_c *APIKeyCreate) AddUsageLogs(v ...*UsageLog) *APIKeyCreate {
	ids := make([]int64, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _c.AddUsageLogIDs(ids...)
}

// Mutation returns the APIKeyMutation object of the builder.
func (_c *APIKeyCreate) Mutation() *APIKeyMutation {
	return _c.mutation
}

// Save creates the APIKey in the database.
func (_c *APIKeyCreate) Save(ctx context.Context) (*APIKey, 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 *APIKeyCreate) SaveX(ctx context.Context) *APIKey {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (_c *APIKeyCreate) Exec(ctx context.Context) error {
	_, err := _c.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_c *APIKeyCreate) 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 *APIKeyCreate) defaults() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		if apikey.DefaultCreatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.DefaultCreatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		if apikey.DefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized apikey.DefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := apikey.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.Status(); !ok {
		v := apikey.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.Quota(); !ok {
		v := apikey.DefaultQuota
		_c.mutation.SetQuota(v)
	}
	if _, ok := _c.mutation.QuotaUsed(); !ok {
		v := apikey.DefaultQuotaUsed
		_c.mutation.SetQuotaUsed(v)
	}
	if _, ok := _c.mutation.RateLimit5h(); !ok {
		v := apikey.DefaultRateLimit5h
		_c.mutation.SetRateLimit5h(v)
	}
	if _, ok := _c.mutation.RateLimit1d(); !ok {
		v := apikey.DefaultRateLimit1d
		_c.mutation.SetRateLimit1d(v)
	}
	if _, ok := _c.mutation.RateLimit7d(); !ok {
		v := apikey.DefaultRateLimit7d
		_c.mutation.SetRateLimit7d(v)
	}
	if _, ok := _c.mutation.Usage5h(); !ok {
		v := apikey.DefaultUsage5h
		_c.mutation.SetUsage5h(v)
	}
	if _, ok := _c.mutation.Usage1d(); !ok {
		v := apikey.DefaultUsage1d
		_c.mutation.SetUsage1d(v)
	}
	if _, ok := _c.mutation.Usage7d(); !ok {
		v := apikey.DefaultUsage7d
		_c.mutation.SetUsage7d(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_c *APIKeyCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "APIKey.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "APIKey.updated_at"`)}
	}
	if _, ok := _c.mutation.UserID(); !ok {
		return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "APIKey.user_id"`)}
	}
	if _, ok := _c.mutation.Key(); !ok {
		return &ValidationError{Name: "key", err: errors.New(`ent: missing required field "APIKey.key"`)}
	}
	if v, ok := _c.mutation.Key(); ok {
		if err := apikey.KeyValidator(v); err != nil {
			return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "APIKey.key": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "APIKey.name"`)}
	}
	if v, ok := _c.mutation.Name(); ok {
		if err := apikey.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "APIKey.name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "APIKey.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := apikey.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "APIKey.status": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Quota(); !ok {
		return &ValidationError{Name: "quota", err: errors.New(`ent: missing required field "APIKey.quota"`)}
	}
	if _, ok := _c.mutation.QuotaUsed(); !ok {
		return &ValidationError{Name: "quota_used", err: errors.New(`ent: missing required field "APIKey.quota_used"`)}
	}
	if _, ok := _c.mutation.RateLimit5h(); !ok {
		return &ValidationError{Name: "rate_limit_5h", err: errors.New(`ent: missing required field "APIKey.rate_limit_5h"`)}
	}
	if _, ok := _c.mutation.RateLimit1d(); !ok {
		return &ValidationError{Name: "rate_limit_1d", err: errors.New(`ent: missing required field "APIKey.rate_limit_1d"`)}
	}
	if _, ok := _c.mutation.RateLimit7d(); !ok {
		return &ValidationError{Name: "rate_limit_7d", err: errors.New(`ent: missing required field "APIKey.rate_limit_7d"`)}
	}
	if _, ok := _c.mutation.Usage5h(); !ok {
		return &ValidationError{Name: "usage_5h", err: errors.New(`ent: missing required field "APIKey.usage_5h"`)}
	}
	if _, ok := _c.mutation.Usage1d(); !ok {
		return &ValidationError{Name: "usage_1d", err: errors.New(`ent: missing required field "APIKey.usage_1d"`)}
	}
	if _, ok := _c.mutation.Usage7d(); !ok {
		return &ValidationError{Name: "usage_7d", err: errors.New(`ent: missing required field "APIKey.usage_7d"`)}
	}
	if len(_c.mutation.UserIDs()) == 0 {
		return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "APIKey.user"`)}
	}
	return nil
}

func (_c *APIKeyCreate) sqlSave(ctx context.Context) (*APIKey, 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
	}
	id := _spec.ID.Value.(int64)
	_node.ID = int64(id)
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *APIKeyCreate) createSpec() (*APIKey, *sqlgraph.CreateSpec) {
	var (
		_node = &APIKey{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(apikey.Table, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeInt64))
	)
	_spec.OnConflict = _c.conflict
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(apikey.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	if value, ok := _c.mutation.Key(); ok {
		_spec.SetField(apikey.FieldKey, field.TypeString, value)
		_node.Key = value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(apikey.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(apikey.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.LastUsedAt(); ok {
		_spec.SetField(apikey.FieldLastUsedAt, field.TypeTime, value)
		_node.LastUsedAt = &value
	}
	if value, ok := _c.mutation.IPWhitelist(); ok {
		_spec.SetField(apikey.FieldIPWhitelist, field.TypeJSON, value)
		_node.IPWhitelist = value
	}
	if value, ok := _c.mutation.IPBlacklist(); ok {
		_spec.SetField(apikey.FieldIPBlacklist, field.TypeJSON, value)
		_node.IPBlacklist = value
	}
	if value, ok := _c.mutation.Quota(); ok {
		_spec.SetField(apikey.FieldQuota, field.TypeFloat64, value)
		_node.Quota = value
	}
	if value, ok := _c.mutation.QuotaUsed(); ok {
		_spec.SetField(apikey.FieldQuotaUsed, field.TypeFloat64, value)
		_node.QuotaUsed = value
	}
	if value, ok := _c.mutation.ExpiresAt(); ok {
		_spec.SetField(apikey.FieldExpiresAt, field.TypeTime, value)
		_node.ExpiresAt = &value
	}
	if value, ok := _c.mutation.RateLimit5h(); ok {
		_spec.SetField(apikey.FieldRateLimit5h, field.TypeFloat64, value)
		_node.RateLimit5h = value
	}
	if value, ok := _c.mutation.RateLimit1d(); ok {
		_spec.SetField(apikey.FieldRateLimit1d, field.TypeFloat64, value)
		_node.RateLimit1d = value
	}
	if value, ok := _c.mutation.RateLimit7d(); ok {
		_spec.SetField(apikey.FieldRateLimit7d, field.TypeFloat64, value)
		_node.RateLimit7d = value
	}
	if value, ok := _c.mutation.Usage5h(); ok {
		_spec.SetField(apikey.FieldUsage5h, field.TypeFloat64, value)
		_node.Usage5h = value
	}
	if value, ok := _c.mutation.Usage1d(); ok {
		_spec.SetField(apikey.FieldUsage1d, field.TypeFloat64, value)
		_node.Usage1d = value
	}
	if value, ok := _c.mutation.Usage7d(); ok {
		_spec.SetField(apikey.FieldUsage7d, field.TypeFloat64, value)
		_node.Usage7d = value
	}
	if value, ok := _c.mutation.Window5hStart(); ok {
		_spec.SetField(apikey.FieldWindow5hStart, field.TypeTime, value)
		_node.Window5hStart = &value
	}
	if value, ok := _c.mutation.Window1dStart(); ok {
		_spec.SetField(apikey.FieldWindow1dStart, field.TypeTime, value)
		_node.Window1dStart = &value
	}
	if value, ok := _c.mutation.Window7dStart(); ok {
		_spec.SetField(apikey.FieldWindow7dStart, field.TypeTime, value)
		_node.Window7dStart = &value
	}
	if nodes := _c.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.UserTable,
			Columns: []string{apikey.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.UserID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   apikey.GroupTable,
			Columns: []string{apikey.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_node.GroupID = &nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	if nodes := _c.mutation.UsageLogsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   apikey.UsageLogsTable,
			Columns: []string{apikey.UsageLogsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
			},
		}
		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.APIKey.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.APIKeyUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *APIKeyCreate) OnConflict(opts ...sql.ConflictOption) *APIKeyUpsertOne {
	_c.conflict = opts
	return &APIKeyUpsertOne{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.APIKey.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *APIKeyCreate) OnConflictColumns(columns ...string) *APIKeyUpsertOne {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &APIKeyUpsertOne{
		create: _c,
	}
}

type (
	// APIKeyUpsertOne is the builder for "upsert"-ing
	//  one APIKey node.
	APIKeyUpsertOne struct {
		create *APIKeyCreate
	}

	// APIKeyUpsert is the "OnConflict" setter.
	APIKeyUpsert struct {
		*sql.UpdateSet
	}
)

// SetUpdatedAt sets the "updated_at" field.
func (u *APIKeyUpsert) SetUpdatedAt(v time.Time) *APIKeyUpsert {
	u.Set(apikey.FieldUpdatedAt, v)
	return u
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateUpdatedAt() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldUpdatedAt)
	return u
}

// SetDeletedAt sets the "deleted_at" field.
func (u *APIKeyUpsert) SetDeletedAt(v time.Time) *APIKeyUpsert {
	u.Set(apikey.FieldDeletedAt, v)
	return u
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateDeletedAt() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldDeletedAt)
	return u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *APIKeyUpsert) ClearDeletedAt() *APIKeyUpsert {
	u.SetNull(apikey.FieldDeletedAt)
	return u
}

// SetUserID sets the "user_id" field.
func (u *APIKeyUpsert) SetUserID(v int64) *APIKeyUpsert {
	u.Set(apikey.FieldUserID, v)
	return u
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateUserID() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldUserID)
	return u
}

// SetKey sets the "key" field.
func (u *APIKeyUpsert) SetKey(v string) *APIKeyUpsert {
	u.Set(apikey.FieldKey, v)
	return u
}

// UpdateKey sets the "key" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateKey() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldKey)
	return u
}

// SetName sets the "name" field.
func (u *APIKeyUpsert) SetName(v string) *APIKeyUpsert {
	u.Set(apikey.FieldName, v)
	return u
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateName() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldName)
	return u
}

// SetGroupID sets the "group_id" field.
func (u *APIKeyUpsert) SetGroupID(v int64) *APIKeyUpsert {
	u.Set(apikey.FieldGroupID, v)
	return u
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateGroupID() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldGroupID)
	return u
}

// ClearGroupID clears the value of the "group_id" field.
func (u *APIKeyUpsert) ClearGroupID() *APIKeyUpsert {
	u.SetNull(apikey.FieldGroupID)
	return u
}

// SetStatus sets the "status" field.
func (u *APIKeyUpsert) SetStatus(v string) *APIKeyUpsert {
	u.Set(apikey.FieldStatus, v)
	return u
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateStatus() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldStatus)
	return u
}

// SetLastUsedAt sets the "last_used_at" field.
func (u *APIKeyUpsert) SetLastUsedAt(v time.Time) *APIKeyUpsert {
	u.Set(apikey.FieldLastUsedAt, v)
	return u
}

// UpdateLastUsedAt sets the "last_used_at" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateLastUsedAt() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldLastUsedAt)
	return u
}

// ClearLastUsedAt clears the value of the "last_used_at" field.
func (u *APIKeyUpsert) ClearLastUsedAt() *APIKeyUpsert {
	u.SetNull(apikey.FieldLastUsedAt)
	return u
}

// SetIPWhitelist sets the "ip_whitelist" field.
func (u *APIKeyUpsert) SetIPWhitelist(v []string) *APIKeyUpsert {
	u.Set(apikey.FieldIPWhitelist, v)
	return u
}

// UpdateIPWhitelist sets the "ip_whitelist" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateIPWhitelist() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldIPWhitelist)
	return u
}

// ClearIPWhitelist clears the value of the "ip_whitelist" field.
func (u *APIKeyUpsert) ClearIPWhitelist() *APIKeyUpsert {
	u.SetNull(apikey.FieldIPWhitelist)
	return u
}

// SetIPBlacklist sets the "ip_blacklist" field.
func (u *APIKeyUpsert) SetIPBlacklist(v []string) *APIKeyUpsert {
	u.Set(apikey.FieldIPBlacklist, v)
	return u
}

// UpdateIPBlacklist sets the "ip_blacklist" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateIPBlacklist() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldIPBlacklist)
	return u
}

// ClearIPBlacklist clears the value of the "ip_blacklist" field.
func (u *APIKeyUpsert) ClearIPBlacklist() *APIKeyUpsert {
	u.SetNull(apikey.FieldIPBlacklist)
	return u
}

// SetQuota sets the "quota" field.
func (u *APIKeyUpsert) SetQuota(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldQuota, v)
	return u
}

// UpdateQuota sets the "quota" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateQuota() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldQuota)
	return u
}

// AddQuota adds v to the "quota" field.
func (u *APIKeyUpsert) AddQuota(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldQuota, v)
	return u
}

// SetQuotaUsed sets the "quota_used" field.
func (u *APIKeyUpsert) SetQuotaUsed(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldQuotaUsed, v)
	return u
}

// UpdateQuotaUsed sets the "quota_used" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateQuotaUsed() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldQuotaUsed)
	return u
}

// AddQuotaUsed adds v to the "quota_used" field.
func (u *APIKeyUpsert) AddQuotaUsed(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldQuotaUsed, v)
	return u
}

// SetExpiresAt sets the "expires_at" field.
func (u *APIKeyUpsert) SetExpiresAt(v time.Time) *APIKeyUpsert {
	u.Set(apikey.FieldExpiresAt, v)
	return u
}

// UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateExpiresAt() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldExpiresAt)
	return u
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (u *APIKeyUpsert) ClearExpiresAt() *APIKeyUpsert {
	u.SetNull(apikey.FieldExpiresAt)
	return u
}

// SetRateLimit5h sets the "rate_limit_5h" field.
func (u *APIKeyUpsert) SetRateLimit5h(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldRateLimit5h, v)
	return u
}

// UpdateRateLimit5h sets the "rate_limit_5h" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateRateLimit5h() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldRateLimit5h)
	return u
}

// AddRateLimit5h adds v to the "rate_limit_5h" field.
func (u *APIKeyUpsert) AddRateLimit5h(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldRateLimit5h, v)
	return u
}

// SetRateLimit1d sets the "rate_limit_1d" field.
func (u *APIKeyUpsert) SetRateLimit1d(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldRateLimit1d, v)
	return u
}

// UpdateRateLimit1d sets the "rate_limit_1d" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateRateLimit1d() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldRateLimit1d)
	return u
}

// AddRateLimit1d adds v to the "rate_limit_1d" field.
func (u *APIKeyUpsert) AddRateLimit1d(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldRateLimit1d, v)
	return u
}

// SetRateLimit7d sets the "rate_limit_7d" field.
func (u *APIKeyUpsert) SetRateLimit7d(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldRateLimit7d, v)
	return u
}

// UpdateRateLimit7d sets the "rate_limit_7d" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateRateLimit7d() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldRateLimit7d)
	return u
}

// AddRateLimit7d adds v to the "rate_limit_7d" field.
func (u *APIKeyUpsert) AddRateLimit7d(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldRateLimit7d, v)
	return u
}

// SetUsage5h sets the "usage_5h" field.
func (u *APIKeyUpsert) SetUsage5h(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldUsage5h, v)
	return u
}

// UpdateUsage5h sets the "usage_5h" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateUsage5h() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldUsage5h)
	return u
}

// AddUsage5h adds v to the "usage_5h" field.
func (u *APIKeyUpsert) AddUsage5h(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldUsage5h, v)
	return u
}

// SetUsage1d sets the "usage_1d" field.
func (u *APIKeyUpsert) SetUsage1d(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldUsage1d, v)
	return u
}

// UpdateUsage1d sets the "usage_1d" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateUsage1d() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldUsage1d)
	return u
}

// AddUsage1d adds v to the "usage_1d" field.
func (u *APIKeyUpsert) AddUsage1d(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldUsage1d, v)
	return u
}

// SetUsage7d sets the "usage_7d" field.
func (u *APIKeyUpsert) SetUsage7d(v float64) *APIKeyUpsert {
	u.Set(apikey.FieldUsage7d, v)
	return u
}

// UpdateUsage7d sets the "usage_7d" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateUsage7d() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldUsage7d)
	return u
}

// AddUsage7d adds v to the "usage_7d" field.
func (u *APIKeyUpsert) AddUsage7d(v float64) *APIKeyUpsert {
	u.Add(apikey.FieldUsage7d, v)
	return u
}

// SetWindow5hStart sets the "window_5h_start" field.
func (u *APIKeyUpsert) SetWindow5hStart(v time.Time) *APIKeyUpsert {
	u.Set(apikey.FieldWindow5hStart, v)
	return u
}

// UpdateWindow5hStart sets the "window_5h_start" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateWindow5hStart() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldWindow5hStart)
	return u
}

// ClearWindow5hStart clears the value of the "window_5h_start" field.
func (u *APIKeyUpsert) ClearWindow5hStart() *APIKeyUpsert {
	u.SetNull(apikey.FieldWindow5hStart)
	return u
}

// SetWindow1dStart sets the "window_1d_start" field.
func (u *APIKeyUpsert) SetWindow1dStart(v time.Time) *APIKeyUpsert {
	u.Set(apikey.FieldWindow1dStart, v)
	return u
}

// UpdateWindow1dStart sets the "window_1d_start" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateWindow1dStart() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldWindow1dStart)
	return u
}

// ClearWindow1dStart clears the value of the "window_1d_start" field.
func (u *APIKeyUpsert) ClearWindow1dStart() *APIKeyUpsert {
	u.SetNull(apikey.FieldWindow1dStart)
	return u
}

// SetWindow7dStart sets the "window_7d_start" field.
func (u *APIKeyUpsert) SetWindow7dStart(v time.Time) *APIKeyUpsert {
	u.Set(apikey.FieldWindow7dStart, v)
	return u
}

// UpdateWindow7dStart sets the "window_7d_start" field to the value that was provided on create.
func (u *APIKeyUpsert) UpdateWindow7dStart() *APIKeyUpsert {
	u.SetExcluded(apikey.FieldWindow7dStart)
	return u
}

// ClearWindow7dStart clears the value of the "window_7d_start" field.
func (u *APIKeyUpsert) ClearWindow7dStart() *APIKeyUpsert {
	u.SetNull(apikey.FieldWindow7dStart)
	return u
}

// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
//	client.APIKey.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *APIKeyUpsertOne) UpdateNewValues() *APIKeyUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		if _, exists := u.create.mutation.CreatedAt(); exists {
			s.SetIgnore(apikey.FieldCreatedAt)
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.APIKey.Create().
//	    OnConflict(sql.ResolveWithIgnore()).
//	    Exec(ctx)
func (u *APIKeyUpsertOne) Ignore() *APIKeyUpsertOne {
	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 *APIKeyUpsertOne) DoNothing() *APIKeyUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the APIKeyCreate.OnConflict
// documentation for more info.
func (u *APIKeyUpsertOne) Update(set func(*APIKeyUpsert)) *APIKeyUpsertOne {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&APIKeyUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *APIKeyUpsertOne) SetUpdatedAt(v time.Time) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateUpdatedAt() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *APIKeyUpsertOne) SetDeletedAt(v time.Time) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateDeletedAt() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *APIKeyUpsertOne) ClearDeletedAt() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUserID sets the "user_id" field.
func (u *APIKeyUpsertOne) SetUserID(v int64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateUserID() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUserID()
	})
}

// SetKey sets the "key" field.
func (u *APIKeyUpsertOne) SetKey(v string) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetKey(v)
	})
}

// UpdateKey sets the "key" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateKey() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateKey()
	})
}

// SetName sets the "name" field.
func (u *APIKeyUpsertOne) SetName(v string) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateName() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateName()
	})
}

// SetGroupID sets the "group_id" field.
func (u *APIKeyUpsertOne) SetGroupID(v int64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateGroupID() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateGroupID()
	})
}

// ClearGroupID clears the value of the "group_id" field.
func (u *APIKeyUpsertOne) ClearGroupID() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearGroupID()
	})
}

// SetStatus sets the "status" field.
func (u *APIKeyUpsertOne) SetStatus(v string) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateStatus() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateStatus()
	})
}

// SetLastUsedAt sets the "last_used_at" field.
func (u *APIKeyUpsertOne) SetLastUsedAt(v time.Time) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetLastUsedAt(v)
	})
}

// UpdateLastUsedAt sets the "last_used_at" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateLastUsedAt() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateLastUsedAt()
	})
}

// ClearLastUsedAt clears the value of the "last_used_at" field.
func (u *APIKeyUpsertOne) ClearLastUsedAt() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearLastUsedAt()
	})
}

// SetIPWhitelist sets the "ip_whitelist" field.
func (u *APIKeyUpsertOne) SetIPWhitelist(v []string) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetIPWhitelist(v)
	})
}

// UpdateIPWhitelist sets the "ip_whitelist" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateIPWhitelist() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateIPWhitelist()
	})
}

// ClearIPWhitelist clears the value of the "ip_whitelist" field.
func (u *APIKeyUpsertOne) ClearIPWhitelist() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearIPWhitelist()
	})
}

// SetIPBlacklist sets the "ip_blacklist" field.
func (u *APIKeyUpsertOne) SetIPBlacklist(v []string) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetIPBlacklist(v)
	})
}

// UpdateIPBlacklist sets the "ip_blacklist" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateIPBlacklist() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateIPBlacklist()
	})
}

// ClearIPBlacklist clears the value of the "ip_blacklist" field.
func (u *APIKeyUpsertOne) ClearIPBlacklist() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearIPBlacklist()
	})
}

// SetQuota sets the "quota" field.
func (u *APIKeyUpsertOne) SetQuota(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetQuota(v)
	})
}

// AddQuota adds v to the "quota" field.
func (u *APIKeyUpsertOne) AddQuota(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddQuota(v)
	})
}

// UpdateQuota sets the "quota" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateQuota() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateQuota()
	})
}

// SetQuotaUsed sets the "quota_used" field.
func (u *APIKeyUpsertOne) SetQuotaUsed(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetQuotaUsed(v)
	})
}

// AddQuotaUsed adds v to the "quota_used" field.
func (u *APIKeyUpsertOne) AddQuotaUsed(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddQuotaUsed(v)
	})
}

// UpdateQuotaUsed sets the "quota_used" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateQuotaUsed() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateQuotaUsed()
	})
}

// SetExpiresAt sets the "expires_at" field.
func (u *APIKeyUpsertOne) SetExpiresAt(v time.Time) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetExpiresAt(v)
	})
}

// UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateExpiresAt() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateExpiresAt()
	})
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (u *APIKeyUpsertOne) ClearExpiresAt() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearExpiresAt()
	})
}

// SetRateLimit5h sets the "rate_limit_5h" field.
func (u *APIKeyUpsertOne) SetRateLimit5h(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetRateLimit5h(v)
	})
}

// AddRateLimit5h adds v to the "rate_limit_5h" field.
func (u *APIKeyUpsertOne) AddRateLimit5h(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddRateLimit5h(v)
	})
}

// UpdateRateLimit5h sets the "rate_limit_5h" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateRateLimit5h() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateRateLimit5h()
	})
}

// SetRateLimit1d sets the "rate_limit_1d" field.
func (u *APIKeyUpsertOne) SetRateLimit1d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetRateLimit1d(v)
	})
}

// AddRateLimit1d adds v to the "rate_limit_1d" field.
func (u *APIKeyUpsertOne) AddRateLimit1d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddRateLimit1d(v)
	})
}

// UpdateRateLimit1d sets the "rate_limit_1d" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateRateLimit1d() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateRateLimit1d()
	})
}

// SetRateLimit7d sets the "rate_limit_7d" field.
func (u *APIKeyUpsertOne) SetRateLimit7d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetRateLimit7d(v)
	})
}

// AddRateLimit7d adds v to the "rate_limit_7d" field.
func (u *APIKeyUpsertOne) AddRateLimit7d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddRateLimit7d(v)
	})
}

// UpdateRateLimit7d sets the "rate_limit_7d" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateRateLimit7d() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateRateLimit7d()
	})
}

// SetUsage5h sets the "usage_5h" field.
func (u *APIKeyUpsertOne) SetUsage5h(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUsage5h(v)
	})
}

// AddUsage5h adds v to the "usage_5h" field.
func (u *APIKeyUpsertOne) AddUsage5h(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddUsage5h(v)
	})
}

// UpdateUsage5h sets the "usage_5h" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateUsage5h() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUsage5h()
	})
}

// SetUsage1d sets the "usage_1d" field.
func (u *APIKeyUpsertOne) SetUsage1d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUsage1d(v)
	})
}

// AddUsage1d adds v to the "usage_1d" field.
func (u *APIKeyUpsertOne) AddUsage1d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddUsage1d(v)
	})
}

// UpdateUsage1d sets the "usage_1d" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateUsage1d() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUsage1d()
	})
}

// SetUsage7d sets the "usage_7d" field.
func (u *APIKeyUpsertOne) SetUsage7d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUsage7d(v)
	})
}

// AddUsage7d adds v to the "usage_7d" field.
func (u *APIKeyUpsertOne) AddUsage7d(v float64) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddUsage7d(v)
	})
}

// UpdateUsage7d sets the "usage_7d" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateUsage7d() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUsage7d()
	})
}

// SetWindow5hStart sets the "window_5h_start" field.
func (u *APIKeyUpsertOne) SetWindow5hStart(v time.Time) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetWindow5hStart(v)
	})
}

// UpdateWindow5hStart sets the "window_5h_start" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateWindow5hStart() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateWindow5hStart()
	})
}

// ClearWindow5hStart clears the value of the "window_5h_start" field.
func (u *APIKeyUpsertOne) ClearWindow5hStart() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearWindow5hStart()
	})
}

// SetWindow1dStart sets the "window_1d_start" field.
func (u *APIKeyUpsertOne) SetWindow1dStart(v time.Time) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetWindow1dStart(v)
	})
}

// UpdateWindow1dStart sets the "window_1d_start" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateWindow1dStart() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateWindow1dStart()
	})
}

// ClearWindow1dStart clears the value of the "window_1d_start" field.
func (u *APIKeyUpsertOne) ClearWindow1dStart() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearWindow1dStart()
	})
}

// SetWindow7dStart sets the "window_7d_start" field.
func (u *APIKeyUpsertOne) SetWindow7dStart(v time.Time) *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetWindow7dStart(v)
	})
}

// UpdateWindow7dStart sets the "window_7d_start" field to the value that was provided on create.
func (u *APIKeyUpsertOne) UpdateWindow7dStart() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateWindow7dStart()
	})
}

// ClearWindow7dStart clears the value of the "window_7d_start" field.
func (u *APIKeyUpsertOne) ClearWindow7dStart() *APIKeyUpsertOne {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearWindow7dStart()
	})
}

// Exec executes the query.
func (u *APIKeyUpsertOne) Exec(ctx context.Context) error {
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for APIKeyCreate.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *APIKeyUpsertOne) 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 *APIKeyUpsertOne) ID(ctx context.Context) (id int64, err error) {
	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 *APIKeyUpsertOne) IDX(ctx context.Context) int64 {
	id, err := u.ID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// APIKeyCreateBulk is the builder for creating many APIKey entities in bulk.
type APIKeyCreateBulk struct {
	config
	err      error
	builders []*APIKeyCreate
	conflict []sql.ConflictOption
}

// Save creates the APIKey entities in the database.
func (_c *APIKeyCreateBulk) Save(ctx context.Context) ([]*APIKey, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*APIKey, 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.(*APIKeyMutation)
				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
				if specs[i].ID.Value != nil {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(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 *APIKeyCreateBulk) SaveX(ctx context.Context) []*APIKey {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (_c *APIKeyCreateBulk) Exec(ctx context.Context) error {
	_, err := _c.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_c *APIKeyCreateBulk) 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.APIKey.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.APIKeyUpsert) {
//			SetCreatedAt(v+v).
//		}).
//		Exec(ctx)
func (_c *APIKeyCreateBulk) OnConflict(opts ...sql.ConflictOption) *APIKeyUpsertBulk {
	_c.conflict = opts
	return &APIKeyUpsertBulk{
		create: _c,
	}
}

// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
//	client.APIKey.Create().
//		OnConflict(sql.ConflictColumns(columns...)).
//		Exec(ctx)
func (_c *APIKeyCreateBulk) OnConflictColumns(columns ...string) *APIKeyUpsertBulk {
	_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
	return &APIKeyUpsertBulk{
		create: _c,
	}
}

// APIKeyUpsertBulk is the builder for "upsert"-ing
// a bulk of APIKey nodes.
type APIKeyUpsertBulk struct {
	create *APIKeyCreateBulk
}

// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
//	client.APIKey.Create().
//		OnConflict(
//			sql.ResolveWithNewValues(),
//		).
//		Exec(ctx)
func (u *APIKeyUpsertBulk) UpdateNewValues() *APIKeyUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(apikey.FieldCreatedAt)
			}
		}
	}))
	return u
}

// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
//	client.APIKey.Create().
//		OnConflict(sql.ResolveWithIgnore()).
//		Exec(ctx)
func (u *APIKeyUpsertBulk) Ignore() *APIKeyUpsertBulk {
	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 *APIKeyUpsertBulk) DoNothing() *APIKeyUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.DoNothing())
	return u
}

// Update allows overriding fields `UPDATE` values. See the APIKeyCreateBulk.OnConflict
// documentation for more info.
func (u *APIKeyUpsertBulk) Update(set func(*APIKeyUpsert)) *APIKeyUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
		set(&APIKeyUpsert{UpdateSet: update})
	}))
	return u
}

// SetUpdatedAt sets the "updated_at" field.
func (u *APIKeyUpsertBulk) SetUpdatedAt(v time.Time) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUpdatedAt(v)
	})
}

// UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateUpdatedAt() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUpdatedAt()
	})
}

// SetDeletedAt sets the "deleted_at" field.
func (u *APIKeyUpsertBulk) SetDeletedAt(v time.Time) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetDeletedAt(v)
	})
}

// UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateDeletedAt() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateDeletedAt()
	})
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (u *APIKeyUpsertBulk) ClearDeletedAt() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearDeletedAt()
	})
}

// SetUserID sets the "user_id" field.
func (u *APIKeyUpsertBulk) SetUserID(v int64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUserID(v)
	})
}

// UpdateUserID sets the "user_id" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateUserID() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUserID()
	})
}

// SetKey sets the "key" field.
func (u *APIKeyUpsertBulk) SetKey(v string) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetKey(v)
	})
}

// UpdateKey sets the "key" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateKey() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateKey()
	})
}

// SetName sets the "name" field.
func (u *APIKeyUpsertBulk) SetName(v string) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetName(v)
	})
}

// UpdateName sets the "name" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateName() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateName()
	})
}

// SetGroupID sets the "group_id" field.
func (u *APIKeyUpsertBulk) SetGroupID(v int64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetGroupID(v)
	})
}

// UpdateGroupID sets the "group_id" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateGroupID() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateGroupID()
	})
}

// ClearGroupID clears the value of the "group_id" field.
func (u *APIKeyUpsertBulk) ClearGroupID() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearGroupID()
	})
}

// SetStatus sets the "status" field.
func (u *APIKeyUpsertBulk) SetStatus(v string) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetStatus(v)
	})
}

// UpdateStatus sets the "status" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateStatus() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateStatus()
	})
}

// SetLastUsedAt sets the "last_used_at" field.
func (u *APIKeyUpsertBulk) SetLastUsedAt(v time.Time) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetLastUsedAt(v)
	})
}

// UpdateLastUsedAt sets the "last_used_at" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateLastUsedAt() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateLastUsedAt()
	})
}

// ClearLastUsedAt clears the value of the "last_used_at" field.
func (u *APIKeyUpsertBulk) ClearLastUsedAt() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearLastUsedAt()
	})
}

// SetIPWhitelist sets the "ip_whitelist" field.
func (u *APIKeyUpsertBulk) SetIPWhitelist(v []string) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetIPWhitelist(v)
	})
}

// UpdateIPWhitelist sets the "ip_whitelist" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateIPWhitelist() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateIPWhitelist()
	})
}

// ClearIPWhitelist clears the value of the "ip_whitelist" field.
func (u *APIKeyUpsertBulk) ClearIPWhitelist() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearIPWhitelist()
	})
}

// SetIPBlacklist sets the "ip_blacklist" field.
func (u *APIKeyUpsertBulk) SetIPBlacklist(v []string) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetIPBlacklist(v)
	})
}

// UpdateIPBlacklist sets the "ip_blacklist" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateIPBlacklist() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateIPBlacklist()
	})
}

// ClearIPBlacklist clears the value of the "ip_blacklist" field.
func (u *APIKeyUpsertBulk) ClearIPBlacklist() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearIPBlacklist()
	})
}

// SetQuota sets the "quota" field.
func (u *APIKeyUpsertBulk) SetQuota(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetQuota(v)
	})
}

// AddQuota adds v to the "quota" field.
func (u *APIKeyUpsertBulk) AddQuota(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddQuota(v)
	})
}

// UpdateQuota sets the "quota" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateQuota() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateQuota()
	})
}

// SetQuotaUsed sets the "quota_used" field.
func (u *APIKeyUpsertBulk) SetQuotaUsed(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetQuotaUsed(v)
	})
}

// AddQuotaUsed adds v to the "quota_used" field.
func (u *APIKeyUpsertBulk) AddQuotaUsed(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddQuotaUsed(v)
	})
}

// UpdateQuotaUsed sets the "quota_used" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateQuotaUsed() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateQuotaUsed()
	})
}

// SetExpiresAt sets the "expires_at" field.
func (u *APIKeyUpsertBulk) SetExpiresAt(v time.Time) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetExpiresAt(v)
	})
}

// UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateExpiresAt() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateExpiresAt()
	})
}

// ClearExpiresAt clears the value of the "expires_at" field.
func (u *APIKeyUpsertBulk) ClearExpiresAt() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearExpiresAt()
	})
}

// SetRateLimit5h sets the "rate_limit_5h" field.
func (u *APIKeyUpsertBulk) SetRateLimit5h(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetRateLimit5h(v)
	})
}

// AddRateLimit5h adds v to the "rate_limit_5h" field.
func (u *APIKeyUpsertBulk) AddRateLimit5h(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddRateLimit5h(v)
	})
}

// UpdateRateLimit5h sets the "rate_limit_5h" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateRateLimit5h() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateRateLimit5h()
	})
}

// SetRateLimit1d sets the "rate_limit_1d" field.
func (u *APIKeyUpsertBulk) SetRateLimit1d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetRateLimit1d(v)
	})
}

// AddRateLimit1d adds v to the "rate_limit_1d" field.
func (u *APIKeyUpsertBulk) AddRateLimit1d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddRateLimit1d(v)
	})
}

// UpdateRateLimit1d sets the "rate_limit_1d" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateRateLimit1d() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateRateLimit1d()
	})
}

// SetRateLimit7d sets the "rate_limit_7d" field.
func (u *APIKeyUpsertBulk) SetRateLimit7d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetRateLimit7d(v)
	})
}

// AddRateLimit7d adds v to the "rate_limit_7d" field.
func (u *APIKeyUpsertBulk) AddRateLimit7d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddRateLimit7d(v)
	})
}

// UpdateRateLimit7d sets the "rate_limit_7d" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateRateLimit7d() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateRateLimit7d()
	})
}

// SetUsage5h sets the "usage_5h" field.
func (u *APIKeyUpsertBulk) SetUsage5h(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUsage5h(v)
	})
}

// AddUsage5h adds v to the "usage_5h" field.
func (u *APIKeyUpsertBulk) AddUsage5h(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddUsage5h(v)
	})
}

// UpdateUsage5h sets the "usage_5h" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateUsage5h() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUsage5h()
	})
}

// SetUsage1d sets the "usage_1d" field.
func (u *APIKeyUpsertBulk) SetUsage1d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUsage1d(v)
	})
}

// AddUsage1d adds v to the "usage_1d" field.
func (u *APIKeyUpsertBulk) AddUsage1d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddUsage1d(v)
	})
}

// UpdateUsage1d sets the "usage_1d" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateUsage1d() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUsage1d()
	})
}

// SetUsage7d sets the "usage_7d" field.
func (u *APIKeyUpsertBulk) SetUsage7d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetUsage7d(v)
	})
}

// AddUsage7d adds v to the "usage_7d" field.
func (u *APIKeyUpsertBulk) AddUsage7d(v float64) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.AddUsage7d(v)
	})
}

// UpdateUsage7d sets the "usage_7d" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateUsage7d() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateUsage7d()
	})
}

// SetWindow5hStart sets the "window_5h_start" field.
func (u *APIKeyUpsertBulk) SetWindow5hStart(v time.Time) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetWindow5hStart(v)
	})
}

// UpdateWindow5hStart sets the "window_5h_start" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateWindow5hStart() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateWindow5hStart()
	})
}

// ClearWindow5hStart clears the value of the "window_5h_start" field.
func (u *APIKeyUpsertBulk) ClearWindow5hStart() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearWindow5hStart()
	})
}

// SetWindow1dStart sets the "window_1d_start" field.
func (u *APIKeyUpsertBulk) SetWindow1dStart(v time.Time) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetWindow1dStart(v)
	})
}

// UpdateWindow1dStart sets the "window_1d_start" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateWindow1dStart() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateWindow1dStart()
	})
}

// ClearWindow1dStart clears the value of the "window_1d_start" field.
func (u *APIKeyUpsertBulk) ClearWindow1dStart() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearWindow1dStart()
	})
}

// SetWindow7dStart sets the "window_7d_start" field.
func (u *APIKeyUpsertBulk) SetWindow7dStart(v time.Time) *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.SetWindow7dStart(v)
	})
}

// UpdateWindow7dStart sets the "window_7d_start" field to the value that was provided on create.
func (u *APIKeyUpsertBulk) UpdateWindow7dStart() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.UpdateWindow7dStart()
	})
}

// ClearWindow7dStart clears the value of the "window_7d_start" field.
func (u *APIKeyUpsertBulk) ClearWindow7dStart() *APIKeyUpsertBulk {
	return u.Update(func(s *APIKeyUpsert) {
		s.ClearWindow7dStart()
	})
}

// Exec executes the query.
func (u *APIKeyUpsertBulk) 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 APIKeyCreateBulk instead", i)
		}
	}
	if len(u.create.conflict) == 0 {
		return errors.New("ent: missing options for APIKeyCreateBulk.OnConflict")
	}
	return u.create.Exec(ctx)
}

// ExecX is like Exec, but panics if an error occurs.
func (u *APIKeyUpsertBulk) ExecX(ctx context.Context) {
	if err := u.create.Exec(ctx); err != nil {
		panic(err)
	}
}
