mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
ent/schema: adding schemautil package (#100)
Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/100 package includes time mixins adding created_at / updated_at fields Reviewed By: a8m Differential Revision: D17950845 fbshipit-source-id: 84c0e7b82ed09857375a42add4254a4a85a52335
This commit is contained in:
committed by
Facebook Github Bot
parent
c6b395904c
commit
38b1fc9e66
53
schema/schemautil/time.go
Normal file
53
schema/schemautil/time.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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("created_at").
|
||||
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("updated_at").
|
||||
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)
|
||||
38
schema/schemautil/time_test.go
Normal file
38
schema/schemautil/time_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package schemautil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTimeMixin(t *testing.T) {
|
||||
t.Run("Create", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fields := CreateTimeMixin{}.Fields()
|
||||
require.Len(t, fields, 1)
|
||||
desc := fields[0].Descriptor()
|
||||
assert.Equal(t, "created_at", desc.Name)
|
||||
assert.True(t, desc.Immutable)
|
||||
assert.NotNil(t, desc.Default)
|
||||
assert.Nil(t, desc.UpdateDefault)
|
||||
})
|
||||
t.Run("Update", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fields := UpdateTimeMixin{}.Fields()
|
||||
require.Len(t, fields, 1)
|
||||
desc := fields[0].Descriptor()
|
||||
assert.Equal(t, "updated_at", desc.Name)
|
||||
assert.True(t, desc.Immutable)
|
||||
assert.NotNil(t, desc.Default)
|
||||
assert.NotNil(t, desc.UpdateDefault)
|
||||
})
|
||||
t.Run("Compose", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fields := TimeMixin{}.Fields()
|
||||
require.Len(t, fields, 2)
|
||||
assert.Equal(t, "created_at", fields[0].Descriptor().Name)
|
||||
assert.Equal(t, "updated_at", fields[1].Descriptor().Name)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user