mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
entc/gen/mutation: add IDs method for mutations
This commit is contained in:
committed by
Ariel Mashraki
parent
e8e4401d27
commit
caa673826a
@@ -100,7 +100,7 @@ func {{ $opt }}(id {{ $n.ID.Type }}) {{ $mutationOption }} {
|
||||
m.oldValue = func(ctx context.Context) (*{{ $n.Name }}, error) {
|
||||
once.Do(func() {
|
||||
if m.done {
|
||||
err = fmt.Errorf("querying old values post mutation is not allowed")
|
||||
err = errors.New("querying old values post mutation is not allowed")
|
||||
} else {
|
||||
value, err = m.Client().{{ $n.Name }}.Get(ctx, id)
|
||||
}
|
||||
@@ -134,7 +134,7 @@ func (m {{ $mutation }}) Client() *Client {
|
||||
// it returns an error otherwise.
|
||||
func (m {{ $mutation }}) Tx() (*Tx, error) {
|
||||
if _, ok := m.driver.(*txDriver); !ok {
|
||||
return nil, fmt.Errorf("{{ $pkg }}: mutation is not running in a transaction")
|
||||
return nil, errors.New("{{ $pkg }}: mutation is not running in a transaction")
|
||||
}
|
||||
tx := &Tx{config: m.config}
|
||||
tx.init()
|
||||
@@ -158,6 +158,26 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
|
||||
return *m.{{ $n.ID.BuilderField }}, true
|
||||
}
|
||||
|
||||
// IDs queries the database and returns the entity ids that match the mutation's predicate.
|
||||
// That means, if the mutation is applied within a transaction with an isolation level such
|
||||
// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated
|
||||
// or updated by the mutation.
|
||||
func (m *{{ $mutation }}) IDs(ctx context.Context) ([]{{ $n.ID.Type }}, error) {
|
||||
switch {
|
||||
case m.op.Is(OpUpdateOne | OpDeleteOne):
|
||||
id, exists := m.ID()
|
||||
if exists {
|
||||
return []{{ $n.ID.Type }}{id}, nil
|
||||
}
|
||||
fallthrough
|
||||
case m.op.Is(OpUpdate | OpDelete):
|
||||
return m.Client().{{ $n.Name }}.Query().Where(m.predicates...).IDs(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
{{ range $f := $n.Fields }}
|
||||
{{ $const := print $n.Package "." $f.Constant }}
|
||||
{{ $p := receiver $f.Type.String }}{{ if eq $p "m" }} {{ $p = "value" }} {{ end }}
|
||||
@@ -185,10 +205,10 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
|
||||
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
|
||||
func (m *{{ $mutation }}) {{ $f.MutationGetOld }}(ctx context.Context) (v {{ if $f.NillableValue }}*{{ end }}{{ $f.Type }}, err error) {
|
||||
if !m.op.Is(OpUpdateOne) {
|
||||
return v, fmt.Errorf("{{ $f.MutationGetOld }} is only allowed on UpdateOne operations")
|
||||
return v, errors.New("{{ $f.MutationGetOld }} is only allowed on UpdateOne operations")
|
||||
}
|
||||
if m.{{ $n.ID.BuilderField }} == nil || m.oldValue == nil {
|
||||
return v, fmt.Errorf("{{ $f.MutationGetOld }} requires an ID field in the mutation")
|
||||
return v, errors.New("{{ $f.MutationGetOld }} requires an ID field in the mutation")
|
||||
}
|
||||
oldValue, err := m.oldValue(ctx)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user