I have a User
entity containing an Email
field. The User
entity id is a ULID, because I want to allow users to change their email addresses, but I want to ensure that the email address is unique on both a CREATE
and an UPDATE
.
I am using Datastore transactions. This is a code fragment:
ctx := context.Background()
k := datastore.NameKey("User", user.ID, nil)
_, err := client.RunInTransaction(ctx, func(t *datastore.Transaction) error {
// other stuff that needs to be in transaction
_, err = t.Put(k, user)
return err
})
return err
The Email
field is indexed. Is there any way to search the User
entity for the current user's email address as part of the transaction?
*datastore.Transaction
does not have a GetAll
method, so I cannot run a query like this:
datastore.NewQuery("User").Filter("Email =", user.Email)
I'm afraid that using
client.GetAll(ctx, q, nil)
will not guarantee isolation within the transaction.