/
time.go
57 lines (45 loc) · 1.33 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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)