Skip to main content

SQLALchemy JSONField implementation for storing dicts at SQL

Project description

SQLAlchemy-JSONField

https://github.com/penguinolog/sqlalchemy_jsonfield/workflows/Python%20package/badge.svg https://img.shields.io/pypi/v/sqlalchemy_jsonfield.svg https://img.shields.io/pypi/pyversions/sqlalchemy_jsonfield.svg https://img.shields.io/pypi/status/sqlalchemy_jsonfield.svg https://img.shields.io/github/license/penguinolog/sqlalchemy_jsonfield.svg https://img.shields.io/badge/code%20style-black-000000.svg

SQLALchemy JSONField implementation for storing dicts at SQL independently from JSON type support.

Why?

SqlAlchemy provides JSON field support for several database types (PostgreSQL and MySQL for now) and semi-working dict <-> JSON <-> VARCHAR example, but… In real scenarios we have tests on sqlite, production on MySQL/MariaDB/Percona/PostgreSQL and some of them (modern Oracle MySQL & PostgreSQL) support JSON, some of them (SQLite, Percona & MariaDB) requires data conversion to Text (not VARCHAR).

As addition, we have different levels of Unicode support on database and connector side, so we may be interested to switch JSON encoding between deployments.

Solution:

SQLALchemy JSONField has API with suport for automatic switch between native JSON and JSON encoded data, and encoding to JSON string can be enforced.

Pros:

  • Free software: Apache license

  • Open Source: https://github.com/penguinolog/sqlalchemy_jsonfield

  • Self-documented code: docstrings with types in comments

  • Uses native JSON by default, but allows to specify different library.

  • Support multiple Python versions

Usage

Direct usage with MariaDB (example extracted from functional tests):

import sqlalchemy_jsonfield

class ExampleTable(Base):
    __tablename__ = table_name
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    row_name = sqlalchemy.Column(
        sqlalchemy.Unicode(64),
        unique=True,
    )
    json_record = sqlalchemy.Column(
        sqlalchemy_jsonfield.JSONField(
            # MariaDB does not support JSON for now
            enforce_string=True,
            # MariaDB connector requires additional parameters for correct UTF-8
            enforce_unicode=False
        ),
        nullable=False
    )

Usage with alternate JSON library:

import sqlalchemy_jsonfield
import ujson

class ExampleTable(Base):
    __tablename__ = table_name
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    row_name = sqlalchemy.Column(
        sqlalchemy.Unicode(64),
        unique=True,
    )
    json_record = sqlalchemy.Column(
        sqlalchemy_jsonfield.JSONField(
            enforce_string=True,
            enforce_unicode=False,
            json=ujson,  # Use ujson instead of standard json.
        ),
        nullable=False
    )

Usage on PostgreSQL/Oracle MySQL(modern version)/SQLite(testing) environments allows to set enforce_string=False and use native JSON fields.

Testing

The main test mechanism for the package sqlalchemy_jsonfield is using tox. Available environments can be collected via tox -l

CI systems

For code checking several CI systems is used in parallel:

  1. GitHub actions: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SQLAlchemy-JSONField-1.0.2.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

SQLAlchemy_JSONField-1.0.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file SQLAlchemy-JSONField-1.0.2.tar.gz.

File metadata

  • Download URL: SQLAlchemy-JSONField-1.0.2.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for SQLAlchemy-JSONField-1.0.2.tar.gz
Algorithm Hash digest
SHA256 dab3abc9d75a1640e7f3d4875564a4199f665d27863da8d5a089e4eaca5e67f2
MD5 f923c939e0dd7f54b57dc02ef6f1c2ca
BLAKE2b-256 d07788de5c9ac1a44db1abb493d9d0995681b200ad625d80a4a289c7be438d80

See more details on using hashes here.

File details

Details for the file SQLAlchemy_JSONField-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for SQLAlchemy_JSONField-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b2945fa1e60b07d5764a7c73b18da427948b35dd4c07c0e94939001dc2dacf77
MD5 8b3879845aac435e0a1bd244809a421f
BLAKE2b-256 fd67d75d119e70863e0519c8eec5fc66714d34ad1ee9e5e73bf4fc8e3d259fac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page