Files
upstream-ent/examples/migration/ent/session_create.go
upstream-mirror 4b3ad0b611
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped
snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 22:12:36 +05:00

327 lines
8.6 KiB
Go

// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/examples/migration/ent/session"
"entgo.io/ent/examples/migration/ent/sessiondevice"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
)
// SessionCreate is the builder for creating a Session entity.
type SessionCreate struct {
config
mutation *SessionMutation
hooks []Hook
}
// SetActive sets the "active" field.
func (_c *SessionCreate) SetActive(v bool) *SessionCreate {
_c.mutation.SetActive(v)
return _c
}
// SetNillableActive sets the "active" field if the given value is not nil.
func (_c *SessionCreate) SetNillableActive(v *bool) *SessionCreate {
if v != nil {
_c.SetActive(*v)
}
return _c
}
// SetIssuedAt sets the "issued_at" field.
func (_c *SessionCreate) SetIssuedAt(v time.Time) *SessionCreate {
_c.mutation.SetIssuedAt(v)
return _c
}
// SetExpiresAt sets the "expires_at" field.
func (_c *SessionCreate) SetExpiresAt(v time.Time) *SessionCreate {
_c.mutation.SetExpiresAt(v)
return _c
}
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_c *SessionCreate) SetNillableExpiresAt(v *time.Time) *SessionCreate {
if v != nil {
_c.SetExpiresAt(*v)
}
return _c
}
// SetToken sets the "token" field.
func (_c *SessionCreate) SetToken(v string) *SessionCreate {
_c.mutation.SetToken(v)
return _c
}
// SetNillableToken sets the "token" field if the given value is not nil.
func (_c *SessionCreate) SetNillableToken(v *string) *SessionCreate {
if v != nil {
_c.SetToken(*v)
}
return _c
}
// SetMethod sets the "method" field.
func (_c *SessionCreate) SetMethod(v map[string]interface{}) *SessionCreate {
_c.mutation.SetMethod(v)
return _c
}
// SetDeviceID sets the "device_id" field.
func (_c *SessionCreate) SetDeviceID(v uuid.UUID) *SessionCreate {
_c.mutation.SetDeviceID(v)
return _c
}
// SetNillableDeviceID sets the "device_id" field if the given value is not nil.
func (_c *SessionCreate) SetNillableDeviceID(v *uuid.UUID) *SessionCreate {
if v != nil {
_c.SetDeviceID(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *SessionCreate) SetID(v uuid.UUID) *SessionCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *SessionCreate) SetNillableID(v *uuid.UUID) *SessionCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetDevice sets the "device" edge to the SessionDevice entity.
func (_c *SessionCreate) SetDevice(v *SessionDevice) *SessionCreate {
return _c.SetDeviceID(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.Active(); !ok {
v := session.DefaultActive
_c.mutation.SetActive(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.Active(); !ok {
return &ValidationError{Name: "active", err: errors.New(`ent: missing required field "Session.active"`)}
}
if _, ok := _c.mutation.IssuedAt(); !ok {
return &ValidationError{Name: "issued_at", err: errors.New(`ent: missing required field "Session.issued_at"`)}
}
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.(*uuid.UUID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *SessionCreate) createSpec() (*Session, *sqlgraph.CreateSpec) {
var (
_node = &Session{config: _c.config}
_spec = sqlgraph.NewCreateSpec(session.Table, sqlgraph.NewFieldSpec(session.FieldID, field.TypeUUID))
)
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.Active(); ok {
_spec.SetField(session.FieldActive, field.TypeBool, value)
_node.Active = value
}
if value, ok := _c.mutation.IssuedAt(); ok {
_spec.SetField(session.FieldIssuedAt, field.TypeTime, value)
_node.IssuedAt = value
}
if value, ok := _c.mutation.ExpiresAt(); ok {
_spec.SetField(session.FieldExpiresAt, field.TypeTime, value)
_node.ExpiresAt = value
}
if value, ok := _c.mutation.Token(); ok {
_spec.SetField(session.FieldToken, field.TypeString, value)
_node.Token = value
}
if value, ok := _c.mutation.Method(); ok {
_spec.SetField(session.FieldMethod, field.TypeJSON, value)
_node.Method = value
}
if nodes := _c.mutation.DeviceIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: session.DeviceTable,
Columns: []string{session.DeviceColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(sessiondevice.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.DeviceID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// SessionCreateBulk is the builder for creating many Session entities in bulk.
type SessionCreateBulk struct {
config
err error
builders []*SessionCreate
}
// 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}
// 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)
}
}