All Projects → ESSS → serialchemy

ESSS / serialchemy

Licence: MIT license
SQLAlchemy model serialization

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to serialchemy

Gino
GINO Is Not ORM - a Python asyncio ORM on SQLAlchemy core.
Stars: ✭ 2,299 (+6661.76%)
Mutual labels:  sqlalchemy
The Flask Mega Tutorial
📖《The Flask Mega-Tutorial》中文2018最新版📗
Stars: ✭ 221 (+550%)
Mutual labels:  sqlalchemy
Flask Boilerplate
Simple flask boilerplate with Postgres, Docker, and Heroku/Zeit now
Stars: ✭ 251 (+638.24%)
Mutual labels:  sqlalchemy
Choochoo
Training Diary
Stars: ✭ 186 (+447.06%)
Mutual labels:  sqlalchemy
Db To Sqlite
CLI tool for exporting tables or queries from any SQL database to a SQLite file
Stars: ✭ 219 (+544.12%)
Mutual labels:  sqlalchemy
Flask Base
A simple Flask boilerplate app with SQLAlchemy, Redis, User Authentication, and more.
Stars: ✭ 2,680 (+7782.35%)
Mutual labels:  sqlalchemy
Clickhouse Sqlalchemy
ClickHouse dialect for SQLAlchemy
Stars: ✭ 166 (+388.24%)
Mutual labels:  sqlalchemy
graphene-sqlalchemy-filter
Filters for Graphene SQLAlchemy integration
Stars: ✭ 117 (+244.12%)
Mutual labels:  sqlalchemy
Factory boy
A test fixtures replacement for Python
Stars: ✭ 2,712 (+7876.47%)
Mutual labels:  sqlalchemy
Statik
Multi-purpose static web site generator aimed at developers.
Stars: ✭ 249 (+632.35%)
Mutual labels:  sqlalchemy
Videospider
抓取豆瓣,bilibili等中的电视剧、电影、动漫演员等信息
Stars: ✭ 186 (+447.06%)
Mutual labels:  sqlalchemy
Eve Sqlalchemy
SQLAlchemy data layer for Eve-powered RESTful APIs
Stars: ✭ 215 (+532.35%)
Mutual labels:  sqlalchemy
Fpage
Tornado project generator. Start a project with tornado, mako/jinjia2 and sqlalchemy/peewee in a minute.
Stars: ✭ 242 (+611.76%)
Mutual labels:  sqlalchemy
Databases
Async database support for Python. 🗄
Stars: ✭ 2,602 (+7552.94%)
Mutual labels:  sqlalchemy
fastapi-debug-toolbar
A debug toolbar for FastAPI.
Stars: ✭ 90 (+164.71%)
Mutual labels:  sqlalchemy
Pytest Flask Sqlalchemy
A pytest plugin for preserving test isolation in Flask-SQLAlchemy using database transactions.
Stars: ✭ 168 (+394.12%)
Mutual labels:  sqlalchemy
Autoline
建议你使用更新的AutoLink平台
Stars: ✭ 227 (+567.65%)
Mutual labels:  sqlalchemy
sqlalchemy exasol
SQLAlchemy dialect for EXASOL
Stars: ✭ 34 (+0%)
Mutual labels:  sqlalchemy
FRDP
Boilerplate code for quick docker implementation of REST API with JWT Authentication using FastAPI, PostgreSQL and PgAdmin ⭐
Stars: ✭ 55 (+61.76%)
Mutual labels:  sqlalchemy
Python For Entrepreneurs Course Demos
Contains all the "handout" materials for Talk Python's Python for Entrepreneurs course. This includes notes and the final version of the website code.
Stars: ✭ 247 (+626.47%)
Mutual labels:  sqlalchemy

Serialchemy

https://sonarcloud.io/api/project_badges/measure?project=ESSS_serialchemy&metric=alert_status

SQLAlchemy model serialization.

Motivation

Serialchemy was developed as a module of Flask-RESTAlchemy, a lib to create Restful APIs using Flask and SQLAlchemy. We first tried marshmallow-sqlalchemy, probably the most well-known lib for SQLAlchemy model serialization, but we faced issues related to nested models. We also think that is possible to build a simpler and more maintainable solution by having SQLAlchemy in mind from the ground up, as opposed to marshmallow-sqlalchemy that had to be designed and built on top of marshmallow.

How to Use it

Serializing Generic Types

Suppose we have an Employee SQLAlchemy model declared:

class Employee(Base):
    __tablename__ = 'Employee'

    id = Column(Integer, primary_key=True)
    fullname = Column(String)
    admission = Column(DateTime, default=datetime(2000, 1, 1))
    company_id = Column(ForeignKey('Company.id'))
    company = relationship(Company)
    company_name = column_property(
        select([Company.name]).where(Company.id == company_id)
    )
    password = Column(String)

Generic Types are automatically serialized by ModelSerializer:

from serialchemy import ModelSerializer

emp = Employee(fullname='Roberto Silva', admission=datetime(2019, 4, 2))

serializer = ModelSerializer(Employee)
serializer.dump(emp)

>> {'id': None,
    'fullname': 'Roberto Silva',
    'admission': '2019-04-02T00:00:00',
    'company_id': None,
    'company_name': None,
    'password': None
    }

New items can be deserialized by the same serializer:

new_employee = {'fullname': 'Jobson Gomes', 'admission': '2018-02-03'}
serializer.load(new_employee)
>> <Employee object at 0x000001C119DE3940>

Serializers do not commit into the database. You must do this by yourself:

emp = serializer.load(new_employee)
session.add(emp)
session.commit()

Custom Serializers

For anything beyond Generic Types we must extend the ModelSerializer class:

class EmployeeSerializer(ModelSerializer):

    password = Field(load_only=True)     # passwords should be only deserialized
    company = NestedModelField(Company)  # dump company as nested object

serializer = EmployeeSerializer(Employee)
serializer.dump(emp)

>> {'id': 1,
    'fullname': 'Roberto Silva',
    'admission': '2019-04-02T00:00:00',
    'company': {'id': 3,
                'name': 'Acme Co'
               }
    }
Extend Polymorphic Serializer

One of the possibilities is to serialize SQLalchemy joined table inheritance and it child tables as well. To do such it's necessary to set a variable with the desired model class name. Take this Employee class with for instance and let us assume it have a joined table inheritance:

class Employee(Base):
    ...
    type = Column(String(50))

    __mapper_args__ = {
        'polymorphic_identity':'employee',
        'polymorphic_on':type
    }

class Engineer(Employee):
    __tablename__ = 'Engineer'
    id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
    association = relationship(Association)

    __mapper_args__ = {
        'polymorphic_identity':'engineer',
    }

To use a extended ModelSerializer class on the Engineer class, you should create the serializer as it follows:

class EmployeeSerializer(PolymorphicModelSerializer): # Since this class will be polymorphic

    password = Field(load_only=True)
    company = NestedModelField(Company)

class EngineerSerializer(EmployeeSerializer):
     __model_class__ = Engineer # This is the table Serialchemy will refer to
    association = NestedModelField(Association)

Contributing

For guidance on setting up a development environment and how to make a contribution to serialchemy, see the contributing guidelines.

Release

A reminder for the maintainers on how to make a new release.

Note that the VERSION should folow the semantic versioning as X.Y.Z Ex.: v1.0.5

  1. Create a release-VERSION branch from upstream/master.
  2. Update CHANGELOG.rst.
  3. Push a branch with the changes.
  4. Once all builds pass, push a VERSION tag to upstream.
  5. Merge the PR.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].