An in-memory database using .json files, akin to MongoDB for lightweight, RAM-based data handling.
This package exports the JSONDatabase class which can be used to operate a database of JSON documents in memory.
Load a Database from a file by providing a mapping of collections to classes, and the path to the database file:
from pathlib import Path
from json_db import JSONDatabase
MAPPING = {
"users": User,
"projects": Project,
}
database_path = Path("path/to/database.json")
db = JSONDatabase.load(MAPPING, database_path)Any of the collections defined in the mapping can be accessed as attributes of the Database object:
users = db.usersAccess specific documents in a collection by their ID:
user_id = "someuserid
user = users.get(user_id)Save a Database to a .json file by providing the path to the database file:
db.save(database_path)Clone this repository, and set the current directory to the root of the repository:
git clone git@github.com:FreddyWordingham/json-db.git
cd json-dbInstall the package and its dependencies:
poetry env use python3.10
poetry installTo ensure code quality and consistency, please set up Git hooks when you start working on the project:
sh ./ci/setup_hooks.shThis will run the pre-commit checks before pushing your changes to GitHub.