feat: first version.
This commit is contained in:
39
entgo/mixin/autoincrement_id.go
Normal file
39
entgo/mixin/autoincrement_id.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/contrib/entproto"
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
var _ ent.Mixin = (*AutoIncrementId)(nil)
|
||||
|
||||
type AutoIncrementId struct{ mixin.Schema }
|
||||
|
||||
func (AutoIncrementId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint32("id").
|
||||
Comment("id").
|
||||
StructTag(`json:"id,omitempty"`).
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "int",
|
||||
dialect.Postgres: "serial",
|
||||
}).
|
||||
Annotations(
|
||||
entproto.Field(1),
|
||||
).
|
||||
Positive().
|
||||
Immutable().
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the AutoIncrementId.
|
||||
func (AutoIncrementId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
85
entgo/mixin/operator.go
Normal file
85
entgo/mixin/operator.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
var _ ent.Mixin = (*CreateBy)(nil)
|
||||
|
||||
type CreateBy struct{ mixin.Schema }
|
||||
|
||||
func (CreateBy) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("create_by").
|
||||
Comment("创建者").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*UpdateBy)(nil)
|
||||
|
||||
type UpdateBy struct{ mixin.Schema }
|
||||
|
||||
func (UpdateBy) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("update_by").
|
||||
Comment("更新者").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*DeleteBy)(nil)
|
||||
|
||||
type DeleteBy struct{ mixin.Schema }
|
||||
|
||||
func (DeleteBy) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("delete_by").
|
||||
Comment("删除者").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*CreatedBy)(nil)
|
||||
|
||||
type CreatedBy struct{ mixin.Schema }
|
||||
|
||||
func (CreatedBy) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("created_by").
|
||||
Comment("创建者").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*UpdatedBy)(nil)
|
||||
|
||||
type UpdatedBy struct{ mixin.Schema }
|
||||
|
||||
func (UpdatedBy) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("updated_by").
|
||||
Comment("更新者").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*DeletedBy)(nil)
|
||||
|
||||
type DeletedBy struct{ mixin.Schema }
|
||||
|
||||
func (DeletedBy) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("deleted_by").
|
||||
Comment("删除者").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
122
entgo/mixin/time.go
Normal file
122
entgo/mixin/time.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
var _ ent.Mixin = (*CreateTime)(nil)
|
||||
|
||||
type CreateTime struct{ mixin.Schema }
|
||||
|
||||
func (CreateTime) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// 创建时间,毫秒
|
||||
field.Int64("create_time").
|
||||
Comment("创建时间").
|
||||
Immutable().
|
||||
Optional().
|
||||
Nillable().
|
||||
DefaultFunc(time.Now().UnixMilli),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*UpdateTime)(nil)
|
||||
|
||||
type UpdateTime struct{ mixin.Schema }
|
||||
|
||||
func (UpdateTime) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// 更新时间,毫秒
|
||||
// 需要注意的是,如果不是程序自动更新,那么这个字段不会被更新,除非在数据库里面下触发器更新。
|
||||
field.Int64("update_time").
|
||||
Comment("更新时间").
|
||||
Optional().
|
||||
Nillable().
|
||||
UpdateDefault(time.Now().UnixMilli),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*DeleteTime)(nil)
|
||||
|
||||
type DeleteTime struct{ mixin.Schema }
|
||||
|
||||
func (DeleteTime) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// 删除时间,毫秒
|
||||
field.Int64("delete_time").
|
||||
Comment("删除时间").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*Time)(nil)
|
||||
|
||||
type Time struct{ mixin.Schema }
|
||||
|
||||
func (Time) Fields() []ent.Field {
|
||||
var fields []ent.Field
|
||||
fields = append(fields, CreateTime{}.Fields()...)
|
||||
fields = append(fields, UpdateTime{}.Fields()...)
|
||||
fields = append(fields, DeleteTime{}.Fields()...)
|
||||
return fields
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*CreatedAt)(nil)
|
||||
|
||||
type CreatedAt struct{ mixin.Schema }
|
||||
|
||||
func (CreatedAt) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// 创建时间
|
||||
field.Time("created_at").
|
||||
Comment("创建时间").
|
||||
Immutable().
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*UpdatedAt)(nil)
|
||||
|
||||
type UpdatedAt struct{ mixin.Schema }
|
||||
|
||||
func (UpdatedAt) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// 更新时间
|
||||
field.Time("updated_at").
|
||||
Comment("更新时间").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*DeletedAt)(nil)
|
||||
|
||||
type DeletedAt struct{ mixin.Schema }
|
||||
|
||||
func (DeletedAt) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// 删除时间
|
||||
field.Time("deleted_at").
|
||||
Comment("删除时间").
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
|
||||
var _ ent.Mixin = (*TimeAt)(nil)
|
||||
|
||||
type TimeAt struct{ mixin.Schema }
|
||||
|
||||
func (TimeAt) Fields() []ent.Field {
|
||||
var fields []ent.Field
|
||||
fields = append(fields, CreatedAt{}.Fields()...)
|
||||
fields = append(fields, UpdatedAt{}.Fields()...)
|
||||
fields = append(fields, DeletedAt{}.Fields()...)
|
||||
return fields
|
||||
}
|
||||
31
entgo/mixin/uuid_id.go
Normal file
31
entgo/mixin/uuid_id.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var _ ent.Mixin = (*UuidId)(nil)
|
||||
|
||||
type UuidId struct{ mixin.Schema }
|
||||
|
||||
func (UuidId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.UUID("id", uuid.UUID{}).
|
||||
Comment("id").
|
||||
Default(uuid.New).
|
||||
Unique().
|
||||
Immutable().
|
||||
StructTag(`json:"id,omitempty"`),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the UuidId.
|
||||
func (UuidId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user