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.

Changelog

For a complete list of changes and updates, see the CHANGELOG file.

Roadmap

For a complete list of future updates and how you can help, see ROADMAP and CONTRIBUTING files.

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.3.2.tar.gz (10.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.3.2-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastjson_db-0.3.2.tar.gz
  • Upload date:
  • Size: 10.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.3.2.tar.gz
Algorithm Hash digest
SHA256 be2415b824f2d6bb071167e4ff6d0a7a7c31ce6cae5f020bd0c422485be33169
MD5 07080b01f29f7b1a6809fb1fd8e402fd
BLAKE2b-256 e19c66b8edf7d9fc5e18290248d2d13665e806cf6d52106187f9983966482e42

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastjson_db-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 10.7 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.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 435970a98c172c5a1f8a74114b72ea24b9d647d1a930ad735303fc894d1e5b69
MD5 57a385635b92592b24d2fe381649df15
BLAKE2b-256 f09fc2614d6f9c154dcdc0c5a13a3ae43a86cf0c7b00925d9b97173253dc590c

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