diff --git a/doc/md/hooks.md b/doc/md/hooks.md index 70f4d74f4..61a6620b4 100755 --- a/doc/md/hooks.md +++ b/doc/md/hooks.md @@ -202,3 +202,39 @@ executes `f(g(h(...)))` on mutations. Also note, that **runtime hooks** are called before **schema hooks**. That is, if `g`, and `h` were defined in the schema, and `f` was registered using `client.Use(...)`, they will be executed as follows: `f(g(h(...)))`. + +## Hook helpers + +The generated hooks package provides several helpers that can help you control when a hook will +be executed. + +```go +package schema + +import ( + "context" + "fmt" + + "/ent/hook" + + "github.com/facebook/ent" + "github.com/facebook/ent/schema/mixin" +) + + +type SomeMixin struct { + mixin.Schema +} + +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"))), + } +} +``` diff --git a/entc/gen/internal/bindata.go b/entc/gen/internal/bindata.go index c20b3d30d..0eb8b3d2b 100644 --- a/entc/gen/internal/bindata.go +++ b/entc/gen/internal/bindata.go @@ -926,7 +926,7 @@ func templateHeaderTmpl() (*asset, error) { return a, nil } -var _templateHookTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x58\xdd\x8f\xdb\xb8\x11\x7f\xb6\xff\x8a\x89\xe0\x00\x52\xaa\xa5\x73\xf7\xd6\x06\x29\xb0\xf0\x25\xc8\x02\xd7\x75\x71\xcd\xf5\xe5\x70\xc8\x31\xd2\xc8\x66\x2d\x93\x02\x49\xef\x07\x1c\xfd\xef\xc5\x0c\x45\x49\xb6\x76\xf7\xf6\xd2\x14\xf7\xb2\x6b\xcf\x0c\xe7\xe3\x37\x1f\x1c\xfa\x78\x5c\xbe\x9a\xaf\x4c\x73\x6f\xd5\x66\xeb\xe1\xfb\xd7\xdf\xfd\xf5\xa2\xb1\xe8\x50\x7b\x78\x2f\x0b\xfc\x6c\xcc\x0e\xae\x74\x21\xe0\xb2\xae\x81\x85\x1c\x10\xdf\xde\x60\x29\xe6\x1f\xb7\xca\x81\x33\x07\x5b\x20\x14\xa6\x44\x50\x0e\x6a\x55\xa0\x76\x58\xc2\x41\x97\x68\xc1\x6f\x11\x2e\x1b\x59\x6c\x11\xbe\x17\xaf\x23\x17\x2a\x73\xd0\xe5\x5c\x69\xe6\xff\x78\xb5\x7a\x77\xfd\xaf\x77\x50\xa9\x1a\xa1\xa3\x59\x63\x3c\x94\xca\x62\xe1\x8d\xbd\x07\x53\x81\x1f\x19\xf3\x16\x51\xcc\x5f\x2d\xdb\x76\x3e\x3f\x1e\xa1\xc4\x4a\x69\x84\x64\x6b\xcc\x2e\x81\x8e\x78\xab\xfc\x16\xf0\xce\xa3\x2e\x61\x01\xc9\x3f\x65\xb1\x93\x1b\x4c\x46\x52\xb3\xe3\x11\x3c\xee\x9b\x5a\x7a\x3a\x8c\xb2\x44\x9b\x80\x20\xd6\xf1\x08\x74\x8e\x54\xa9\x7d\x63\xac\x87\xe4\x78\x84\x85\x58\x19\x5d\xa9\x8d\xe8\x94\x41\xdb\x26\x6c\x6b\xd1\xec\x36\xf0\xb7\xb7\xf0\x59\x3a\x7c\x48\x8a\x85\xac\xd4\x1b\x84\x85\x26\xc1\x85\xb8\x36\x25\xba\xe8\xc5\x42\xcb\x3d\x12\xbd\xb1\x4a\x7b\x58\x68\x71\x4d\x84\xe4\xfd\x41\x17\xbd\xab\x0b\x7f\xdf\x0c\x42\x15\x24\xaf\x5e\x3a\xf1\xd2\x25\xc1\xfa\x42\x8b\x7f\x1c\xbc\xf4\xca\x68\x3e\x4b\x46\x67\xcb\x25\x7c\xdc\x22\xf4\x16\xda\x16\x58\x89\x72\x20\x35\xc8\x52\x36\x9e\x72\x64\x40\xd6\xb5\xb9\x65\xe0\x0f\x0e\x09\x6d\x63\x4b\xa5\xa5\xbd\x67\x1d\xd5\x41\x17\xa4\x18\xa4\x0b\xba\x44\x67\x02\xf6\x64\xd2\x58\x31\x9f\xb1\xde\xb1\x21\x3a\x94\x16\x46\x7b\xbc\xf3\x84\x08\xfd\xcf\xa1\x8f\xa3\x6d\x33\x48\x23\x74\x6d\x2b\xfe\x2d\xeb\x03\xe6\x80\xd6\x1a\x9b\x05\xd7\x39\x1e\x84\x42\xd6\xb5\x83\x2a\x2d\xfc\x5d\x0e\xfb\x4c\xcc\x67\xa4\x1a\xd2\x6a\x6c\x2e\xeb\xa4\x49\x0a\x26\x56\xf7\x30\xb2\x14\x61\x7a\xc2\x3e\x1c\xe7\xb3\xd9\xfe\x26\x07\xb3\x23\xc0\xf7\x22\x1d\xfb\x3d\x9f\xcd\x54\x05\x2f\xcc\x8e\xc5\x66\x16\xfd\xc1\x6a\xd0\xaa\xce\xa1\xda\x7b\xf1\x8e\x54\x54\x69\x72\xd0\x78\xd7\x60\xe1\xb1\x0c\x30\x11\x80\xac\xe2\xe5\x47\x01\x81\x35\x86\x23\xa1\xe0\xe6\xb3\x59\x3b\xef\x55\xc6\x98\x6f\xb2\xf9\xec\xa4\x26\x97\x4b\x58\x19\x5d\x2a\xd6\x49\xc9\x04\x2a\x6a\x8a\xbb\xa3\xc5\x8c\x89\x39\x6b\x5f\x9d\x30\x1e\xce\xca\x04\x9d\xcf\xc6\xd4\x6c\xea\x52\x97\xb0\xb1\xe6\xd0\xb8\xc1\x82\x0b\xfd\xc5\xed\x7d\xfd\x03\x98\x06\x6d\x28\x04\xce\xcd\xa5\x2e\xd3\x4a\x59\xe7\x73\x70\x48\x67\x06\x0f\x72\x9a\x1f\x1e\x84\x10\x3d\x29\x1b\xf9\x77\x9c\xf7\xc1\xb3\xa7\x7f\x20\x9b\xe4\x2f\x67\x84\x92\xc3\xd6\x63\xc9\xc0\x97\x2f\xf0\x22\x38\xd2\x93\xc6\xa9\xab\x64\xed\xb0\x83\xbe\x32\x16\x3e\xe5\x1c\x28\xa5\x3e\x34\x2d\xbb\xcc\x27\x48\xf7\x54\xcf\xb9\x22\xd6\x34\x4a\xa4\xb7\x07\xa4\x14\x86\xd4\xad\xed\x53\x70\xae\x7f\x3a\x47\x73\x6d\xff\x5c\x30\x27\x58\x3e\x01\x65\x88\xf4\x99\x48\x3e\x0e\x64\xa7\xe6\x1c\xc7\x0e\xdf\x08\xe4\xb5\xf1\xa0\x71\x23\x3d\x52\x0b\x6c\xd4\x0d\xea\x01\xd2\x0e\xbc\x6b\xe3\xd3\x53\xd0\xbe\x35\x42\x9d\x82\x93\xb2\x18\x7c\xfc\x20\xdd\xba\x09\x3d\x3a\xb4\xa7\x47\xe7\x95\xde\x0c\x73\x21\x64\x7c\xf0\x9a\x4f\xa5\xa6\x19\x9b\x5e\x37\x4f\x78\xfe\xe9\xab\xfd\xde\x8b\x75\x93\x66\xe2\xca\xa5\xa6\x89\x7e\xd3\x41\xf2\xd6\x51\xe2\x4a\x55\x78\x48\x3e\x48\xf7\x5e\x61\x5d\xba\x04\x12\xfe\x90\x30\xed\xb2\x2c\xb1\xec\x19\xc3\xb7\xc0\x5d\xd5\x28\xed\x88\xcf\x1f\x3a\x62\x02\x17\x7c\xd1\x5e\xc4\x8b\x31\x16\xca\x0e\xef\x5d\x34\x1f\xaf\xbe\x3d\xfa\xad\x61\xee\x06\x7d\x64\x86\x13\xa4\x85\x6e\x8b\xe8\x33\xdd\x3d\x67\x70\xdf\xc8\x5a\x95\x92\x11\xff\x4d\x8c\xd4\xb5\xed\x6f\x40\x33\x91\xdd\x8b\xf7\xca\x48\x4f\xca\x1c\x70\xde\x2a\xbd\xc9\x3b\x39\xea\xb6\x40\x39\x4b\xc7\xff\x9a\x0f\xea\x88\xe3\x11\x54\x05\x1a\x7b\x17\xcf\x20\x6b\xdb\x4f\x3c\xad\xc3\x45\x80\x77\xca\x79\x17\x6e\xa8\x93\xb0\x82\xe3\xd9\x1b\x78\xd1\x89\x3c\x3e\xa5\x62\x9b\x86\x50\xfb\x3e\xed\x62\x0d\xe7\xfe\xef\x8e\x4d\x3c\x0b\xae\xb5\x0f\x0e\x96\x50\x34\xa3\xab\xf0\xaa\x02\xbc\xc3\xe2\x40\x63\x80\x26\x68\x18\x04\x7c\x23\x86\x25\x74\x34\x13\x96\xcb\xf9\x72\x39\xfb\x60\xcc\x4e\x5c\x55\xe9\xca\xec\x9b\x83\xc7\xcb\x1b\xb4\x72\x83\x39\xdf\x5b\x7d\xa1\xa7\x42\x88\x2c\x87\xd3\x22\x67\x62\x96\x91\x1e\xae\x96\xab\x2a\xdd\xee\xc6\x89\x25\xd5\xdd\xd0\x1b\x4d\x9c\x33\x81\xf3\xee\xd5\x78\xe7\x27\xd5\xc1\x7b\xc8\x84\x36\xae\xb4\x73\x2e\xd2\xbe\x98\xfe\xe1\x49\xf6\xf4\x1a\xf4\xf4\xa0\xde\xee\xd8\xf9\x4c\x0c\xeb\x57\xb7\xc7\x9c\x64\x8f\x64\xa6\x22\xed\x68\x4e\xae\xf5\xa3\x49\x34\xba\xbe\x07\x2a\xd3\x81\x3e\x9a\x97\x21\xa3\x24\x28\xd6\x3a\xfd\xd1\x6c\x4e\xd6\x99\x1f\xb0\x46\x8f\x5f\x46\x94\x95\x45\xe9\x71\xc8\xe0\x5a\x3f\x98\xc1\xe9\xec\x7d\x3c\x87\x5c\x04\x79\x3f\xb4\xb3\xac\x8b\xe9\x67\x5d\xa3\x73\xe0\x76\xaa\xf9\xfa\xa0\x82\x92\x49\x60\x3f\x37\xa5\x3c\x0d\x2c\x50\xd6\x7a\x14\x5b\x77\xf6\x1b\xc5\x47\x57\xe9\x10\x63\x0c\xf2\x27\xfc\x0f\x6d\xb2\x41\xb4\x5f\x45\xfd\x56\x12\x8d\x58\x8e\x5e\x17\x43\x70\x2e\xf0\xf6\xd2\x17\x5b\x30\x4d\x8c\x35\xec\xf4\x1f\x33\x20\xdb\x2e\xcd\xe0\x97\x5f\xa7\x0e\x2d\x97\x7d\x45\x4d\xd8\x81\x3b\x0b\xee\xa4\x4f\x96\x40\x40\x2a\xcb\xf9\x44\x4b\x7f\xdb\x1e\xb2\xee\xfc\xb3\xe0\xd9\xf2\xeb\x80\xfb\xed\x9b\x36\xef\xf3\x2f\x8f\xdf\x69\xdd\xc7\x9e\x26\x2f\xdd\x90\x0f\xba\x2c\xb5\xf1\xe1\x09\x88\x25\x3d\x43\x78\x23\x18\x1a\x34\xaa\xe1\x4e\xa1\xca\x89\xa9\x5f\x6d\xa5\xd2\x20\x39\xc5\x94\xf9\x5a\x39\x4f\x8f\x47\xaa\x00\x7a\x61\x96\xa4\x1b\xab\x0a\x0b\xaf\x6e\xb0\xbe\x07\xb5\xa7\x8d\xe7\x73\x8d\x22\xb4\x7c\x81\x50\x70\x3b\x96\x39\x28\x0f\xb7\xaa\xae\x41\xd6\xb7\xf2\xde\xc1\xd6\xd4\x25\xf7\x86\xa3\x07\x9e\xc3\x91\xe2\xee\x27\x02\x66\x18\x5b\xa2\x8d\xaf\x1c\x76\xc7\x79\x7b\x28\x78\xc7\x0c\xd2\x93\x42\x89\xcb\x23\xde\x86\x03\xc1\x03\xf2\x5f\xe3\x2d\x14\x4c\x8b\xb6\xe2\x12\xd9\xc9\xa6\x41\xa5\x10\xe2\x4c\x67\xd6\x19\x1f\x7a\x86\xbf\x1f\x65\xd3\xa0\x2e\xd3\x89\x0f\xa9\x56\x75\x96\x77\x36\x84\xc8\xfa\x6d\x91\x9f\x71\x74\x34\x0c\x8c\x29\xa2\xb1\xcf\x88\x5b\x29\x2d\x6b\xe6\x75\x7e\xa6\x45\xb0\x1b\xba\x28\xfd\xdd\x7b\x67\x1f\xcb\xf2\xf9\xd5\x4b\x23\x4b\x51\xd9\xd7\xa8\xd3\x42\xb0\x63\x19\x5c\xc0\x77\x6f\x40\xc1\xdf\xdf\xc2\xeb\x37\xa0\x2e\x2e\x42\xf9\x45\xf5\x6f\xa1\x13\xfc\x45\xfd\x1a\x6d\x9e\x3d\x75\x3b\xea\x70\x1f\x5c\x32\x72\xdd\x0f\x38\xbc\xce\x51\x60\x39\xc8\xb2\xa4\x3d\x8e\x0b\xa0\xc1\x42\x55\x0a\x4b\x86\x80\x0e\xc9\x0e\x35\x49\xa8\x69\xec\x4b\xa5\xdf\xb3\xab\xda\xdc\x4e\xb0\x0a\xa6\x9e\x93\x5b\x8d\xb7\x3c\x9e\x78\xb3\x91\x3b\x9c\x26\x36\x87\xd7\xf9\x09\x34\x7f\xa1\x2f\xe1\x63\x36\x52\xf0\x16\xba\xd2\x88\x94\x3c\x62\x44\xe5\xf0\xb4\xe0\x48\xec\xa4\xd8\xa2\x44\x84\xf0\x5d\xf8\xf1\xeb\x79\x10\x32\xef\xeb\x31\x0c\xb6\xd2\xd0\x3d\x1d\xed\xbc\x25\x0a\xd1\x21\xcd\x52\xa3\x68\xc3\x8b\xa3\xdb\xe8\xfe\x1b\x00\x00\xff\xff\xca\xfd\x92\xb9\x94\x14\x00\x00") +var _templateHookTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x58\xdd\x8f\xdb\xb8\x11\x7f\xb6\xff\x8a\x89\xe0\x00\x52\xaa\xa5\x73\xf7\xd6\x06\x29\xb0\xf0\x25\xc8\x02\xd7\x75\x71\xcd\xf5\xe5\x70\xc8\x31\xd2\xc8\x66\x2d\x93\x02\x49\xef\x07\x1c\xfd\xef\xc5\x0c\x45\x49\xb6\x76\xf7\xf6\xd2\x14\xf7\xb2\x6b\xcf\x0c\xe7\xe3\x37\x1f\x1c\xfa\x78\x5c\xbe\x9a\xaf\x4c\x73\x6f\xd5\x66\xeb\xe1\xfb\xd7\xdf\xfd\xf5\xa2\xb1\xe8\x50\x7b\x78\x2f\x0b\xfc\x6c\xcc\x0e\xae\x74\x21\xe0\xb2\xae\x81\x85\x1c\x10\xdf\xde\x60\x29\xe6\x1f\xb7\xca\x81\x33\x07\x5b\x20\x14\xa6\x44\x50\x0e\x6a\x55\xa0\x76\x58\xc2\x41\x97\x68\xc1\x6f\x11\x2e\x1b\x59\x6c\x11\xbe\x17\xaf\x23\x17\x2a\x73\xd0\xe5\x5c\x69\xe6\xff\x78\xb5\x7a\x77\xfd\xaf\x77\x50\xa9\x1a\xa1\xa3\x59\x63\x3c\x94\xca\x62\xe1\x8d\xbd\x07\x53\x81\x1f\x19\xf3\x16\x51\xcc\x5f\x2d\xdb\x76\x3e\x3f\x1e\xa1\xc4\x4a\x69\x84\x64\x6b\xcc\x2e\x81\x8e\x78\xab\xfc\x16\xf0\xce\xa3\x2e\x61\x01\xc9\x3f\x65\xb1\x93\x1b\x4c\x46\x52\xb3\xe3\x11\x3c\xee\x9b\x5a\x7a\x3a\x8c\xb2\x44\x9b\x80\x20\xd6\xf1\x08\x74\x8e\x54\xa9\x7d\x63\xac\x87\xe4\x78\x84\x85\x58\x19\x5d\xa9\x8d\xe8\x94\x41\xdb\x26\x6c\x6b\xd1\xec\x36\xf0\xb7\xb7\xf0\x59\x3a\x7c\x48\x8a\x85\xac\xd4\x1b\x84\x85\x26\xc1\x85\xb8\x36\x25\xba\xe8\xc5\x42\xcb\x3d\x12\xbd\xb1\x4a\x7b\x58\x68\x71\x4d\x84\xe4\xfd\x41\x17\xbd\xab\x0b\x7f\xdf\x0c\x42\x15\x24\xaf\x5e\x3a\xf1\xd2\x25\xc1\xfa\x42\x8b\x7f\x1c\xbc\xf4\xca\x68\x3e\x4b\x46\x67\xcb\x25\x7c\xdc\x22\xf4\x16\xda\x16\x58\x89\x72\x20\x35\xc8\x52\x36\x9e\x72\x64\x40\xd6\xb5\xb9\x65\xe0\x0f\x0e\x09\x6d\x63\x4b\xa5\xa5\xbd\x67\x1d\xd5\x41\x17\xa4\x18\xa4\x0b\xba\x44\x67\x02\xf6\x64\xd2\x58\x31\x9f\xb1\xde\xb1\x21\x3a\x94\x16\x46\x7b\xbc\xf3\x84\x08\xfd\xcf\xa1\x8f\xa3\x6d\x33\x48\x23\x74\x6d\x2b\xfe\x2d\xeb\x03\xe6\x80\xd6\x1a\x9b\x05\xd7\x39\x1e\x84\x42\xd6\xb5\x83\x2a\x2d\xfc\x5d\x0e\xfb\x4c\xcc\x67\xa4\x1a\xd2\x6a\x6c\x2e\xeb\xa4\x49\x0a\x26\x56\xf7\x30\xb2\x14\x61\x7a\xc2\x3e\x1c\xe7\xb3\xd9\xfe\x26\x07\xb3\x23\xc0\xf7\x22\x1d\xfb\x3d\x9f\xcd\x54\x05\x2f\xcc\x8e\xc5\x66\x16\xfd\xc1\x6a\xd0\xaa\xce\xa1\xda\x7b\xf1\x8e\x54\x54\x69\x72\xd0\x78\xd7\x60\xe1\xb1\x0c\x30\x11\x80\xac\xe2\xe5\x47\x01\x81\x35\x86\x23\xa1\xe0\xe6\xb3\x59\x3b\xef\x55\xc6\x98\x6f\xb2\xf9\xec\xa4\x26\x97\x4b\x58\x19\x5d\x2a\xd6\x49\xc9\x04\x2a\x6a\x8a\xbb\xa3\xc5\x8c\x89\x39\x6b\x5f\x9d\x30\x1e\xce\xca\x04\x9d\xcf\xc6\xd4\x6c\xea\x52\x97\xb0\xb1\xe6\xd0\xb8\xc1\x82\x0b\xfd\xc5\xed\x7d\xfd\x03\x98\x06\x6d\x28\x04\xce\xcd\xa5\x2e\xd3\x4a\x59\xe7\x73\x70\x48\x67\x06\x0f\x72\x9a\x1f\x1e\x84\x10\x3d\x29\x1b\xf9\x77\x9c\xf7\xc1\xb3\xa7\x7f\x20\x9b\xe4\x2f\x67\x84\x92\xc3\xd6\x63\xc9\xc0\x97\x2f\xf0\x22\x38\xd2\x93\xc6\xa9\xab\x64\xed\xb0\x83\xbe\x32\x16\x3e\xe5\x1c\x28\xa5\x3e\x34\x2d\xbb\xcc\x27\x48\xf7\x54\xcf\xb9\x22\xd6\x34\x4a\xa4\xb7\x07\xa4\x14\x86\xd4\xad\xed\x53\x70\xae\x7f\x3a\x47\x73\x6d\xff\x5c\x30\x27\x58\x3e\x01\x65\x88\xf4\x99\x48\x3e\x0e\x64\xa7\xe6\x1c\xc7\x0e\xdf\x08\xe4\xb5\xf1\xa0\x71\x23\x3d\x52\x0b\x6c\xd4\x0d\xea\x01\xd2\x0e\xbc\x6b\xe3\xd3\x53\xd0\xbe\x35\x42\x9d\x82\x93\xb2\x18\x7c\xfc\x20\xdd\xba\x09\x3d\x3a\xb4\xa7\x47\xe7\x95\xde\x0c\x73\x21\x64\x7c\xf0\x9a\x4f\xa5\xa6\x19\x9b\x5e\x37\x4f\x78\xfe\xe9\xab\xfd\xde\x8b\x75\x93\x66\xe2\xca\xa5\xa6\x89\x7e\xd3\x41\xf2\xd6\x51\xe2\x4a\x55\x78\x48\x3e\x48\xf7\x5e\x61\x5d\xba\x04\x12\xfe\x90\x30\xed\xb2\x2c\xb1\xec\x19\xc3\xb7\xc0\x5d\xd5\x28\xed\x88\xcf\x1f\x3a\x62\x02\x17\x7c\xd1\x5e\xc4\x8b\x31\x16\xca\x0e\xef\x5d\x34\x1f\xaf\xbe\x3d\xfa\xad\x61\xee\x06\x7d\x64\x86\x13\xa4\x85\x6e\x8b\xe8\x33\xdd\x3d\x67\x70\xdf\xc8\x5a\x95\x92\x11\xff\x4d\x8c\xd4\xb5\xed\x6f\x40\x33\x91\xdd\x8b\xf7\xca\x48\x4f\xca\x1c\x70\xde\x2a\xbd\xc9\x3b\x39\xea\xb6\x40\x39\x4b\xc7\xff\x9a\x0f\xea\x88\xe3\x11\x54\x05\x1a\x7b\x17\xcf\x20\x6b\xdb\x4f\x3c\xad\xc3\x45\x80\x77\xca\x79\x17\x6e\xa8\x93\xb0\x82\xe3\xd9\x1b\x78\xd1\x89\x3c\x3e\xa5\x62\x9b\x86\x50\xfb\x3e\xed\x62\x0d\xe7\xfe\xef\x8e\x4d\x3c\x0b\xae\xb5\x0f\x0e\x96\x50\x34\xa3\xab\xf0\xaa\x02\xbc\xc3\xe2\x40\x63\x80\x26\x68\x18\x04\x7c\x23\x86\x25\x74\x34\x13\x96\xcb\xf9\x72\x39\x23\x9e\xb8\xaa\xd2\x95\xd9\x37\x07\x8f\x97\x37\x68\xe5\x06\x73\xbe\xb7\xfa\x42\x4f\x85\x10\x59\x0e\xa7\x45\xce\xc4\x2c\x23\x3d\x5c\x2d\x57\x55\xba\xdd\x8d\x13\xfb\xc1\x98\x5d\x37\xf4\x46\x13\xe7\x4c\xe0\xbc\x7b\x35\xde\xf9\x49\x75\xf0\x1e\x32\xa1\x8d\x2b\xed\x9c\x8b\xb4\x2f\xa6\x7f\x78\x92\x3d\xbd\x06\x3d\x3d\xa8\xb7\x3b\x76\x3e\x13\xc3\xfa\xd5\xed\x31\x27\xd9\x23\x99\xa9\x48\x3b\x9a\x93\x6b\xfd\x68\x12\x8d\xae\xef\x81\xca\x74\xa0\x8f\xe6\xe5\x28\xa3\x6b\x9d\xfe\x68\x36\x27\xeb\xcc\x0f\x58\xa3\xc7\x2f\x23\xca\xca\xa2\xf4\x38\x64\x70\xad\x1f\xcc\xe0\x74\xf6\x3e\x9e\x43\x2e\x82\xbc\x1f\xda\x59\xd6\xc5\xf4\xb3\xae\xd1\x39\x70\x3b\xd5\x7c\x7d\x50\x41\xc9\x24\xb0\x9f\x9b\x52\x9e\x06\x16\x28\x6b\x3d\x8a\xad\x3b\xfb\x8d\xe2\xa3\xab\x74\x88\x31\x06\xf9\x13\xfe\x87\x36\xd9\x20\xda\xaf\xa2\x7e\x2b\x89\x46\x2c\x47\xaf\x8b\x21\x38\x17\x78\x7b\xe9\x8b\x2d\x98\x26\xc6\x1a\x76\xfa\x8f\x19\x90\x6d\x97\x66\xf0\xcb\xaf\x53\x87\x96\xcb\xbe\xa2\x26\xec\xc0\x9d\x05\x77\xd2\x27\x4b\x20\x20\x95\xe5\x7c\xa2\xa5\xbf\x6d\x0f\x59\x77\xfe\x59\xf0\x6c\xf9\x75\xc0\xfd\xf6\x4d\x9b\xf7\xf9\x97\xc7\xef\xb4\xee\x63\x4f\x93\x97\x6e\xc8\x07\x5d\x96\xda\xf8\xf0\x04\xc4\x92\x9e\x21\xbc\x11\x0c\x0d\x1a\xd5\x70\xa7\x50\xe5\xc4\xd4\xaf\xb6\x52\x69\x90\x9c\x62\xca\x7c\xad\x9c\xa7\xc7\x23\x55\x00\xbd\x30\x4b\xd2\x8d\x55\x85\x85\x57\x37\x58\xdf\x83\xda\xd3\xc6\xf3\xb9\x46\x11\x5a\xbe\x40\x28\xb8\x1d\xcb\x1c\x94\x87\x5b\x55\xd7\x20\xeb\x5b\x79\xef\x60\x6b\xea\x92\x7b\xc3\xd1\x03\xcf\xe1\x48\x71\xf7\x13\x01\x33\x8c\x2d\xd1\xc6\x57\x0e\xbb\xe3\xbc\x3d\x14\xbc\x63\x06\xe9\x49\xa1\xc4\xe5\x11\x6f\xc3\x81\xe0\x01\xf9\xaf\xf1\x16\x0a\xa6\x45\x5b\x71\x89\xec\x64\xd3\xa0\x52\x08\x71\xa6\x33\xeb\x8c\x0f\x3d\xc3\xdf\x8f\xb2\x69\x50\x97\xe9\xc4\x87\x54\xab\x3a\xcb\x3b\x1b\x42\x64\xfd\xb6\xc8\xcf\x38\x3a\x1a\x06\xc6\x14\xd1\xd8\x67\xc4\xad\x94\x96\x35\xf3\x3a\x3f\xd3\x22\xd8\x0d\x5d\x94\xfe\xee\xbd\xb3\x8f\x65\xf9\xfc\xea\xa5\x91\xa5\xa8\xec\x6b\xd4\x69\x21\xd8\xb1\x0c\x2e\xe0\xbb\x37\xa0\xe0\xef\x6f\xe1\xf5\x1b\x50\x17\x17\xa1\xfc\xa2\xfa\xb7\xd0\x09\xfe\xa2\x7e\x8d\x36\xcf\x9e\xba\x1d\x75\xb8\x0f\x2e\x19\xb9\xee\x07\x1c\x5e\xe7\x28\xb0\x1c\x64\x59\xd2\x1e\xc7\x05\xd0\x60\xa1\x2a\x85\x25\x43\x40\x87\x64\x87\x9a\x24\xd4\x34\xf6\xa5\xd2\xef\xd9\x55\x6d\x6e\x27\x58\x05\x53\xcf\xc9\xad\xc6\x5b\x1e\x4f\xbc\xd9\xc8\x1d\x4e\x13\x9b\xc3\xeb\xfc\x04\x9a\xbf\xd0\x97\xf0\x31\x1b\x29\x78\x0b\x5d\x69\x44\x4a\x1e\x31\xa2\x72\x78\x5a\x70\x24\x76\x52\x6c\x51\x22\x42\xf8\x2e\xfc\xf8\xf5\x3c\x08\x99\xf7\xf5\x18\x06\x5b\x69\xe8\x9e\x8e\x76\xde\x12\x85\xe8\x90\x66\xa9\x51\xb4\xe1\xc5\xd1\x6d\x74\xff\x0d\x00\x00\xff\xff\x6f\xf8\xfb\xdb\x94\x14\x00\x00") func templateHookTmplBytes() ([]byte, error) { return bindataRead( diff --git a/entc/gen/template/hook.tmpl b/entc/gen/template/hook.tmpl index 9fb976a68..3a7ba14ad 100644 --- a/entc/gen/template/hook.tmpl +++ b/entc/gen/template/hook.tmpl @@ -100,7 +100,7 @@ func HasOp(op {{ $pkg }}.Op) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk {{ $pkg }}.Hook, cond Condition) {{ $pkg }}.Hook { return func(next {{ $pkg }}.Mutator) {{ $pkg }}.Mutator { diff --git a/entc/integration/config/ent/hook/hook.go b/entc/integration/config/ent/hook/hook.go index 7b9795687..4db8d60fb 100644 --- a/entc/integration/config/ent/hook/hook.go +++ b/entc/integration/config/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/customid/ent/hook/hook.go b/entc/integration/customid/ent/hook/hook.go index 009fdda71..392d4cb49 100644 --- a/entc/integration/customid/ent/hook/hook.go +++ b/entc/integration/customid/ent/hook/hook.go @@ -172,7 +172,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/ent/hook/hook.go b/entc/integration/ent/hook/hook.go index f85578e0e..55414623f 100644 --- a/entc/integration/ent/hook/hook.go +++ b/entc/integration/ent/hook/hook.go @@ -276,7 +276,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/gremlin/ent/hook/hook.go b/entc/integration/gremlin/ent/hook/hook.go index 7e2d0522a..0c3089a44 100644 --- a/entc/integration/gremlin/ent/hook/hook.go +++ b/entc/integration/gremlin/ent/hook/hook.go @@ -276,7 +276,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/hooks/ent/hook/hook.go b/entc/integration/hooks/ent/hook/hook.go index 1f27702b6..2ab969853 100644 --- a/entc/integration/hooks/ent/hook/hook.go +++ b/entc/integration/hooks/ent/hook/hook.go @@ -133,7 +133,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/hooks/ent/schema/user.go b/entc/integration/hooks/ent/schema/user.go index 8b3e7f48a..2b21191bd 100644 --- a/entc/integration/hooks/ent/schema/user.go +++ b/entc/integration/hooks/ent/schema/user.go @@ -8,6 +8,8 @@ import ( "context" "fmt" + "github.com/facebook/ent/entc/integration/hooks/ent/hook" + "github.com/facebook/ent" "github.com/facebook/ent/schema/edge" "github.com/facebook/ent/schema/field" @@ -57,36 +59,40 @@ func (VersionMixin) Fields() []ent.Field { } 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 []ent.Hook{ - 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 || !m.Op().Is(ent.OpUpdateOne) { - 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 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) + }) } } diff --git a/entc/integration/idtype/ent/hook/hook.go b/entc/integration/idtype/ent/hook/hook.go index 556258a43..8e110c53f 100644 --- a/entc/integration/idtype/ent/hook/hook.go +++ b/entc/integration/idtype/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/json/ent/hook/hook.go b/entc/integration/json/ent/hook/hook.go index 104cabab0..8d264ffb2 100644 --- a/entc/integration/json/ent/hook/hook.go +++ b/entc/integration/json/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/migrate/entv1/hook/hook.go b/entc/integration/migrate/entv1/hook/hook.go index cd39bad25..a3568aec3 100644 --- a/entc/integration/migrate/entv1/hook/hook.go +++ b/entc/integration/migrate/entv1/hook/hook.go @@ -133,7 +133,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk entv1.Hook, cond Condition) entv1.Hook { return func(next entv1.Mutator) entv1.Mutator { diff --git a/entc/integration/migrate/entv2/hook/hook.go b/entc/integration/migrate/entv2/hook/hook.go index 2bcb0184b..1de9c182b 100644 --- a/entc/integration/migrate/entv2/hook/hook.go +++ b/entc/integration/migrate/entv2/hook/hook.go @@ -159,7 +159,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk entv2.Hook, cond Condition) entv2.Hook { return func(next entv2.Mutator) entv2.Mutator { diff --git a/entc/integration/privacy/ent/hook/hook.go b/entc/integration/privacy/ent/hook/hook.go index 891e43357..1f7895311 100644 --- a/entc/integration/privacy/ent/hook/hook.go +++ b/entc/integration/privacy/ent/hook/hook.go @@ -133,7 +133,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/entc/integration/template/ent/hook/hook.go b/entc/integration/template/ent/hook/hook.go index 0a630d1a0..2e3813e8b 100644 --- a/entc/integration/template/ent/hook/hook.go +++ b/entc/integration/template/ent/hook/hook.go @@ -146,7 +146,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/edgeindex/ent/hook/hook.go b/examples/edgeindex/ent/hook/hook.go index aadd56311..0c1da84a0 100644 --- a/examples/edgeindex/ent/hook/hook.go +++ b/examples/edgeindex/ent/hook/hook.go @@ -133,7 +133,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/entcpkg/ent/hook/hook.go b/examples/entcpkg/ent/hook/hook.go index d51a8f648..4add8722d 100644 --- a/examples/entcpkg/ent/hook/hook.go +++ b/examples/entcpkg/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/m2m2types/ent/hook/hook.go b/examples/m2m2types/ent/hook/hook.go index 54a03a30a..074ed6ded 100644 --- a/examples/m2m2types/ent/hook/hook.go +++ b/examples/m2m2types/ent/hook/hook.go @@ -133,7 +133,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/m2mbidi/ent/hook/hook.go b/examples/m2mbidi/ent/hook/hook.go index 33ecf505f..5c7a08e05 100644 --- a/examples/m2mbidi/ent/hook/hook.go +++ b/examples/m2mbidi/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/m2mrecur/ent/hook/hook.go b/examples/m2mrecur/ent/hook/hook.go index 675c130d1..85e6e3966 100644 --- a/examples/m2mrecur/ent/hook/hook.go +++ b/examples/m2mrecur/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/o2m2types/ent/hook/hook.go b/examples/o2m2types/ent/hook/hook.go index 338db32b1..bc68e3d82 100644 --- a/examples/o2m2types/ent/hook/hook.go +++ b/examples/o2m2types/ent/hook/hook.go @@ -133,7 +133,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/o2mrecur/ent/hook/hook.go b/examples/o2mrecur/ent/hook/hook.go index c523aa36f..60237ad17 100644 --- a/examples/o2mrecur/ent/hook/hook.go +++ b/examples/o2mrecur/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/o2o2types/ent/hook/hook.go b/examples/o2o2types/ent/hook/hook.go index 6ad0430c0..93e332d84 100644 --- a/examples/o2o2types/ent/hook/hook.go +++ b/examples/o2o2types/ent/hook/hook.go @@ -133,7 +133,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/o2obidi/ent/hook/hook.go b/examples/o2obidi/ent/hook/hook.go index cd13ae634..730da80db 100644 --- a/examples/o2obidi/ent/hook/hook.go +++ b/examples/o2obidi/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/o2orecur/ent/hook/hook.go b/examples/o2orecur/ent/hook/hook.go index 4f818d6f2..280c55975 100644 --- a/examples/o2orecur/ent/hook/hook.go +++ b/examples/o2orecur/ent/hook/hook.go @@ -120,7 +120,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/start/ent/hook/hook.go b/examples/start/ent/hook/hook.go index 1c7718886..3854b155e 100644 --- a/examples/start/ent/hook/hook.go +++ b/examples/start/ent/hook/hook.go @@ -146,7 +146,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator { diff --git a/examples/traversal/ent/hook/hook.go b/examples/traversal/ent/hook/hook.go index 199983857..d3fa96b95 100644 --- a/examples/traversal/ent/hook/hook.go +++ b/examples/traversal/ent/hook/hook.go @@ -146,7 +146,7 @@ func HasFields(field string, fields ...string) Condition { // If executes the given hook under condition. // -// Hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) +// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...))) // func If(hk ent.Hook, cond Condition) ent.Hook { return func(next ent.Mutator) ent.Mutator {