snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
112
entc/integration/hooks/ent/schema/card.go
Normal file
112
entc/integration/hooks/ent/schema/card.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// 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 schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
gen "entgo.io/ent/entc/integration/hooks/ent"
|
||||
"entgo.io/ent/entc/integration/hooks/ent/card"
|
||||
"entgo.io/ent/entc/integration/hooks/ent/hook"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
// RejectUpdate rejects ~all update operations
|
||||
// that are not on a specific entity.
|
||||
type RejectUpdate struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (RejectUpdate) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
hook.If(
|
||||
hook.Reject(ent.OpUpdate),
|
||||
// Accept only updates that contains the "expired_at" field.
|
||||
hook.Not(hook.HasFields(card.FieldExpiredAt)),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// Card holds the schema definition for the CreditCard entity.
|
||||
type Card struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (Card) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
RejectUpdate{},
|
||||
}
|
||||
}
|
||||
|
||||
func (Card) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
hook.On(
|
||||
func(next ent.Mutator) ent.Mutator {
|
||||
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
||||
num, ok := m.Field(card.FieldNumber)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("missing card number value")
|
||||
}
|
||||
// Validator in hooks.
|
||||
if len(num.(string)) < 4 {
|
||||
return nil, fmt.Errorf("card number is too short")
|
||||
}
|
||||
return next.Mutate(ctx, m)
|
||||
})
|
||||
},
|
||||
ent.OpCreate,
|
||||
),
|
||||
func(next ent.Mutator) ent.Mutator {
|
||||
return hook.CardFunc(func(ctx context.Context, m *gen.CardMutation) (ent.Value, error) {
|
||||
m.SetInHook("value was set in hook")
|
||||
if _, ok := m.Name(); !ok {
|
||||
m.SetName("unknown")
|
||||
}
|
||||
return next.Mutate(ctx, m)
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (Card) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("number").
|
||||
Immutable().
|
||||
Default("unknown").
|
||||
NotEmpty(),
|
||||
field.String("name").
|
||||
Optional().
|
||||
Comment("Exact name written on card"),
|
||||
field.Time("created_at").
|
||||
Default(time.Now),
|
||||
field.String("in_hook").
|
||||
Comment("InHook is a mandatory field that is set by the hook."),
|
||||
field.Time("expired_at").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
func (Card) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("owner", User.Type).
|
||||
Ref("cards").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
func (Card) Interceptors() []ent.Interceptor {
|
||||
return []ent.Interceptor{
|
||||
ent.InterceptFunc(func(next ent.Querier) ent.Querier {
|
||||
return ent.QuerierFunc(func(ctx context.Context, q ent.Query) (ent.Value, error) {
|
||||
return next.Query(ctx, q)
|
||||
})
|
||||
}),
|
||||
}
|
||||
}
|
||||
85
entc/integration/hooks/ent/schema/mixin.go
Normal file
85
entc/integration/hooks/ent/schema/mixin.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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 schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
gen "entgo.io/ent/entc/integration/hooks/ent"
|
||||
"entgo.io/ent/entc/integration/hooks/ent/hook"
|
||||
"entgo.io/ent/entc/integration/hooks/ent/intercept"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
type SoftDeleteMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (SoftDeleteMixin) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Time("delete_time").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
type softDeleteKey struct{}
|
||||
|
||||
// SkipSoftDelete returns a new context that skips the soft-delete interceptor/mutators.
|
||||
func SkipSoftDelete(parent context.Context) context.Context {
|
||||
return context.WithValue(parent, softDeleteKey{}, true)
|
||||
}
|
||||
|
||||
func (d SoftDeleteMixin) Interceptors() []ent.Interceptor {
|
||||
return []ent.Interceptor{
|
||||
intercept.TraverseFunc(func(ctx context.Context, q intercept.Query) error {
|
||||
// Skip soft-delete, means include soft-deleted entities.
|
||||
if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
|
||||
return nil
|
||||
}
|
||||
d.P(q)
|
||||
return nil
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func (d SoftDeleteMixin) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
hook.On(
|
||||
func(next ent.Mutator) ent.Mutator {
|
||||
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
||||
// Skip soft-delete, means delete the entity permanently.
|
||||
if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
|
||||
return next.Mutate(ctx, m)
|
||||
}
|
||||
mx, ok := m.(interface {
|
||||
SetOp(ent.Op)
|
||||
Client() *gen.Client
|
||||
SetDeleteTime(time.Time)
|
||||
WhereP(...func(*sql.Selector))
|
||||
})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
d.P(mx)
|
||||
mx.SetOp(ent.OpUpdate)
|
||||
mx.SetDeleteTime(time.Now())
|
||||
return mx.Client().Mutate(ctx, m)
|
||||
})
|
||||
},
|
||||
ent.OpDeleteOne|ent.OpDelete,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (d SoftDeleteMixin) P(w interface{ WhereP(...func(*sql.Selector)) }) {
|
||||
w.WhereP(
|
||||
sql.FieldIsNull(d.Fields()[0].Descriptor().Name),
|
||||
)
|
||||
}
|
||||
40
entc/integration/hooks/ent/schema/pet.go
Normal file
40
entc/integration/hooks/ent/schema/pet.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// 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 schema
|
||||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
// Pet holds the schema definition for the Pet entity.
|
||||
type Pet struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Mixin of the Pet.
|
||||
func (Pet) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
SoftDeleteMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the Pet.
|
||||
func (Pet) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("name").
|
||||
Optional(),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the Pet.
|
||||
func (Pet) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("owner", User.Type).
|
||||
Ref("pets").
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
122
entc/integration/hooks/ent/schema/user.go
Normal file
122
entc/integration/hooks/ent/schema/user.go
Normal file
@@ -0,0 +1,122 @@
|
||||
// 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 schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"entgo.io/ent/entc/integration/hooks/ent/user"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/entc/integration/hooks/ent/hook"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
)
|
||||
|
||||
// User holds the schema definition for the User entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Mixin of the User.
|
||||
func (User) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
VersionMixin{},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("name"),
|
||||
field.Uint("worth").
|
||||
Optional(),
|
||||
field.String("password").
|
||||
Optional().
|
||||
Sensitive(),
|
||||
field.Bool("active").
|
||||
Default(true),
|
||||
}
|
||||
}
|
||||
|
||||
// Edges of the User.
|
||||
func (User) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.To("cards", Card.Type),
|
||||
edge.To("pets", Pet.Type),
|
||||
edge.To("friends", User.Type),
|
||||
edge.To("best_friend", User.Type).
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
// Hooks of the User.
|
||||
func (User) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
hook.If(
|
||||
hook.FixedError(errors.New("password cannot be edited on update-many")),
|
||||
hook.And(
|
||||
hook.HasOp(ent.OpUpdate),
|
||||
hook.Or(
|
||||
hook.HasFields(user.FieldPassword),
|
||||
hook.HasClearedFields(user.FieldPassword),
|
||||
),
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
type VersionMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (VersionMixin) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("version").
|
||||
Default(0),
|
||||
}
|
||||
}
|
||||
|
||||
func (VersionMixin) Hooks() []ent.Hook {
|
||||
return []ent.Hook{
|
||||
hook.On(VersionHook(), ent.OpUpdateOne),
|
||||
}
|
||||
}
|
||||
|
||||
func VersionHook() ent.Hook {
|
||||
type OldSetVersion interface {
|
||||
SetVersion(int)
|
||||
Version() (int, bool)
|
||||
OldVersion(context.Context) (int, error)
|
||||
}
|
||||
return func(next ent.Mutator) ent.Mutator {
|
||||
// A hook that validates the "version" field is incremented by 1 on each update.
|
||||
// Note that this is just a dummy example, and it doesn't promise consistency in
|
||||
// the database.
|
||||
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
||||
ver, ok := m.(OldSetVersion)
|
||||
if !ok {
|
||||
return next.Mutate(ctx, m)
|
||||
}
|
||||
oldV, err := ver.OldVersion(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
curV, exists := ver.Version()
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("version field is required in update mutation")
|
||||
}
|
||||
if curV != oldV+1 {
|
||||
return nil, fmt.Errorf("version field must be incremented by 1")
|
||||
}
|
||||
// Add an SQL predicate that validates the "version" column is equal
|
||||
// to "oldV" (it wasn't changed during the mutation by other process).
|
||||
return next.Mutate(ctx, m)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user