entc/gen: add indexes, edges and hooks to mixin (#431)

This commit is contained in:
Ariel Mashraki
2020-04-20 13:40:56 +03:00
committed by GitHub
parent cec1dd1edf
commit 1c49159d18
22 changed files with 454 additions and 249 deletions

81
schema/mixin/mixin.go Normal file
View File

@@ -0,0 +1,81 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package mixin
import (
"time"
"github.com/facebookincubator/ent"
"github.com/facebookincubator/ent/schema/field"
)
// Schema is the default implementation for the ent.Mixin interface.
// It should be embedded in end-user mixin as follows:
//
// type M struct {
// mixin.Schema
// }
//
type Schema struct{}
// Fields of the mixin.
func (Schema) Fields() []ent.Field { return nil }
// Edges of the mixin.
func (Schema) Edges() []ent.Edge { return nil }
// Indexes of the mixin.
func (Schema) Indexes() []ent.Index { return nil }
// Hooks of the mixin.
func (Schema) Hooks() []ent.Hook { return nil }
// time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*Schema)(nil)
// CreateTime adds created at time field.
type CreateTime struct{ Schema }
// Fields of the create time mixin.
func (CreateTime) Fields() []ent.Field {
return []ent.Field{
field.Time("create_time").
Default(time.Now).
Immutable(),
}
}
// create time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*CreateTime)(nil)
// UpdateTime adds updated at time field.
type UpdateTime struct{ Schema }
// Fields of the update time mixin.
func (UpdateTime) Fields() []ent.Field {
return []ent.Field{
field.Time("update_time").
Default(time.Now).
UpdateDefault(time.Now).
Immutable(),
}
}
// create time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*UpdateTime)(nil)
// Time composes create/update time mixin.
type Time struct{ Schema }
// Fields of the time mixin.
func (Time) Fields() []ent.Field {
return append(
CreateTime{}.Fields(),
UpdateTime{}.Fields()...,
)
}
// time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*Time)(nil)

View File

@@ -2,7 +2,7 @@
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package schemautil
package mixin
import (
"testing"
@@ -14,7 +14,7 @@ import (
func TestTimeMixin(t *testing.T) {
t.Run("Create", func(t *testing.T) {
t.Parallel()
fields := CreateTimeMixin{}.Fields()
fields := CreateTime{}.Fields()
require.Len(t, fields, 1)
desc := fields[0].Descriptor()
assert.Equal(t, "create_time", desc.Name)
@@ -24,7 +24,7 @@ func TestTimeMixin(t *testing.T) {
})
t.Run("Update", func(t *testing.T) {
t.Parallel()
fields := UpdateTimeMixin{}.Fields()
fields := UpdateTime{}.Fields()
require.Len(t, fields, 1)
desc := fields[0].Descriptor()
assert.Equal(t, "update_time", desc.Name)
@@ -34,7 +34,7 @@ func TestTimeMixin(t *testing.T) {
})
t.Run("Compose", func(t *testing.T) {
t.Parallel()
fields := TimeMixin{}.Fields()
fields := Time{}.Fields()
require.Len(t, fields, 2)
assert.Equal(t, "create_time", fields[0].Descriptor().Name)
assert.Equal(t, "update_time", fields[1].Descriptor().Name)

View File

@@ -1,57 +0,0 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package schemautil
import (
"time"
"github.com/facebookincubator/ent"
"github.com/facebookincubator/ent/schema/field"
)
// CreateTimeMixin adds created at time field.
type CreateTimeMixin struct{}
// Fields of the create time mixin.
func (CreateTimeMixin) Fields() []ent.Field {
return []ent.Field{
field.Time("create_time").
Default(time.Now).
Immutable(),
}
}
// create time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*CreateTimeMixin)(nil)
// UpdateTimeMixin adds updated at time field.
type UpdateTimeMixin struct{}
// Fields of the update time mixin.
func (UpdateTimeMixin) Fields() []ent.Field {
return []ent.Field{
field.Time("update_time").
Default(time.Now).
UpdateDefault(time.Now).
Immutable(),
}
}
// create time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*UpdateTimeMixin)(nil)
// TimeMixin composes create/update time mixin.
type TimeMixin struct{}
// Fields of the time mixin.
func (TimeMixin) Fields() []ent.Field {
return append(
CreateTimeMixin{}.Fields(),
UpdateTimeMixin{}.Fields()...,
)
}
// time mixin must implement `Mixin` interface.
var _ ent.Mixin = (*TimeMixin)(nil)