feat(schema): 添加基础混入定义公共时间字段
This commit is contained in:
1 parent
5281459fcb
commit
fe86c081f4
16 files changed
+1715
-14
No files matched your search
@@ -0,0 +1,29 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
// BaseMixin 定义公共的基础混入
|
||||
type BaseMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
// Fields 定义所有表都拥有的公共字段
|
||||
func (BaseMixin) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
// 创建时间:默认当前时间,不可修改(Immutable)
|
||||
field.Time("created_at").
|
||||
Default(time.Now).
|
||||
Immutable(),
|
||||
|
||||
// 更新时间:默认当前时间,并且在每次数据 Update 时自动更新为当前时间
|
||||
field.Time("updated_at").
|
||||
Default(time.Now).
|
||||
UpdateDefault(time.Now),
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,41 @@
|
||||
package schema
|
||||
|
||||
import "entgo.io/ent"
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// User holds the schema definition for the User entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (User) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
BaseMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return nil
|
||||
return []ent.Field{
|
||||
field.String("username").
|
||||
Unique().
|
||||
NotEmpty(),
|
||||
field.String("password").
|
||||
Sensitive(),
|
||||
field.String("email").
|
||||
Optional().
|
||||
Match(regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)), // 邮箱格式正则校验
|
||||
field.String("ip").
|
||||
Optional().
|
||||
Match(regexp.MustCompile(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$|^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$`)),
|
||||
field.String("addr").
|
||||
Optional().
|
||||
MaxLen(500),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the User.
|
||||
|
||||
Reference in new issue
Block a user