Files
ent/schema/schemautil/time.go
Alex Snast a5b1805da3 ent/entc: rename time mixin fields (#109)
Summary:
Pull Request resolved: https://github.com/facebookincubator/ent/pull/109

- created_at -> create_time
- updated_at -> update_time

Reviewed By: a8m

Differential Revision: D18035896

fbshipit-source-id: ec9aef703c444554eb0f65a668703a1a6607b93e
2019-10-22 03:45:52 -07:00

58 lines
1.3 KiB
Go

// 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)