Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning key identifier as part of data returned by query #594

Closed
byrneciaran opened this issue May 13, 2015 · 3 comments
Closed

Returning key identifier as part of data returned by query #594

byrneciaran opened this issue May 13, 2015 · 3 comments
Assignees
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.

Comments

@byrneciaran
Copy link

I use the key identifier extensively throughout my app as my "id". The namespace and kind doesn't change. Is it possible to return the key identifier inline with the data returned from queries or should I just include the identifier as a property on the entity ?

For example, if you look at this simple example of an entity returned by a query:

[ { key: { namespace: 'mynamespace', path: [Object] },data: { name: 'My Entity'} }]

What I would like is for my data object to look like:

data:{id:XXXXX,name:'My Entity}

At the moment to include the key identifier with my data object I need to do something like:

var dao = entity.data
dao.id = entity.key.path.pop()

NOTE - I use pop() here since it's in the examples but I think it's a bad idea. Poping the identifier removes it from the path array. If I go on to reuse the key, it's incomplete (e.g. if I did a get and update). I think entity.key.path[entity.key.path.length - 1] is a better idea.

Anyway, if I am returning thousands of entities I can write (using async.js) something like:

 ds.runQuery(query, function(err,entities){
   async.map(entities,DAO,callback);
 });

function DAO(entity,callback) {
  var dao = entity.data;
  dao.id = entity.key.path.pop();
  callback(null, dao);
}

The problem I have is that (1) this is quite cumbersome (2) I haven't benchmarked but I'm sure there is a performance hit.

Is there a better way to do this ?

@jgeewax
Copy link
Contributor

jgeewax commented May 13, 2015

Some related issues:

I think #574 would solve your issue.


Regarding key.path.pop(): I can't find anything in the docs -- can you maybe open a separate issue to remove those examples? I think you're right that it's not the best way to get the Key name/id.

@jgeewax jgeewax added type: question Request for information or clarification. Not an issue. api: datastore Issues related to the Datastore API. labels May 13, 2015
@jgeewax jgeewax added this to the Datastore Future milestone May 13, 2015
@byrneciaran
Copy link
Author

Thanks @jgeewax!

My specific issue refers to retrieving entities. I actually think getting the id is ok when saving an entity as it's only one entity. The problem is that retrieving multiple entities requires something like map to populate the data object.

This could be resolved by automatically populating the id or name property on the data object returned. This is similar to what happens in MongoDB whereby the _id is automatically set. If you want to exclude to exclude the id or name property you could do so through a projection, but by default it's included.

@jgeewax
Copy link
Contributor

jgeewax commented May 13, 2015

Gotcha, @byrneciaran

Our worry (outlined in #508) is that key isn't a reserved word, so if someone had a property called key, we'd blast it out with the entity key... So we have to figure something out there... Feel free to add your use case over on that issue so we can make sure whatever we decide makes sense for everyone!

sofisl pushed a commit that referenced this issue Nov 10, 2022
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
sofisl pushed a commit that referenced this issue Nov 10, 2022
sofisl pushed a commit that referenced this issue Sep 13, 2023
…andwritten libraries (#594)

- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 429395631

Source-Link: googleapis/googleapis@84594b3

Source-Link: googleapis/googleapis-gen@ed74f97
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWQ3NGY5NzBmZDgyOTE0ODc0ZTZiMjdiMDQ3NjNjZmE2NmJhZmU5YiJ9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants