英文:
Create base entity in go ent framework
问题
我是你的中文翻译助手,以下是你要翻译的内容:
我刚开始学习golang,并且正在使用ent框架作为ORM。每个模式都应该有created_at
和updated_at
字段,我希望只写一次,而不是为每个模式都写一次,例如:
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.String("email"),
field.Time("created_at").Default(time.Now).Immutable(),
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
}
}
.....
func (Product) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.Float("price"),
field.Time("created_at").Default(time.Now).Immutable(),
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
}
}
我之前使用面向对象的语言,可以通过某种BaseEntity
让所有其他实体继承它来实现这一点,但是不确定在这种情况下该如何做。
英文:
I'm new to golang, and am using ent framework as an ORM.
Every schema should have the fields created_at
and updated_at
, and I want to write it once rather then write it for every schema i.e:
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.String("email"),
field.Time("created_at").Default(time.Now).Immutable(),
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
}
}
.....
func (Product) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.Float("price"),
field.Time("created_at").Default(time.Now).Immutable(),
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
}
}
I came from OOP lagnageus where I can do that it with some kind of BaseEntity
that all other entities inherits from it, but not sure how to do it in this case.
答案1
得分: 5
你可以使用MixIn,它允许你创建可重用的ent.Schema代码片段,可以通过组合将其注入到其他模式中,因此你可以在另一个模式中定义你的时间戳值,并将其混入到需要的其他模式中,所以你的时间戳(基本实体)混入模式将如下所示:
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
"time"
)
type TimeStamp struct {
mixin.Schema
}
func (TimeStamp) Fields() []ent.Field {
return []ent.Field{
field.Time("created_at").Default(time.Now).Immutable(),
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
}
}
现在当它被定义并且你需要将此时间戳模式注入到其他模式中时,你需要在该模式的MixIn()中注入它,它将如下所示:
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
......
func (Product) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
所以你最终的模式将如下所示。
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.String("email"),
}
}
.....
func (Product) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
func (Product) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.Float("price"),
}
}
此外,ent提供了内置的时间混入,可用于向模式添加create_time
和update_time
字段,它被定义为mixin.Time{}
。
更多详细信息,请参阅Mixin。
英文:
You can use MixIn which allows you to create reusable pieces of ent.Schema code that can be injected into other schemas using composition so, you can define your timestamp values in another schema and just mix it in the other schemas where you need it so your timestamp(base entity) mixin schema will look like this
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
"time"
)
type TimeStamp struct {
mixin.Schema
}
func (TimeStamp) Fields() []ent.Field {
return []ent.Field{
field.Time("created_at").Default(time.Now).Immutable(),
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
}
}
Now when it is defined and you need to inject this timestamp schema into your other schema. For this what you have to do is to inject it in the MixIn() of that schema which will look like this
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
......
func (Product) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
So you final schemas will look like this.
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.String("email"),
}
}
.....
func (Product) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeStamp{},
}
}
func (Product) Fields() []ent.Field {
return []ent.Field{
field.Int("id"),
field.String("name").MaxLen(70),
field.Float("price"),
}
}
Also ent provides builtin time mixins that can be used for adding the create_time
and update_time
fields to the schema and it is defined as mixin.Time{}
.
For more details you can look into Mixin
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论