compressed_json

Compressed json type.

class sqlalchemy_mate.types.compressed_json.CompressedJSONType(*args, **kwargs)[source]

This column store json serialized object and automatically compress it in form of binary before writing to the database.

This column should be a json serializable python type such as combination of list, dict, string, int, float, bool. Also you can use other standard json api compatible library for better serialization / deserialization support.

NOTE, this type doesn’t support JSON path query, it treats the object as a whole and compress it to save storage only.

Parameters

json_lib – optional, the json library you want to use. It should have json.dumps method takes object as first arg, and returns a json string. Should also have json.loads method takes string as first arg, returns the original object.

# standard json api compatible json library
import jsonpickle

class Order(Base):
    ...

    id = Column(Integer, primary_key=True)
    items = CompressedJSONType(json_lib=jsonpickle)

items = [
    {"item_name": "apple", "quantity": 12},
    {"item_name": "banana", "quantity": 6},
    {"item_name": "cherry", "quantity": 3},
]

order = Order(id=1, items=items)
with Session(engine) as ses:
    ses.add(order)
    ses.save()

    order = ses.get(Order, 1)
    assert order.items == items

    # WHERE ... = ... also works
    stmt = select(Order).where(Order.items==items)
    order = ses.scalars(stmt).one()
impl

alias of sqlalchemy.sql.sqltypes.LargeBinary

load_dialect_impl(dialect)[source]

Return a TypeEngine object corresponding to a dialect.

This is an end-user override hook that can be used to provide differing types depending on the given dialect. It is used by the TypeDecorator implementation of type_engine() to help determine what type should ultimately be returned for a given TypeDecorator.

By default returns self.impl.

process_bind_param(value, dialect)[source]

Receive a bound parameter value to be converted.

Custom subclasses of _types.TypeDecorator should override this method to provide custom behaviors for incoming data values. This method is called at statement execution time and is passed the literal Python data value which is to be associated with a bound parameter in the statement.

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.

See also

types_typedecorator

_types.TypeDecorator.process_result_value()

process_result_value(value, dialect)[source]

Receive a result-row column value to be converted.

Custom subclasses of _types.TypeDecorator should override this method to provide custom behaviors for data values being received in result rows coming from the database. This method is called at result fetching time and is passed the literal Python data value that’s extracted from a database result row.

The operation could be anything desired to perform custom behavior, such as transforming or deserializing data.

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

  • dialect – the Dialect in use.

See also

types_typedecorator

_types.TypeDecorator.process_bind_param()