Breeze / breeze-client

Breeze for JavaScript clients
MIT License
38 stars 16 forks source link

Parse back from EntityKey.toString() to get FK #63

Open mahish opened 2 years ago

mahish commented 2 years ago

Hi, I didnt find native way how to parse back a value returned from EntityKey.toString() (doc). I can see the returned string value is concatenated as follows: [EntityType.name, foreignKeyValue].join('-'). Is there any native, correct way how to get the FK(s)? Of course, I can do entityKeyString.split("-")[1] but feels dirty. Am I missing something? Thanks.

marcelgood commented 2 years ago

No, there is not. EntityKey.toString() creates a human readable representation. If you want to serialize/deserialize the key use EntityKey.toJSON() and EntityKey.fromJSON().

jtraband commented 2 years ago

You can also look at the 'keyProperties' of the EntityType and then extract them from the entity yourself directly. See http://breeze.github.io/doc-js/api-docs/classes/entitytype.html#keyproperties

mahish commented 2 years ago

Thank you. I use the human readable version of EntityKey to create unique ID for presentation layer of my app (since it is more specific and safe than simple FK value). Eg.:

id: entityKey.toString(),
get parentId() {
  ...
  return parentEntityKey.toString();
},
set parentId(parentEntityKey) {
  ...
  // change value of FK property
  this.entity[propertyName] = parentEntityKey.split("-")[1];
}

As I can see there is specific mechanism with globally defined ENTITY_KEY_DELIMITER to get the string. So it would be nice to have a built-in method to parse it back. .toJSON does not help me here.