feat: ent mixin.
This commit is contained in:
21
entgo/mixin/creator_id.go
Normal file
21
entgo/mixin/creator_id.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
_mixin "entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
type CreatorId struct {
|
||||
_mixin.Schema
|
||||
}
|
||||
|
||||
func (CreatorId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint64("creator_id").
|
||||
Comment("创建者用户ID").
|
||||
Immutable().
|
||||
Optional().
|
||||
Nillable(),
|
||||
}
|
||||
}
|
||||
37
entgo/mixin/snowflake_id.go
Normal file
37
entgo/mixin/snowflake_id.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
|
||||
"github.com/tx7do/go-utils/sonyflake"
|
||||
)
|
||||
|
||||
type SnowflackId struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (SnowflackId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Uint64("id").
|
||||
Comment("id").
|
||||
DefaultFunc(sonyflake.GenerateSonyflake).
|
||||
Positive().
|
||||
Immutable().
|
||||
StructTag(`json:"id,omitempty"`).
|
||||
SchemaType(map[string]string{
|
||||
dialect.MySQL: "bigint",
|
||||
dialect.Postgres: "bigint",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the SnowflackId.
|
||||
func (SnowflackId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
33
entgo/mixin/string_id.go
Normal file
33
entgo/mixin/string_id.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package mixin
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type StringId struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (StringId) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("id").
|
||||
Comment("id").
|
||||
MaxLen(25).
|
||||
NotEmpty().
|
||||
Unique().
|
||||
Immutable().
|
||||
Match(regexp.MustCompile("^[0-9a-zA-Z_\\-]+$")).
|
||||
StructTag(`json:"id,omitempty"`),
|
||||
}
|
||||
}
|
||||
|
||||
// Indexes of the StringId.
|
||||
func (StringId) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("id"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user