Skip to main content

mappeing jsonschema for sqlalchemy models

Project description

# -*- coding:utf-8 -*-
import sqlalchemy as sa
import sqlalchemy.orm as orm
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class Group(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)


class User(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")


## ------SingleModelWalker--------
import pprint as pp
from alchemyjsonschema import SchemaFactory
from alchemyjsonschema import SingleModelWalker

factory = SchemaFactory(SingleModelWalker)
pp.pprint(factory.create(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'}
"""


## ------OneModelOnlyWalker--------
import pprint as pp
from alchemyjsonschema import SchemaFactory
from alchemyjsonschema import OneModelOnlyWalker

factory = SchemaFactory(OneModelOnlyWalker)
pp.pprint(factory.create(User))

"""
{'properties': {'name': {'maxLength': 255, 'type': 'string'},
                'pk': {'description': 'primary key', 'type': 'integer'}},
 'required': ['pk'],
 'title': 'User',
 'type': 'object'}
"""


## ------AlsoChildrenWalker--------
import pprint as pp
from alchemyjsonschema import SchemaFactory
from alchemyjsonschema import AlsoChildrenWalker

factory = SchemaFactory(AlsoChildrenWalker)
pp.pprint(factory.create(User))

"""
{'properties': {'group': {'name': {'maxLength': 255, 'type': 'string'},
                          'pk': {'description': 'primary key',
                                 'type': 'integer'}},
                'name': {'maxLength': 255, 'type': 'string'},
                'pk': {'description': 'primary key', 'type': 'integer'}},
 'required': ['pk'],
 'title': 'User',
 'type': 'object'}
"""

pp.pprint(factory.create(Group))

"""
{'description': 'model for test',
 'properties': {'name': {'maxLength': 255, 'type': 'string'},
                'pk': {'description': 'primary key', 'type': 'integer'},
                'users': {'items': {'name': {'maxLength': 255,
                                             'type': 'string'},
                                    'pk': {'description': 'primary key',
                                           'type': 'integer'}},
                          'type': 'array'}},
 'required': ['pk', 'name'],
 'title': 'Group',
 'type': 'object'}
"""

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alchemyjsonschema-0.1.1.tar.gz (13.2 kB view details)

Uploaded Source

File details

Details for the file alchemyjsonschema-0.1.1.tar.gz.

File metadata

File hashes

Hashes for alchemyjsonschema-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b2f7168c34a0be6dfd6c3d1c355b7b0d1f7f6550becb68f4358e666082b4d98a
MD5 a12946586ea2d57408ff49fefb1d2f52
BLAKE2b-256 697a3bead7fe30dffd15936432a6af4aa281c7b8bf51c22377b98f2b8a42c876

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page