Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the complete key from the returned object of an ancestor query in google datastore?

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: 'CloSVGoTc350ZXN0cHJvamVjdC0zN2ZiNnI9CxIEdXNlchiAgID409OICgw‌​LEgRzaW1zGICAgICAgIA‌​KDAsSDmNsaWVudFNldHR‌​wsdrfGICAgICA5NEKDBg‌​AIAA=' } ]

like image 755
Akash Avatar asked Dec 06 '16 13:12

Akash


People also ask

What is a Datastore key?

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.

How do I update entity in Datastore?

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() .

What is Datastore kind?

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.


3 Answers

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();
like image 183
Frank Wilson Avatar answered Sep 25 '22 02:09

Frank Wilson


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.

like image 38
Paritosh Avatar answered Sep 24 '22 02:09

Paritosh


The keys were retrieved by: res[0][0][datastore.KEY]

like image 38
Akash Avatar answered Sep 24 '22 02:09

Akash