Getting Started
Examples
To start using QuickSave, import it and initialize your database:
from qsave import QuickSave
db = QuickSave(path="path/to/your/file.json", pretty=True)
The pretty argument beautifies the saved data for better readability (optional).
Basic Usage
By default, changes are automatically saved when the with
block ends:
with db.session() as session:
session["key"] = "value"
print(session.get("key")) # Output: None, not yet saved
# Exiting the block automatically commits changes
with db.session() as session:
print(session.get("key")) # Output: value
Manual Commit (commit_on_expire=False)
For full control over when changes are saved, use commit_on_expire=False:
with db.session(commit_on_expire=False) as session:
session["key"] = "manual_value"
print(session.get("key")) # Output: None, not yet saved
session.commit() # Now changes are saved
print(session.get("key")) # Output: manual_value
with db.session() as session:
print(session.get("key")) # Output: manual_value
Commit and Rollback
You can manually save or discard changes during a session:
with db.session() as session:
session["key"] = "temp_value"
session.rollback() # Discard changes
print(session.get("key")) # Output: None
Nested Data
with db.session() as session:
session["nested"] = {"key": [1, 2, 3]}
session.commit()
with db.session() as session:
print(session["nested"]) # Output: {"key": [1, 2, 3]}
Async version:
from qsave.asyncio import AsyncQuickSave
db = AsyncQuickSave("path/to/your/file.json")
async def main():
async with db.session(False) as session:
print(len(session))
await session.commit()
await session.rollback() # NOTE: after commit, rollback does nothing :(
# only commit and rollback need to be awaited
# other functionalities remain the same as sync version