I am using ancestor query to retrieve the entity from google datastore using nodejs
query = datastore.createQuery(entity).hasAncestor(key)
where the key is
key = datastore.key([kind_name_of_parent, id_of_parent])
I am able to retrieve the objects but i would like to get the complete key of the retrieved object, whereas the returned array only contains the returned objects and the endCursor.
How can i get the complete key? or, can i get the complete key from the endCursor?
An example for my query result is:
[{ modTS: 1481006473081,
modLoc: null,
modUid: 0,
createTS: 1481006473081 } ],
{ moreResults: 'NO_MORE_RESULTS',
endCursor: 'CloSVGoTc350ZXN0cHJvamVjdC0zN2ZiNnI9CxIEdXNlchiAgID409OICgwLEgRzaW1zGICAgICAgIAKDAsSDmNsaWVudFNldHRwsdrfGICAgICA5NEKDBgAIAA=' } ]
Each entity in a Datastore mode database has a key that uniquely identifies it. The key consists of the following components: The namespace of the entity, which allows for multitenancy. The kind of the entity, which categorizes it for the purpose of queries.
Updating entities To update an existing entity, modify the attributes of the Entity object, then pass it to the DatastoreService. put() method. The object data overwrites the existing entity. The entire object is sent to Datastore with every call to put() .
An Entity is an individual record that gets stored and retrieved from the datastore. The Kind is the unique string identifier of the type of entity.
Since datastore client v0.42.2 the key is now referred using a Symbol on the datastore client datastoreClient.KEY
.
Run this on the CLI, if it doesn't work the first time run it again (first time might fail because of 'eventual consistency').
'use strict';
const Datastore = require('@google-cloud/datastore'),
projectId = 'your-project-id',
datastore = Datastore({
projectId: projectId
}),
pkind = 'Foo',
pname = 'foo',
kind = 'Bar',
name = 'bar',
parentKey = datastore.key([pkind, pname ]),
entityKey = datastore.key([pkind, pname, kind, name]),
entity = {
key: entityKey,
data: {
propa: 'valuea'
}
},
query = datastore.createQuery().hasAncestor(parentKey).limit(5);
let complete = false;
datastore.save(entity).then(() => {
datastore.runQuery(query).then((res) => {
try {
console.log('parent key ', res[0][0][datastore.KEY].parent);
} finally {
complete = true;
}
});
});
function waitUntilComplete() {
if (!complete)
setTimeout(waitUntilComplete, 1000);
}
waitUntilComplete();
The latest update to the datastore SDK has changed the way keys are accessed in an entity.
Earlier the entity had a key called key
which had a complete JSON object with information of the key.
After the update, the key is now referred using Symbols, a new ES6 datatype.
The key will be referenced using entity[datastoreClient.KEY]
assuming datastoreClient
is correctly authenticated/initialised object of the datasotore SDK.
The keys were retrieved by:
res[0][0][datastore.KEY]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With