doc/hook: additional example for rejecting op (#2407)

This commit is contained in:
Ariel Mashraki
2022-03-16 16:09:04 +02:00
committed by GitHub
parent c071898395
commit 9bb800105d
12 changed files with 355 additions and 7 deletions

View File

@@ -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"),
),
),
),
}
}
```