postread_create.go•19.6 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/post"
"github.com/Southclaws/storyden/internal/ent/postread"
"github.com/rs/xid"
)
// PostReadCreate is the builder for creating a PostRead entity.
type PostReadCreate struct {
config
mutation *PostReadMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetRootPostID sets the "root_post_id" field.
func (_c *PostReadCreate) SetRootPostID(v xid.ID) *PostReadCreate {
_c.mutation.SetRootPostID(v)
return _c
}
// SetAccountID sets the "account_id" field.
func (_c *PostReadCreate) SetAccountID(v xid.ID) *PostReadCreate {
_c.mutation.SetAccountID(v)
return _c
}
// SetLastSeenAt sets the "last_seen_at" field.
func (_c *PostReadCreate) SetLastSeenAt(v time.Time) *PostReadCreate {
_c.mutation.SetLastSeenAt(v)
return _c
}
// SetID sets the "id" field.
func (_c *PostReadCreate) SetID(v xid.ID) *PostReadCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *PostReadCreate) SetNillableID(v *xid.ID) *PostReadCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetRootPost sets the "root_post" edge to the Post entity.
func (_c *PostReadCreate) SetRootPost(v *Post) *PostReadCreate {
return _c.SetRootPostID(v.ID)
}
// SetAccount sets the "account" edge to the Account entity.
func (_c *PostReadCreate) SetAccount(v *Account) *PostReadCreate {
return _c.SetAccountID(v.ID)
}
// Mutation returns the PostReadMutation object of the builder.
func (_c *PostReadCreate) Mutation() *PostReadMutation {
return _c.mutation
}
// Save creates the PostRead in the database.
func (_c *PostReadCreate) Save(ctx context.Context) (*PostRead, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *PostReadCreate) SaveX(ctx context.Context) *PostRead {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *PostReadCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *PostReadCreate) 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 *PostReadCreate) defaults() {
if _, ok := _c.mutation.ID(); !ok {
v := postread.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *PostReadCreate) check() error {
if _, ok := _c.mutation.RootPostID(); !ok {
return &ValidationError{Name: "root_post_id", err: errors.New(`ent: missing required field "PostRead.root_post_id"`)}
}
if _, ok := _c.mutation.AccountID(); !ok {
return &ValidationError{Name: "account_id", err: errors.New(`ent: missing required field "PostRead.account_id"`)}
}
if _, ok := _c.mutation.LastSeenAt(); !ok {
return &ValidationError{Name: "last_seen_at", err: errors.New(`ent: missing required field "PostRead.last_seen_at"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := postread.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "PostRead.id": %w`, err)}
}
}
if len(_c.mutation.RootPostIDs()) == 0 {
return &ValidationError{Name: "root_post", err: errors.New(`ent: missing required edge "PostRead.root_post"`)}
}
if len(_c.mutation.AccountIDs()) == 0 {
return &ValidationError{Name: "account", err: errors.New(`ent: missing required edge "PostRead.account"`)}
}
return nil
}
func (_c *PostReadCreate) sqlSave(ctx context.Context) (*PostRead, 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 *PostReadCreate) createSpec() (*PostRead, *sqlgraph.CreateSpec) {
var (
_node = &PostRead{config: _c.config}
_spec = sqlgraph.NewCreateSpec(postread.Table, sqlgraph.NewFieldSpec(postread.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.LastSeenAt(); ok {
_spec.SetField(postread.FieldLastSeenAt, field.TypeTime, value)
_node.LastSeenAt = value
}
if nodes := _c.mutation.RootPostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.RootPostTable,
Columns: []string{postread.RootPostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.RootPostID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.AccountTable,
Columns: []string{postread.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.PostRead.Create().
// SetRootPostID(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.PostReadUpsert) {
// SetRootPostID(v+v).
// }).
// Exec(ctx)
func (_c *PostReadCreate) OnConflict(opts ...sql.ConflictOption) *PostReadUpsertOne {
_c.conflict = opts
return &PostReadUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.PostRead.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *PostReadCreate) OnConflictColumns(columns ...string) *PostReadUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &PostReadUpsertOne{
create: _c,
}
}
type (
// PostReadUpsertOne is the builder for "upsert"-ing
// one PostRead node.
PostReadUpsertOne struct {
create *PostReadCreate
}
// PostReadUpsert is the "OnConflict" setter.
PostReadUpsert struct {
*sql.UpdateSet
}
)
// SetRootPostID sets the "root_post_id" field.
func (u *PostReadUpsert) SetRootPostID(v xid.ID) *PostReadUpsert {
u.Set(postread.FieldRootPostID, v)
return u
}
// UpdateRootPostID sets the "root_post_id" field to the value that was provided on create.
func (u *PostReadUpsert) UpdateRootPostID() *PostReadUpsert {
u.SetExcluded(postread.FieldRootPostID)
return u
}
// SetAccountID sets the "account_id" field.
func (u *PostReadUpsert) SetAccountID(v xid.ID) *PostReadUpsert {
u.Set(postread.FieldAccountID, v)
return u
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *PostReadUpsert) UpdateAccountID() *PostReadUpsert {
u.SetExcluded(postread.FieldAccountID)
return u
}
// SetLastSeenAt sets the "last_seen_at" field.
func (u *PostReadUpsert) SetLastSeenAt(v time.Time) *PostReadUpsert {
u.Set(postread.FieldLastSeenAt, v)
return u
}
// UpdateLastSeenAt sets the "last_seen_at" field to the value that was provided on create.
func (u *PostReadUpsert) UpdateLastSeenAt() *PostReadUpsert {
u.SetExcluded(postread.FieldLastSeenAt)
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.PostRead.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(postread.FieldID)
// }),
// ).
// Exec(ctx)
func (u *PostReadUpsertOne) UpdateNewValues() *PostReadUpsertOne {
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(postread.FieldID)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.PostRead.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *PostReadUpsertOne) Ignore() *PostReadUpsertOne {
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 *PostReadUpsertOne) DoNothing() *PostReadUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the PostReadCreate.OnConflict
// documentation for more info.
func (u *PostReadUpsertOne) Update(set func(*PostReadUpsert)) *PostReadUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&PostReadUpsert{UpdateSet: update})
}))
return u
}
// SetRootPostID sets the "root_post_id" field.
func (u *PostReadUpsertOne) SetRootPostID(v xid.ID) *PostReadUpsertOne {
return u.Update(func(s *PostReadUpsert) {
s.SetRootPostID(v)
})
}
// UpdateRootPostID sets the "root_post_id" field to the value that was provided on create.
func (u *PostReadUpsertOne) UpdateRootPostID() *PostReadUpsertOne {
return u.Update(func(s *PostReadUpsert) {
s.UpdateRootPostID()
})
}
// SetAccountID sets the "account_id" field.
func (u *PostReadUpsertOne) SetAccountID(v xid.ID) *PostReadUpsertOne {
return u.Update(func(s *PostReadUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *PostReadUpsertOne) UpdateAccountID() *PostReadUpsertOne {
return u.Update(func(s *PostReadUpsert) {
s.UpdateAccountID()
})
}
// SetLastSeenAt sets the "last_seen_at" field.
func (u *PostReadUpsertOne) SetLastSeenAt(v time.Time) *PostReadUpsertOne {
return u.Update(func(s *PostReadUpsert) {
s.SetLastSeenAt(v)
})
}
// UpdateLastSeenAt sets the "last_seen_at" field to the value that was provided on create.
func (u *PostReadUpsertOne) UpdateLastSeenAt() *PostReadUpsertOne {
return u.Update(func(s *PostReadUpsert) {
s.UpdateLastSeenAt()
})
}
// Exec executes the query.
func (u *PostReadUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for PostReadCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *PostReadUpsertOne) 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 *PostReadUpsertOne) 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: PostReadUpsertOne.ID is not supported by MySQL driver. Use PostReadUpsertOne.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 *PostReadUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// PostReadCreateBulk is the builder for creating many PostRead entities in bulk.
type PostReadCreateBulk struct {
config
err error
builders []*PostReadCreate
conflict []sql.ConflictOption
}
// Save creates the PostRead entities in the database.
func (_c *PostReadCreateBulk) Save(ctx context.Context) ([]*PostRead, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*PostRead, 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.(*PostReadMutation)
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 *PostReadCreateBulk) SaveX(ctx context.Context) []*PostRead {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *PostReadCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *PostReadCreateBulk) 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.PostRead.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.PostReadUpsert) {
// SetRootPostID(v+v).
// }).
// Exec(ctx)
func (_c *PostReadCreateBulk) OnConflict(opts ...sql.ConflictOption) *PostReadUpsertBulk {
_c.conflict = opts
return &PostReadUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.PostRead.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *PostReadCreateBulk) OnConflictColumns(columns ...string) *PostReadUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &PostReadUpsertBulk{
create: _c,
}
}
// PostReadUpsertBulk is the builder for "upsert"-ing
// a bulk of PostRead nodes.
type PostReadUpsertBulk struct {
create *PostReadCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.PostRead.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(postread.FieldID)
// }),
// ).
// Exec(ctx)
func (u *PostReadUpsertBulk) UpdateNewValues() *PostReadUpsertBulk {
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(postread.FieldID)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.PostRead.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *PostReadUpsertBulk) Ignore() *PostReadUpsertBulk {
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 *PostReadUpsertBulk) DoNothing() *PostReadUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the PostReadCreateBulk.OnConflict
// documentation for more info.
func (u *PostReadUpsertBulk) Update(set func(*PostReadUpsert)) *PostReadUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&PostReadUpsert{UpdateSet: update})
}))
return u
}
// SetRootPostID sets the "root_post_id" field.
func (u *PostReadUpsertBulk) SetRootPostID(v xid.ID) *PostReadUpsertBulk {
return u.Update(func(s *PostReadUpsert) {
s.SetRootPostID(v)
})
}
// UpdateRootPostID sets the "root_post_id" field to the value that was provided on create.
func (u *PostReadUpsertBulk) UpdateRootPostID() *PostReadUpsertBulk {
return u.Update(func(s *PostReadUpsert) {
s.UpdateRootPostID()
})
}
// SetAccountID sets the "account_id" field.
func (u *PostReadUpsertBulk) SetAccountID(v xid.ID) *PostReadUpsertBulk {
return u.Update(func(s *PostReadUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *PostReadUpsertBulk) UpdateAccountID() *PostReadUpsertBulk {
return u.Update(func(s *PostReadUpsert) {
s.UpdateAccountID()
})
}
// SetLastSeenAt sets the "last_seen_at" field.
func (u *PostReadUpsertBulk) SetLastSeenAt(v time.Time) *PostReadUpsertBulk {
return u.Update(func(s *PostReadUpsert) {
s.SetLastSeenAt(v)
})
}
// UpdateLastSeenAt sets the "last_seen_at" field to the value that was provided on create.
func (u *PostReadUpsertBulk) UpdateLastSeenAt() *PostReadUpsertBulk {
return u.Update(func(s *PostReadUpsert) {
s.UpdateLastSeenAt()
})
}
// Exec executes the query.
func (u *PostReadUpsertBulk) 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 PostReadCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for PostReadCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *PostReadUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}