https://github.com/patarapolw/tinydb-constraint
Apply constraints before inserting and updating TinyDB records.
https://github.com/patarapolw/tinydb-constraint
tinydb tinydb-python-nosql
Last synced: 3 months ago
JSON representation
Apply constraints before inserting and updating TinyDB records.
- Host: GitHub
- URL: https://github.com/patarapolw/tinydb-constraint
- Owner: patarapolw
- License: mit
- Created: 2018-10-01T03:02:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-02T05:01:07.000Z (over 7 years ago)
- Last Synced: 2025-10-14T19:03:29.448Z (8 months ago)
- Topics: tinydb, tinydb-python-nosql
- Language: Python
- Size: 24.4 KB
- Stars: 1
- Watchers: 0
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tinydb-constraint
[](https://pypi.python.org/pypi/tinydb-constraint/)
[](https://pypi.python.org/pypi/tinydb-constraint/)
Apply constraints before inserting and updating TinyDB records.
## Installation
Method 1:
```commandline
$ pip install tinydb-constraint
```
Method 2:
- Clone the project from GitHub
- [Get poetry](https://github.com/sdispater/poetry) and `poetry install tinydb-constraint --path PATH/TO/TINYDB/CONSTRAINT`
## Usage
```python
>>> 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)
}
```
## 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.
```python
>>> 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](https://github.com/patarapolw/tinydb-viewer) - View records generated from TinyDB and alike (e.g. list of dictionaries.)