The user with no hat
The user with no hat

Reputation: 10846

How to get "relationships" elements efficiently on datastore?

I'm developing a social login system using google datastore. I need to authenticate the user using its social identity and then return the information of all its identities. The client can login with multiple social accounts and also with an identity created on my site so it basically has multiple social identities plus my site identity. Currently I'm using running 3 queries (sequentially) which I feel it's a bit too much so I'm wondering if there is a better way to do this:

// get the username registered with my site(if is registered)
     - userID  = SELECT userID From social WHERE socialID == $socialID
// get the data of the user
     - userData = SELECT * from MyData WHERE userID == userID
// get the data of any other identity it uses / has linked to the user id
     - otherSocial = select * FROM social WHERE userID=userID and socialID != $socialID

Upvotes: 0

Views: 52

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

  1. You can get userData by its key, which is faster and cheaper than running a query. In order to be able to do that, you should use userId as an id for userData.

  2. Your third query is probably needed only in some rare circumstances, e.g. when a user accesses account settings. In either case, I would not worry too much about these queries: they retrieve a small number of entities, which means that they execute very fast.

  3. You can store some data in a session, so you don't have to retrieve it until the next session. I store a LoginOption entity, which is an equivalent of your userId and socialId. Thus, I can bypass the first query until a user logs out or a session expires.

Upvotes: 1

Related Questions