email_create.go•21.3 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/Southclaws/storyden/internal/ent/account"
"github.com/Southclaws/storyden/internal/ent/email"
"github.com/rs/xid"
)
// EmailCreate is the builder for creating a Email entity.
type EmailCreate struct {
config
mutation *EmailMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *EmailCreate) SetCreatedAt(v time.Time) *EmailCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *EmailCreate) SetNillableCreatedAt(v *time.Time) *EmailCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetAccountID sets the "account_id" field.
func (_c *EmailCreate) SetAccountID(v xid.ID) *EmailCreate {
_c.mutation.SetAccountID(v)
return _c
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_c *EmailCreate) SetNillableAccountID(v *xid.ID) *EmailCreate {
if v != nil {
_c.SetAccountID(*v)
}
return _c
}
// SetEmailAddress sets the "email_address" field.
func (_c *EmailCreate) SetEmailAddress(v string) *EmailCreate {
_c.mutation.SetEmailAddress(v)
return _c
}
// SetVerificationCode sets the "verification_code" field.
func (_c *EmailCreate) SetVerificationCode(v string) *EmailCreate {
_c.mutation.SetVerificationCode(v)
return _c
}
// SetVerified sets the "verified" field.
func (_c *EmailCreate) SetVerified(v bool) *EmailCreate {
_c.mutation.SetVerified(v)
return _c
}
// SetNillableVerified sets the "verified" field if the given value is not nil.
func (_c *EmailCreate) SetNillableVerified(v *bool) *EmailCreate {
if v != nil {
_c.SetVerified(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *EmailCreate) SetID(v xid.ID) *EmailCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *EmailCreate) SetNillableID(v *xid.ID) *EmailCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetAccount sets the "account" edge to the Account entity.
func (_c *EmailCreate) SetAccount(v *Account) *EmailCreate {
return _c.SetAccountID(v.ID)
}
// Mutation returns the EmailMutation object of the builder.
func (_c *EmailCreate) Mutation() *EmailMutation {
return _c.mutation
}
// Save creates the Email in the database.
func (_c *EmailCreate) Save(ctx context.Context) (*Email, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *EmailCreate) SaveX(ctx context.Context) *Email {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *EmailCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *EmailCreate) 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 *EmailCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := email.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.Verified(); !ok {
v := email.DefaultVerified
_c.mutation.SetVerified(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := email.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *EmailCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Email.created_at"`)}
}
if _, ok := _c.mutation.EmailAddress(); !ok {
return &ValidationError{Name: "email_address", err: errors.New(`ent: missing required field "Email.email_address"`)}
}
if v, ok := _c.mutation.EmailAddress(); ok {
if err := email.EmailAddressValidator(v); err != nil {
return &ValidationError{Name: "email_address", err: fmt.Errorf(`ent: validator failed for field "Email.email_address": %w`, err)}
}
}
if _, ok := _c.mutation.VerificationCode(); !ok {
return &ValidationError{Name: "verification_code", err: errors.New(`ent: missing required field "Email.verification_code"`)}
}
if v, ok := _c.mutation.VerificationCode(); ok {
if err := email.VerificationCodeValidator(v); err != nil {
return &ValidationError{Name: "verification_code", err: fmt.Errorf(`ent: validator failed for field "Email.verification_code": %w`, err)}
}
}
if _, ok := _c.mutation.Verified(); !ok {
return &ValidationError{Name: "verified", err: errors.New(`ent: missing required field "Email.verified"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := email.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Email.id": %w`, err)}
}
}
return nil
}
func (_c *EmailCreate) sqlSave(ctx context.Context) (*Email, error) {
if err := _c.check(); err != nil {
return nil, err
}
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*xid.ID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *EmailCreate) createSpec() (*Email, *sqlgraph.CreateSpec) {
var (
_node = &Email{config: _c.config}
_spec = sqlgraph.NewCreateSpec(email.Table, sqlgraph.NewFieldSpec(email.FieldID, field.TypeString))
)
_spec.OnConflict = _c.conflict
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(email.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.EmailAddress(); ok {
_spec.SetField(email.FieldEmailAddress, field.TypeString, value)
_node.EmailAddress = value
}
if value, ok := _c.mutation.VerificationCode(); ok {
_spec.SetField(email.FieldVerificationCode, field.TypeString, value)
_node.VerificationCode = value
}
if value, ok := _c.mutation.Verified(); ok {
_spec.SetField(email.FieldVerified, field.TypeBool, value)
_node.Verified = value
}
if nodes := _c.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: email.AccountTable,
Columns: []string{email.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.AccountID = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.Email.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.EmailUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *EmailCreate) OnConflict(opts ...sql.ConflictOption) *EmailUpsertOne {
_c.conflict = opts
return &EmailUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Email.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *EmailCreate) OnConflictColumns(columns ...string) *EmailUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &EmailUpsertOne{
create: _c,
}
}
type (
// EmailUpsertOne is the builder for "upsert"-ing
// one Email node.
EmailUpsertOne struct {
create *EmailCreate
}
// EmailUpsert is the "OnConflict" setter.
EmailUpsert struct {
*sql.UpdateSet
}
)
// SetAccountID sets the "account_id" field.
func (u *EmailUpsert) SetAccountID(v xid.ID) *EmailUpsert {
u.Set(email.FieldAccountID, v)
return u
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *EmailUpsert) UpdateAccountID() *EmailUpsert {
u.SetExcluded(email.FieldAccountID)
return u
}
// ClearAccountID clears the value of the "account_id" field.
func (u *EmailUpsert) ClearAccountID() *EmailUpsert {
u.SetNull(email.FieldAccountID)
return u
}
// SetVerificationCode sets the "verification_code" field.
func (u *EmailUpsert) SetVerificationCode(v string) *EmailUpsert {
u.Set(email.FieldVerificationCode, v)
return u
}
// UpdateVerificationCode sets the "verification_code" field to the value that was provided on create.
func (u *EmailUpsert) UpdateVerificationCode() *EmailUpsert {
u.SetExcluded(email.FieldVerificationCode)
return u
}
// SetVerified sets the "verified" field.
func (u *EmailUpsert) SetVerified(v bool) *EmailUpsert {
u.Set(email.FieldVerified, v)
return u
}
// UpdateVerified sets the "verified" field to the value that was provided on create.
func (u *EmailUpsert) UpdateVerified() *EmailUpsert {
u.SetExcluded(email.FieldVerified)
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.Email.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(email.FieldID)
// }),
// ).
// Exec(ctx)
func (u *EmailUpsertOne) UpdateNewValues() *EmailUpsertOne {
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(email.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(email.FieldCreatedAt)
}
if _, exists := u.create.mutation.EmailAddress(); exists {
s.SetIgnore(email.FieldEmailAddress)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Email.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *EmailUpsertOne) Ignore() *EmailUpsertOne {
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 *EmailUpsertOne) DoNothing() *EmailUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the EmailCreate.OnConflict
// documentation for more info.
func (u *EmailUpsertOne) Update(set func(*EmailUpsert)) *EmailUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&EmailUpsert{UpdateSet: update})
}))
return u
}
// SetAccountID sets the "account_id" field.
func (u *EmailUpsertOne) SetAccountID(v xid.ID) *EmailUpsertOne {
return u.Update(func(s *EmailUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *EmailUpsertOne) UpdateAccountID() *EmailUpsertOne {
return u.Update(func(s *EmailUpsert) {
s.UpdateAccountID()
})
}
// ClearAccountID clears the value of the "account_id" field.
func (u *EmailUpsertOne) ClearAccountID() *EmailUpsertOne {
return u.Update(func(s *EmailUpsert) {
s.ClearAccountID()
})
}
// SetVerificationCode sets the "verification_code" field.
func (u *EmailUpsertOne) SetVerificationCode(v string) *EmailUpsertOne {
return u.Update(func(s *EmailUpsert) {
s.SetVerificationCode(v)
})
}
// UpdateVerificationCode sets the "verification_code" field to the value that was provided on create.
func (u *EmailUpsertOne) UpdateVerificationCode() *EmailUpsertOne {
return u.Update(func(s *EmailUpsert) {
s.UpdateVerificationCode()
})
}
// SetVerified sets the "verified" field.
func (u *EmailUpsertOne) SetVerified(v bool) *EmailUpsertOne {
return u.Update(func(s *EmailUpsert) {
s.SetVerified(v)
})
}
// UpdateVerified sets the "verified" field to the value that was provided on create.
func (u *EmailUpsertOne) UpdateVerified() *EmailUpsertOne {
return u.Update(func(s *EmailUpsert) {
s.UpdateVerified()
})
}
// Exec executes the query.
func (u *EmailUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for EmailCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *EmailUpsertOne) 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 *EmailUpsertOne) ID(ctx context.Context) (id xid.ID, err error) {
if u.create.driver.Dialect() == dialect.MySQL {
// In case of "ON CONFLICT", there is no way to get back non-numeric ID
// fields from the database since MySQL does not support the RETURNING clause.
return id, errors.New("ent: EmailUpsertOne.ID is not supported by MySQL driver. Use EmailUpsertOne.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 *EmailUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// EmailCreateBulk is the builder for creating many Email entities in bulk.
type EmailCreateBulk struct {
config
err error
builders []*EmailCreate
conflict []sql.ConflictOption
}
// Save creates the Email entities in the database.
func (_c *EmailCreateBulk) Save(ctx context.Context) ([]*Email, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Email, 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.(*EmailMutation)
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 *EmailCreateBulk) SaveX(ctx context.Context) []*Email {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *EmailCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *EmailCreateBulk) 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.Email.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.EmailUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *EmailCreateBulk) OnConflict(opts ...sql.ConflictOption) *EmailUpsertBulk {
_c.conflict = opts
return &EmailUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Email.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *EmailCreateBulk) OnConflictColumns(columns ...string) *EmailUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &EmailUpsertBulk{
create: _c,
}
}
// EmailUpsertBulk is the builder for "upsert"-ing
// a bulk of Email nodes.
type EmailUpsertBulk struct {
create *EmailCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Email.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(email.FieldID)
// }),
// ).
// Exec(ctx)
func (u *EmailUpsertBulk) UpdateNewValues() *EmailUpsertBulk {
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(email.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(email.FieldCreatedAt)
}
if _, exists := b.mutation.EmailAddress(); exists {
s.SetIgnore(email.FieldEmailAddress)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Email.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *EmailUpsertBulk) Ignore() *EmailUpsertBulk {
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 *EmailUpsertBulk) DoNothing() *EmailUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the EmailCreateBulk.OnConflict
// documentation for more info.
func (u *EmailUpsertBulk) Update(set func(*EmailUpsert)) *EmailUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&EmailUpsert{UpdateSet: update})
}))
return u
}
// SetAccountID sets the "account_id" field.
func (u *EmailUpsertBulk) SetAccountID(v xid.ID) *EmailUpsertBulk {
return u.Update(func(s *EmailUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *EmailUpsertBulk) UpdateAccountID() *EmailUpsertBulk {
return u.Update(func(s *EmailUpsert) {
s.UpdateAccountID()
})
}
// ClearAccountID clears the value of the "account_id" field.
func (u *EmailUpsertBulk) ClearAccountID() *EmailUpsertBulk {
return u.Update(func(s *EmailUpsert) {
s.ClearAccountID()
})
}
// SetVerificationCode sets the "verification_code" field.
func (u *EmailUpsertBulk) SetVerificationCode(v string) *EmailUpsertBulk {
return u.Update(func(s *EmailUpsert) {
s.SetVerificationCode(v)
})
}
// UpdateVerificationCode sets the "verification_code" field to the value that was provided on create.
func (u *EmailUpsertBulk) UpdateVerificationCode() *EmailUpsertBulk {
return u.Update(func(s *EmailUpsert) {
s.UpdateVerificationCode()
})
}
// SetVerified sets the "verified" field.
func (u *EmailUpsertBulk) SetVerified(v bool) *EmailUpsertBulk {
return u.Update(func(s *EmailUpsert) {
s.SetVerified(v)
})
}
// UpdateVerified sets the "verified" field to the value that was provided on create.
func (u *EmailUpsertBulk) UpdateVerified() *EmailUpsertBulk {
return u.Update(func(s *EmailUpsert) {
s.UpdateVerified()
})
}
// Exec executes the query.
func (u *EmailUpsertBulk) 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 EmailCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for EmailCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *EmailUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}