为sqlalchemy模型映射jsonschema
alchemyjsonschema的Python项目详细描述
功能
alchemyjsonschema是用于将sqlalchemys的模型转换为jsonschema的库。
- 使用alchemyjsonschema作为命令
- 使用alchemyjsonschema作为库
作为库
有三种输出样式。
- noforeignkeywalker–忽略关系
- foreignkeywalker–期望有关关系的信息是外键
- StructuralWalker–完整集输出(期望有关关系的信息是完整的JSON数据)
示例
转储具有以上三种输出样式的json。
目标模型在这里。组和用户。
# -*- coding:utf-8 -*-importsqlalchemyassaimportsqlalchemy.ormasormfromsqlalchemy.ext.declarativeimportdeclarative_baseBase=declarative_base()classGroup(Base):"""model for test"""__tablename__="Group"pk=sa.Column(sa.Integer,primary_key=True,doc="primary key")name=sa.Column(sa.String(255),default="",nullable=False)classUser(Base):__tablename__="User"pk=sa.Column(sa.Integer,primary_key=True,doc="primary key")name=sa.Column(sa.String(255),default="",nullable=True)group_id=sa.Column(sa.Integer,sa.ForeignKey(Group.pk),nullable=False)group=orm.relationship(Group,uselist=False,backref="users")
非外星键行者
importpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportNoForeignKeyWalkerfactory=SchemaFactory(NoForeignKeyWalker)pp.pprint(factory(User))""" {'properties': {'name': {'maxLength': 255, 'type': 'string'}, 'pk': {'description': 'primary key', 'type': 'integer'}}, 'required': ['pk'], 'title': 'User', 'type': 'object'} """
外国键行者
importpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportForeignKeyWalkerfactory=SchemaFactory(ForeignKeyWalker)pp.pprint(factory(User))"""
{'properties': {'group_id': {'type': 'integer'},
'name': {'maxLength': 255, 'type': 'string'},
'pk': {'description': 'primary key', 'type': 'integer'}},
'required': ['pk', 'group_id'],
'title': 'User',
'type': 'object'}
"""
结构步行机
importpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportStructuralWalkerfactory=SchemaFactory(StructuralWalker)pp.pprint(factory(User))"""
{'definitions': {'Group': {'properties': {'pk': {'description': 'primary key',
'type': 'integer'},
'name': {'maxLength': 255,
'type': 'string'}},
'type': 'object'}},
'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
'name': {'maxLength': 255, 'type': 'string'},
'group': {'$ref': '#/definitions/Group'}},
'required': ['pk'],
'title': 'User',
'type': 'object'}
"""pp.pprint(factory(Group))"""
{'definitions': {'User': {'properties': {'pk': {'description': 'primary key',
'type': 'integer'},
'name': {'maxLength': 255,
'type': 'string'}},
'type': 'object'}},
'description': 'model for test',
'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
'name': {'maxLength': 255, 'type': 'string'},
'users': {'items': {'$ref': '#/definitions/User'},
'type': 'array'}},
'required': ['pk', 'name'],
'title': 'Group',
'type': 'object'}
"""
作为命令
使用alchemyjsonschema as命令(命令名也是alchemyjsonschema)。
帮助
$ alchemyjsonschema --help usage: alchemyjsonschema [-h][--walker {noforeignkey,foreignkey,structural}][--decision {default,fullset}][--depth DEPTH][--out OUT] target positional arguments: target the module or class to extract schemas from optional arguments: -h, --help show this help message and exit --walker {noforeignkey,foreignkey,structural} --decision {default,fullset} --depth DEPTH --out OUT output to file
以上两种模型定义(用户、组)存在于
目标是类位置或模块位置。例如,
- 类位置–alchemyjsonschema.tests.models:user
- 模块位置–alchemyjsonschema.tests.models
示例
通过命令行使用structural walker(–walker structural)。 当然,noforeignkeywalker是noforeignkey,而foreignkeywalker是foreignkey。
$ alchemyjsonschema --walker structural alchemyjsonschema.tests.models:Group {"definitions": {"Group": {"properties": {"color": {"enum": ["red", "green", "yellow", "blue"], "maxLength": 6, "type": "string"}, "created_at": {"format": "date-time", "type": "string"}, "name": {"maxLength": 255, "type": "string"}, "pk": {"description": "primary key", "type": "integer"}, "users": {"items": {"$ref": "#/definitions/User"}, "type": "array"}}, "required": ["pk"], "title": "Group", "type": "object"}, "User": {"properties": {"created_at": {"format": "date-time", "type": "string"}, "name": {"maxLength": 255, "type": "string"}, "pk": {"description": "primary key", "type": "integer"}}, "required": ["pk"], "type": "object"}}}
直接使用walker类时,输出不相同。这是类似于swagger(openapi 2.0)工具的便捷输出。
附录:什么是“决定”?
什么是“决定”?(待办事项:温和描述)
$ alchemyjsonschema --walker structural alchemyjsonschema.tests.models:User | jq . -S > /tmp/default.json $ alchemyjsonschema --decision useforeignkey --walker structural alchemyjsonschema.tests.models:User | jq . -S > /tmp/useforeignkey.json $ diff -u /tmp/default.json /tmp/useforeignkey.json
--- /tmp/default.json 2017-01-02 22:49:44.000000000 +0900 +++ /tmp/useforeignkey.json 2017-01-02 22:53:13.000000000 +0900 @@ -1,43 +1,14 @@ { "definitions": { - "Group": { - "properties": { - "color": { - "enum": [ - "red", - "green", - "yellow", - "blue" - ], - "maxLength": 6, - "type": "string" - }, - "created_at": { - "format": "date-time", - "type": "string" - }, - "name": { - "maxLength": 255, - "type": "string" - }, - "pk": { - "description": "primary key", - "type": "integer" - } - }, - "required": [ - "pk" - ], - "type": "object" - }, "User": { "properties": { "created_at": { "format": "date-time", "type": "string" }, - "group": { - "$ref": "#/definitions/Group" + "group_id": { + "relation": "group", + "type": "integer" }, "name": { "maxLength": 255,
0.6.0
- 修复jsonschema更新
0.4.2
- 修复用模块(不是模型类)调用命令的错误
0.4.0
- 删除不必要的功能(11)
0.3
- 夸张的支持(感谢iSysd)