https://github.com/fl1yd/lightdb
Lightweight JSON Database for Python
https://github.com/fl1yd/lightdb
database dbms lightdb lightweight-database orm python
Last synced: about 2 months ago
JSON representation
Lightweight JSON Database for Python
- Host: GitHub
- URL: https://github.com/fl1yd/lightdb
- Owner: Fl1yd
- License: mit
- Created: 2021-09-09T06:25:06.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-07T10:43:52.000Z (11 months ago)
- Last Synced: 2024-08-10T06:52:31.236Z (10 months ago)
- Topics: database, dbms, lightdb, lightweight-database, orm, python
- Language: Python
- Homepage: https://lightdb.readthedocs.io
- Size: 67.4 KB
- Stars: 22
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.rst
- License: LICENSE
Awesome Lists containing this project
README
![]()
LightDB: Lightweight JSON Database for PythonWhat is this?
LightDB is a simple and lightweight JSON database for Python that allows users to efficiently write data to a file. It is designed to be easy to use, making it a great choice for developers who need a fast and reliable way to store and retrieve data.
Installing
You can install LightDB using
pip
:
pip install LightDBUsage
To use LightDB, first import the
LightDB
class from thelightdb
package:
from lightdb import LightDBThen, create a
LightDB
object by passing in the path to a JSON file where the database will be stored:
db = LightDB("db.json")You can then set key-value pairs in the database using the
set()
method:
db.set("name", "Alice")
db.set("age", 30)To save the changes, use the
save()
method:
db.save()Using Models
LightDB supports defining models for more structured and convenient data management. Here’s how to use models with LightDB:
First, import the necessary classes:
from typing import List, Dict, Anyfrom lightdb import LightDB
from lightdb.models import ModelDefine your model by extending the
Model
class:
class User(Model, table="users"):
name: str
age: int
items: List[str] = []
extra: Dict[str, Any] = {}Create a new instance of the model and save it to the database:
user = User.create(name="Alice", age=30)Retrieve a user from the database:
user = User.get(User.name == "Alice")
# or user = User.get(name="Alice")
print(user.name, user.age)Update a user’s information and save it:
user.name = "Kristy"
user.save()Filter users based on certain criteria:
users = User.filter(User.age >= 20)
for user in users:
print(user.name)Delete a user:
user.delete()License
LightDB is licensed under the MIT License.