我希望能够(在运行时)使用 Gramex 的 FormHandler 微服务在特定事件(例如单击按钮)上动态创建或更改数据库模式。
FormHandler 支持在规范中定义列。
例如,此配置创建一个名为profile
4 列的表:用户、密码、年龄和 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
但是如果这需要在运行时改变,例如当用户点击一个按钮时,你可以使用FunctionHandlergramex.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
列作为文本。
注意:没有删除列的选项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句