Maps an openapi schema to SQLAlchemy models.
Project description
openapi-SQLAlchemy
Translates an openapi schema to SQLAlchemy models.
Installation
python3 -m pip install openapi-SQLAlchemy
# To be able to load yaml file
python3 -m pip install PyYAML
Example
For example, given the following openapi specification:
# example-spec.yml
openapi: "3.0.0"
info:
title: Test Schema
description: API to illustrate openapi-SQLALchemy 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 yaml import load, Loader
from sqlalchemy.ext.declarative import declarative_base
from openapi_sqlalchemy import init_model_factory
Base = declarative_base()
with open("example-spec.yml") as spec_file:
SPEC = load(spec_file, Loader=Loader)
MODEL_FACTORY = init_model_factory(base=Base, spec=SPEC)
Employee = MODEL_FACTORY(name="Employee")
Features
Supported
The following features are supported:
integer
(32 and 64 bit),number
(float only),boolean
andstring
.
Not Supported
The following features are on the backlog:
allOf
inheritance,$ref
references for columns,$ref
references for models,- foreign keys and
- relationships.
Contributing
Fork and checkout the repository. To install:
python3 -m venv venv
python3 -m pip install -e .
To run tests:
tox
Make your changes and raise a pull request.
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
Built Distribution
File details
Details for the file openapi-SQLAlchemy-0.1.1.tar.gz
.
File metadata
- Download URL: openapi-SQLAlchemy-0.1.1.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 931a69be73cb6d1ea4774ba59c3e0c395ec39e3a31bbcb638a52f7713bdc70b2 |
|
MD5 | f88ab1ad8fa6fde371396bf2e2834231 |
|
BLAKE2b-256 | 4aab19652022a0aefd32d0a4d1fb752cb92a69a54a93bcb8c280dcd9346593d3 |
File details
Details for the file openapi_SQLAlchemy-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: openapi_SQLAlchemy-0.1.1-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62e2fe6f903a04ce589adb79647a83fc3fe41e0c4c931a81a006eaf2303db79c |
|
MD5 | 3128681f98ff7f95539a833567a29150 |
|
BLAKE2b-256 | b37881677008ac0b899663b9a779d1a1c7a1d038d8ce302ac1b77e1ded013898 |