A lightweight, pure Python library for file-based database operations with encryption support. ZFDB provides a secure and easy way to store and manage data in a local file system using ZIP archives.
- π Password-based encryption
- π ZIP-based storage with compression
- π Record search capabilities
- π·οΈ Metadata support
- β Data integrity validation
- π Automatic compaction
- πΎ Backup functionality
- π JSON support
- π Pure Python (no external dependencies)
Clone the repository or install the package using pip
:
pip install zfdb
# or
git clone https://github.com/semolex/zfdb.git
cd zfdb
from pathlib import Path
from zfdb import Database, DatabaseConfig
# Create a database configuration
config = DatabaseConfig(
name="mydb",
path=Path("mydb.zip"),
password="secret123", # Optional
compression_level=9
)
# Initialize database
db = Database(config)
# Insert records
db.insert(
"user1",
'{"name": "John Doe", "email": "john@example.com"}',
metadata={"type": "user"}
)
# Read records
record = db.get("user1")
if record:
user_data = record.json # Parse as JSON
print(f"User: {user_data['name']}")
print(f"Record created: {record.metadata['created_at']}")
# Search records
results = db.search("user")
print(f"Found records: {results}")
# Create backup
db.backup("mydb_backup.zip")
from zfdb import DatabaseConfig
from pathlib import Path
config = DatabaseConfig(
name="mydb", # Database name
path=Path("mydb.zip"), # Database file path
password="secret123", # Optional encryption password
compression_level=6, # ZIP compression level (0-9)
max_size=1024 * 1024 * 100, # Maximum database size (100MB)
auto_compact=True, # Enable automatic compaction
version="1.0.0" # Database version
)
# Insert JSON data
db.insert(
"config1",
'{"setting": "value"}',
metadata={"type": "configuration"}
)
# Insert text data
db.insert(
"note1",
"This is a text note",
metadata={"type": "note"}
)
# Insert binary data
db.insert(
"binary1",
b"\x00\x01\x02\x03",
metadata={"type": "binary"}
)
# Get record
record = db.get("config1")
# Access data in different formats
raw_data = record.raw # bytes
text_data = record.text # str
json_data = record.json # parsed JSON
# Access metadata
created_at = record.metadata['created_at']
record_size = record.metadata['size']
db.update(
"note1",
"Updated content",
metadata={"updated_at": datetime.utcnow().isoformat()}
)
db.delete("note1")
all_records = db.list_records()
# Search by name pattern
notes = db.search("note")
configs = db.search("config")
# Compact database (remove deleted records)
db.compact()
# Create backup
db.backup("backup.zip")
ZFDB provides several security features:
- Password Protection: Database contents are encrypted using a password-derived key
- Data Integrity: Each record includes a SHA-256 checksum
- Size Limits: Configurable database size limits
- Validation: Automatic data integrity checking
- Regular Backups: Use the
backup()
method regularly - Error Handling: Always handle potential exceptions:
try: record = db.get("key") except DatabaseError as e: logger.error(f"Database error: {e}")
- Resource Management: Close database when done:
try: # Use database finally: db.compact() # Optional cleanup
- Not a real database
- Not suitable for concurrent access
- No built-in indexing
- Limited query capabilities
- Not recommended for very large datasets
- Simple encryption (not suitable for highly sensitive data)
poetry run black zfdb/
poetry run isort zfdb/
poetry run mypy zfdb/
poetry run pytest tests/
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/yourfeature
) - Commit your changes (
git commit -m 'Add some yourfeature'
) - Push to the branch (
git push origin feature/yourfeature
) - Open a Pull Request
- Use
isort
,mypy
andblack
for code formatting and type checking
This project is licensed under the MIT License - see the LICENSE file for details.
- Created to be used in local applications to persists data in case there is no other database available
- Inspired by simple key-value stores
- Built using Python standard library components
- Designed for simplicity and ease of use