Skip to content

Access properties directly from the entity #508

Description

@pcostell

When modifying or accessing entity properties, consistently you have to step into the data property:

my_entity.data.my_property
my_entity.data.my_other_property

Compare this to the other gcloud datastore APIs:

Python:

my_entity['my_property']

Ruby:

my_entity[:my_property]

It would be nice if properties could be accessed on the entity directly.

Activity

  1. ryanseys commented on Apr 27, 2015

    @ryanseys
    Contributor

    Seems our entities are comprised of key and data properties, so that the user can access their key value by going entity.key instead of entity.__key__ I believe. I'd suspect we'd have to change that if we wanted all the properties to be on the first level of the entity object like you described. Unless we wanted to potentially clobber entity.key but I believe key is not a reserved value as far as datastore is concerned, only __key__ is reserved.

  2. jgeewax commented on Apr 30, 2015

    @jgeewax
    Contributor

    I think the issue comes specifically because Javascript doesn't allow you to differentiate between a method (entity.key()), a property (entity.key), and an attribute (entity['key']) -- as far as JS is concerned, it's all the same thing.

    One option would be to add a getter method for the key (entity.getKey()) as a shortcut for the __key__ attribute.

    This doesn't address the issue where things could be blasted out (ie, getKey as a property in Datastore would be blasted out by the getKey method), so if we do something like this we'd need to also expose the "raw data" on the entity object as well.

    In short though, I think the common case will be "very few people will have a property called getKey" -- we should optimize for that and allow people "go the long way" (ie, entity.data.getKey) if they are uncommon enough to name a property as that. (Note that apparently property names are allowed to have spaces -- and even start with spaces -- so there will be edge cases that we should make possible but not easy.)

    @ryanseys What do you think?

  3. added this to the Datastore Future milestone on Apr 30, 2015
  4. ryanseys commented on Apr 30, 2015

    @ryanseys
    Contributor

    This sounds like a job well-suited for Proxies! Unfortunately Node doesn't universally support those. I think we might be able to get around this by attaching a special getter and setter to the getKey property of the entity object that detects when it's reassigned, and properly diverts the reassignment to the underlying property... I'll see what I can think up.

  5. stephenplusplus commented on Oct 10, 2016

    @stephenplusplus
    Contributor

    Gave this a shot using a Symbol as a key: #1682

  6. modified the milestones: , Datastore Future on Oct 10, 2016
  7. added a commit that references this issue on Sep 15, 2022
  8. 33 remaining items

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

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions