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.3.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_jsonfield-1.0.3-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_jsonfield-1.0.3.tar.gz.

File metadata

  • Download URL: sqlalchemy_jsonfield-1.0.3.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlalchemy_jsonfield-1.0.3.tar.gz
Algorithm Hash digest
SHA256 162099ff6b6f475105afff90b15da114cbd87fd18df94e7d091df9f9d0e4f69f
MD5 7026d6191d8f1c3874978de773a0fdf2
BLAKE2b-256 bc159555c858379eaa76853722a4d63c6d9d4227c5889c0f9080d6b759c19100

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_jsonfield-1.0.3.tar.gz:

Publisher: pythonpackage.yml on penguinolog/sqlalchemy_jsonfield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlalchemy_jsonfield-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_jsonfield-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a73ec0685a9c4330ca1374d4ff9c169748f9d29aacb5b3380f617fb9e597edfa
MD5 0cd918906c6c9d86855e0f57329346ea
BLAKE2b-256 003a5c75c8032d8b066cb6e757cd58a741084fbb3764a5cbb76c34ed9190a1e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_jsonfield-1.0.3-py3-none-any.whl:

Publisher: pythonpackage.yml on penguinolog/sqlalchemy_jsonfield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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