Skip to main content

JSONField for MySQL Peewee library

This package is a JSONField on steroids for use with ORM Peewee, adding functions for working with NoSQL data (JSON fields)

By default, in Peewee, JSONField is a simple field inherited from TextField without any settings and additional methods, however, in MySQL/MariaDB there are many methods for working with JSON data that are either not used or need to be implemented yourself. This package is designed to fix this situation.

Installation

pip install peewee-jsonfield

Using

Suppose you have a table with a JSON type field, for example:

class TestModel(Model):
    id: int | AutoField = AutoField()
    data: dict | JSONField = JSONField()

With this library, you can use the SQL method JSON_SET using a simple python-specific syntax:

# Adding an integer variable to the "data" root
TestModel.data.jset('$.v_integer_key', 10).where(TestModel.id == 1).execute()
# Adding an dict variable to the "data" root
query: ModelUpdate = TestModel.data.jset('$.v_dict_key', {'nested1': 10})
query = query.where(TestModel.id == 1)
# ... any other where
query.execute()

You can use SQL method JSON_MERGE_PATCH to add nested variables in dict:

# Adding an nested dict variable to the root.v_dict_key
TestModel.data.jset('$.v_dict_key.nested_variable', 'nested_string').where(TestModel.id == 1).execute()

# Adding an nested list variable to the root.v_dict_key
TestModel.data.jset('$.v_dict_key.nested_list', [1, 2, 3]).where(TestModel.id == 1).execute()

Also, if you already have an object, instead of completely overwriting (.save()), you can use the UPDATE functions, specifying it as target and execute=True if the request needs to be executed immediately:

obj: TestModel = TestModel.get(TestModel.id == 1)
# some reading code
TestModel.data.jset('$.v_integer_key', 30, target=obj, execute=True)
TestModel.data.jset('$.v_string_key', 'testing new library', target=obj, execute=True)

To remove fields from a JSON field, you can use the .jremove() method:

TestModel.data.jremove('$.v_integer_key', target=obj, execute=True)
TestModel.data.jremove('$.v_dict_key.nested1', target=obj, execute=True)

# Query without WHERE
TestModel.data.jremove('$.v_string_key', execute=True)

Additional options

This field can also take additional options from the dbhandle object:

  • dbhandle.json_ensure_ascii = True - setting the ensure_ascii parameter in the json library when saving data to the database
  • dbhandle.json_use_detailed = False - setting the indent=2 parameter in the json library and using the intermediate SQL formatting function JSON_DETAILED when saving data to the database

More examples and try

View and run the file jsonfield_play.py

Before starting, you need to set environment variables to access the database: db_name, db_port, db_passwd and others (db_host, db_port, opt_table_temporary, opt_json_ensure_ascii, opt_json_use_detailed) as needed

TODO

  • Implement arrays methods
  • Add method's description
  • Add basic jpath checks

Limitations

This library is focused on working with MariaDB and MySQL DBMS and, most likely, will not work with others, since the syntax of SQL functions differs

Metadata

Release files for peewee-jsonfield 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for peewee-jsonfield 0.0.4
File Size Uploaded
peewee-jsonfield-0.0.4.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for peewee-jsonfield 0.0.4
File Interpreter ABI Platform
peewee_jsonfield-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / peewee-jsonfield-0.0.4.tar.gz

Download URL peewee-jsonfield-0.0.4.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6a481a672393e257eadd9741091634e1d8b9460c914debe1e41c7036e8e357da
BLAKE2b-256 checksum
How to use checksums
8798ebf724f176717fbb9b43e0f6c72184bf46592fa5fd5f0357e3dafa4dd2b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / peewee_jsonfield-0.0.4-py3-none-any.whl

Download URL peewee_jsonfield-0.0.4-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb5d52e9022765241ff5fc65b256da4f522a28ec1200d1246f7ace330f302ea0
BLAKE2b-256 checksum
How to use checksums
874cb79cd57067a343a54b52277882ea46f4cdea27ccffce0d8893e6c43080a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page