我希望能够(在运行时)使用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

但如果在运行时需要更改,例如当用户单击按钮时,可以使用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列作为文本.

注意:没有删除列的选项.

Database相关问答推荐

使用 prisma ORM 在我的迁移中手动添加触发器

在 SQL Server 中找出调用存储过程

我们如何使用 Hibernate 和 JPA 调用存储过程?

在 PostgreSQL 触发函数中使用 pg_notify

如何连接到 MDF 数据库文件?

MySQL workbench:如何将 mysql 数据库导出到 .sql 文件?

使用 2 个进程处理数据库

寻找基于磁盘的类 redis 数据库

如何在 MSSQL 2005 中创建递归查询?

如何在大型数据库中使用 typeahead.js

按最强 LIKE 排序 SQL?

从数据库行在 Golang 中创建map

不带 WHERE 子句的 UPDATE 查询

有人可以详细解释 SOLR requestHandlers 和 responseWriters 吗?

使用 PHP/PDO 判断数据库表是否存在

数据库查询时间复杂度

数据库与平面文本文件:当性能不是问题时, Select 一个而不是另一个的一些技术原因是什么?

ManyToOneRel 和 ForeignKey 的区别?

Rails 控制台 - 查找在某天创建的位置

带数据库的Electron 应用程序