Skip to main content

O! My Models (omymodels) is a library to generate from SQL DDL Python Models for GinoORM.

Project description

O! My Models

badge1 badge2 badge3

O! My Models (omymodels) is a library to generate from SQL DDL Python Models for GinoORM (I hope to add several more ORMs in future) and Pydantic.

By default method create_models generate GinoORM models, to ger Pydantic models output use argument models_type='pydantic'

For example,

from omymodels import create_models


ddl = """
CREATE table user_history (
     runid                 decimal(21) null
    ,job_id                decimal(21)  null
    ,id                    varchar(100) not null
    ,user              varchar(100) not null
    ,status                varchar(10) not null
    ,event_time            timestamp not null default now()
    ,comment           varchar(1000) not null default 'none'
    ) ;


"""
result = create_models(ddl, models_type='pydantic')['code']

 # and output will be:
import datetime
from typing import Optional
from pydantic import BaseModel


class UserHistory(BaseModel):

    runid: Optional[int]
    job_id: Optional[int]
    id: str
    user: str
    status: str
    event_time: datetime.datetime
    comment: str

GinoORM example. If you provide an input like:

CREATE TABLE "users" (
  "id" SERIAL PRIMARY KEY,
  "name" varchar,
  "created_at" timestamp,
  "updated_at" timestamp,
  "country_code" int,
  "default_language" int
);

CREATE TABLE "languages" (
  "id" int PRIMARY KEY,
  "code" varchar(2) NOT NULL,
  "name" varchar NOT NULL
);

and you will get output:

from gino import Gino


db = Gino()


class Users(db.Model):

    __tablename__ = 'users'

    id = db.Column(db.Integer(), autoincrement=True, primary_key=True)
    name = db.Column(db.String())
    created_at = db.Column(db.TIMESTAMP())
    updated_at = db.Column(db.TIMESTAMP())
    country_code = db.Column(db.Integer())
    default_language = db.Column(db.Integer())


class Languages(db.Model):

    __tablename__ = 'languages'

    id = db.Column(db.Integer(), primary_key=True)
    code = db.Column(db.String(2))
    name = db.Column(db.String())

How to install

pip install omymodels

How to use

From cli

omm path/to/your.ddl

# for example
omm tests/test_two_tables.sql

You can define target path where to save models with -t, –target flag:

# for example
omm tests/test_two_tables.sql -t test_path/test_models.py

If you want generate the Pydantic models - just use flag -m or –models_type=’pydantic’

omm /path/to/your.ddl -m pydantic

# or
omm /path/to/your.ddl --models_type pydantic

Small library is used for parse DDL- https://github.com/xnuinside/simple-ddl-parser.

What to do if types not supported in O! My Models and you cannot wait until PR will be approved

First of all, to parse types correct from DDL to models - they must be in types mypping, for Gino it exitst in this file:

omymodels/gino/types.py types_mapping

If you need to use fast type that not exist in mapping - just do a path before call code with types_mapping.update()

for example:

from omymodels.gino import types
from omymodels import create_models

types.types_mapping.update({'your_type_from_ddl': 'db.TypeInGino'})

ddl = "YOUR DDL with your custom your_type_from_ddl"

models = create_models(ddl)

#### And similar for Pydantic types

from omymodels.pydantic import types  types_mapping
from omymodels import create_models

types.types_mapping.update({'your_type_from_ddl': 'db.TypeInGino'})

ddl = "YOUR DDL with your custom your_type_from_ddl"

models = create_models(ddl, models_type='pydantic')

TODO in next releases

  1. Add ForeignKey generation for GinoORM Models

How to contribute

Please describe issue that you want to solve and open the PR, I will review it as soon as possible.

Any questions? Ping me in Telegram: https://t.me/xnuinside

Changelog

v0.4.1

  1. Added correct work with table names contains multiple ‘-’

v0.4.0

  1. Added generation for Pydantic models from ddl

  2. Main method create_gino_models renamed to create_models

v0.3.0

  1. Generated Index for ‘index’ statement in table_args (not unique constrait as previously)

  2. Fix issue with column size as tuple (4,2)

v0.2.0

  1. Valid generating columns in models: autoincrement, default, type, arrays, unique, primary key and etc.

  2. Added creating table_args for indexes

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

omymodels-0.4.3.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

omymodels-0.4.3-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file omymodels-0.4.3.tar.gz.

File metadata

  • Download URL: omymodels-0.4.3.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.7 Darwin/19.6.0

File hashes

Hashes for omymodels-0.4.3.tar.gz
Algorithm Hash digest
SHA256 cc8d107ee8b13ba48495cc80df381d3e4a9a1bae3238ab502d7df10e9865513f
MD5 c67ae9903abd33183882e13ba6d568af
BLAKE2b-256 e61d8bdd209378840a6e3512acc23ec93e5cf39ef00c8dee5b6a956416f72c77

See more details on using hashes here.

File details

Details for the file omymodels-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: omymodels-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.7 Darwin/19.6.0

File hashes

Hashes for omymodels-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5766f6b0bbb56719ced8f3123cf6e93f68fffd8d05b6da21b0e938145a4ee7ed
MD5 61107d676b936a54fbc95812701fe41d
BLAKE2b-256 32f18c76e5b3b75298d8672bfa1583728c98d57f5c8eddafd12e156627d2fd30

See more details on using hashes here.

Supported by

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