JSONField for Peewee
Project description
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file peewee-jsonfield-0.0.4.tar.gz.
File metadata
- Download URL: peewee-jsonfield-0.0.4.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a481a672393e257eadd9741091634e1d8b9460c914debe1e41c7036e8e357da
|
|
| MD5 |
251f0d2b38f5a65efc70e4d25e0ae6f2
|
|
| BLAKE2b-256 |
8798ebf724f176717fbb9b43e0f6c72184bf46592fa5fd5f0357e3dafa4dd2b9
|
File details
Details for the file peewee_jsonfield-0.0.4-py3-none-any.whl.
File metadata
- Download URL: peewee_jsonfield-0.0.4-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb5d52e9022765241ff5fc65b256da4f522a28ec1200d1246f7ace330f302ea0
|
|
| MD5 |
a123ac9eec3f8936d67220a4c6524033
|
|
| BLAKE2b-256 |
874cb79cd57067a343a54b52277882ea46f4cdea27ccffce0d8893e6c43080a3
|