Skip to main content

Apply constraints before inserting and updating TinyDB records.

Project description


PyPI version PyPI license

Apply constraints before inserting and updating TinyDB records.


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


>>> from tinydb import TinyDB
>>> from tinydb_constraint import ConstraintTable
>>> from datetime import datetime
>>> TinyDB.table_class = ConstraintTable
>>> db = TinyDB('db.json')
>>> 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)


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.



  • Add ForeignKey constraints.

Related projects

  • tinydb-viewer - View records generated from TinyDB and alike (e.g. list of dictionaries.)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
tinydb_constraint- (15.2 kB) Copy SHA256 hash SHA256 Wheel py2.py3
tinydb-constraint- (5.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page