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 theensure_asciiparameter in thejsonlibrary when saving data to the databasedbhandle.json_use_detailed = False- setting theindent=2parameter in thejsonlibrary and using the intermediate SQL formatting functionJSON_DETAILEDwhen 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
jpathchecks
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)
| File | Size | Uploaded | |
|---|---|---|---|
| peewee-jsonfield-0.0.4.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|