SQLAlchemy-core integration with marshmallow
Project description
marshmallow-sa-core
SQLAlchemy-core integration with marshmallow (de)serialization library.
Declare table
table.json
:
{
"name": "market",
"fields": [
{"name": "id", "type": "int", "constraints": {"required": true}},
{"name": "name", "type": "str", "constraints": {"required": true}},
{"name": "type", "type": "str"},
{"name": "city", "type": "str"},
{"name": "tel", "type": "str"},
{"name": "address", "type": "str", "constraints": {"unique": true}},
{"name": "lng", "type": "float", "constraints": {"minimum": -180, "maximum": 180}},
{"name": "lat", "type": "float", "constraints": {"minimum": -90, "maximum": 90}},
{"name": "create_time", "type": "datetime"},
{"name": "update_time", "type": "datetime"}
],
"primaryKey": ["id"]
}
Generate(Deserialize) SQLAlchemy Table
>>> import json
>>> with open('table.json', 'r') as fp:
table_definition = json.load(fp)
>>> from marshmallow_sa_core import JSONTableSchema
>>> schema = JSONTableSchema()
>>> table = schema.load(table_definition)
>>> table
Table('market',
MetaData(),
Column('id', Integer(), table=<market>, primary_key=True, nullable=False),
Column('name', String(), table=<market>, nullable=False),
...
check DDL of create table:
>>> from sqlalchemy import create_engine
>>> from sqlalchemy.schema import CreateTable
>>> db = create_engine('sqlite:///:memory:')
>>> print(CreateTable(table).compile(db))
CREATE TABLE market (
id INTEGER NOT NULL,
name VARCHAR NOT NULL,
type VARCHAR,
city VARCHAR,
tel VARCHAR,
address VARCHAR,
lng FLOAT CHECK ("lng" <= 180.0) CHECK ("lng" >= -180.0),
lat FLOAT CHECK ("lat" <= 90.0) CHECK ("lat" >= -90.0),
create_time DATETIME,
update_time DATETIME,
PRIMARY KEY (id),
UNIQUE (address)
)
Serialize SQLAlchemy Table object
>>> from sqlalchemy import Table, MetaData, Column, Integer, String, DateTime
>>> table = Table(
'employee',
MetaData(),
Column('id', Integer, primary_key=True),
Column('LastName', String, nullable=False),
Column('FirstName', String, nullable=False),
Column('Title', String),
Column('BirthDate', DateTime),
Column('HireDate', DateTime),
)
>>> from marshmallow_sa_core import JSONTableSchema
>>> from pprint import pp
>>> pp(JSONTableSchema().dump(table))
{'schema': None,
'fields': [{'type': 'int',
'constraints': {'required': True},
'name': 'id',
'__version__': '0.0.4'},
{'type': 'str',
'constraints': {'required': True},
'name': 'LastName',
'__version__': '0.0.4'},
{'type': 'str',
'constraints': {'required': True},
'name': 'FirstName',
'__version__': '0.0.4'},
{'type': 'str', 'name': 'Title', '__version__': '0.0.4'},
{'type': 'datetime', 'name': 'BirthDate', '__version__': '0.0.4'},
{'type': 'datetime', 'name': 'HireDate', '__version__': '0.0.4'}],
'name': 'employee',
'primaryKey': ['id'],
'__version__': '0.0.4'}
Get it now
$ pip install marshmallow-sa-core
License
MIT licensed. See the bundled LICENSE file for more details.
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 marshmallow_sa_core-0.0.5.tar.gz
.
File metadata
- Download URL: marshmallow_sa_core-0.0.5.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b63208cbb8a4219470b8da3f9df7c5672ebe8888e5a04d2553150794ead6074d |
|
MD5 | ae4607a7af7e138393f424e341916437 |
|
BLAKE2b-256 | 1bf7fbfb51f4aa5fc7942e4a10ee837837072e9b64775993a710e16b2140cc56 |
File details
Details for the file marshmallow_sa_core-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: marshmallow_sa_core-0.0.5-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42f4d530ecac8ecc37d3e1d3ce9a986601aa2beb81a6480f3392da31c92bcce3 |
|
MD5 | 6cb39adc8ef39f3f1362b4a2684f77dc |
|
BLAKE2b-256 | 224a9f6c7f0136d5cc57025c6953132fc80d22b988dff6c26f8a58211a5f68ea |