entc/gen: move mutation hooks logic to a shared generic function (#3180)

This commit is contained in:
Ariel Mashraki
2022-12-19 17:36:29 +02:00
committed by GitHub
parent 4e662b9e51
commit 34bd0b7b6f
443 changed files with 3082 additions and 19327 deletions

View File

@@ -12,6 +12,9 @@ in the LICENSE file in the root directory of this source tree.
{{ $mutation := print $receiver ".mutation" }}
func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name }}, error) {
if err := {{ $receiver }}.check(); err != nil {
return nil, err
}
_node, _spec := {{ $receiver }}.createSpec()
if err := sqlgraph.CreateNode(ctx, {{ $receiver }}.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
@@ -49,6 +52,10 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name
}
{{- end }}
{{- end }}
{{- if $.HasOneFieldID }}
{{ $mutation }}.{{ $.ID.BuilderField }} = &_node.{{ $.ID.StructField }}
{{ $mutation }}.done = true
{{- end }}
return _node, nil
}