GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Provide helper methods on entity.Key for Kind, parent, name, and ID · Issue #574 · googleapis/google-cloud-node

Provide helper methods on entity.Key for Kind, parent, name, and ID #574

New issue
Copy link
New issue
Copy link
Closed
Closed
Provide helper methods on entity.Key for Kind, parent, name, and ID#574
Copy link
Assignees
stephenplusplus
Labels
api: datastoreIssues related to the Datastore API.
Milestone
Datastore Future

Description

@jgeewax
jgeewax
opened on May 9, 2015
Issue body actions

(In reference to https://github.com/GoogleCloudPlatform/gcloud-node/blob/412598cad200198caaac5dbe83700fd1665b2102/lib/datastore/entity.js#L54)

It'd be really nice if we had some helper methods on the Key class, so that you could do the following...

var gcloud = require('gcloud');
var dataset = gcloud.datastore.dataset({...});

Auto-generated ID example:

entity = {
  key: dataset.key('Person').
  data: {name: 'Jim'}
};

dataset.save(entity, function(err, entity) {
  var key = entity.key;
  console.log('kind is', key.kind); // 'Person'
  console.log('id is', key.id);  // 1234567 (auto-generated ID in this case)
  console.log('name is', key.name); // undefined or null
  console.log('parent is', key.parent); // undefined or null
});

Specific name and parent example

entity = {
  key: dataset.key(['Person', 'mom', 'Person', 'jim']).
  data: {name: 'Jim'}
};

dataset.save(entity, function(err, entity) {
  var key = entity.key;
  console.log('kind is', key.kind); // 'Person'
  console.log('id is', key.id);  // undefined or null
  console.log('name is', key.name); // 'jim'
  console.log('parent is', key.parent); // {key object}
  console.log('parent kind is', key.parent.kind); // 'Person'
  console.log('parent id is', key.parent.id); // undefined or null
  console.log('parent name is', key.parent.name); // 'mom'
  console.log('parent parent is', key.parent.parent); // undefined or null
});

Is this a reasonable request?

Activity

Next
  1. jgeewax
    added
    enhancement
    api: datastoreIssues related to the Datastore API.
    on May 9, 2015
  2. jgeewax
    added this to the Datastore Future milestone on May 9, 2015
  3. ryanseys commented on May 11, 2015

    @ryanseys
    ryanseys
    on May 11, 2015
    Contributor
    More actions

    Yeah this seems nice. I like this. Would this functionality mimic existing similar functionality from another gcloud library (python? ruby?) that we could take a look at? Or can we just run with this idea on our own?

  4. jgeewax commented on May 11, 2015

    @jgeewax
    jgeewax
    on May 11, 2015
    ContributorAuthor
    More actions

    I think the Python methods are modeled after ext.db and ext.db which is the closest we'll get to "being official" (see http://googlecloudplatform.github.io/gcloud-python/latest/datastore-keys.html and https://github.com/GoogleCloudPlatform/gcloud-python/blob/master/gcloud/datastore/key.py)

  5. stephenplusplus commented on Nov 23, 2015

    @stephenplusplus
    stephenplusplus
    on Nov 23, 2015
    Contributor
    More actions

    I'll put this in queue for after #897 merges.

  6. stephenplusplus
    self-assigned this
    on Nov 23, 2015
  7. stephenplusplus commented on Dec 1, 2015

    @stephenplusplus
    stephenplusplus
    on Dec 1, 2015
    Contributor
    More actions

    Here's what I've got. Look okay @jgeewax?

    Your example

    new Key({ path: ['Person', 'mom', 'Person', 'jim'] })
    // returns:
    Key {
      namespace: undefined,
      path: [ 'Person', 'mom', 'Person', 'jim' ],
      name: 'jim',
      kind: 'Person',
      parent:
       Key {
         namespace: undefined,
         path: [ 'Person', 'mom' ],
         name: 'mom',
         kind: 'Person' } }

    Example with extra level of hierarchy and numeric ID

    new Key({ path: ['Person', 'mom', 'Person', 'jim', 'SmallPerson', 1] })
    // returns:
    Key {
      namespace: undefined,
      path: [ 'Person', 'mom', 'Person', 'jim', 'SmallPerson', 1 ],
      id: 1,
      kind: 'SmallPerson',
      parent:
       Key {
         namespace: undefined,
         path: [ 'Person', 'mom', 'Person', 'jim' ],
         name: 'jim',
         kind: 'Person',
         parent:
          Key {
            namespace: undefined,
            path: [Object],
            name: 'mom',
            kind: 'Person' } } }
  8. jgeewax commented on Dec 1, 2015

    @jgeewax
    jgeewax
    on Dec 1, 2015
    ContributorAuthor
    More actions

    I don't spot anything crazy, but just to be sure -- this is a method that derives the results at eval time, right? Or is it duplicate information being stored?

  9. stephenplusplus commented on Dec 1, 2015

    @stephenplusplus
    stephenplusplus
    on Dec 1, 2015
    Contributor
    More actions

    At the time of instantiation, the parts of a key are split apart to create named properties for the given path array. So all of these things are stored in memory with the object. This is how all of our custom type constructors work.

    The duplicate information, do you mean the path? I kept that around since I thought it would be unfair for the user to lose a reference to their original input.

  10. jgeewax commented on Dec 1, 2015

    @jgeewax
    jgeewax
    on Dec 1, 2015 via email
    ContributorAuthor
    More actions
    Ah, sorry, to be clear I'm basically saying... If I change one piece of information on this object, do the other pieces (that should be the same) change too ? Or is there only one way to change the information, and therefore they're always in sync ?
  11. stephenplusplus commented on Dec 1, 2015

    @stephenplusplus
    stephenplusplus
    on Dec 1, 2015
    Contributor
    More actions

    Gotcha. What types of changes should we support? We can prevent a parent from being overwritten for example, or just a kind, an ID, etc. Or, we allow everything.

    We would have to compute the path when it's asked for or update it when one of its parts changes... probably easier to compute the path on request.

    Here's a working constructor that allows anything to be changed and path will follow:

    function Key(options) {
      this.namespace = options.namespace;
    
      if (options.path.length % 2 === 0) {
        var identifier = options.path.pop();
    
        if (is.number(identifier)) {
          this.id = identifier;
        } else if (is.string(identifier)) {
          this.name = identifier;
        }
      }
    
      this.kind = options.path.pop();
    
      if (options.path.length > 0) {
        this.parent = new Key(options);
      }
    
      // `path` is computed on demand to consider any changes that may have been
      // made to the key.
      Object.defineProperty(this, 'path', {
        enumerable: true,
        get: function () {
          return arrify(this.parent && this.parent.path)
            .concat([this.kind, this.name || this.id]);
        }
      });
    }
    
    var key = new Key({ path: ['Person', 'mom', 'Person', 'jim'] });
    console.log(key.path);
    // [ 'Person', 'mom', 'Person', 'jim' ]
    
    key.parent.name = 'sam';
    console.log(key.path);
    // [ 'Person', 'sam', 'Person', 'jim' ]
    
    console.log(key.parent.path);
    // [ 'Person', 'sam' ]

    The one case I can think of that isn't covered is if a user had an ID but now assigns a name. But then I'm back to how mutable we should allow a Key path to be?

  12. jgeewax commented on Dec 1, 2015

    @jgeewax
    jgeewax
    on Dec 1, 2015
    ContributorAuthor
    More actions

    Yep - the on-demand eval SGTM. Basically anything that is derived would ideally be on-demand evaluated... right? which would include ID / name, ie:

    var key = new Key({ path: ['Person', 'mom', 'Person', 'jim'] });
    console.log(key.path);
    // [ 'Person', 'mom', 'Person', 'jim' ]
    
    key.parent.name = null;
    key.parent.id = 1
    console.log(key.path);
    // [ 'Person', 1, 'Person', 'jim' ]
    
    console.log(key.parent.path);
    // [ 'Person', 1 ]
  13. stephenplusplus commented on Dec 1, 2015

    @stephenplusplus
    stephenplusplus
    on Dec 1, 2015
    Contributor
    More actions

    Yeah that works fine. PR incoming!

  14. callmehiphop
    closed this as completedin #983on Dec 1, 2015
  15. 27 remaining items

  16. sofisl
    added 4 commits that reference this issue on Jan 27, 2026

    chore: remove circle CI test (#574)

    3178a3e

    chore: remove circle CI test (#574)

    7f9c46b

    chore: remove circle CI test (#574)

    bf3e9b9

    chore: remove circle CI test (#574)

    ea37747
  17. GautamSharda
    added 2 commits that reference this issue on Feb 2, 2026

    build: update .OwlBot.lock with new version of post-processor (#574)

    5beb7e9

    build: update .OwlBot.lock with new version of post-processor (#574)

    4782a61
  18. sofisl
    added 2 commits that reference this issue on Feb 4, 2026

    feat: add eslintignore for sameple generated code (#1302) (#574)

    3605fb1

    chore: release 3.2.0 (#575)

    00e33d9
  19. GautamSharda
    added a commit that references this issue on Feb 5, 2026

    chore: new owl bot post processor docker image (#574)

    602575d
  20. GautamSharda
    added 3 commits that reference this issue on Feb 23, 2026

    chore: new owl bot post processor docker image (#574)

    e3170f4

    chore(deps): update dependency sinon to v21 (#574)

    0a3e21d

    chore: new owl bot post processor docker image (#574)

    e564bb4
  21. sofisl
    added a commit that references this issue on Feb 26, 2026

    chore(deps): update dependency typescript to ~3.4.0 (#574)

    ff33243
  22. GautamSharda
    added 2 commits that reference this issue on Mar 5, 2026

    chore: release 5.3.0 (#574)

    c4c7dfd

    chore: release 3.1.0 (#574)

    f529789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @stephenplusplus
    stephenplusplus

Labels

api: datastoreIssues related to the Datastore API.

Type

No type

Projects

No projects

    Milestone

    • Datastore FutureNo due date

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.