session_create.go•18.5 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/session"
"github.com/rs/xid"
)
// SessionCreate is the builder for creating a Session entity.
type SessionCreate struct {
config
mutation *SessionMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *SessionCreate) SetCreatedAt(v time.Time) *SessionCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *SessionCreate) SetNillableCreatedAt(v *time.Time) *SessionCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetAccountID sets the "account_id" field.
func (_c *SessionCreate) SetAccountID(v xid.ID) *SessionCreate {
_c.mutation.SetAccountID(v)
return _c
}
// SetExpiresAt sets the "expires_at" field.
func (_c *SessionCreate) SetExpiresAt(v time.Time) *SessionCreate {
_c.mutation.SetExpiresAt(v)
return _c
}
// SetRevokedAt sets the "revoked_at" field.
func (_c *SessionCreate) SetRevokedAt(v time.Time) *SessionCreate {
_c.mutation.SetRevokedAt(v)
return _c
}
// SetNillableRevokedAt sets the "revoked_at" field if the given value is not nil.
func (_c *SessionCreate) SetNillableRevokedAt(v *time.Time) *SessionCreate {
if v != nil {
_c.SetRevokedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *SessionCreate) SetID(v xid.ID) *SessionCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *SessionCreate) SetNillableID(v *xid.ID) *SessionCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetAccount sets the "account" edge to the Account entity.
func (_c *SessionCreate) SetAccount(v *Account) *SessionCreate {
return _c.SetAccountID(v.ID)
}
// Mutation returns the SessionMutation object of the builder.
func (_c *SessionCreate) Mutation() *SessionMutation {
return _c.mutation
}
// Save creates the Session in the database.
func (_c *SessionCreate) Save(ctx context.Context) (*Session, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *SessionCreate) SaveX(ctx context.Context) *Session {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *SessionCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *SessionCreate) 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 *SessionCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := session.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := session.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *SessionCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Session.created_at"`)}
}
if _, ok := _c.mutation.AccountID(); !ok {
return &ValidationError{Name: "account_id", err: errors.New(`ent: missing required field "Session.account_id"`)}
}
if v, ok := _c.mutation.AccountID(); ok {
if err := session.AccountIDValidator(v.String()); err != nil {
return &ValidationError{Name: "account_id", err: fmt.Errorf(`ent: validator failed for field "Session.account_id": %w`, err)}
}
}
if _, ok := _c.mutation.ExpiresAt(); !ok {
return &ValidationError{Name: "expires_at", err: errors.New(`ent: missing required field "Session.expires_at"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := session.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "Session.id": %w`, err)}
}
}
if len(_c.mutation.AccountIDs()) == 0 {
return &ValidationError{Name: "account", err: errors.New(`ent: missing required edge "Session.account"`)}
}
return nil
}
func (_c *SessionCreate) sqlSave(ctx context.Context) (*Session, 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 *SessionCreate) createSpec() (*Session, *sqlgraph.CreateSpec) {
var (
_node = &Session{config: _c.config}
_spec = sqlgraph.NewCreateSpec(session.Table, sqlgraph.NewFieldSpec(session.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(session.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.ExpiresAt(); ok {
_spec.SetField(session.FieldExpiresAt, field.TypeTime, value)
_node.ExpiresAt = value
}
if value, ok := _c.mutation.RevokedAt(); ok {
_spec.SetField(session.FieldRevokedAt, field.TypeTime, value)
_node.RevokedAt = &value
}
if nodes := _c.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: session.AccountTable,
Columns: []string{session.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.Session.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.SessionUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *SessionCreate) OnConflict(opts ...sql.ConflictOption) *SessionUpsertOne {
_c.conflict = opts
return &SessionUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Session.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *SessionCreate) OnConflictColumns(columns ...string) *SessionUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &SessionUpsertOne{
create: _c,
}
}
type (
// SessionUpsertOne is the builder for "upsert"-ing
// one Session node.
SessionUpsertOne struct {
create *SessionCreate
}
// SessionUpsert is the "OnConflict" setter.
SessionUpsert struct {
*sql.UpdateSet
}
)
// SetRevokedAt sets the "revoked_at" field.
func (u *SessionUpsert) SetRevokedAt(v time.Time) *SessionUpsert {
u.Set(session.FieldRevokedAt, v)
return u
}
// UpdateRevokedAt sets the "revoked_at" field to the value that was provided on create.
func (u *SessionUpsert) UpdateRevokedAt() *SessionUpsert {
u.SetExcluded(session.FieldRevokedAt)
return u
}
// ClearRevokedAt clears the value of the "revoked_at" field.
func (u *SessionUpsert) ClearRevokedAt() *SessionUpsert {
u.SetNull(session.FieldRevokedAt)
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.Session.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(session.FieldID)
// }),
// ).
// Exec(ctx)
func (u *SessionUpsertOne) UpdateNewValues() *SessionUpsertOne {
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(session.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(session.FieldCreatedAt)
}
if _, exists := u.create.mutation.AccountID(); exists {
s.SetIgnore(session.FieldAccountID)
}
if _, exists := u.create.mutation.ExpiresAt(); exists {
s.SetIgnore(session.FieldExpiresAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Session.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *SessionUpsertOne) Ignore() *SessionUpsertOne {
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 *SessionUpsertOne) DoNothing() *SessionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the SessionCreate.OnConflict
// documentation for more info.
func (u *SessionUpsertOne) Update(set func(*SessionUpsert)) *SessionUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&SessionUpsert{UpdateSet: update})
}))
return u
}
// SetRevokedAt sets the "revoked_at" field.
func (u *SessionUpsertOne) SetRevokedAt(v time.Time) *SessionUpsertOne {
return u.Update(func(s *SessionUpsert) {
s.SetRevokedAt(v)
})
}
// UpdateRevokedAt sets the "revoked_at" field to the value that was provided on create.
func (u *SessionUpsertOne) UpdateRevokedAt() *SessionUpsertOne {
return u.Update(func(s *SessionUpsert) {
s.UpdateRevokedAt()
})
}
// ClearRevokedAt clears the value of the "revoked_at" field.
func (u *SessionUpsertOne) ClearRevokedAt() *SessionUpsertOne {
return u.Update(func(s *SessionUpsert) {
s.ClearRevokedAt()
})
}
// Exec executes the query.
func (u *SessionUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for SessionCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *SessionUpsertOne) 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 *SessionUpsertOne) 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: SessionUpsertOne.ID is not supported by MySQL driver. Use SessionUpsertOne.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 *SessionUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// SessionCreateBulk is the builder for creating many Session entities in bulk.
type SessionCreateBulk struct {
config
err error
builders []*SessionCreate
conflict []sql.ConflictOption
}
// Save creates the Session entities in the database.
func (_c *SessionCreateBulk) Save(ctx context.Context) ([]*Session, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Session, 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.(*SessionMutation)
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 *SessionCreateBulk) SaveX(ctx context.Context) []*Session {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *SessionCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *SessionCreateBulk) 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.Session.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.SessionUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *SessionCreateBulk) OnConflict(opts ...sql.ConflictOption) *SessionUpsertBulk {
_c.conflict = opts
return &SessionUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.Session.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *SessionCreateBulk) OnConflictColumns(columns ...string) *SessionUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &SessionUpsertBulk{
create: _c,
}
}
// SessionUpsertBulk is the builder for "upsert"-ing
// a bulk of Session nodes.
type SessionUpsertBulk struct {
create *SessionCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.Session.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(session.FieldID)
// }),
// ).
// Exec(ctx)
func (u *SessionUpsertBulk) UpdateNewValues() *SessionUpsertBulk {
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(session.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(session.FieldCreatedAt)
}
if _, exists := b.mutation.AccountID(); exists {
s.SetIgnore(session.FieldAccountID)
}
if _, exists := b.mutation.ExpiresAt(); exists {
s.SetIgnore(session.FieldExpiresAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.Session.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *SessionUpsertBulk) Ignore() *SessionUpsertBulk {
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 *SessionUpsertBulk) DoNothing() *SessionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the SessionCreateBulk.OnConflict
// documentation for more info.
func (u *SessionUpsertBulk) Update(set func(*SessionUpsert)) *SessionUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&SessionUpsert{UpdateSet: update})
}))
return u
}
// SetRevokedAt sets the "revoked_at" field.
func (u *SessionUpsertBulk) SetRevokedAt(v time.Time) *SessionUpsertBulk {
return u.Update(func(s *SessionUpsert) {
s.SetRevokedAt(v)
})
}
// UpdateRevokedAt sets the "revoked_at" field to the value that was provided on create.
func (u *SessionUpsertBulk) UpdateRevokedAt() *SessionUpsertBulk {
return u.Update(func(s *SessionUpsert) {
s.UpdateRevokedAt()
})
}
// ClearRevokedAt clears the value of the "revoked_at" field.
func (u *SessionUpsertBulk) ClearRevokedAt() *SessionUpsertBulk {
return u.Update(func(s *SessionUpsert) {
s.ClearRevokedAt()
})
}
// Exec executes the query.
func (u *SessionUpsertBulk) 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 SessionCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for SessionCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *SessionUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}