Apply constraints before inserting and updating TinyDB records.
Project description
tinydb-constraint
Apply constraints before inserting and updating TinyDB records.
Installation
Method 1:
$ pip install tinydb-constraint
Method 2:
- Clone the project from GitHub
- Get poetry and
poetry install tinydb-constraint --path PATH/TO/TINYDB/CONSTRAINT
Usage
>>> from tinydb import TinyDB
>>> from tinydb_constraint import ConstraintTable
>>> from datetime import datetime
>>> db = TinyDB('db.json')
>>> db.table_class = ConstraintTable
>>> db.set_schema({
... 'record_id': int,
... 'modified': datetime
... })
>>> db.schema
{
'record_id': Constraint(type_=int, unique=False, not_null=False),
'modified': Constraint(type_=datetime.datetime, unique=False, not_null=False)
}
If you want to enable TinyDB-constraint for all databases in a session, run:
>>> from tinydb import TinyDB
>>> from tinydb_constraint import ConstraintTable
>>> TinyDB.table_class = ConstraintTable
Note
I haven't modified the serialization yet, so datetime
type will actually produce datetime.isoformat()
, and to set datetime
, you have to pass a dateutil.parser.parse()
-parsable string.
Advanced usage
Database schema is also settable via Constraint
object.
>>> from tinydb_constraint import Constraint
>>> db.set_schema({
... 'user_id': Constraint(type_=int, unique=True, not_null=True)
... })
If you want to disable certain string sanitization features, like stripping spaces or checking if string can be converted to datetime, this can be done by setting environmental variables.
TINYDB_SANITIZE=0
TINYDB_DATETIME=0
Plan
- Add ForeignKey constraints.
Related projects
- tinydb-viewer - View records generated from TinyDB and alike (e.g. list of dictionaries.)
Project details
Release history Release notifications | RSS feed
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
Hashes for tinydb-constraint-0.1.3.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7245151103204aeb06a1542bac988996c7c740d5761134cdbe263f13a2ec33de |
|
MD5 | 862fa3620a820bc416e91b0bfc732202 |
|
BLAKE2b-256 | 6ee49a9a859e48eabd03f286bfe1037e85dedc845eb4ee9bcf8792434c1d5a71 |
Hashes for tinydb_constraint-0.1.3.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3466a86de1419d715d0abbe5cae669bfdeda395d4e8ad2067ecc5ff11cd049d |
|
MD5 | 26372221d86d4cd55a1f577d020b903e |
|
BLAKE2b-256 | df0ad9710b4b7dff30f13b2a869079bd099129a241adc0947be64594c3ebdd43 |