Unique email in Google Datastore

自作多情 提交于 2019-12-24 10:08:54

问题


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.


回答1:


The short answer is no, you cannot use a query as part of a transaction unless you are querying a specific entity group. Global queries are alway eventually consistent. However, to put everything in a single entity group would likely limit write throughput too much.

A workaround is you can have another Kind with entities that map email addresses to users. Then you can, in a transaction, check the email Entity and if it doesn't exist or it points to a bad location, set the email Entity and the user Entity all as a single transaction.



来源:https://stackoverflow.com/questions/45820737/unique-email-in-google-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!