Discussion:
[google-appengine] 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?
Simon Cox
2018-12-06 15:55:43 UTC
Permalink
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']))
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/7ff1f5f2-d59d-4eb5-82b4-e446466c7395%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Omair (Cloud Platform Support)' via Google App Engine
2018-12-07 15:17:08 UTC
Permalink
Hi Simon,

Cloud Datastore client libraries supported by App Engine standard can be
seen by visiting the link here
<https://cloud.google.com/datastore/docs/reference/libraries#google_app_engine_standard_environment_client_libraries>.
To get an overview of the Python NDB client library along with sample code
please visit the link here
<https://cloud.google.com/appengine/docs/standard/python/ndb/>.

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
<https://cloud.google.com/support/docs/community>article for better
understanding.

Hope this helps.
Post by Simon Cox
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
'''
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()
query.filter(model._properties[filter['field']] == filter['value'
])
query.filter(model._properties[filter['field']] < filter['value'])
query.filter(model._properties[filter['field']] <= filter['value'
])
query.filter(model._properties[filter['field']] > filter['value'])
query.filter(model._properties[filter['field']] >= filter['value'
])
query.filter(model._properties[filter['field']] != filter['value'
])
query.filter(model._properties[filter['field']].IN(filter['value'
]))
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/612be887-fe3b-4ace-8f63-857e27c0697f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...