Skip to content

joonas-yoon/json-as-db

Repository files navigation

JSON as DB

PyPI Version Badge Python Version Badge Read The Docs PyTest Badge

Documentation: https://json-as-db.readthedocs.io/


Using JSON as very lightweight database. Work with JSON-like object for your database operations.

Major Features:

  • Simple: define your model by typing your fields using python types, build queries using python comparison operators

  • Developer experience: field/method autocompletion, type hints, data validation, perform database operations with a functional API

  • Fully typed: leverage static analysis to reduce runtime issues

  • Serialization: built in JSON serialization and JSON schema generation

  • Pure python implementation: No external dependencies of packages, except only for shortuuid to create to safe and unique ID

Quick Installation

Installing via pip:

pip install json-as-db

Examples

Load database from JSON file by its path, and then adding any object into database.

>>> import json_as_db as jad
>>> db = jad.Database()      # Declare an instance
>>> db.load('output.json')   # Load database from file (optional)
>>> db.add([{                # Add items what you want to add
...   "id": "1002",
...   "type": "Chocolate"
... })
['FqkmbYFSCRCAHQWydhM69v', 'RUJGcVBFANvNRReXa8U3En']

Find any in database with query-like parameters.

>>> from json_as_db import Key
>>> db[db.find(Key('type') == 'Chocolate')]
{ "id": "1002", "type": "Chocolate" }
>>> db[db.find((Key('age') > 25) & (Key('name') == 'Charles'))]
[{ "age": 33, "name": "Charles", "job": "Locksmith" }, { "age": 26, ... } ]

Save database into file as JSON format. You can read from this saved file. It supports keyword parameters for JSON formatter and options to file saving.

>>> db.save('output.json', json_args={'indent': 4}, file_args={'encoding': 'utf-8'})
"""
The following contents from file: output.json
{
    "created_at": "2022-12-25T16:50:02.459068",
    "creator": "json_as_db",
    "data": {
        "FqkmbYFSCRCAHQWydhM69v": {
            "id": "1001",
            "type": "Regular"
        },
        "RUJGcVBFANvNRReXa8U3En": {
            "id": "1002",
            "type": "Chocolate"
        }
    },
    "updated_at": "2022-12-28T16:51:36.276790",
    "version": "0.2.4"
}
"""

Represents database as follows in a table visualization.

>>> print(db)
age  grouped  ...  job                name
32   True     ...  Camera operator    Layne
17   False    ...  Flying instructor  Somerled
9    True     ...  Inventor           Joon-Ho
...  ...      ...  ...                ...
23   None     ...  Publican           Melanie
54   True     ...  Racing driver      Eike
41   None     ...  Barrister          Tanja


[100 items, 9 keys]

Benchmark

(avg. time per operation with 10K items) json_as_db pandas
Loads from file 149.11810 ms 153.71676 ms
Append items 8.96103 ms 2760.27654 ms
Search a item 9.87914 ms 2.59354 ms
Get an item by key 0.0039 ms 0.0689 ms
Updating a item 0.0074 ms 0.0148 ms
Updating 5 items in a row 0.0130 ms 0.9432 ms
Remove an item 0.0012 ms 6.0930 ms

Please see the details on BENCHMARK.

Contributing

Contributing guidelines can be found CONTRIBUTING.

Welcome all contributions to the community and feel free to contribute.

License

Under the MIT license. See the LICENSE file for more info.


PyPI Download Badge Hits Badge