nodejs-datastore icon indicating copy to clipboard operation
nodejs-datastore copied to clipboard

[Question] Create entity with parent.

Open Kumjami opened this issue 5 years ago • 2 comments

Hi all!

I wonder if it is possible to create a entity adding the parent URL safe key.

Does anyone know?

Thank you in advance

Edit: I see that parent is created here. However, there is no way to create a entity without specifying the id and with parent.

Kumjami avatar Jan 28 '20 19:01 Kumjami

CC: @AVaksman any thoughts on this one?

bcoe avatar Jan 28 '20 22:01 bcoe

After being investigating for a while I found the way to do it, however, that's pretty annoying and not working really well. I'll explain:

You can specify the path, so if within the path you specify the Kind and the Id of the parent alongside the Kind of the entity you are creating, the link will be stablished.

That being said, it has some caveats, like the fact that you need to explicitly convert parents ID into datastore.int object for the link to work correctly.

The next level, to link nested entities, you need the full path from the higher level parent in order for that to work. So there is no easy way to link an entity with another one and then to inherit the links of this parent. There is suppose to be an easy way to sort this out, which is to use the path field from the parent. But by doing that we face the issue that ids are in a string format and therefore, the link doesn't work just by concatenating paths.

Example:

Vehicle -> Brand -> Car -> Model

Vehicle have an id like 1234

So a Vehicle entity, lets call it motorised would have the Key path ["Vehicle", "1234"] -> bear in mind that the real id is 1234 as an integer/number not as string.

So then, when you want to create a Brand called Ford you have to build the key with the following path ["Vehicle", 1234, "Brand"]. After inserting this entity you get the Key autocompleted with the following path ["Vehicle", "1234", "Brand", "5678"] -> note that numbers are now strings.

So if then, while creating a brand you would like to link it, you can't just use the property path of the key for the Ford brand, you have to transform the ids which were converted into string to numbers first.

For me, the ideal situation would be to be able to simply pass the parent path as it is, or even just the key in order to get the linking done correctly.

I hope this comment make sense and/or is understandable :D

Kumjami avatar Jan 29 '20 12:01 Kumjami