-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathent.go
46 lines (39 loc) · 1.11 KB
/
ent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package hutils
import (
"errors"
"time"
"unicode/utf8"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
)
// BaseSchema 替换ent.Schema使用,包含套路字段
type BaseSchema struct {
ent.Schema
}
func (BaseSchema) Mixin() []ent.Mixin {
return []ent.Mixin{BaseMixin{}}
}
// BaseMixin model补充一些套路字段
type BaseMixin struct {
mixin.Schema
}
func (BaseMixin) Fields() []ent.Field {
datetime := map[string]string{dialect.MySQL: "datetime"}
return []ent.Field{
field.String("uid").DefaultFunc(NewUUID).MaxLen(32).MinLen(32).Unique().Immutable(),
field.Time("created_at").Default(time.Now).Immutable().SchemaType(datetime),
field.Time("updated_at").Default(time.Now).SchemaType(datetime).UpdateDefault(time.Now),
field.Time("deactivated_at").Optional().Nillable().SchemaType(datetime),
}
}
// MaxRuneCount 字符串最大长度(包括中文)
func MaxRuneCount(maxLen int) func(s string) error {
return func(s string) error {
if utf8.RuneCountInString(s) > maxLen {
return errors.New("value is more than the max length")
}
return nil
}
}