Skip to content

datatstore: Provide a way to get the generated ID from a Key object #171

Description

@rakyll
var key = ds.key('Kind', null)
ds.put({
  key: key,
  data: //...
}, function(err, key) {
   /// how to i read the key ID?
});

Activity

  1. stephenplusplus commented on Sep 4, 2014

    @stephenplusplus
    Contributor

    Any thoughts for how to provide this? An id property on the Key object? Exposing a getId method to return the last item in the path array (would that even work?)?

    This also got me thinking. Well, 1: we don't have a put method, but I assume you meant save :) And 2: if it's not too much magic, we could update the key object reference passed in to save, as opposed to only returning a new key object in the callback. The callback would behave as it currently does, but would also update the original key object.

    If you have an idea for an implementation, I'm happy to give it a shot.

  2. rakyll commented on Sep 4, 2014

    @rakyll
    ContributorAuthor

    Exposing a getId method to return the last item in the path array (would that even work?)?

    This would work.

    But we should additionally provide a pseudo getter for the full path as well. Parent entity keys should be recoverable as well.

    The callback would behave as it currently does, but would also update the original key object.

    This doesn't fix the problem of not being able to read the ID. You would like to use the ID somewhere else (e.g. as a key to memcache or on the URL to the item's detail page).

    We should at least have a getId and getPath before the release.

  3. rakyll commented on Sep 4, 2014

    @rakyll
    ContributorAuthor
  4. stephenplusplus commented on Sep 4, 2014

    @stephenplusplus
    Contributor

    This doesn't fix the problem of not being able to read the ID.

    Nope, was just to be a convenience, since currently you would be left with 2 keys that were meant to refer to the same thing. I'll give it a shot, then run it by you via PR.

  5. added a commit that references this issue on Sep 4, 2014
    9147af7
  6. stephenplusplus commented on Sep 4, 2014

    @stephenplusplus
    Contributor

    #175 should fix this issue. I'll work on the second part soon.

  7. stephenplusplus commented on Sep 6, 2014

    @stephenplusplus
    Contributor

    It sounds like we decided against providing this feature. The link to @proppy's code shows how you can get the id:

    key.path_.pop(); // possibly `key.path.pop()` soon.

    Re-open if I was mistaken, and this feature can be achieved reliably.

  8. added this to the Datastore Stable milestone on Feb 2, 2015
  9. 50 remaining items

  10. added 2 commits that reference this issue on Mar 5, 2026
  11. added a commit that references this issue on Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

🚨This issue needs some love.api: datastoreIssues related to the Datastore API.triage meI really want to be triaged.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions