diff --git a/api.go b/api.go index bfa7c7e..beadf9f 100644 --- a/api.go +++ b/api.go @@ -84,19 +84,14 @@ func Upsert[T any](db *DB, object T, ns ...uint64) (uint64, T, bool, error) { return 0, object, false, err } - if gid != 0 { - gid, _, err = getByGidWithObject[T](ctx, n, gid, object) - if err != nil && err != utils.ErrNoObjFound { - return 0, object, false, err - } - wasFound = err == nil - } else if cf != nil { - gid, _, err = getByConstrainedFieldWithObject[T](ctx, n, *cf, object) + if gid != 0 || cf != nil { + gid, err = getExistingObject[T](ctx, n, gid, cf, object) if err != nil && err != utils.ErrNoObjFound { return 0, object, false, err } wasFound = err == nil } + if gid == 0 { gid, err = db.z.nextUID() if err != nil { diff --git a/mutation_create.go b/api_mutation_gen.go similarity index 100% rename from mutation_create.go rename to api_mutation_gen.go diff --git a/mutation_helpers.go b/mutation_helpers.go index 899e61b..d4b0cf4 100644 --- a/mutation_helpers.go +++ b/mutation_helpers.go @@ -54,16 +54,8 @@ func getUidOrMutate[T any](ctx context.Context, db *DB, n *Namespace, object T) if err != nil { return 0, err } - if gid != 0 { - gid, _, err = getByGidWithObject[T](ctx, n, gid, object) - if err != nil && err != utils.ErrNoObjFound { - return 0, err - } - if err == nil { - return gid, nil - } - } else if cf != nil { - gid, _, err = getByConstrainedFieldWithObject[T](ctx, n, *cf, object) + if gid != 0 || cf != nil { + gid, err = getExistingObject(ctx, n, gid, cf, object) if err != nil && err != utils.ErrNoObjFound { return 0, err } diff --git a/query_execution.go b/query_execution.go index 3a0133b..dc77b66 100644 --- a/query_execution.go +++ b/query_execution.go @@ -184,3 +184,16 @@ func executeQuery[T any](ctx context.Context, n *Namespace, queryParams QueryPar return gids, objs, nil } + +func getExistingObject[T any](ctx context.Context, n *Namespace, gid uint64, cf *ConstrainedField, object T) (uint64, error) { + var err error + if gid != 0 { + gid, _, err = getByGidWithObject[T](ctx, n, gid, object) + } else if cf != nil { + gid, _, err = getByConstrainedFieldWithObject[T](ctx, n, *cf, object) + } + if err != nil { + return 0, err + } + return gid, nil +}