Skip to main content

A lightweight JSON database with dataclass support.

Project description

FastJson-db

A lightweight JSON-based database for Python.
fastjson-db allows you to store, retrieve, and manipulate data in a simple JSON file with a minimal and easy-to-use API.

Features

  • Lightweight and simple to use
  • CRUD operations: insert, get, update, delete
  • Automatic unique IDs for records
  • Optional fast backend using orjson if installed
  • Human-readable JSON file

Installation

pip install fastjson-db

Documentation

For further information, see the docs/ folder on github.

Examples

Some basic examples on how to user FastJson-db

Creating a Class

To manipulate JsonTables, you need to create a JsonModel subclass (dataclass), so the JsonTable only accepts that especific JsonModel subclass.

from fastjson_db import JsonModel
from dataclasses import dataclass

@dataclass
class User(JsonModel):
    _id: int
    name: str = ""
    password: str = ""

It's obrigatory using _id field or it will not result in error when quering.

Creating a JsonTable

JsonTables are the ones inserting and updating your dataclasses in .json files. They will automaticly create and facilitate the usage of .json "tables", trying to simulate a simple database.

from fastjson_db import JsonModel, JsonTable
from dataclasses import dataclass

@dataclass
class User(JsonModel):
    _id: int = 0
    name: str = ""
    password: str = ""
    
user = User(name="Allan", password="123")

user_table = JsonTable("users.json", User)

For easier table manipulation and avoid various JsonTables manipulating the same .json table, register the table in TABLE_REGISTRY.

TABLE_REGISTRY[User] = user_table

Creating a JsonQuerier

JsonQueriers will make queries and searchs in the .json files / tables.

from fastjson_db import JsonModel, JsonTable, JsonQuerier
from dataclasses import dataclass

@dataclass
class User(JsonModel):
    _id: int = 0
    name: str = ""
    password: str = ""
    age: int = 0

user_table = JsonTable("users.json", User)
TABLE_REGISTRY[User] = user_table

user_table.insert(User(_id=1, name="Alice", password="abc", age=25))
user_table.insert(User(_id=2, name="Bob", password="123", age=30))
user_table.insert(User(_id=3, name="Charlie", password="xyz", age=35))

querier = JsonQuerier(user_table)

alice = querier.filter(name="Alice")
print("Filter:", alice)

not_30 = querier.exclude(age=30)
print("Exclude:", not_30)

older_than_30 = querier.custom(lambda u: u.age > 30)
print("Custom:", older_than_30)

bob = querier.get_first(name="Bob")
print("Get first:", bob)

ordered = querier.order_by("age")
print("Order by age:", ordered)

These are the main functions of JsonQuerier. You can have multiple queriers in the same table without generating Exceptions, but it's not recomended.

Using a ForeignKey

FastJson-db tries to simulate ForeignKeys with a class called ForeignKey. You need to just create a ForeignKey field with the JsonModel it's related to.

from fastjson_db import JsonModel, JsonTable, ForeignKey
from dataclasses import dataclass

@dataclass
class User(JsonModel):
    _id: int = 0
    name: str = ""

@dataclass
class Post(JsonModel):
    _id: int = 0
    title: str = ""
    author: ForeignKey[User] = ForeignKey(User)

user_table = JsonTable("users.json", User)
post_table = JsonTable("posts.json", Post)
TABLE_REGISTRY[User] = user_table
TABLE_REGISTRY[Post] = post_table

alice = User(_id=1, name="Alice")
user_table.insert(alice)

post = Post(_id=1, title="Meu primeiro post")
post.author.set(alice)
post_table.insert(post)

retrieved_post = post_table.get_first(_id=1)
author = retrieved_post.author.get()
print("Post:", retrieved_post.title)
print("Author:", author.name if author else "None")

So in this example we user a basic query to find a "Author" in the Post Table.

Project details


Download files

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

Source Distribution

fastjson_db-0.2.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastjson_db-0.2.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file fastjson_db-0.2.0.tar.gz.

File metadata

  • Download URL: fastjson_db-0.2.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for fastjson_db-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5752980e884e26b3d744bbdf65b8287042efc31704a0e0c4ff2062ba6b1732ed
MD5 935546bbcefea75b852abc67f374a403
BLAKE2b-256 d6f51a160edb11214f3a415bf7e5b323a698e5bbf470c139b815e64fee8e63e2

See more details on using hashes here.

File details

Details for the file fastjson_db-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: fastjson_db-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for fastjson_db-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ade55403bca5691da1123ef81815fa26dc65200d6639c8365032d7340682a876
MD5 55811b20ca674c4c4e12ef29ce7477a4
BLAKE2b-256 795c772a8a386c49bc1bd5247219d7469130aa87ccad893823783c5ec29e564c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page