Skip to content

Commit

Permalink
doc/hook: additional example for rejecting op (ent#2407)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored and gitlawr committed Apr 13, 2022
1 parent 2a5c5e1 commit c42c4ae
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 7 deletions.
14 changes: 14 additions & 0 deletions doc/md/hooks.md
Expand Up @@ -246,11 +246,25 @@ func (SomeMixin) Hooks() []ent.Hook {
return []ent.Hook{
// Execute "HookA" only for the UpdateOne and DeleteOne operations.
hook.On(HookA(), ent.OpUpdateOne|ent.OpDeleteOne),

// Don't execute "HookB" on Create operation.
hook.Unless(HookB(), ent.OpCreate),

// Execute "HookC" only if the ent.Mutation is changing the "status" field,
// and clearing the "dirty" field.
hook.If(HookC(), hook.And(hook.HasFields("status"), hook.HasClearedFields("dirty"))),

// Disallow changing the "password" field on Update (many) operation.
hook.If(
hook.FixedError(errors.New("password cannot be edited on update many")),
hook.And(
hook.HasOp(ent.OpUpdate),
hook.Or(
hook.HasFields("password"),
hook.HasClearedFields("password"),
),
),
),
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion entc/integration/hooks/ent/internal/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion entc/integration/hooks/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 74 additions & 1 deletion entc/integration/hooks/ent/mutation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions entc/integration/hooks/ent/runtime/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion entc/integration/hooks/ent/schema/user.go
Expand Up @@ -6,11 +6,13 @@ package schema

import (
"context"
"errors"
"fmt"

"entgo.io/ent/entc/integration/hooks/ent/hook"
"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"
Expand All @@ -34,6 +36,9 @@ func (User) Fields() []ent.Field {
field.String("name"),
field.Uint("worth").
Optional(),
field.String("password").
Optional().
Sensitive(),
}
}

Expand All @@ -47,6 +52,22 @@ func (User) Edges() []ent.Edge {
}
}

// 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
}
Expand Down
11 changes: 10 additions & 1 deletion entc/integration/hooks/ent/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion entc/integration/hooks/ent/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c42c4ae

Please sign in to comment.