What is the best way to encode an NDB query into a (RESTful) GET(/POST) request? Are the tools already out there to do that job?

65 views
Skip to first unread message

Simon Cox

unread,
Dec 6, 2018, 2:13:01 PM12/6/18
to Google App Engine
I would like to allow the client side to arbitrarily query NDB, using the full query syntax available for NDB (which can filter on unlimited fields with ==, <, <=, >, >=, !=, IN, and can use OR and AND).

How do I encode the query in a GET request? And how do I take the encoded query and convert to an NDB query server-side?

Are there any libraries that already solve the problem? I'm using Python 2.7.

Here is an (untested) attempt at what I'm trying to do, but which doesn't do any boolean operations:

'''

    Aiming for the following:


    query = Account.query()  # Retrieve all Account entitites

   query2 = query1.filter(Account.userid >= 40)  # Filter on userid >= 40

   query3 = query2.filter(Account.userid < 50)  # Filter on userid < 50 too


'''


from google.appengine.ext import ndb

import models


modelmap = {


    'Account' : models.account

   # etc

}



# this would come from the GET request, just mocked up here as an example

restquery = {


    'entity' : 'Account'


    'filters' = [

       {'field' : 'userid',

       'operator' : '>=',

       'value' : 40},

       {'field' : 'userid',

       'operator' : '<',

       'value' : 50},

   ]

}



model = models[restquery['entity']]


query = model.query()


for filter in filters:


    if filter['operator'] == '==':


        query.filter(model._properties[filter['field']] == filter['value'])


    elif filter['operator'] == '<':


        query.filter(model._properties[filter['field']] < filter['value'])    


    elif filter['operator'] == '<=':


        query.filter(model._properties[filter['field']] <= filter['value'])
 

    elif filter['operator'] == '>':


        query.filter(model._properties[filter['field']] > filter['value'])  


    elif filter['operator'] == '>=':


        query.filter(model._properties[filter['field']] >= filter['value'])  


    elif filter['operator'] == '!=':


        query.filter(model._properties[filter['field']] != filter['value'])  


    elif filter['operator'] == '<':

     

        query.filter(model._properties[filter['field']].IN(filter['value']))  



Omair (Cloud Platform Support)

unread,
Dec 7, 2018, 10:17:08 AM12/7/18
to Google App Engine

Hi Simon,


Cloud Datastore client libraries supported by App Engine standard can be seen by visiting the link here. To get an overview of the Python NDB client library along with sample code please visit the link here.


Since your question is technical in nature I would suggest that you post your question on StackOverflow as Google Groups are reserved for general product discussion, StackOverflow for technical questions whereas Issue Tracker for product bugs and feature requests. To get a better support you should post to the relevant forum. Please read the Community Support article for better understanding.


Hope this helps.


Simon Cox

unread,
Dec 10, 2018, 11:01:50 PM12/10/18
to Google App Engine
Thanks Omair.

I strongly suspect that this question will be moderated on StackOverflow as it is opinion / best-practice based.

Mohammad I (Cloud Platform Support)

unread,
Dec 13, 2018, 5:41:57 PM12/13/18
to Google App Engine

Hello Simon,


Please look at this document "The Python NDB Client Library Overview" which was provided earlier that describes Python NDB Client Library which allows App Engine Python apps to connect to Cloud Datastore. The document also provides sample codes.


Please kindly note that Google Groups hosts discussion forums where users are likely to find information like service status updates and release notes, and ranging from book recommendations to creative shortcuts. If you believe that what you've encountered is platform specific issue you can report this by creating an issue in the Issue Tracker and Google Cloud Support team member will assist you to resolve your issue.  If your issue is not a platform issue but rather a problem with how you've configured your code, although you're not sure what it may be specifically, you should post to StackOverflow as it was suggested at the previous message and community of developers will assist you.

Reply all
Reply to author
Forward
0 new messages