From 29c064f702b312dd168f9ea8fa1207c92b8c2b5c Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Tue, 29 Aug 2023 16:22:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20id=E5=AD=97=E6=AE=B5mixin=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0immutable=EF=BC=9BtimeMixin=E5=88=86=E5=89=B2=E4=B8=BA?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/orm/nent/mixins/id.go | 1 + core/orm/nent/mixins/sort.go | 22 +++++------ core/orm/nent/mixins/time.go | 72 ++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 34 deletions(-) diff --git a/core/orm/nent/mixins/id.go b/core/orm/nent/mixins/id.go index 71d0edd..2374f67 100644 --- a/core/orm/nent/mixins/id.go +++ b/core/orm/nent/mixins/id.go @@ -26,6 +26,7 @@ func (d DistributedIDMixin) Fields() []ent.Field { field.Int64("id"). Annotations(entsql.Annotation{Incremental: tea.Bool(false)}). DefaultFunc(d.idFn). + Immutable(). Comment("Primary Key | 主键"), } } diff --git a/core/orm/nent/mixins/sort.go b/core/orm/nent/mixins/sort.go index 414cc6e..033e9d6 100644 --- a/core/orm/nent/mixins/sort.go +++ b/core/orm/nent/mixins/sort.go @@ -1,23 +1,23 @@ package mixins import ( - "entgo.io/ent" - "entgo.io/ent/schema/field" - "entgo.io/ent/schema/mixin" + "entgo.io/ent" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/mixin" ) // SortMixin implements the ent.Mixin for sharing // sort fields with package schemas. type SortMixin struct { - // We embed the `mixin.Schema` to avoid - // implementing the rest of the methods. - mixin.Schema + // We embed the `mixin.Schema` to avoid + // implementing the rest of the methods. + mixin.Schema } func (SortMixin) Fields() []ent.Field { - return []ent.Field{ - field.Uint32("sort"). - Default(1). - Comment("Sort number | 排序号"), - } + return []ent.Field{ + field.Uint32("sort"). + Default(1). + Comment("Sort number | 排序号"), + } } diff --git a/core/orm/nent/mixins/time.go b/core/orm/nent/mixins/time.go index 1f8cfef..6a86897 100644 --- a/core/orm/nent/mixins/time.go +++ b/core/orm/nent/mixins/time.go @@ -1,34 +1,60 @@ package mixins import ( - "entgo.io/ent" - "entgo.io/ent/dialect" - "entgo.io/ent/schema/field" - "entgo.io/ent/schema/mixin" - "time" + "entgo.io/ent" + "entgo.io/ent/dialect" + "entgo.io/ent/schema/field" + "entgo.io/ent/schema/mixin" + "time" ) // TimeMixin implements the ent.Mixin for sharing -// time fields with package schemas. +// all time fields with package schemas. type TimeMixin struct { - mixin.Schema + mixin.Schema } func (TimeMixin) Fields() []ent.Field { - return []ent.Field{ - field.Time("created_at"). - Immutable(). - Default(time.Now). - SchemaType(map[string]string{ - dialect.MySQL: "datetime", - }). - Comment("创建时间"), - field.Time("updated_at"). - Default(time.Now). - UpdateDefault(time.Now). - SchemaType(map[string]string{ - dialect.MySQL: "datetime", - }). - Comment("更新时间"), - } + return append( + CreateTimeMixin{}.Fields(), + UpdateTimeMixin{}.Fields()..., + ) +} + +// CreateTimeMixin implements the ent.Mixin for sharing +// time fields with package schemas. +type CreateTimeMixin struct { + mixin.Schema +} + +// Fields of the create-time mixin. +func (CreateTimeMixin) Fields() []ent.Field { + return []ent.Field{ + field.Time("created_at"). + Default(time.Now). + Immutable(). + SchemaType(map[string]string{ + dialect.MySQL: "datetime", + }). + Comment("Create Time | 创建时间"), + } +} + +// UpdateTimeMixin implements the ent.Mixin for sharing +// time fields with package schemas. +type UpdateTimeMixin struct { + mixin.Schema +} + +// Fields of the update-time mixin. +func (UpdateTimeMixin) Fields() []ent.Field { + return []ent.Field{ + field.Time("updated_at"). + Default(time.Now). + UpdateDefault(time.Now). + SchemaType(map[string]string{ + dialect.MySQL: "datetime", + }). + Comment("Update Time | 更新时间"), + } }