Maps an OpenAPI schema to SQLAlchemy models.
Project description
OpenAlchemy
Translates an OpenAPI schema to SQLAlchemy models.
Installation
python -m pip install OpenAlchemy
# To be able to load yaml file
python -m pip install OpenAlchemy[yaml]
Example
For example, given the following OpenAPI specification:
# ./examples/simple-example-spec.yml
openapi: "3.0.0"
info:
title: Test Schema
description: API to illustrate OpenAlchemy MVP.
version: "0.1"
paths:
/employee:
get:
summary: Used to retrieve all employees.
responses:
200:
description: Return all employees from the database.
content:
application/json:
schema:
type: array
items:
"$ref": "#/components/schemas/Employee"
components:
schemas:
Employee:
description: Person that works for a company.
type: object
x-tablename: employee
properties:
id:
type: integer
description: Unique identifier for the employee.
example: 0
x-primary-key: true
x-autoincrement: true
name:
type: string
description: The name of the employee.
example: David Andersson
x-index: true
division:
type: string
description: The part of the company the employee works in.
example: Engineering
x-index: true
salary:
type: number
description: The amount of money the employee is paid.
example: 1000000.00
required:
- id
- name
- division
The SQLALchemy models file then becomes:
# models.py
from open_alchemy import init_yaml
init_yaml("./examples/simple-example-spec.yml")
The Base and Employee objects can be accessed:
from open_alchemy.models import Base
from open_alchemy.models import Employee
An extensive set of examples with a range of features is here:
An example API has been defined using connexion and Flask here:
Documentation
Features
- initializing from JSON,
- initializing from YAML,
- automatically generate a models file,
integer
(32 and 64 bit),number
(float only),boolean
,string
,password
,byte
,binary
,date
,date-time
,$ref
references for columns and models,- primary keys,
- auto incrementing,
- indexes,
- composite indexes,
- unique constraints,
- composite unique constraints,
- column nullability,
- foreign keys,
- many to one relationships,
- one to one relationships,
- one to many relationships,
- many to many relationships,
- custom foreign keys for relationships,
- back references for relationships,
allOf
inheritance for columns and models,from_dict
model methods to construct from dictionaries,to_dict
model methods to convert instances to dictionaries and- exposing created models under
open_alchemy.models
removing the need formodels.py
files.
Contributing
Fork and checkout the repository. To install:
python -m venv venv
source ./venv/bin/activate
python -m pip install -e .[dev,test]
To run tests:
tox
Make your changes and raise a pull request.
Compiling Docs
python -m venv venv
cd docs
make html
This creates the index.html
file in docs/build/html/index.html
.
Release Commands
rm -r dist/*
python -m pip install --upgrade setuptools wheel
python setup.py sdist bdist_wheel
python -m pip install --upgrade twine
python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
OpenAlchemy-0.12.1.tar.gz
(34.0 kB
view hashes)
Built Distribution
Close
Hashes for OpenAlchemy-0.12.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca8e32924e7602aaf941f7400c59b961a51f795c7b5d18014f57f478b2011e85 |
|
MD5 | 84658311c84c7e97e10c501280fe4ae9 |
|
BLAKE2b-256 | 8e4137db869cf9353b02b9f20c9a5af69c3899716e371c5af8612c3f7df1c28b |