Simon Cox
2018-12-06 15:55:43 UTC
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']))
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.
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.