From b27c96f93202db79127c9e610bd96cd52c733cbc Mon Sep 17 00:00:00 2001 From: tx7do Date: Wed, 1 Nov 2023 18:23:12 +0800 Subject: [PATCH] feat: ent mixin. --- entgo/mixin/creator_id.go | 21 +++++++++++++++++++++ entgo/mixin/snowflake_id.go | 37 +++++++++++++++++++++++++++++++++++++ entgo/mixin/string_id.go | 33 +++++++++++++++++++++++++++++++++ tag.bat | 2 +- 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 entgo/mixin/creator_id.go create mode 100644 entgo/mixin/snowflake_id.go create mode 100644 entgo/mixin/string_id.go diff --git a/entgo/mixin/creator_id.go b/entgo/mixin/creator_id.go new file mode 100644 index 0000000..0e0ba67 --- /dev/null +++ b/entgo/mixin/creator_id.go @@ -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(), + } +} diff --git a/entgo/mixin/snowflake_id.go b/entgo/mixin/snowflake_id.go new file mode 100644 index 0000000..8880c0d --- /dev/null +++ b/entgo/mixin/snowflake_id.go @@ -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"), + } +} diff --git a/entgo/mixin/string_id.go b/entgo/mixin/string_id.go new file mode 100644 index 0000000..c527da6 --- /dev/null +++ b/entgo/mixin/string_id.go @@ -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"), + } +} diff --git a/tag.bat b/tag.bat index c261d55..2073bf5 100644 --- a/tag.bat +++ b/tag.bat @@ -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