0
votes

There is a method to get all the data using Node.js and Google Cloud Datastore.

var query = ContactModel.query();//contact model is a schema which instantiates gstore schema

query.run().then((result) => {
    const response = result[0];
     var entities       = response.entities;
    callback('',entities);
});

Is there a way to run custom query or simply say filters using Node.js and Google Cloud Datastore. I could find only one query example using Node.js and Google Cloud Datastore.

2

2 Answers

0
votes
var query = ContactModel.query()

query.filter(key, value)

query.run().then((result) => {
  const response = result[0],
        entities = response.entities

  callback('',entities)
})

The filter function will filter with respect to the key and value, filter function takes an optional third argument, the third one being the condition like equal to, less than, etc.. but if you provide only two arguments, it will check for equality. There are other functions like limit, order, groupBy etc. Find the documentation here.

0
votes

I suggest you use gstore-node, it has a very simple and efficient API, and the documentation has many examples of how to make advanced querys

For example in the documentation it is detailed how to list all the elements

// blog-post.model.js

// Create Schema
const blogPostSchema = new gstore.Schema({
    title : { type: 'string' },
    isDraft: { type: 'boolean' }
});

// List query settings
const listQuerySettings = {
    limit : 10,
    order : { property: 'title', descending: true }, // descending defaults to false and is optional
    select : 'title',
    ancestors : ['Parent', 123],  // will add an "hasAncestor" filter
    filters : ['isDraft', false] // operator defaults to "=",
};

// Add settings to schema
blogPostSchema.queries('list', listQuerySettings);

// Create Model
const BlogPost = gstore.model('BlogPost', blogPostSchema);