Skip to main content

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 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

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

peewee-jsonfield-0.0.4.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

peewee_jsonfield-0.0.4-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

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

Hashes for peewee-jsonfield-0.0.4.tar.gz
Algorithm Hash digest
SHA256 6a481a672393e257eadd9741091634e1d8b9460c914debe1e41c7036e8e357da
MD5 251f0d2b38f5a65efc70e4d25e0ae6f2
BLAKE2b-256 8798ebf724f176717fbb9b43e0f6c72184bf46592fa5fd5f0357e3dafa4dd2b9

See more details on using hashes here.

File details

Details for the file peewee_jsonfield-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for peewee_jsonfield-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bb5d52e9022765241ff5fc65b256da4f522a28ec1200d1246f7ace330f302ea0
MD5 a123ac9eec3f8936d67220a4c6524033
BLAKE2b-256 874cb79cd57067a343a54b52277882ea46f4cdea27ccffce0d8893e6c43080a3

See more details on using hashes here.

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