我希望能够(在运行时)使用Gramex的FormHandler microservice在特定事件(例如,单击按钮)上动态创建或更改DB模式.
我希望能够(在运行时)使用Gramex的FormHandler microservice在特定事件(例如,单击按钮)上动态创建或更改DB模式.
FormHandler支持defining columns in the spec.
例如,此配置创建了一个名为profile
的表,其中包含4列:user、password、age和id.
url:
handler: FormHandler
kwargs:
url: 'postgresql://$USER:$PASS@server/db' # Pick any database
table: profile # Pick any table name to create
id: id # The "id" column is primary key
# Define your table's columns
columns:
user: TEXT # Use any SQL type allowed by DB
password: VARCHAR(40) # including customizations
age:
type: INTEGER # You can also specify as a dict
nullable: true # Allows NULL values for this field
default: 0 # that default to zero
id:
type: INTEGER # Define an integer ID column
primary_key: true # as a primary key
autoincrement: true # that auto-increments
但如果在运行时需要更改,例如当用户单击按钮时,可以使用FunctionHandler和gramex.data.alter()
例如,将此添加到您的gramex.yaml
:
url:
alter:
pattern: /alter
handler: FunctionHandler
kwargs:
# You can decide the columns dynamically here
function: >
gramex.data.alter(url, table, columns={
col: 'TEXT' for col in handler.args.get('col', [])
})
调用/alter?col=email
时,函数会添加email
列作为文本.
注意:没有删除列的选项.