You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool-biz/core/orm/nent/mixins/id.go

47 lines
999 B
Go

package mixins
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
"git.noahlan.cn/noahlan/ntool/nstd/tea"
)
// DistributedIDMixin implements the ent.Mixin for sharing
// Distributed ID field with package schemas.
type DistributedIDMixin struct {
mixin.Schema
idFn func() int64
}
func NewDistributedIDMixin(defaultIDFunc func() int64) DistributedIDMixin {
return DistributedIDMixin{
idFn: defaultIDFunc,
}
}
func (d DistributedIDMixin) Fields() []ent.Field {
return []ent.Field{
field.Int64("id").
Annotations(entsql.Annotation{Incremental: tea.Bool(false)}).
DefaultFunc(d.idFn).
Immutable().
Comment("Primary Key | 主键"),
}
}
// DefaultIDMixin implements the ent.Mixin for sharing
// ID field with package schemas.
type DefaultIDMixin struct {
mixin.Schema
}
func (DefaultIDMixin) Fields() []ent.Field {
return []ent.Field{
field.Int64("id").
Default(1).
Comment("Primary Key | 主键"),
}
}