Skip to content

Commit

Permalink
Make flask-restalchemy make use of serialchemy
Browse files Browse the repository at this point in the history
- Improve README file
- Move conftest to the root folder
- Created SessionBasedFields base class to flag which fields require
a SQLAlchemy session for deserialization
- Use data-regression for tests
- Remove RTD docs generation
  • Loading branch information
igortg committed Feb 12, 2019
1 parent b505dcd commit 4014971
Show file tree
Hide file tree
Showing 32 changed files with 519 additions and 534 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
History
=======

0.1.0 (2019-02-06)
0.1.0 (2019-02-12)
------------------

* First release on PyPI.
99 changes: 92 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
Serialchemy
======================================================================


.. image:: https://img.shields.io/pypi/v/serialchemy.svg
.. TODO: Publish to PyPi
.. image:: https://img.shields.io/pypi/v/serialchemy.svg
:target: https://pypi.python.org/pypi/serialchemy

.. image:: https://img.shields.io/pypi/pyversions/serialchemy.svg
.. image:: https://img.shields.io/pypi/pyversions/serialchemy.svg
:target: https://pypi.org/project/serialchemy
.. image:: https://img.shields.io/travis/ESSS/serialchemy.svg
Expand All @@ -21,10 +20,96 @@ Serialchemy
.. image:: https://img.shields.io/readthedocs/pip.svg
:target: https://serialchemy.readthedocs.io/en/latest/

What is Serialchemy ?
================================================================================
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 <https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/67>`_. 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_.

.. _SQLAlchemy: www.sqlalchemy.org
.. _marshmallow-sqlalchemy: http://marshmallow-sqlalchemy.readthedocs.io
.. _marshmallow: https://marshmallow.readthedocs.io
.. _Flask-RESTAlchemy: https://github.com/ESSS/flask-restalchemy

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()

.. _`Generic Types`: https://docs.sqlalchemy.org/en/rel_1_2/core/type_basics.html#generic-types

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)

Serializers for SQLAlchemy models.
>> {'id': 1,
'fullname': 'Roberto Silva',
'admission': '2019-04-02T00:00:00',
'company': {'id': 3,
'name': 'Acme Co'
}
}


Contributing
Expand Down
3 changes: 0 additions & 3 deletions docs/api.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/changelog.rst

This file was deleted.

156 changes: 0 additions & 156 deletions docs/conf.py

This file was deleted.

1 change: 0 additions & 1 deletion docs/contributing.rst

This file was deleted.

Binary file removed docs/img/logo.png
Binary file not shown.
19 changes: 0 additions & 19 deletions docs/index.rst

This file was deleted.

51 changes: 0 additions & 51 deletions docs/installation.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/readme.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/usage.rst

This file was deleted.

Loading

0 comments on commit 4014971

Please sign in to comment.