feat: ent mixin.

This commit is contained in:
tx7do
2023-11-01 18:23:12 +08:00
parent 836f4e2461
commit b27c96f932
4 changed files with 92 additions and 1 deletions

21
entgo/mixin/creator_id.go Normal file
View 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(),
}
}

View 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
View 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"),
}
}

View File

@@ -1,6 +1,6 @@
git tag v1.1.4
git tag bank_card/v1.1.0
git tag entgo/v1.1.4
git tag entgo/v1.1.5
git tag geoip/v1.1.0
git push origin --tags