From a817f7550be3f537414fce5708f196ba0a1b00ee Mon Sep 17 00:00:00 2001 From: Achim Rohn Date: Fri, 12 Sep 2025 09:31:21 +0200 Subject: [PATCH] Add a mixin for time fields --- schema/ent/example/ent/group.go | 25 ++ schema/ent/example/ent/group/group.go | 24 ++ schema/ent/example/ent/group/where.go | 91 +++++ schema/ent/example/ent/group_create.go | 51 +++ schema/ent/example/ent/group_query.go | 8 +- schema/ent/example/ent/group_update.go | 37 ++ schema/ent/example/ent/migrate/schema.go | 10 +- schema/ent/example/ent/mutation.go | 436 +++++++++++++++++------ schema/ent/example/ent/runtime.go | 49 ++- schema/ent/example/ent/schema/group.go | 9 +- schema/ent/example/ent/schema/todo.go | 8 + schema/ent/example/ent/schema/user.go | 11 +- schema/ent/example/ent/todo.go | 25 ++ schema/ent/example/ent/todo/todo.go | 24 ++ schema/ent/example/ent/todo/where.go | 91 +++++ schema/ent/example/ent/todo_create.go | 51 +++ schema/ent/example/ent/todo_query.go | 8 +- schema/ent/example/ent/todo_update.go | 37 ++ schema/ent/example/ent/user.go | 44 +-- schema/ent/example/ent/user/user.go | 44 +-- schema/ent/example/ent/user/where.go | 168 ++++----- schema/ent/example/ent/user_create.go | 100 +++--- schema/ent/example/ent/user_query.go | 8 +- schema/ent/example/ent/user_update.go | 36 +- schema/ent/time_mixin.go | 27 ++ 25 files changed, 1086 insertions(+), 336 deletions(-) create mode 100644 schema/ent/time_mixin.go diff --git a/schema/ent/example/ent/group.go b/schema/ent/example/ent/group.go index b905b8f..a66d729 100644 --- a/schema/ent/example/ent/group.go +++ b/schema/ent/example/ent/group.go @@ -6,6 +6,7 @@ import ( "ersteller-lib/schema/ent/example/ent/group" "fmt" "strings" + "time" "entgo.io/ent" "entgo.io/ent/dialect/sql" @@ -16,6 +17,10 @@ type Group struct { config `json:"-"` // ID of the ent. ID int `json:"id,omitempty"` + // CreatedAt holds the value of the "created_at" field. + CreatedAt time.Time `json:"created_at,omitempty"` + // UpdatedAt holds the value of the "updated_at" field. + UpdatedAt time.Time `json:"updated_at,omitempty"` // Name holds the value of the "name" field. Name string `json:"name,omitempty"` // Edges holds the relations/edges for other nodes in the graph. @@ -62,6 +67,8 @@ func (*Group) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullInt64) case group.FieldName: values[i] = new(sql.NullString) + case group.FieldCreatedAt, group.FieldUpdatedAt: + values[i] = new(sql.NullTime) default: values[i] = new(sql.UnknownType) } @@ -83,6 +90,18 @@ func (_m *Group) assignValues(columns []string, values []any) error { return fmt.Errorf("unexpected type %T for field id", value) } _m.ID = int(value.Int64) + case group.FieldCreatedAt: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field created_at", values[i]) + } else if value.Valid { + _m.CreatedAt = value.Time + } + case group.FieldUpdatedAt: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field updated_at", values[i]) + } else if value.Valid { + _m.UpdatedAt = value.Time + } case group.FieldName: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field name", values[i]) @@ -135,6 +154,12 @@ func (_m *Group) String() string { var builder strings.Builder builder.WriteString("Group(") builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("created_at=") + builder.WriteString(_m.CreatedAt.Format(time.ANSIC)) + builder.WriteString(", ") + builder.WriteString("updated_at=") + builder.WriteString(_m.UpdatedAt.Format(time.ANSIC)) + builder.WriteString(", ") builder.WriteString("name=") builder.WriteString(_m.Name) builder.WriteByte(')') diff --git a/schema/ent/example/ent/group/group.go b/schema/ent/example/ent/group/group.go index b06380b..b8588bc 100644 --- a/schema/ent/example/ent/group/group.go +++ b/schema/ent/example/ent/group/group.go @@ -3,6 +3,8 @@ package group import ( + "time" + "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" ) @@ -12,6 +14,10 @@ const ( Label = "group" // FieldID holds the string denoting the id field in the database. FieldID = "id" + // FieldCreatedAt holds the string denoting the created_at field in the database. + FieldCreatedAt = "created_at" + // FieldUpdatedAt holds the string denoting the updated_at field in the database. + FieldUpdatedAt = "updated_at" // FieldName holds the string denoting the name field in the database. FieldName = "name" // EdgeUsers holds the string denoting the users edge name in mutations. @@ -37,6 +43,8 @@ const ( // Columns holds all SQL columns for group fields. var Columns = []string{ FieldID, + FieldCreatedAt, + FieldUpdatedAt, FieldName, } @@ -57,6 +65,12 @@ func ValidColumn(column string) bool { } var ( + // DefaultCreatedAt holds the default value on creation for the "created_at" field. + DefaultCreatedAt func() time.Time + // DefaultUpdatedAt holds the default value on creation for the "updated_at" field. + DefaultUpdatedAt func() time.Time + // UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field. + UpdateDefaultUpdatedAt func() time.Time // DefaultName holds the default value on creation for the "name" field. DefaultName string ) @@ -69,6 +83,16 @@ func ByID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldID, opts...).ToFunc() } +// ByCreatedAt orders the results by the created_at field. +func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() +} + +// ByUpdatedAt orders the results by the updated_at field. +func ByUpdatedAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldUpdatedAt, opts...).ToFunc() +} + // ByName orders the results by the name field. func ByName(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldName, opts...).ToFunc() diff --git a/schema/ent/example/ent/group/where.go b/schema/ent/example/ent/group/where.go index 4259b10..091d52f 100644 --- a/schema/ent/example/ent/group/where.go +++ b/schema/ent/example/ent/group/where.go @@ -4,6 +4,7 @@ package group import ( "ersteller-lib/schema/ent/example/ent/predicate" + "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -54,11 +55,101 @@ func IDLTE(id int) predicate.Group { return predicate.Group(sql.FieldLTE(FieldID, id)) } +// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. +func CreatedAt(v time.Time) predicate.Group { + return predicate.Group(sql.FieldEQ(FieldCreatedAt, v)) +} + +// UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ. +func UpdatedAt(v time.Time) predicate.Group { + return predicate.Group(sql.FieldEQ(FieldUpdatedAt, v)) +} + // Name applies equality check predicate on the "name" field. It's identical to NameEQ. func Name(v string) predicate.Group { return predicate.Group(sql.FieldEQ(FieldName, v)) } +// CreatedAtEQ applies the EQ predicate on the "created_at" field. +func CreatedAtEQ(v time.Time) predicate.Group { + return predicate.Group(sql.FieldEQ(FieldCreatedAt, v)) +} + +// CreatedAtNEQ applies the NEQ predicate on the "created_at" field. +func CreatedAtNEQ(v time.Time) predicate.Group { + return predicate.Group(sql.FieldNEQ(FieldCreatedAt, v)) +} + +// CreatedAtIn applies the In predicate on the "created_at" field. +func CreatedAtIn(vs ...time.Time) predicate.Group { + return predicate.Group(sql.FieldIn(FieldCreatedAt, vs...)) +} + +// CreatedAtNotIn applies the NotIn predicate on the "created_at" field. +func CreatedAtNotIn(vs ...time.Time) predicate.Group { + return predicate.Group(sql.FieldNotIn(FieldCreatedAt, vs...)) +} + +// CreatedAtGT applies the GT predicate on the "created_at" field. +func CreatedAtGT(v time.Time) predicate.Group { + return predicate.Group(sql.FieldGT(FieldCreatedAt, v)) +} + +// CreatedAtGTE applies the GTE predicate on the "created_at" field. +func CreatedAtGTE(v time.Time) predicate.Group { + return predicate.Group(sql.FieldGTE(FieldCreatedAt, v)) +} + +// CreatedAtLT applies the LT predicate on the "created_at" field. +func CreatedAtLT(v time.Time) predicate.Group { + return predicate.Group(sql.FieldLT(FieldCreatedAt, v)) +} + +// CreatedAtLTE applies the LTE predicate on the "created_at" field. +func CreatedAtLTE(v time.Time) predicate.Group { + return predicate.Group(sql.FieldLTE(FieldCreatedAt, v)) +} + +// UpdatedAtEQ applies the EQ predicate on the "updated_at" field. +func UpdatedAtEQ(v time.Time) predicate.Group { + return predicate.Group(sql.FieldEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field. +func UpdatedAtNEQ(v time.Time) predicate.Group { + return predicate.Group(sql.FieldNEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtIn applies the In predicate on the "updated_at" field. +func UpdatedAtIn(vs ...time.Time) predicate.Group { + return predicate.Group(sql.FieldIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field. +func UpdatedAtNotIn(vs ...time.Time) predicate.Group { + return predicate.Group(sql.FieldNotIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtGT applies the GT predicate on the "updated_at" field. +func UpdatedAtGT(v time.Time) predicate.Group { + return predicate.Group(sql.FieldGT(FieldUpdatedAt, v)) +} + +// UpdatedAtGTE applies the GTE predicate on the "updated_at" field. +func UpdatedAtGTE(v time.Time) predicate.Group { + return predicate.Group(sql.FieldGTE(FieldUpdatedAt, v)) +} + +// UpdatedAtLT applies the LT predicate on the "updated_at" field. +func UpdatedAtLT(v time.Time) predicate.Group { + return predicate.Group(sql.FieldLT(FieldUpdatedAt, v)) +} + +// UpdatedAtLTE applies the LTE predicate on the "updated_at" field. +func UpdatedAtLTE(v time.Time) predicate.Group { + return predicate.Group(sql.FieldLTE(FieldUpdatedAt, v)) +} + // NameEQ applies the EQ predicate on the "name" field. func NameEQ(v string) predicate.Group { return predicate.Group(sql.FieldEQ(FieldName, v)) diff --git a/schema/ent/example/ent/group_create.go b/schema/ent/example/ent/group_create.go index 619e1bd..1d14d41 100644 --- a/schema/ent/example/ent/group_create.go +++ b/schema/ent/example/ent/group_create.go @@ -9,6 +9,7 @@ import ( "ersteller-lib/schema/ent/example/ent/todo" "ersteller-lib/schema/ent/example/ent/user" "fmt" + "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" @@ -21,6 +22,34 @@ type GroupCreate struct { hooks []Hook } +// SetCreatedAt sets the "created_at" field. +func (_c *GroupCreate) SetCreatedAt(v time.Time) *GroupCreate { + _c.mutation.SetCreatedAt(v) + return _c +} + +// SetNillableCreatedAt sets the "created_at" field if the given value is not nil. +func (_c *GroupCreate) SetNillableCreatedAt(v *time.Time) *GroupCreate { + if v != nil { + _c.SetCreatedAt(*v) + } + return _c +} + +// SetUpdatedAt sets the "updated_at" field. +func (_c *GroupCreate) SetUpdatedAt(v time.Time) *GroupCreate { + _c.mutation.SetUpdatedAt(v) + return _c +} + +// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. +func (_c *GroupCreate) SetNillableUpdatedAt(v *time.Time) *GroupCreate { + if v != nil { + _c.SetUpdatedAt(*v) + } + return _c +} + // SetName sets the "name" field. func (_c *GroupCreate) SetName(v string) *GroupCreate { _c.mutation.SetName(v) @@ -100,6 +129,14 @@ func (_c *GroupCreate) ExecX(ctx context.Context) { // defaults sets the default values of the builder before save. func (_c *GroupCreate) defaults() { + if _, ok := _c.mutation.CreatedAt(); !ok { + v := group.DefaultCreatedAt() + _c.mutation.SetCreatedAt(v) + } + if _, ok := _c.mutation.UpdatedAt(); !ok { + v := group.DefaultUpdatedAt() + _c.mutation.SetUpdatedAt(v) + } if _, ok := _c.mutation.Name(); !ok { v := group.DefaultName _c.mutation.SetName(v) @@ -108,6 +145,12 @@ func (_c *GroupCreate) defaults() { // check runs all checks and user-defined validators on the builder. func (_c *GroupCreate) check() error { + if _, ok := _c.mutation.CreatedAt(); !ok { + return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Group.created_at"`)} + } + if _, ok := _c.mutation.UpdatedAt(); !ok { + return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Group.updated_at"`)} + } if _, ok := _c.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Group.name"`)} } @@ -137,6 +180,14 @@ func (_c *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) { _node = &Group{config: _c.config} _spec = sqlgraph.NewCreateSpec(group.Table, sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt)) ) + if value, ok := _c.mutation.CreatedAt(); ok { + _spec.SetField(group.FieldCreatedAt, field.TypeTime, value) + _node.CreatedAt = value + } + if value, ok := _c.mutation.UpdatedAt(); ok { + _spec.SetField(group.FieldUpdatedAt, field.TypeTime, value) + _node.UpdatedAt = value + } if value, ok := _c.mutation.Name(); ok { _spec.SetField(group.FieldName, field.TypeString, value) _node.Name = value diff --git a/schema/ent/example/ent/group_query.go b/schema/ent/example/ent/group_query.go index d4bc35c..64c24e7 100644 --- a/schema/ent/example/ent/group_query.go +++ b/schema/ent/example/ent/group_query.go @@ -335,12 +335,12 @@ func (_q *GroupQuery) WithTodos(opts ...func(*TodoQuery)) *GroupQuery { // Example: // // var v []struct { -// Name string `json:"name,omitempty"` +// CreatedAt time.Time `json:"created_at,omitempty"` // Count int `json:"count,omitempty"` // } // // client.Group.Query(). -// GroupBy(group.FieldName). +// GroupBy(group.FieldCreatedAt). // Aggregate(ent.Count()). // Scan(ctx, &v) func (_q *GroupQuery) GroupBy(field string, fields ...string) *GroupGroupBy { @@ -358,11 +358,11 @@ func (_q *GroupQuery) GroupBy(field string, fields ...string) *GroupGroupBy { // Example: // // var v []struct { -// Name string `json:"name,omitempty"` +// CreatedAt time.Time `json:"created_at,omitempty"` // } // // client.Group.Query(). -// Select(group.FieldName). +// Select(group.FieldCreatedAt). // Scan(ctx, &v) func (_q *GroupQuery) Select(fields ...string) *GroupSelect { _q.ctx.Fields = append(_q.ctx.Fields, fields...) diff --git a/schema/ent/example/ent/group_update.go b/schema/ent/example/ent/group_update.go index c8ebb26..f4dc2dd 100644 --- a/schema/ent/example/ent/group_update.go +++ b/schema/ent/example/ent/group_update.go @@ -10,6 +10,7 @@ import ( "ersteller-lib/schema/ent/example/ent/todo" "ersteller-lib/schema/ent/example/ent/user" "fmt" + "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -29,6 +30,12 @@ func (_u *GroupUpdate) Where(ps ...predicate.Group) *GroupUpdate { return _u } +// SetUpdatedAt sets the "updated_at" field. +func (_u *GroupUpdate) SetUpdatedAt(v time.Time) *GroupUpdate { + _u.mutation.SetUpdatedAt(v) + return _u +} + // SetName sets the "name" field. func (_u *GroupUpdate) SetName(v string) *GroupUpdate { _u.mutation.SetName(v) @@ -122,6 +129,7 @@ func (_u *GroupUpdate) RemoveTodos(v ...*Todo) *GroupUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (_u *GroupUpdate) Save(ctx context.Context) (int, error) { + _u.defaults() return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } @@ -147,6 +155,14 @@ func (_u *GroupUpdate) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (_u *GroupUpdate) defaults() { + if _, ok := _u.mutation.UpdatedAt(); !ok { + v := group.UpdateDefaultUpdatedAt() + _u.mutation.SetUpdatedAt(v) + } +} + func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) { _spec := sqlgraph.NewUpdateSpec(group.Table, group.Columns, sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt)) if ps := _u.mutation.predicates; len(ps) > 0 { @@ -156,6 +172,9 @@ func (_u *GroupUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(group.FieldUpdatedAt, field.TypeTime, value) + } if value, ok := _u.mutation.Name(); ok { _spec.SetField(group.FieldName, field.TypeString, value) } @@ -269,6 +288,12 @@ type GroupUpdateOne struct { mutation *GroupMutation } +// SetUpdatedAt sets the "updated_at" field. +func (_u *GroupUpdateOne) SetUpdatedAt(v time.Time) *GroupUpdateOne { + _u.mutation.SetUpdatedAt(v) + return _u +} + // SetName sets the "name" field. func (_u *GroupUpdateOne) SetName(v string) *GroupUpdateOne { _u.mutation.SetName(v) @@ -375,6 +400,7 @@ func (_u *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOne // Save executes the query and returns the updated Group entity. func (_u *GroupUpdateOne) Save(ctx context.Context) (*Group, error) { + _u.defaults() return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } @@ -400,6 +426,14 @@ func (_u *GroupUpdateOne) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (_u *GroupUpdateOne) defaults() { + if _, ok := _u.mutation.UpdatedAt(); !ok { + v := group.UpdateDefaultUpdatedAt() + _u.mutation.SetUpdatedAt(v) + } +} + func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) { _spec := sqlgraph.NewUpdateSpec(group.Table, group.Columns, sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt)) id, ok := _u.mutation.ID() @@ -426,6 +460,9 @@ func (_u *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) } } } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(group.FieldUpdatedAt, field.TypeTime, value) + } if value, ok := _u.mutation.Name(); ok { _spec.SetField(group.FieldName, field.TypeString, value) } diff --git a/schema/ent/example/ent/migrate/schema.go b/schema/ent/example/ent/migrate/schema.go index 74875af..f33d4e6 100644 --- a/schema/ent/example/ent/migrate/schema.go +++ b/schema/ent/example/ent/migrate/schema.go @@ -11,6 +11,8 @@ var ( // GroupsColumns holds the columns for the "groups" table. GroupsColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "created_at", Type: field.TypeTime}, + {Name: "updated_at", Type: field.TypeTime}, {Name: "name", Type: field.TypeString, Default: ""}, } // GroupsTable holds the schema information for the "groups" table. @@ -22,6 +24,8 @@ var ( // TodosColumns holds the columns for the "todos" table. TodosColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "created_at", Type: field.TypeTime}, + {Name: "updated_at", Type: field.TypeTime}, {Name: "title", Type: field.TypeString, Default: ""}, {Name: "completed", Type: field.TypeBool, Default: false}, {Name: "todo_group", Type: field.TypeInt, Nullable: true}, @@ -34,7 +38,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "todos_groups_group", - Columns: []*schema.Column{TodosColumns[3]}, + Columns: []*schema.Column{TodosColumns[5]}, RefColumns: []*schema.Column{GroupsColumns[0]}, OnDelete: schema.SetNull, }, @@ -43,10 +47,10 @@ var ( // UsersColumns holds the columns for the "users" table. UsersColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, - {Name: "email", Type: field.TypeString, Default: "unknown@localhost"}, - {Name: "password", Type: field.TypeString, Default: ""}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, + {Name: "email", Type: field.TypeString, Default: "unknown@localhost"}, + {Name: "password", Type: field.TypeString, Default: ""}, } // UsersTable holds the schema information for the "users" table. UsersTable = &schema.Table{ diff --git a/schema/ent/example/ent/mutation.go b/schema/ent/example/ent/mutation.go index bb87d1f..0fd62bc 100644 --- a/schema/ent/example/ent/mutation.go +++ b/schema/ent/example/ent/mutation.go @@ -37,6 +37,8 @@ type GroupMutation struct { op Op typ string id *int + created_at *time.Time + updated_at *time.Time name *string clearedFields map[string]struct{} users map[int]struct{} @@ -148,6 +150,78 @@ func (m *GroupMutation) IDs(ctx context.Context) ([]int, error) { } } +// SetCreatedAt sets the "created_at" field. +func (m *GroupMutation) SetCreatedAt(t time.Time) { + m.created_at = &t +} + +// CreatedAt returns the value of the "created_at" field in the mutation. +func (m *GroupMutation) CreatedAt() (r time.Time, exists bool) { + v := m.created_at + if v == nil { + return + } + return *v, true +} + +// OldCreatedAt returns the old "created_at" field's value of the Group entity. +// If the Group object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *GroupMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldCreatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err) + } + return oldValue.CreatedAt, nil +} + +// ResetCreatedAt resets all changes to the "created_at" field. +func (m *GroupMutation) ResetCreatedAt() { + m.created_at = nil +} + +// SetUpdatedAt sets the "updated_at" field. +func (m *GroupMutation) SetUpdatedAt(t time.Time) { + m.updated_at = &t +} + +// UpdatedAt returns the value of the "updated_at" field in the mutation. +func (m *GroupMutation) UpdatedAt() (r time.Time, exists bool) { + v := m.updated_at + if v == nil { + return + } + return *v, true +} + +// OldUpdatedAt returns the old "updated_at" field's value of the Group entity. +// If the Group object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *GroupMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUpdatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUpdatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err) + } + return oldValue.UpdatedAt, nil +} + +// ResetUpdatedAt resets all changes to the "updated_at" field. +func (m *GroupMutation) ResetUpdatedAt() { + m.updated_at = nil +} + // SetName sets the "name" field. func (m *GroupMutation) SetName(s string) { m.name = &s @@ -326,7 +400,13 @@ func (m *GroupMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *GroupMutation) Fields() []string { - fields := make([]string, 0, 1) + fields := make([]string, 0, 3) + if m.created_at != nil { + fields = append(fields, group.FieldCreatedAt) + } + if m.updated_at != nil { + fields = append(fields, group.FieldUpdatedAt) + } if m.name != nil { fields = append(fields, group.FieldName) } @@ -338,6 +418,10 @@ func (m *GroupMutation) Fields() []string { // schema. func (m *GroupMutation) Field(name string) (ent.Value, bool) { switch name { + case group.FieldCreatedAt: + return m.CreatedAt() + case group.FieldUpdatedAt: + return m.UpdatedAt() case group.FieldName: return m.Name() } @@ -349,6 +433,10 @@ func (m *GroupMutation) Field(name string) (ent.Value, bool) { // database failed. func (m *GroupMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { + case group.FieldCreatedAt: + return m.OldCreatedAt(ctx) + case group.FieldUpdatedAt: + return m.OldUpdatedAt(ctx) case group.FieldName: return m.OldName(ctx) } @@ -360,6 +448,20 @@ func (m *GroupMutation) OldField(ctx context.Context, name string) (ent.Value, e // type. func (m *GroupMutation) SetField(name string, value ent.Value) error { switch name { + case group.FieldCreatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetCreatedAt(v) + return nil + case group.FieldUpdatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUpdatedAt(v) + return nil case group.FieldName: v, ok := value.(string) if !ok { @@ -416,6 +518,12 @@ func (m *GroupMutation) ClearField(name string) error { // It returns an error if the field is not defined in the schema. func (m *GroupMutation) ResetField(name string) error { switch name { + case group.FieldCreatedAt: + m.ResetCreatedAt() + return nil + case group.FieldUpdatedAt: + m.ResetUpdatedAt() + return nil case group.FieldName: m.ResetName() return nil @@ -539,6 +647,8 @@ type TodoMutation struct { op Op typ string id *int + created_at *time.Time + updated_at *time.Time title *string completed *bool clearedFields map[string]struct{} @@ -647,6 +757,78 @@ func (m *TodoMutation) IDs(ctx context.Context) ([]int, error) { } } +// SetCreatedAt sets the "created_at" field. +func (m *TodoMutation) SetCreatedAt(t time.Time) { + m.created_at = &t +} + +// CreatedAt returns the value of the "created_at" field in the mutation. +func (m *TodoMutation) CreatedAt() (r time.Time, exists bool) { + v := m.created_at + if v == nil { + return + } + return *v, true +} + +// OldCreatedAt returns the old "created_at" field's value of the Todo entity. +// If the Todo object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *TodoMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldCreatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err) + } + return oldValue.CreatedAt, nil +} + +// ResetCreatedAt resets all changes to the "created_at" field. +func (m *TodoMutation) ResetCreatedAt() { + m.created_at = nil +} + +// SetUpdatedAt sets the "updated_at" field. +func (m *TodoMutation) SetUpdatedAt(t time.Time) { + m.updated_at = &t +} + +// UpdatedAt returns the value of the "updated_at" field in the mutation. +func (m *TodoMutation) UpdatedAt() (r time.Time, exists bool) { + v := m.updated_at + if v == nil { + return + } + return *v, true +} + +// OldUpdatedAt returns the old "updated_at" field's value of the Todo entity. +// If the Todo object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *TodoMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUpdatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUpdatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err) + } + return oldValue.UpdatedAt, nil +} + +// ResetUpdatedAt resets all changes to the "updated_at" field. +func (m *TodoMutation) ResetUpdatedAt() { + m.updated_at = nil +} + // SetTitle sets the "title" field. func (m *TodoMutation) SetTitle(s string) { m.title = &s @@ -792,7 +974,13 @@ func (m *TodoMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *TodoMutation) Fields() []string { - fields := make([]string, 0, 2) + fields := make([]string, 0, 4) + if m.created_at != nil { + fields = append(fields, todo.FieldCreatedAt) + } + if m.updated_at != nil { + fields = append(fields, todo.FieldUpdatedAt) + } if m.title != nil { fields = append(fields, todo.FieldTitle) } @@ -807,6 +995,10 @@ func (m *TodoMutation) Fields() []string { // schema. func (m *TodoMutation) Field(name string) (ent.Value, bool) { switch name { + case todo.FieldCreatedAt: + return m.CreatedAt() + case todo.FieldUpdatedAt: + return m.UpdatedAt() case todo.FieldTitle: return m.Title() case todo.FieldCompleted: @@ -820,6 +1012,10 @@ func (m *TodoMutation) Field(name string) (ent.Value, bool) { // database failed. func (m *TodoMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { + case todo.FieldCreatedAt: + return m.OldCreatedAt(ctx) + case todo.FieldUpdatedAt: + return m.OldUpdatedAt(ctx) case todo.FieldTitle: return m.OldTitle(ctx) case todo.FieldCompleted: @@ -833,6 +1029,20 @@ func (m *TodoMutation) OldField(ctx context.Context, name string) (ent.Value, er // type. func (m *TodoMutation) SetField(name string, value ent.Value) error { switch name { + case todo.FieldCreatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetCreatedAt(v) + return nil + case todo.FieldUpdatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUpdatedAt(v) + return nil case todo.FieldTitle: v, ok := value.(string) if !ok { @@ -896,6 +1106,12 @@ func (m *TodoMutation) ClearField(name string) error { // It returns an error if the field is not defined in the schema. func (m *TodoMutation) ResetField(name string) error { switch name { + case todo.FieldCreatedAt: + m.ResetCreatedAt() + return nil + case todo.FieldUpdatedAt: + m.ResetUpdatedAt() + return nil case todo.FieldTitle: m.ResetTitle() return nil @@ -986,10 +1202,10 @@ type UserMutation struct { op Op typ string id *int - email *string - password *string created_at *time.Time updated_at *time.Time + email *string + password *string clearedFields map[string]struct{} group map[int]struct{} removedgroup map[int]struct{} @@ -1097,78 +1313,6 @@ func (m *UserMutation) IDs(ctx context.Context) ([]int, error) { } } -// SetEmail sets the "email" field. -func (m *UserMutation) SetEmail(s string) { - m.email = &s -} - -// Email returns the value of the "email" field in the mutation. -func (m *UserMutation) Email() (r string, exists bool) { - v := m.email - if v == nil { - return - } - return *v, true -} - -// OldEmail returns the old "email" field's value of the User entity. -// If the User object wasn't provided to the builder, the object is fetched from the database. -// An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *UserMutation) OldEmail(ctx context.Context) (v string, err error) { - if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldEmail is only allowed on UpdateOne operations") - } - if m.id == nil || m.oldValue == nil { - return v, errors.New("OldEmail requires an ID field in the mutation") - } - oldValue, err := m.oldValue(ctx) - if err != nil { - return v, fmt.Errorf("querying old value for OldEmail: %w", err) - } - return oldValue.Email, nil -} - -// ResetEmail resets all changes to the "email" field. -func (m *UserMutation) ResetEmail() { - m.email = nil -} - -// SetPassword sets the "password" field. -func (m *UserMutation) SetPassword(s string) { - m.password = &s -} - -// Password returns the value of the "password" field in the mutation. -func (m *UserMutation) Password() (r string, exists bool) { - v := m.password - if v == nil { - return - } - return *v, true -} - -// OldPassword returns the old "password" field's value of the User entity. -// If the User object wasn't provided to the builder, the object is fetched from the database. -// An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *UserMutation) OldPassword(ctx context.Context) (v string, err error) { - if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldPassword is only allowed on UpdateOne operations") - } - if m.id == nil || m.oldValue == nil { - return v, errors.New("OldPassword requires an ID field in the mutation") - } - oldValue, err := m.oldValue(ctx) - if err != nil { - return v, fmt.Errorf("querying old value for OldPassword: %w", err) - } - return oldValue.Password, nil -} - -// ResetPassword resets all changes to the "password" field. -func (m *UserMutation) ResetPassword() { - m.password = nil -} - // SetCreatedAt sets the "created_at" field. func (m *UserMutation) SetCreatedAt(t time.Time) { m.created_at = &t @@ -1241,6 +1385,78 @@ func (m *UserMutation) ResetUpdatedAt() { m.updated_at = nil } +// SetEmail sets the "email" field. +func (m *UserMutation) SetEmail(s string) { + m.email = &s +} + +// Email returns the value of the "email" field in the mutation. +func (m *UserMutation) Email() (r string, exists bool) { + v := m.email + if v == nil { + return + } + return *v, true +} + +// OldEmail returns the old "email" field's value of the User entity. +// If the User object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *UserMutation) OldEmail(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldEmail is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldEmail requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldEmail: %w", err) + } + return oldValue.Email, nil +} + +// ResetEmail resets all changes to the "email" field. +func (m *UserMutation) ResetEmail() { + m.email = nil +} + +// SetPassword sets the "password" field. +func (m *UserMutation) SetPassword(s string) { + m.password = &s +} + +// Password returns the value of the "password" field in the mutation. +func (m *UserMutation) Password() (r string, exists bool) { + v := m.password + if v == nil { + return + } + return *v, true +} + +// OldPassword returns the old "password" field's value of the User entity. +// If the User object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *UserMutation) OldPassword(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldPassword is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldPassword requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldPassword: %w", err) + } + return oldValue.Password, nil +} + +// ResetPassword resets all changes to the "password" field. +func (m *UserMutation) ResetPassword() { + m.password = nil +} + // AddGroupIDs adds the "group" edge to the Group entity by ids. func (m *UserMutation) AddGroupIDs(ids ...int) { if m.group == nil { @@ -1330,18 +1546,18 @@ func (m *UserMutation) Type() string { // AddedFields(). func (m *UserMutation) Fields() []string { fields := make([]string, 0, 4) - if m.email != nil { - fields = append(fields, user.FieldEmail) - } - if m.password != nil { - fields = append(fields, user.FieldPassword) - } if m.created_at != nil { fields = append(fields, user.FieldCreatedAt) } if m.updated_at != nil { fields = append(fields, user.FieldUpdatedAt) } + if m.email != nil { + fields = append(fields, user.FieldEmail) + } + if m.password != nil { + fields = append(fields, user.FieldPassword) + } return fields } @@ -1350,14 +1566,14 @@ func (m *UserMutation) Fields() []string { // schema. func (m *UserMutation) Field(name string) (ent.Value, bool) { switch name { - case user.FieldEmail: - return m.Email() - case user.FieldPassword: - return m.Password() case user.FieldCreatedAt: return m.CreatedAt() case user.FieldUpdatedAt: return m.UpdatedAt() + case user.FieldEmail: + return m.Email() + case user.FieldPassword: + return m.Password() } return nil, false } @@ -1367,14 +1583,14 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) { // database failed. func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { - case user.FieldEmail: - return m.OldEmail(ctx) - case user.FieldPassword: - return m.OldPassword(ctx) case user.FieldCreatedAt: return m.OldCreatedAt(ctx) case user.FieldUpdatedAt: return m.OldUpdatedAt(ctx) + case user.FieldEmail: + return m.OldEmail(ctx) + case user.FieldPassword: + return m.OldPassword(ctx) } return nil, fmt.Errorf("unknown User field %s", name) } @@ -1384,20 +1600,6 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er // type. func (m *UserMutation) SetField(name string, value ent.Value) error { switch name { - case user.FieldEmail: - v, ok := value.(string) - if !ok { - return fmt.Errorf("unexpected type %T for field %s", value, name) - } - m.SetEmail(v) - return nil - case user.FieldPassword: - v, ok := value.(string) - if !ok { - return fmt.Errorf("unexpected type %T for field %s", value, name) - } - m.SetPassword(v) - return nil case user.FieldCreatedAt: v, ok := value.(time.Time) if !ok { @@ -1412,6 +1614,20 @@ func (m *UserMutation) SetField(name string, value ent.Value) error { } m.SetUpdatedAt(v) return nil + case user.FieldEmail: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetEmail(v) + return nil + case user.FieldPassword: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetPassword(v) + return nil } return fmt.Errorf("unknown User field %s", name) } @@ -1461,18 +1677,18 @@ func (m *UserMutation) ClearField(name string) error { // It returns an error if the field is not defined in the schema. func (m *UserMutation) ResetField(name string) error { switch name { - case user.FieldEmail: - m.ResetEmail() - return nil - case user.FieldPassword: - m.ResetPassword() - return nil case user.FieldCreatedAt: m.ResetCreatedAt() return nil case user.FieldUpdatedAt: m.ResetUpdatedAt() return nil + case user.FieldEmail: + m.ResetEmail() + return nil + case user.FieldPassword: + m.ResetPassword() + return nil } return fmt.Errorf("unknown User field %s", name) } diff --git a/schema/ent/example/ent/runtime.go b/schema/ent/example/ent/runtime.go index e1449de..33fd177 100644 --- a/schema/ent/example/ent/runtime.go +++ b/schema/ent/example/ent/runtime.go @@ -14,14 +14,40 @@ import ( // (default values, validators, hooks and policies) and stitches it // to their package variables. func init() { + groupMixin := schema.Group{}.Mixin() + groupMixinFields0 := groupMixin[0].Fields() + _ = groupMixinFields0 groupFields := schema.Group{}.Fields() _ = groupFields + // groupDescCreatedAt is the schema descriptor for created_at field. + groupDescCreatedAt := groupMixinFields0[0].Descriptor() + // group.DefaultCreatedAt holds the default value on creation for the created_at field. + group.DefaultCreatedAt = groupDescCreatedAt.Default.(func() time.Time) + // groupDescUpdatedAt is the schema descriptor for updated_at field. + groupDescUpdatedAt := groupMixinFields0[1].Descriptor() + // group.DefaultUpdatedAt holds the default value on creation for the updated_at field. + group.DefaultUpdatedAt = groupDescUpdatedAt.Default.(func() time.Time) + // group.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. + group.UpdateDefaultUpdatedAt = groupDescUpdatedAt.UpdateDefault.(func() time.Time) // groupDescName is the schema descriptor for name field. groupDescName := groupFields[0].Descriptor() // group.DefaultName holds the default value on creation for the name field. group.DefaultName = groupDescName.Default.(string) + todoMixin := schema.Todo{}.Mixin() + todoMixinFields0 := todoMixin[0].Fields() + _ = todoMixinFields0 todoFields := schema.Todo{}.Fields() _ = todoFields + // todoDescCreatedAt is the schema descriptor for created_at field. + todoDescCreatedAt := todoMixinFields0[0].Descriptor() + // todo.DefaultCreatedAt holds the default value on creation for the created_at field. + todo.DefaultCreatedAt = todoDescCreatedAt.Default.(func() time.Time) + // todoDescUpdatedAt is the schema descriptor for updated_at field. + todoDescUpdatedAt := todoMixinFields0[1].Descriptor() + // todo.DefaultUpdatedAt holds the default value on creation for the updated_at field. + todo.DefaultUpdatedAt = todoDescUpdatedAt.Default.(func() time.Time) + // todo.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. + todo.UpdateDefaultUpdatedAt = todoDescUpdatedAt.UpdateDefault.(func() time.Time) // todoDescTitle is the schema descriptor for title field. todoDescTitle := todoFields[0].Descriptor() // todo.DefaultTitle holds the default value on creation for the title field. @@ -30,8 +56,21 @@ func init() { todoDescCompleted := todoFields[1].Descriptor() // todo.DefaultCompleted holds the default value on creation for the completed field. todo.DefaultCompleted = todoDescCompleted.Default.(bool) + userMixin := schema.User{}.Mixin() + userMixinFields0 := userMixin[0].Fields() + _ = userMixinFields0 userFields := schema.User{}.Fields() _ = userFields + // userDescCreatedAt is the schema descriptor for created_at field. + userDescCreatedAt := userMixinFields0[0].Descriptor() + // user.DefaultCreatedAt holds the default value on creation for the created_at field. + user.DefaultCreatedAt = userDescCreatedAt.Default.(func() time.Time) + // userDescUpdatedAt is the schema descriptor for updated_at field. + userDescUpdatedAt := userMixinFields0[1].Descriptor() + // user.DefaultUpdatedAt holds the default value on creation for the updated_at field. + user.DefaultUpdatedAt = userDescUpdatedAt.Default.(func() time.Time) + // user.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. + user.UpdateDefaultUpdatedAt = userDescUpdatedAt.UpdateDefault.(func() time.Time) // userDescEmail is the schema descriptor for email field. userDescEmail := userFields[0].Descriptor() // user.DefaultEmail holds the default value on creation for the email field. @@ -40,14 +79,4 @@ func init() { userDescPassword := userFields[1].Descriptor() // user.DefaultPassword holds the default value on creation for the password field. user.DefaultPassword = userDescPassword.Default.(string) - // userDescCreatedAt is the schema descriptor for created_at field. - userDescCreatedAt := userFields[2].Descriptor() - // user.DefaultCreatedAt holds the default value on creation for the created_at field. - user.DefaultCreatedAt = userDescCreatedAt.Default.(time.Time) - // userDescUpdatedAt is the schema descriptor for updated_at field. - userDescUpdatedAt := userFields[3].Descriptor() - // user.DefaultUpdatedAt holds the default value on creation for the updated_at field. - user.DefaultUpdatedAt = userDescUpdatedAt.Default.(time.Time) - // user.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. - user.UpdateDefaultUpdatedAt = userDescUpdatedAt.UpdateDefault.(func() time.Time) } diff --git a/schema/ent/example/ent/schema/group.go b/schema/ent/example/ent/schema/group.go index 9acdfaf..5e7e19f 100644 --- a/schema/ent/example/ent/schema/group.go +++ b/schema/ent/example/ent/schema/group.go @@ -1,6 +1,8 @@ package schema import ( + "ersteller-lib/schema/ent" + "entgo.io/ent" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" @@ -10,13 +12,18 @@ type Group struct { ent.Schema } +func (Group) Mixin() []ent.Mixin { + return []ent.Mixin{ + ersteller_ent.TimeMixin{}, + } +} + func (Group) Fields() []ent.Field { return []ent.Field{ field.String("name").Default(""), } } -// Add edges to define relationships func (Group) Edges() []ent.Edge { return []ent.Edge{ // Back-reference to users that belong to this group diff --git a/schema/ent/example/ent/schema/todo.go b/schema/ent/example/ent/schema/todo.go index 0cdd6ac..5051e5f 100644 --- a/schema/ent/example/ent/schema/todo.go +++ b/schema/ent/example/ent/schema/todo.go @@ -1,6 +1,8 @@ package schema import ( + "ersteller-lib/schema/ent" + "entgo.io/ent" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" @@ -10,6 +12,12 @@ type Todo struct { ent.Schema } +func (Todo) Mixin() []ent.Mixin { + return []ent.Mixin{ + ersteller_ent.TimeMixin{}, + } +} + func (Todo) Fields() []ent.Field { return []ent.Field{ field.String("title").Default(""), diff --git a/schema/ent/example/ent/schema/user.go b/schema/ent/example/ent/schema/user.go index df361f2..2f3774f 100644 --- a/schema/ent/example/ent/schema/user.go +++ b/schema/ent/example/ent/schema/user.go @@ -1,7 +1,7 @@ package schema import ( - "time" + "ersteller-lib/schema/ent" "entgo.io/ent" "entgo.io/ent/schema/edge" @@ -13,15 +13,18 @@ type User struct { ent.Schema } +func (User) Mixin() []ent.Mixin { + return []ent.Mixin{ + ersteller_ent.TimeMixin{}, + } +} + // Fields of the User. func (User) Fields() []ent.Field { return []ent.Field{ field.String("email"). Default("unknown@localhost"), field.String("password").Default(""), - field.Time("created_at").Default(time.Now()).Immutable(), - field.Time("updated_at").Default(time.Now()). - UpdateDefault(time.Now), } } diff --git a/schema/ent/example/ent/todo.go b/schema/ent/example/ent/todo.go index 47a7384..144f47d 100644 --- a/schema/ent/example/ent/todo.go +++ b/schema/ent/example/ent/todo.go @@ -7,6 +7,7 @@ import ( "ersteller-lib/schema/ent/example/ent/todo" "fmt" "strings" + "time" "entgo.io/ent" "entgo.io/ent/dialect/sql" @@ -17,6 +18,10 @@ type Todo struct { config `json:"-"` // ID of the ent. ID int `json:"id,omitempty"` + // CreatedAt holds the value of the "created_at" field. + CreatedAt time.Time `json:"created_at,omitempty"` + // UpdatedAt holds the value of the "updated_at" field. + UpdatedAt time.Time `json:"updated_at,omitempty"` // Title holds the value of the "title" field. Title string `json:"title,omitempty"` // Completed holds the value of the "completed" field. @@ -59,6 +64,8 @@ func (*Todo) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullInt64) case todo.FieldTitle: values[i] = new(sql.NullString) + case todo.FieldCreatedAt, todo.FieldUpdatedAt: + values[i] = new(sql.NullTime) case todo.ForeignKeys[0]: // todo_group values[i] = new(sql.NullInt64) default: @@ -82,6 +89,18 @@ func (_m *Todo) assignValues(columns []string, values []any) error { return fmt.Errorf("unexpected type %T for field id", value) } _m.ID = int(value.Int64) + case todo.FieldCreatedAt: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field created_at", values[i]) + } else if value.Valid { + _m.CreatedAt = value.Time + } + case todo.FieldUpdatedAt: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field updated_at", values[i]) + } else if value.Valid { + _m.UpdatedAt = value.Time + } case todo.FieldTitle: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field title", values[i]) @@ -142,6 +161,12 @@ func (_m *Todo) String() string { var builder strings.Builder builder.WriteString("Todo(") builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) + builder.WriteString("created_at=") + builder.WriteString(_m.CreatedAt.Format(time.ANSIC)) + builder.WriteString(", ") + builder.WriteString("updated_at=") + builder.WriteString(_m.UpdatedAt.Format(time.ANSIC)) + builder.WriteString(", ") builder.WriteString("title=") builder.WriteString(_m.Title) builder.WriteString(", ") diff --git a/schema/ent/example/ent/todo/todo.go b/schema/ent/example/ent/todo/todo.go index beff589..2d428ac 100644 --- a/schema/ent/example/ent/todo/todo.go +++ b/schema/ent/example/ent/todo/todo.go @@ -3,6 +3,8 @@ package todo import ( + "time" + "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" ) @@ -12,6 +14,10 @@ const ( Label = "todo" // FieldID holds the string denoting the id field in the database. FieldID = "id" + // FieldCreatedAt holds the string denoting the created_at field in the database. + FieldCreatedAt = "created_at" + // FieldUpdatedAt holds the string denoting the updated_at field in the database. + FieldUpdatedAt = "updated_at" // FieldTitle holds the string denoting the title field in the database. FieldTitle = "title" // FieldCompleted holds the string denoting the completed field in the database. @@ -32,6 +38,8 @@ const ( // Columns holds all SQL columns for todo fields. var Columns = []string{ FieldID, + FieldCreatedAt, + FieldUpdatedAt, FieldTitle, FieldCompleted, } @@ -58,6 +66,12 @@ func ValidColumn(column string) bool { } var ( + // DefaultCreatedAt holds the default value on creation for the "created_at" field. + DefaultCreatedAt func() time.Time + // DefaultUpdatedAt holds the default value on creation for the "updated_at" field. + DefaultUpdatedAt func() time.Time + // UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field. + UpdateDefaultUpdatedAt func() time.Time // DefaultTitle holds the default value on creation for the "title" field. DefaultTitle string // DefaultCompleted holds the default value on creation for the "completed" field. @@ -72,6 +86,16 @@ func ByID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldID, opts...).ToFunc() } +// ByCreatedAt orders the results by the created_at field. +func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() +} + +// ByUpdatedAt orders the results by the updated_at field. +func ByUpdatedAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldUpdatedAt, opts...).ToFunc() +} + // ByTitle orders the results by the title field. func ByTitle(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldTitle, opts...).ToFunc() diff --git a/schema/ent/example/ent/todo/where.go b/schema/ent/example/ent/todo/where.go index cb78775..f9b6002 100644 --- a/schema/ent/example/ent/todo/where.go +++ b/schema/ent/example/ent/todo/where.go @@ -4,6 +4,7 @@ package todo import ( "ersteller-lib/schema/ent/example/ent/predicate" + "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -54,6 +55,16 @@ func IDLTE(id int) predicate.Todo { return predicate.Todo(sql.FieldLTE(FieldID, id)) } +// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. +func CreatedAt(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldEQ(FieldCreatedAt, v)) +} + +// UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ. +func UpdatedAt(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldEQ(FieldUpdatedAt, v)) +} + // Title applies equality check predicate on the "title" field. It's identical to TitleEQ. func Title(v string) predicate.Todo { return predicate.Todo(sql.FieldEQ(FieldTitle, v)) @@ -64,6 +75,86 @@ func Completed(v bool) predicate.Todo { return predicate.Todo(sql.FieldEQ(FieldCompleted, v)) } +// CreatedAtEQ applies the EQ predicate on the "created_at" field. +func CreatedAtEQ(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldEQ(FieldCreatedAt, v)) +} + +// CreatedAtNEQ applies the NEQ predicate on the "created_at" field. +func CreatedAtNEQ(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldNEQ(FieldCreatedAt, v)) +} + +// CreatedAtIn applies the In predicate on the "created_at" field. +func CreatedAtIn(vs ...time.Time) predicate.Todo { + return predicate.Todo(sql.FieldIn(FieldCreatedAt, vs...)) +} + +// CreatedAtNotIn applies the NotIn predicate on the "created_at" field. +func CreatedAtNotIn(vs ...time.Time) predicate.Todo { + return predicate.Todo(sql.FieldNotIn(FieldCreatedAt, vs...)) +} + +// CreatedAtGT applies the GT predicate on the "created_at" field. +func CreatedAtGT(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldGT(FieldCreatedAt, v)) +} + +// CreatedAtGTE applies the GTE predicate on the "created_at" field. +func CreatedAtGTE(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldGTE(FieldCreatedAt, v)) +} + +// CreatedAtLT applies the LT predicate on the "created_at" field. +func CreatedAtLT(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldLT(FieldCreatedAt, v)) +} + +// CreatedAtLTE applies the LTE predicate on the "created_at" field. +func CreatedAtLTE(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldLTE(FieldCreatedAt, v)) +} + +// UpdatedAtEQ applies the EQ predicate on the "updated_at" field. +func UpdatedAtEQ(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field. +func UpdatedAtNEQ(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldNEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtIn applies the In predicate on the "updated_at" field. +func UpdatedAtIn(vs ...time.Time) predicate.Todo { + return predicate.Todo(sql.FieldIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field. +func UpdatedAtNotIn(vs ...time.Time) predicate.Todo { + return predicate.Todo(sql.FieldNotIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtGT applies the GT predicate on the "updated_at" field. +func UpdatedAtGT(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldGT(FieldUpdatedAt, v)) +} + +// UpdatedAtGTE applies the GTE predicate on the "updated_at" field. +func UpdatedAtGTE(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldGTE(FieldUpdatedAt, v)) +} + +// UpdatedAtLT applies the LT predicate on the "updated_at" field. +func UpdatedAtLT(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldLT(FieldUpdatedAt, v)) +} + +// UpdatedAtLTE applies the LTE predicate on the "updated_at" field. +func UpdatedAtLTE(v time.Time) predicate.Todo { + return predicate.Todo(sql.FieldLTE(FieldUpdatedAt, v)) +} + // TitleEQ applies the EQ predicate on the "title" field. func TitleEQ(v string) predicate.Todo { return predicate.Todo(sql.FieldEQ(FieldTitle, v)) diff --git a/schema/ent/example/ent/todo_create.go b/schema/ent/example/ent/todo_create.go index a0870aa..0dc2520 100644 --- a/schema/ent/example/ent/todo_create.go +++ b/schema/ent/example/ent/todo_create.go @@ -8,6 +8,7 @@ import ( "ersteller-lib/schema/ent/example/ent/group" "ersteller-lib/schema/ent/example/ent/todo" "fmt" + "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" @@ -20,6 +21,34 @@ type TodoCreate struct { hooks []Hook } +// SetCreatedAt sets the "created_at" field. +func (_c *TodoCreate) SetCreatedAt(v time.Time) *TodoCreate { + _c.mutation.SetCreatedAt(v) + return _c +} + +// SetNillableCreatedAt sets the "created_at" field if the given value is not nil. +func (_c *TodoCreate) SetNillableCreatedAt(v *time.Time) *TodoCreate { + if v != nil { + _c.SetCreatedAt(*v) + } + return _c +} + +// SetUpdatedAt sets the "updated_at" field. +func (_c *TodoCreate) SetUpdatedAt(v time.Time) *TodoCreate { + _c.mutation.SetUpdatedAt(v) + return _c +} + +// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. +func (_c *TodoCreate) SetNillableUpdatedAt(v *time.Time) *TodoCreate { + if v != nil { + _c.SetUpdatedAt(*v) + } + return _c +} + // SetTitle sets the "title" field. func (_c *TodoCreate) SetTitle(v string) *TodoCreate { _c.mutation.SetTitle(v) @@ -102,6 +131,14 @@ func (_c *TodoCreate) ExecX(ctx context.Context) { // defaults sets the default values of the builder before save. func (_c *TodoCreate) defaults() { + if _, ok := _c.mutation.CreatedAt(); !ok { + v := todo.DefaultCreatedAt() + _c.mutation.SetCreatedAt(v) + } + if _, ok := _c.mutation.UpdatedAt(); !ok { + v := todo.DefaultUpdatedAt() + _c.mutation.SetUpdatedAt(v) + } if _, ok := _c.mutation.Title(); !ok { v := todo.DefaultTitle _c.mutation.SetTitle(v) @@ -114,6 +151,12 @@ func (_c *TodoCreate) defaults() { // check runs all checks and user-defined validators on the builder. func (_c *TodoCreate) check() error { + if _, ok := _c.mutation.CreatedAt(); !ok { + return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Todo.created_at"`)} + } + if _, ok := _c.mutation.UpdatedAt(); !ok { + return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Todo.updated_at"`)} + } if _, ok := _c.mutation.Title(); !ok { return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "Todo.title"`)} } @@ -146,6 +189,14 @@ func (_c *TodoCreate) createSpec() (*Todo, *sqlgraph.CreateSpec) { _node = &Todo{config: _c.config} _spec = sqlgraph.NewCreateSpec(todo.Table, sqlgraph.NewFieldSpec(todo.FieldID, field.TypeInt)) ) + if value, ok := _c.mutation.CreatedAt(); ok { + _spec.SetField(todo.FieldCreatedAt, field.TypeTime, value) + _node.CreatedAt = value + } + if value, ok := _c.mutation.UpdatedAt(); ok { + _spec.SetField(todo.FieldUpdatedAt, field.TypeTime, value) + _node.UpdatedAt = value + } if value, ok := _c.mutation.Title(); ok { _spec.SetField(todo.FieldTitle, field.TypeString, value) _node.Title = value diff --git a/schema/ent/example/ent/todo_query.go b/schema/ent/example/ent/todo_query.go index 83bf57f..edfe87a 100644 --- a/schema/ent/example/ent/todo_query.go +++ b/schema/ent/example/ent/todo_query.go @@ -299,12 +299,12 @@ func (_q *TodoQuery) WithGroup(opts ...func(*GroupQuery)) *TodoQuery { // Example: // // var v []struct { -// Title string `json:"title,omitempty"` +// CreatedAt time.Time `json:"created_at,omitempty"` // Count int `json:"count,omitempty"` // } // // client.Todo.Query(). -// GroupBy(todo.FieldTitle). +// GroupBy(todo.FieldCreatedAt). // Aggregate(ent.Count()). // Scan(ctx, &v) func (_q *TodoQuery) GroupBy(field string, fields ...string) *TodoGroupBy { @@ -322,11 +322,11 @@ func (_q *TodoQuery) GroupBy(field string, fields ...string) *TodoGroupBy { // Example: // // var v []struct { -// Title string `json:"title,omitempty"` +// CreatedAt time.Time `json:"created_at,omitempty"` // } // // client.Todo.Query(). -// Select(todo.FieldTitle). +// Select(todo.FieldCreatedAt). // Scan(ctx, &v) func (_q *TodoQuery) Select(fields ...string) *TodoSelect { _q.ctx.Fields = append(_q.ctx.Fields, fields...) diff --git a/schema/ent/example/ent/todo_update.go b/schema/ent/example/ent/todo_update.go index ad2152c..9c516b2 100644 --- a/schema/ent/example/ent/todo_update.go +++ b/schema/ent/example/ent/todo_update.go @@ -9,6 +9,7 @@ import ( "ersteller-lib/schema/ent/example/ent/predicate" "ersteller-lib/schema/ent/example/ent/todo" "fmt" + "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" @@ -28,6 +29,12 @@ func (_u *TodoUpdate) Where(ps ...predicate.Todo) *TodoUpdate { return _u } +// SetUpdatedAt sets the "updated_at" field. +func (_u *TodoUpdate) SetUpdatedAt(v time.Time) *TodoUpdate { + _u.mutation.SetUpdatedAt(v) + return _u +} + // SetTitle sets the "title" field. func (_u *TodoUpdate) SetTitle(v string) *TodoUpdate { _u.mutation.SetTitle(v) @@ -88,6 +95,7 @@ func (_u *TodoUpdate) ClearGroup() *TodoUpdate { // Save executes the query and returns the number of nodes affected by the update operation. func (_u *TodoUpdate) Save(ctx context.Context) (int, error) { + _u.defaults() return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } @@ -113,6 +121,14 @@ func (_u *TodoUpdate) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (_u *TodoUpdate) defaults() { + if _, ok := _u.mutation.UpdatedAt(); !ok { + v := todo.UpdateDefaultUpdatedAt() + _u.mutation.SetUpdatedAt(v) + } +} + func (_u *TodoUpdate) sqlSave(ctx context.Context) (_node int, err error) { _spec := sqlgraph.NewUpdateSpec(todo.Table, todo.Columns, sqlgraph.NewFieldSpec(todo.FieldID, field.TypeInt)) if ps := _u.mutation.predicates; len(ps) > 0 { @@ -122,6 +138,9 @@ func (_u *TodoUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(todo.FieldUpdatedAt, field.TypeTime, value) + } if value, ok := _u.mutation.Title(); ok { _spec.SetField(todo.FieldTitle, field.TypeString, value) } @@ -177,6 +196,12 @@ type TodoUpdateOne struct { mutation *TodoMutation } +// SetUpdatedAt sets the "updated_at" field. +func (_u *TodoUpdateOne) SetUpdatedAt(v time.Time) *TodoUpdateOne { + _u.mutation.SetUpdatedAt(v) + return _u +} + // SetTitle sets the "title" field. func (_u *TodoUpdateOne) SetTitle(v string) *TodoUpdateOne { _u.mutation.SetTitle(v) @@ -250,6 +275,7 @@ func (_u *TodoUpdateOne) Select(field string, fields ...string) *TodoUpdateOne { // Save executes the query and returns the updated Todo entity. func (_u *TodoUpdateOne) Save(ctx context.Context) (*Todo, error) { + _u.defaults() return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks) } @@ -275,6 +301,14 @@ func (_u *TodoUpdateOne) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (_u *TodoUpdateOne) defaults() { + if _, ok := _u.mutation.UpdatedAt(); !ok { + v := todo.UpdateDefaultUpdatedAt() + _u.mutation.SetUpdatedAt(v) + } +} + func (_u *TodoUpdateOne) sqlSave(ctx context.Context) (_node *Todo, err error) { _spec := sqlgraph.NewUpdateSpec(todo.Table, todo.Columns, sqlgraph.NewFieldSpec(todo.FieldID, field.TypeInt)) id, ok := _u.mutation.ID() @@ -301,6 +335,9 @@ func (_u *TodoUpdateOne) sqlSave(ctx context.Context) (_node *Todo, err error) { } } } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(todo.FieldUpdatedAt, field.TypeTime, value) + } if value, ok := _u.mutation.Title(); ok { _spec.SetField(todo.FieldTitle, field.TypeString, value) } diff --git a/schema/ent/example/ent/user.go b/schema/ent/example/ent/user.go index 3d2620a..a08d68d 100644 --- a/schema/ent/example/ent/user.go +++ b/schema/ent/example/ent/user.go @@ -17,14 +17,14 @@ type User struct { config `json:"-"` // ID of the ent. ID int `json:"id,omitempty"` - // Email holds the value of the "email" field. - Email string `json:"email,omitempty"` - // Password holds the value of the "password" field. - Password string `json:"password,omitempty"` // CreatedAt holds the value of the "created_at" field. CreatedAt time.Time `json:"created_at,omitempty"` // UpdatedAt holds the value of the "updated_at" field. UpdatedAt time.Time `json:"updated_at,omitempty"` + // Email holds the value of the "email" field. + Email string `json:"email,omitempty"` + // Password holds the value of the "password" field. + Password string `json:"password,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the UserQuery when eager-loading is set. Edges UserEdges `json:"edges"` @@ -81,18 +81,6 @@ func (_m *User) assignValues(columns []string, values []any) error { return fmt.Errorf("unexpected type %T for field id", value) } _m.ID = int(value.Int64) - case user.FieldEmail: - if value, ok := values[i].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field email", values[i]) - } else if value.Valid { - _m.Email = value.String - } - case user.FieldPassword: - if value, ok := values[i].(*sql.NullString); !ok { - return fmt.Errorf("unexpected type %T for field password", values[i]) - } else if value.Valid { - _m.Password = value.String - } case user.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { return fmt.Errorf("unexpected type %T for field created_at", values[i]) @@ -105,6 +93,18 @@ func (_m *User) assignValues(columns []string, values []any) error { } else if value.Valid { _m.UpdatedAt = value.Time } + case user.FieldEmail: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field email", values[i]) + } else if value.Valid { + _m.Email = value.String + } + case user.FieldPassword: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field password", values[i]) + } else if value.Valid { + _m.Password = value.String + } default: _m.selectValues.Set(columns[i], values[i]) } @@ -146,17 +146,17 @@ func (_m *User) String() string { var builder strings.Builder builder.WriteString("User(") builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID)) - builder.WriteString("email=") - builder.WriteString(_m.Email) - builder.WriteString(", ") - builder.WriteString("password=") - builder.WriteString(_m.Password) - builder.WriteString(", ") builder.WriteString("created_at=") builder.WriteString(_m.CreatedAt.Format(time.ANSIC)) builder.WriteString(", ") builder.WriteString("updated_at=") builder.WriteString(_m.UpdatedAt.Format(time.ANSIC)) + builder.WriteString(", ") + builder.WriteString("email=") + builder.WriteString(_m.Email) + builder.WriteString(", ") + builder.WriteString("password=") + builder.WriteString(_m.Password) builder.WriteByte(')') return builder.String() } diff --git a/schema/ent/example/ent/user/user.go b/schema/ent/example/ent/user/user.go index 43c2de5..42daa50 100644 --- a/schema/ent/example/ent/user/user.go +++ b/schema/ent/example/ent/user/user.go @@ -14,14 +14,14 @@ const ( Label = "user" // FieldID holds the string denoting the id field in the database. FieldID = "id" - // FieldEmail holds the string denoting the email field in the database. - FieldEmail = "email" - // FieldPassword holds the string denoting the password field in the database. - FieldPassword = "password" // FieldCreatedAt holds the string denoting the created_at field in the database. FieldCreatedAt = "created_at" // FieldUpdatedAt holds the string denoting the updated_at field in the database. FieldUpdatedAt = "updated_at" + // FieldEmail holds the string denoting the email field in the database. + FieldEmail = "email" + // FieldPassword holds the string denoting the password field in the database. + FieldPassword = "password" // EdgeGroup holds the string denoting the group edge name in mutations. EdgeGroup = "group" // Table holds the table name of the user in the database. @@ -36,10 +36,10 @@ const ( // Columns holds all SQL columns for user fields. var Columns = []string{ FieldID, - FieldEmail, - FieldPassword, FieldCreatedAt, FieldUpdatedAt, + FieldEmail, + FieldPassword, } var ( @@ -59,16 +59,16 @@ func ValidColumn(column string) bool { } var ( + // DefaultCreatedAt holds the default value on creation for the "created_at" field. + DefaultCreatedAt func() time.Time + // DefaultUpdatedAt holds the default value on creation for the "updated_at" field. + DefaultUpdatedAt func() time.Time + // UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field. + UpdateDefaultUpdatedAt func() time.Time // DefaultEmail holds the default value on creation for the "email" field. DefaultEmail string // DefaultPassword holds the default value on creation for the "password" field. DefaultPassword string - // DefaultCreatedAt holds the default value on creation for the "created_at" field. - DefaultCreatedAt time.Time - // DefaultUpdatedAt holds the default value on creation for the "updated_at" field. - DefaultUpdatedAt time.Time - // UpdateDefaultUpdatedAt holds the default value on update for the "updated_at" field. - UpdateDefaultUpdatedAt func() time.Time ) // OrderOption defines the ordering options for the User queries. @@ -79,16 +79,6 @@ func ByID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldID, opts...).ToFunc() } -// ByEmail orders the results by the email field. -func ByEmail(opts ...sql.OrderTermOption) OrderOption { - return sql.OrderByField(FieldEmail, opts...).ToFunc() -} - -// ByPassword orders the results by the password field. -func ByPassword(opts ...sql.OrderTermOption) OrderOption { - return sql.OrderByField(FieldPassword, opts...).ToFunc() -} - // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() @@ -99,6 +89,16 @@ func ByUpdatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldUpdatedAt, opts...).ToFunc() } +// ByEmail orders the results by the email field. +func ByEmail(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldEmail, opts...).ToFunc() +} + +// ByPassword orders the results by the password field. +func ByPassword(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldPassword, opts...).ToFunc() +} + // ByGroupCount orders the results by group count. func ByGroupCount(opts ...sql.OrderTermOption) OrderOption { return func(s *sql.Selector) { diff --git a/schema/ent/example/ent/user/where.go b/schema/ent/example/ent/user/where.go index 74eabbe..cd80c1e 100644 --- a/schema/ent/example/ent/user/where.go +++ b/schema/ent/example/ent/user/where.go @@ -55,6 +55,16 @@ func IDLTE(id int) predicate.User { return predicate.User(sql.FieldLTE(FieldID, id)) } +// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. +func CreatedAt(v time.Time) predicate.User { + return predicate.User(sql.FieldEQ(FieldCreatedAt, v)) +} + +// UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ. +func UpdatedAt(v time.Time) predicate.User { + return predicate.User(sql.FieldEQ(FieldUpdatedAt, v)) +} + // Email applies equality check predicate on the "email" field. It's identical to EmailEQ. func Email(v string) predicate.User { return predicate.User(sql.FieldEQ(FieldEmail, v)) @@ -65,16 +75,86 @@ func Password(v string) predicate.User { return predicate.User(sql.FieldEQ(FieldPassword, v)) } -// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. -func CreatedAt(v time.Time) predicate.User { +// CreatedAtEQ applies the EQ predicate on the "created_at" field. +func CreatedAtEQ(v time.Time) predicate.User { return predicate.User(sql.FieldEQ(FieldCreatedAt, v)) } -// UpdatedAt applies equality check predicate on the "updated_at" field. It's identical to UpdatedAtEQ. -func UpdatedAt(v time.Time) predicate.User { +// CreatedAtNEQ applies the NEQ predicate on the "created_at" field. +func CreatedAtNEQ(v time.Time) predicate.User { + return predicate.User(sql.FieldNEQ(FieldCreatedAt, v)) +} + +// CreatedAtIn applies the In predicate on the "created_at" field. +func CreatedAtIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldIn(FieldCreatedAt, vs...)) +} + +// CreatedAtNotIn applies the NotIn predicate on the "created_at" field. +func CreatedAtNotIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldNotIn(FieldCreatedAt, vs...)) +} + +// CreatedAtGT applies the GT predicate on the "created_at" field. +func CreatedAtGT(v time.Time) predicate.User { + return predicate.User(sql.FieldGT(FieldCreatedAt, v)) +} + +// CreatedAtGTE applies the GTE predicate on the "created_at" field. +func CreatedAtGTE(v time.Time) predicate.User { + return predicate.User(sql.FieldGTE(FieldCreatedAt, v)) +} + +// CreatedAtLT applies the LT predicate on the "created_at" field. +func CreatedAtLT(v time.Time) predicate.User { + return predicate.User(sql.FieldLT(FieldCreatedAt, v)) +} + +// CreatedAtLTE applies the LTE predicate on the "created_at" field. +func CreatedAtLTE(v time.Time) predicate.User { + return predicate.User(sql.FieldLTE(FieldCreatedAt, v)) +} + +// UpdatedAtEQ applies the EQ predicate on the "updated_at" field. +func UpdatedAtEQ(v time.Time) predicate.User { return predicate.User(sql.FieldEQ(FieldUpdatedAt, v)) } +// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field. +func UpdatedAtNEQ(v time.Time) predicate.User { + return predicate.User(sql.FieldNEQ(FieldUpdatedAt, v)) +} + +// UpdatedAtIn applies the In predicate on the "updated_at" field. +func UpdatedAtIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field. +func UpdatedAtNotIn(vs ...time.Time) predicate.User { + return predicate.User(sql.FieldNotIn(FieldUpdatedAt, vs...)) +} + +// UpdatedAtGT applies the GT predicate on the "updated_at" field. +func UpdatedAtGT(v time.Time) predicate.User { + return predicate.User(sql.FieldGT(FieldUpdatedAt, v)) +} + +// UpdatedAtGTE applies the GTE predicate on the "updated_at" field. +func UpdatedAtGTE(v time.Time) predicate.User { + return predicate.User(sql.FieldGTE(FieldUpdatedAt, v)) +} + +// UpdatedAtLT applies the LT predicate on the "updated_at" field. +func UpdatedAtLT(v time.Time) predicate.User { + return predicate.User(sql.FieldLT(FieldUpdatedAt, v)) +} + +// UpdatedAtLTE applies the LTE predicate on the "updated_at" field. +func UpdatedAtLTE(v time.Time) predicate.User { + return predicate.User(sql.FieldLTE(FieldUpdatedAt, v)) +} + // EmailEQ applies the EQ predicate on the "email" field. func EmailEQ(v string) predicate.User { return predicate.User(sql.FieldEQ(FieldEmail, v)) @@ -205,86 +285,6 @@ func PasswordContainsFold(v string) predicate.User { return predicate.User(sql.FieldContainsFold(FieldPassword, v)) } -// CreatedAtEQ applies the EQ predicate on the "created_at" field. -func CreatedAtEQ(v time.Time) predicate.User { - return predicate.User(sql.FieldEQ(FieldCreatedAt, v)) -} - -// CreatedAtNEQ applies the NEQ predicate on the "created_at" field. -func CreatedAtNEQ(v time.Time) predicate.User { - return predicate.User(sql.FieldNEQ(FieldCreatedAt, v)) -} - -// CreatedAtIn applies the In predicate on the "created_at" field. -func CreatedAtIn(vs ...time.Time) predicate.User { - return predicate.User(sql.FieldIn(FieldCreatedAt, vs...)) -} - -// CreatedAtNotIn applies the NotIn predicate on the "created_at" field. -func CreatedAtNotIn(vs ...time.Time) predicate.User { - return predicate.User(sql.FieldNotIn(FieldCreatedAt, vs...)) -} - -// CreatedAtGT applies the GT predicate on the "created_at" field. -func CreatedAtGT(v time.Time) predicate.User { - return predicate.User(sql.FieldGT(FieldCreatedAt, v)) -} - -// CreatedAtGTE applies the GTE predicate on the "created_at" field. -func CreatedAtGTE(v time.Time) predicate.User { - return predicate.User(sql.FieldGTE(FieldCreatedAt, v)) -} - -// CreatedAtLT applies the LT predicate on the "created_at" field. -func CreatedAtLT(v time.Time) predicate.User { - return predicate.User(sql.FieldLT(FieldCreatedAt, v)) -} - -// CreatedAtLTE applies the LTE predicate on the "created_at" field. -func CreatedAtLTE(v time.Time) predicate.User { - return predicate.User(sql.FieldLTE(FieldCreatedAt, v)) -} - -// UpdatedAtEQ applies the EQ predicate on the "updated_at" field. -func UpdatedAtEQ(v time.Time) predicate.User { - return predicate.User(sql.FieldEQ(FieldUpdatedAt, v)) -} - -// UpdatedAtNEQ applies the NEQ predicate on the "updated_at" field. -func UpdatedAtNEQ(v time.Time) predicate.User { - return predicate.User(sql.FieldNEQ(FieldUpdatedAt, v)) -} - -// UpdatedAtIn applies the In predicate on the "updated_at" field. -func UpdatedAtIn(vs ...time.Time) predicate.User { - return predicate.User(sql.FieldIn(FieldUpdatedAt, vs...)) -} - -// UpdatedAtNotIn applies the NotIn predicate on the "updated_at" field. -func UpdatedAtNotIn(vs ...time.Time) predicate.User { - return predicate.User(sql.FieldNotIn(FieldUpdatedAt, vs...)) -} - -// UpdatedAtGT applies the GT predicate on the "updated_at" field. -func UpdatedAtGT(v time.Time) predicate.User { - return predicate.User(sql.FieldGT(FieldUpdatedAt, v)) -} - -// UpdatedAtGTE applies the GTE predicate on the "updated_at" field. -func UpdatedAtGTE(v time.Time) predicate.User { - return predicate.User(sql.FieldGTE(FieldUpdatedAt, v)) -} - -// UpdatedAtLT applies the LT predicate on the "updated_at" field. -func UpdatedAtLT(v time.Time) predicate.User { - return predicate.User(sql.FieldLT(FieldUpdatedAt, v)) -} - -// UpdatedAtLTE applies the LTE predicate on the "updated_at" field. -func UpdatedAtLTE(v time.Time) predicate.User { - return predicate.User(sql.FieldLTE(FieldUpdatedAt, v)) -} - // HasGroup applies the HasEdge predicate on the "group" edge. func HasGroup() predicate.User { return predicate.User(func(s *sql.Selector) { diff --git a/schema/ent/example/ent/user_create.go b/schema/ent/example/ent/user_create.go index 60dd511..3c58c9d 100644 --- a/schema/ent/example/ent/user_create.go +++ b/schema/ent/example/ent/user_create.go @@ -21,34 +21,6 @@ type UserCreate struct { hooks []Hook } -// SetEmail sets the "email" field. -func (_c *UserCreate) SetEmail(v string) *UserCreate { - _c.mutation.SetEmail(v) - return _c -} - -// SetNillableEmail sets the "email" field if the given value is not nil. -func (_c *UserCreate) SetNillableEmail(v *string) *UserCreate { - if v != nil { - _c.SetEmail(*v) - } - return _c -} - -// SetPassword sets the "password" field. -func (_c *UserCreate) SetPassword(v string) *UserCreate { - _c.mutation.SetPassword(v) - return _c -} - -// SetNillablePassword sets the "password" field if the given value is not nil. -func (_c *UserCreate) SetNillablePassword(v *string) *UserCreate { - if v != nil { - _c.SetPassword(*v) - } - return _c -} - // SetCreatedAt sets the "created_at" field. func (_c *UserCreate) SetCreatedAt(v time.Time) *UserCreate { _c.mutation.SetCreatedAt(v) @@ -77,6 +49,34 @@ func (_c *UserCreate) SetNillableUpdatedAt(v *time.Time) *UserCreate { return _c } +// SetEmail sets the "email" field. +func (_c *UserCreate) SetEmail(v string) *UserCreate { + _c.mutation.SetEmail(v) + return _c +} + +// SetNillableEmail sets the "email" field if the given value is not nil. +func (_c *UserCreate) SetNillableEmail(v *string) *UserCreate { + if v != nil { + _c.SetEmail(*v) + } + return _c +} + +// SetPassword sets the "password" field. +func (_c *UserCreate) SetPassword(v string) *UserCreate { + _c.mutation.SetPassword(v) + return _c +} + +// SetNillablePassword sets the "password" field if the given value is not nil. +func (_c *UserCreate) SetNillablePassword(v *string) *UserCreate { + if v != nil { + _c.SetPassword(*v) + } + return _c +} + // AddGroupIDs adds the "group" edge to the Group entity by IDs. func (_c *UserCreate) AddGroupIDs(ids ...int) *UserCreate { _c.mutation.AddGroupIDs(ids...) @@ -127,6 +127,14 @@ func (_c *UserCreate) ExecX(ctx context.Context) { // defaults sets the default values of the builder before save. func (_c *UserCreate) defaults() { + if _, ok := _c.mutation.CreatedAt(); !ok { + v := user.DefaultCreatedAt() + _c.mutation.SetCreatedAt(v) + } + if _, ok := _c.mutation.UpdatedAt(); !ok { + v := user.DefaultUpdatedAt() + _c.mutation.SetUpdatedAt(v) + } if _, ok := _c.mutation.Email(); !ok { v := user.DefaultEmail _c.mutation.SetEmail(v) @@ -135,30 +143,22 @@ func (_c *UserCreate) defaults() { v := user.DefaultPassword _c.mutation.SetPassword(v) } - if _, ok := _c.mutation.CreatedAt(); !ok { - v := user.DefaultCreatedAt - _c.mutation.SetCreatedAt(v) - } - if _, ok := _c.mutation.UpdatedAt(); !ok { - v := user.DefaultUpdatedAt - _c.mutation.SetUpdatedAt(v) - } } // check runs all checks and user-defined validators on the builder. func (_c *UserCreate) check() error { - if _, ok := _c.mutation.Email(); !ok { - return &ValidationError{Name: "email", err: errors.New(`ent: missing required field "User.email"`)} - } - if _, ok := _c.mutation.Password(); !ok { - return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)} - } if _, ok := _c.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)} } if _, ok := _c.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)} } + if _, ok := _c.mutation.Email(); !ok { + return &ValidationError{Name: "email", err: errors.New(`ent: missing required field "User.email"`)} + } + if _, ok := _c.mutation.Password(); !ok { + return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)} + } return nil } @@ -185,14 +185,6 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { _node = &User{config: _c.config} _spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt)) ) - if value, ok := _c.mutation.Email(); ok { - _spec.SetField(user.FieldEmail, field.TypeString, value) - _node.Email = value - } - if value, ok := _c.mutation.Password(); ok { - _spec.SetField(user.FieldPassword, field.TypeString, value) - _node.Password = value - } if value, ok := _c.mutation.CreatedAt(); ok { _spec.SetField(user.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value @@ -201,6 +193,14 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } + if value, ok := _c.mutation.Email(); ok { + _spec.SetField(user.FieldEmail, field.TypeString, value) + _node.Email = value + } + if value, ok := _c.mutation.Password(); ok { + _spec.SetField(user.FieldPassword, field.TypeString, value) + _node.Password = value + } if nodes := _c.mutation.GroupIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, diff --git a/schema/ent/example/ent/user_query.go b/schema/ent/example/ent/user_query.go index b4b1cc2..d5068f7 100644 --- a/schema/ent/example/ent/user_query.go +++ b/schema/ent/example/ent/user_query.go @@ -299,12 +299,12 @@ func (_q *UserQuery) WithGroup(opts ...func(*GroupQuery)) *UserQuery { // Example: // // var v []struct { -// Email string `json:"email,omitempty"` +// CreatedAt time.Time `json:"created_at,omitempty"` // Count int `json:"count,omitempty"` // } // // client.User.Query(). -// GroupBy(user.FieldEmail). +// GroupBy(user.FieldCreatedAt). // Aggregate(ent.Count()). // Scan(ctx, &v) func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy { @@ -322,11 +322,11 @@ func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy { // Example: // // var v []struct { -// Email string `json:"email,omitempty"` +// CreatedAt time.Time `json:"created_at,omitempty"` // } // // client.User.Query(). -// Select(user.FieldEmail). +// Select(user.FieldCreatedAt). // Scan(ctx, &v) func (_q *UserQuery) Select(fields ...string) *UserSelect { _q.ctx.Fields = append(_q.ctx.Fields, fields...) diff --git a/schema/ent/example/ent/user_update.go b/schema/ent/example/ent/user_update.go index 01b2fa6..8e1123d 100644 --- a/schema/ent/example/ent/user_update.go +++ b/schema/ent/example/ent/user_update.go @@ -29,6 +29,12 @@ func (_u *UserUpdate) Where(ps ...predicate.User) *UserUpdate { return _u } +// SetUpdatedAt sets the "updated_at" field. +func (_u *UserUpdate) SetUpdatedAt(v time.Time) *UserUpdate { + _u.mutation.SetUpdatedAt(v) + return _u +} + // SetEmail sets the "email" field. func (_u *UserUpdate) SetEmail(v string) *UserUpdate { _u.mutation.SetEmail(v) @@ -57,12 +63,6 @@ func (_u *UserUpdate) SetNillablePassword(v *string) *UserUpdate { return _u } -// SetUpdatedAt sets the "updated_at" field. -func (_u *UserUpdate) SetUpdatedAt(v time.Time) *UserUpdate { - _u.mutation.SetUpdatedAt(v) - return _u -} - // AddGroupIDs adds the "group" edge to the Group entity by IDs. func (_u *UserUpdate) AddGroupIDs(ids ...int) *UserUpdate { _u.mutation.AddGroupIDs(ids...) @@ -149,15 +149,15 @@ func (_u *UserUpdate) sqlSave(ctx context.Context) (_node int, err error) { } } } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) + } if value, ok := _u.mutation.Email(); ok { _spec.SetField(user.FieldEmail, field.TypeString, value) } if value, ok := _u.mutation.Password(); ok { _spec.SetField(user.FieldPassword, field.TypeString, value) } - if value, ok := _u.mutation.UpdatedAt(); ok { - _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) - } if _u.mutation.GroupCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, @@ -223,6 +223,12 @@ type UserUpdateOne struct { mutation *UserMutation } +// SetUpdatedAt sets the "updated_at" field. +func (_u *UserUpdateOne) SetUpdatedAt(v time.Time) *UserUpdateOne { + _u.mutation.SetUpdatedAt(v) + return _u +} + // SetEmail sets the "email" field. func (_u *UserUpdateOne) SetEmail(v string) *UserUpdateOne { _u.mutation.SetEmail(v) @@ -251,12 +257,6 @@ func (_u *UserUpdateOne) SetNillablePassword(v *string) *UserUpdateOne { return _u } -// SetUpdatedAt sets the "updated_at" field. -func (_u *UserUpdateOne) SetUpdatedAt(v time.Time) *UserUpdateOne { - _u.mutation.SetUpdatedAt(v) - return _u -} - // AddGroupIDs adds the "group" edge to the Group entity by IDs. func (_u *UserUpdateOne) AddGroupIDs(ids ...int) *UserUpdateOne { _u.mutation.AddGroupIDs(ids...) @@ -373,15 +373,15 @@ func (_u *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) { } } } + if value, ok := _u.mutation.UpdatedAt(); ok { + _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) + } if value, ok := _u.mutation.Email(); ok { _spec.SetField(user.FieldEmail, field.TypeString, value) } if value, ok := _u.mutation.Password(); ok { _spec.SetField(user.FieldPassword, field.TypeString, value) } - if value, ok := _u.mutation.UpdatedAt(); ok { - _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) - } if _u.mutation.GroupCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, diff --git a/schema/ent/time_mixin.go b/schema/ent/time_mixin.go new file mode 100644 index 0000000..f060f49 --- /dev/null +++ b/schema/ent/time_mixin.go @@ -0,0 +1,27 @@ +package ersteller_ent + +import ( + "time" + + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/mixin" +) + +// TimeMixin implements the ent.Mixin for sharing +// time fields with package schemas. +type TimeMixin struct { + mixin.Schema +} + +// Fields of the TimeMixin. +func (TimeMixin) Fields() []ent.Field { + return []ent.Field{ + field.Time("created_at"). + Default(time.Now). + Immutable(), + field.Time("updated_at"). + Default(time.Now). + UpdateDefault(time.Now), + } +}