feat: id字段mixin添加immutable;timeMixin分割为两个

main v1.2.3
NoahLan 1 year ago
parent ed7599302c
commit 29c064f702

@ -26,6 +26,7 @@ func (d DistributedIDMixin) Fields() []ent.Field {
field.Int64("id"). field.Int64("id").
Annotations(entsql.Annotation{Incremental: tea.Bool(false)}). Annotations(entsql.Annotation{Incremental: tea.Bool(false)}).
DefaultFunc(d.idFn). DefaultFunc(d.idFn).
Immutable().
Comment("Primary Key | 主键"), Comment("Primary Key | 主键"),
} }
} }

@ -1,23 +1,23 @@
package mixins package mixins
import ( import (
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin" "entgo.io/ent/schema/mixin"
) )
// SortMixin implements the ent.Mixin for sharing // SortMixin implements the ent.Mixin for sharing
// sort fields with package schemas. // sort fields with package schemas.
type SortMixin struct { type SortMixin struct {
// We embed the `mixin.Schema` to avoid // We embed the `mixin.Schema` to avoid
// implementing the rest of the methods. // implementing the rest of the methods.
mixin.Schema mixin.Schema
} }
func (SortMixin) Fields() []ent.Field { func (SortMixin) Fields() []ent.Field {
return []ent.Field{ return []ent.Field{
field.Uint32("sort"). field.Uint32("sort").
Default(1). Default(1).
Comment("Sort number | 排序号"), Comment("Sort number | 排序号"),
} }
} }

@ -1,34 +1,60 @@
package mixins package mixins
import ( import (
"entgo.io/ent" "entgo.io/ent"
"entgo.io/ent/dialect" "entgo.io/ent/dialect"
"entgo.io/ent/schema/field" "entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin" "entgo.io/ent/schema/mixin"
"time" "time"
) )
// TimeMixin implements the ent.Mixin for sharing // TimeMixin implements the ent.Mixin for sharing
// time fields with package schemas. // all time fields with package schemas.
type TimeMixin struct { type TimeMixin struct {
mixin.Schema mixin.Schema
} }
func (TimeMixin) Fields() []ent.Field { func (TimeMixin) Fields() []ent.Field {
return []ent.Field{ return append(
field.Time("created_at"). CreateTimeMixin{}.Fields(),
Immutable(). UpdateTimeMixin{}.Fields()...,
Default(time.Now). )
SchemaType(map[string]string{ }
dialect.MySQL: "datetime",
}). // CreateTimeMixin implements the ent.Mixin for sharing
Comment("创建时间"), // time fields with package schemas.
field.Time("updated_at"). type CreateTimeMixin struct {
Default(time.Now). mixin.Schema
UpdateDefault(time.Now). }
SchemaType(map[string]string{
dialect.MySQL: "datetime", // Fields of the create-time mixin.
}). func (CreateTimeMixin) Fields() []ent.Field {
Comment("更新时间"), 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 | 更新时间"),
}
} }

Loading…
Cancel
Save