在Go的ent框架中创建基本实体。

huangapple go评论102阅读模式
英文:

Create base entity in go ent framework

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始学习golang,并且正在使用ent框架作为ORM。每个模式都应该有created_atupdated_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_timeupdate_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

huangapple
  • 本文由 发表于 2021年8月29日 18:05:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/68971830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定