GithubHelp home page GithubHelp logo

ptzagk / alchemyjsonschema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from podhmo/alchemyjsonschema

0.0 0.0 0.0 101 KB

convert sqlalchemy model to jsonschema.

Makefile 0.18% Python 99.55% Shell 0.27%

alchemyjsonschema's Introduction

alchemyjsonschema

image

# -*- 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(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(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(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'}
"""

has alchemyjsonschema command

help

$ alchemyjsonschema --help
usage: alchemyjsonschema [-h]
                         [--walker {noforeignkey,foreignkey,structual,control}]
                         [--decision {default,comfortable}] [--depth DEPTH]
                         [--decision-relationship DECISION_RELATIONSHIP]
                         [--decision-foreignkey DECISION_FOREIGNKEY]
                         [--out-dir OUT_DIR]
                         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,structual,control}
  --decision {default,comfortable}
  --depth DEPTH
  --decision-relationship DECISION_RELATIONSHIP
  --decision-foreignkey DECISION_FOREIGNKEY
  --out-dir OUT_DIR

target models

class Group(Base):
    __tablename__ = "Group"
    query = Session.query_property()

    pk = sa.Column(sa.Integer, primary_key=True, doc="primary key")
    name = sa.Column(sa.String(255), default="", nullable=False)
    color = sa.Column(sa.Enum("red", "green", "yellow", "blue"))
    created_at = sa.Column(sa.DateTime, nullable=True)


class User(Base):
    __tablename__ = "User"
    query = Session.query_property()

    pk = sa.Column(sa.Integer, primary_key=True, doc="primary key")
    name = sa.Column(sa.String(255), default="", nullable=False)
    group_id = sa.Column(sa.Integer, sa.ForeignKey(Group.pk), nullable=False)
    group = orm.relationship(Group, uselist=False, backref="users")
    created_at = sa.Column(sa.DateTime, nullable=True)

dump schema (commandline)

$ alchemyjsonschema alchemyjsonschema.tests.models:Group --walker structual

{
  "required": [
    "pk"
  ],
  "definitions": {
    "User": {
      "required": [
        "pk"
      ],
      "properties": {
        "pk": {
          "description": "primary key",
          "type": "integer"
        },
        "name": {
          "maxLength": 255,
          "type": "string"
        },
        "created_at": {
          "format": "date-time",
          "type": "string"
        }
      },
      "type": "object"
    }
  },
  "properties": {
    "pk": {
      "description": "primary key",
      "type": "integer"
    },
    "name": {
      "maxLength": 255,
      "type": "string"
    },
    "color": {
      "enum": [
        "red",
        "green",
        "yellow",
        "blue"
      ],
      "maxLength": 6,
      "type": "string"
    },
    "created_at": {
      "format": "date-time",
      "type": "string"
    },
    "users": {
      "items": {
        "$ref": "#/definitions/User"
      },
      "type": "array"
    }
  },
  "title": "Group",
  "type": "object"
}

alchemyjsonschema's People

Contributors

podhmo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.