mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/hooks: initial work for mutations and hooks (#377)
* entc/hooks: initial work for mutations and hooks * ent/schema: adding policy to schema * ent: change op string to uint * entc: move entschema to runtime and enable smooth transition * entc/privacy: adding privacy template * all: goimports * intg/hooks: mutation client/tx and basic schema tests * ent/privacy: adding more verbose decisions * entc/gen: edge-ids getter and additional tests * all: regen assets * entc/gen: fix client hookd propagation * intg: add deletion example * intg/privacy: remove old entschema package * typed privacy * ent/privacy: hooks shouldn't be called on privacy deny * entc/gen: fix schema hooks invocation order * remove read policy from public api * update circleci go orb Co-authored-by: Ariel Mashraki <ariel@mashraki.co.il>
This commit is contained in:
@@ -7,6 +7,7 @@ in the LICENSE file in the root directory of this source tree.
|
||||
{{ define "dialect/sql/create" }}
|
||||
{{ $builder := pascal $.Scope.Builder }}
|
||||
{{ $receiver := receiver $builder }}
|
||||
{{ $mutation := print $receiver ".mutation" }}
|
||||
|
||||
func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name }}, error) {
|
||||
var (
|
||||
@@ -20,23 +21,23 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) (*{{ $.Name
|
||||
}
|
||||
)
|
||||
{{- if $.ID.UserDefined }}
|
||||
if value := {{ $receiver }}.{{ $.ID.BuilderField }}; value != nil {
|
||||
{{ $.Receiver }}.ID = *value
|
||||
_spec.ID.Value = *value
|
||||
if id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}(); ok {
|
||||
{{ $.Receiver }}.ID = id
|
||||
_spec.ID.Value = id
|
||||
}
|
||||
{{- end }}
|
||||
{{- range $_, $f := $.Fields }}
|
||||
if value := {{ $receiver }}.{{ $f.BuilderField }}; value != nil {
|
||||
if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
|
||||
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
||||
Type: field.{{ $f.Type.ConstName }},
|
||||
Value: *value,
|
||||
Value: value,
|
||||
Column: {{ $.Package }}.{{ $f.Constant }},
|
||||
})
|
||||
{{ $.Receiver }}.{{ $f.StructField }} = {{ if not $f.Nillable }}*{{ end }}value
|
||||
{{ $.Receiver }}.{{ $f.StructField }} = {{ if $f.Nillable }}&{{ end }}value
|
||||
}
|
||||
{{- end }}
|
||||
{{- range $_, $e := $.Edges }}
|
||||
if nodes := {{ $receiver }}.{{ $e.BuilderField }}; len(nodes) > 0 {
|
||||
if nodes := {{ $mutation }}.{{ $e.StructField }}IDs(); len(nodes) > 0 {
|
||||
{{- with extend $ "Edge" $e "Nodes" true "Zero" "nil" }}
|
||||
{{ template "dialect/sql/defedge" . }}{{/* defined in sql/update.tmpl */}}
|
||||
{{- end }}
|
||||
|
||||
@@ -8,6 +8,7 @@ in the LICENSE file in the root directory of this source tree.
|
||||
{{ $pkg := $.Scope.Package }}
|
||||
{{ $builder := pascal $.Scope.Builder }}
|
||||
{{ $receiver := receiver $builder }}
|
||||
{{ $mutation := print $receiver ".mutation" }}
|
||||
{{ $one := hasSuffix $builder "One" }}
|
||||
{{- $zero := 0 }}{{ if $one }}{{ $zero = "nil" }}{{ end }}
|
||||
{{- $ret := "n" }}{{ if $one }}{{ $ret = $.Receiver }}{{ end }}
|
||||
@@ -18,15 +19,18 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }}
|
||||
Table: {{ $.Package }}.Table,
|
||||
Columns: {{ $.Package }}.Columns,
|
||||
ID: &sqlgraph.FieldSpec{
|
||||
{{- if $one }}
|
||||
Value: {{ $receiver }}.id,
|
||||
{{- end }}
|
||||
Type: field.{{ $.ID.Type.ConstName }},
|
||||
Column: {{ $.Package }}.{{ $.ID.Constant }},
|
||||
},
|
||||
},
|
||||
}
|
||||
{{- if not $one }}
|
||||
{{- if $one }}
|
||||
id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}()
|
||||
if !ok {
|
||||
return {{ $zero }}, fmt.Errorf("missing {{ $.Name }}.ID for update")
|
||||
}
|
||||
_spec.Node.ID.Value = id
|
||||
{{- else }}
|
||||
if ps := {{ $receiver }}.predicates; len(ps) > 0 {
|
||||
_spec.Predicate = func(selector *sql.Selector) {
|
||||
for i := range ps {
|
||||
@@ -37,25 +41,25 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }}
|
||||
{{- end }}
|
||||
{{- range $_, $f := $.Fields }}
|
||||
{{- if or (not $f.Immutable) $f.UpdateDefault }}
|
||||
if value := {{ $receiver }}.{{ $f.BuilderField }}; value != nil {
|
||||
if value, ok := {{ $mutation }}.{{ $f.MutationGet }}(); ok {
|
||||
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
|
||||
Type: field.{{ $f.Type.ConstName }},
|
||||
Value: *value,
|
||||
Value: value,
|
||||
Column: {{ $.Package }}.{{ $f.Constant }},
|
||||
})
|
||||
}
|
||||
{{- if $f.Type.Numeric }}
|
||||
if value := {{ $receiver }}.add{{ $f.BuilderField }}; value != nil {
|
||||
if value, ok := {{ $mutation }}.Added{{ $f.StructField }}(); ok {
|
||||
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
|
||||
Type: field.{{ $f.Type.ConstName }},
|
||||
Value: *value,
|
||||
Value: value,
|
||||
Column: {{ $.Package }}.{{ $f.Constant }},
|
||||
})
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- if $f.Optional }}
|
||||
if {{ $receiver }}.clear{{ $f.BuilderField }} {
|
||||
if {{ $mutation }}.{{ $f.StructField }}Cleared() {
|
||||
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
|
||||
Type: field.{{ $f.Type.ConstName }},
|
||||
Column: {{ $.Package }}.{{ $f.Constant }},
|
||||
@@ -65,21 +69,21 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }}
|
||||
{{- end }}
|
||||
{{- range $_, $e := $.Edges }}
|
||||
{{- if $e.Unique }}
|
||||
if {{ $receiver }}.cleared{{ $e.StructField }} {
|
||||
if {{ $mutation }}.{{ $e.StructField }}Cleared() {
|
||||
{{- with extend $ "Edge" $e }}
|
||||
{{ template "dialect/sql/defedge" . }}
|
||||
{{- end }}
|
||||
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||||
}
|
||||
{{- else }}
|
||||
if nodes := {{ $receiver }}.removed{{ $e.StructField }}; len(nodes) > 0 {
|
||||
if nodes := {{ $mutation }}.Removed{{ $e.StructField }}IDs(); len(nodes) > 0 {
|
||||
{{- with extend $ "Edge" $e "Nodes" true "Zero" $zero }}
|
||||
{{ template "dialect/sql/defedge" . }}
|
||||
{{- end }}
|
||||
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||||
}
|
||||
{{- end }}
|
||||
if nodes := {{ $receiver }}.{{ $e.BuilderField }}; len(nodes) > 0 {
|
||||
if nodes := {{ $mutation }}.{{ $e.StructField }}IDs(); len(nodes) > 0 {
|
||||
{{- with extend $ "Edge" $e "Nodes" true "Zero" $zero }}
|
||||
{{ template "dialect/sql/defedge" . }}
|
||||
{{- end }}
|
||||
@@ -123,7 +127,7 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }}
|
||||
},
|
||||
}
|
||||
{{- with $.Scope.Nodes }}
|
||||
for k, _ := range nodes {
|
||||
for _, k := range nodes {
|
||||
{{- $id := $e.Type.ID -}}
|
||||
{{- /* Convert string-ids that are stored as int in the database */ -}}
|
||||
{{- if and (not $id.UserDefined) $id.IsString }}
|
||||
|
||||
Reference in New Issue
Block a user