61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package schema
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
ge "goweb/ent"
|
|
"goweb/ent/hook"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/schema/edge"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// User holds the schema definition for the User entity.
|
|
type User struct {
|
|
ent.Schema
|
|
}
|
|
|
|
// Fields of the User.
|
|
func (User) Fields() []ent.Field {
|
|
return []ent.Field{
|
|
field.String("name").
|
|
NotEmpty(),
|
|
field.String("email").
|
|
NotEmpty().
|
|
Unique(),
|
|
field.String("password").
|
|
Sensitive().
|
|
NotEmpty(),
|
|
field.Time("created_at").
|
|
Default(time.Now).
|
|
Immutable(),
|
|
}
|
|
}
|
|
|
|
// Edges of the User.
|
|
func (User) Edges() []ent.Edge {
|
|
return []ent.Edge{
|
|
edge.From("owner", PasswordToken.Type).
|
|
Ref("user"),
|
|
}
|
|
}
|
|
|
|
func (User) Hooks() []ent.Hook {
|
|
return []ent.Hook{
|
|
hook.On(
|
|
func(next ent.Mutator) ent.Mutator {
|
|
return hook.UserFunc(func(ctx context.Context, m *ge.UserMutation) (ent.Value, error) {
|
|
if v, exists := m.Email(); exists {
|
|
m.SetEmail(strings.ToLower(v))
|
|
}
|
|
return next.Mutate(ctx, m)
|
|
})
|
|
},
|
|
// Limit the hook only for these operations.
|
|
ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne,
|
|
),
|
|
}
|
|
}
|