bag.sqlalchemy.json_col module

A transparent JSON column for SQLAlchemy models.

The content of this module was basically pasted from http://docs.sqlalchemy.org/en/latest/orm/extensions/mutable.html and altered such that the value is never None.

class bag.sqlalchemy.json_col.JSONEncodedDict(*args, **kwargs)[source]

Bases: sqlalchemy.sql.type_api.TypeDecorator

Represents an immutable structure as a json-encoded string.

impl

alias of sqlalchemy.sql.sqltypes.Unicode

process_bind_param(value, dialect)[source]

Receive a bound parameter value to be converted.

Subclasses override this method to return the value that should be passed along to the underlying TypeEngine object, and from there to the DBAPI execute() method.

The operation could be anything desired to perform custom behavior, such as transforming or serializing data. This could also be used as a hook for validating logic.

This operation should be designed with the reverse operation in mind, which would be the process_result_value method of this class.

Parameters
  • value – Data to operate upon, of any type expected by this method in the subclass. Can be None.

  • dialect – the Dialect in use.

process_result_value(value, dialect)[source]

Receive a result-row column value to be converted.

Subclasses should implement this method to operate on data fetched from the database.

Subclasses override this method to return the value that should be passed back to the application, given a value that is already processed by the underlying TypeEngine object, originally from the DBAPI cursor method fetchone() or similar.

The operation could be anything desired to perform custom behavior, such as transforming or serializing data. This could also be used as a hook for validating logic.

Parameters
  • value – Data to operate upon, of any type expected by this method in the subclass. Can be None.

  • dialect – the Dialect in use.

This operation should be designed to be reversible by the “process_bind_param” method of this class.

class bag.sqlalchemy.json_col.MutableDict[source]

Bases: sqlalchemy.ext.mutable.Mutable, dict

A dict that knows when it gets changed.

Usage in a SQLAlchemy model:

data = Column(MutableDict.as_mutable(JSONEncodedDict))
classmethod coerce(key, value)[source]

Convert plain dictionaries to MutableDict.