Reputation: 4553
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=' } ]
Upvotes: 5
Views: 1213
Reputation: 3250
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();
Upvotes: 4
Reputation: 1129
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.
Upvotes: 2