I want to be able to (at run time) create or alter a DB schema dynamically on a particular event (e.g. click of a button) using FormHandler microservice of Gramex.
How to create or alter a DB schema dynamically (at run time) using Gramex FormHandler
128 Views Asked by sandeep At
3
There are 3 best solutions below
0

You can do it using queryfunction of FormHandler which can modify the query based on the query parameters passed in the url.
Refer the link below for more https://gramener.com/gramex/guide/formhandler/#formhandler-queryfunction
0

You should try this:
In your yaml handler:
queryfunction: mymodule.sales_query(args)
In your python code:
def sales_query(args):
cities = args.get('ct', [])
if len(cities) > 0:
vals = ', '.join("'%s'" % pymysql.escape_string(v) for v in cities)
return 'SELECT * FROM sales WHERE city IN (%s)' % vals
else:
return 'SELECT * FROM sales'
Reference from: https://gramener.com/gramex/guide/formhandler/#formhandler-queryfunction
FormHandler supports defining columns in the spec.
For example, this configuration creates a table called
profile
with 4 columns: user, password, age and id.But if this needs to be changed at runtime, e.g. when a user clicks on a button, you can use a FunctionHandler with
gramex.data.alter()
For example, add this to your
gramex.yaml
:When
/alter?col=email
is called, the function adds anemail
column as text.NOTE: There's no option to DELETE columns.