Skip to main content

SafeDB

What is SafeDB?

SafeDB is a simple and secure database framework for Python. Its purpose is to provide a simple yet fast way to store data while also allowing for more flexibility than SQL.

Wouldn't it be slower than SQL?

No, because even though SafeDB is written in Python, it uses different caching algorithms to store data on disk and also in RAM and also uses multiple indexing methods for different columns.

How is it more flexible than SQL?

Well, unlike SQL, SafeDB uses lists and dictionaries to store data. This means you can store any type of data (including custom objects).

Can I use threading and multiprocessing?

SafeDB is perfectly thread-safe (with or without GIL), but isn't made to be used with multiprocessing.

Installation

To install SafeDB, run the following command:

pip install -U safedb

To upgrade SafeDB, run the following command:

pip install --upgrade safedb

Documentation

Basic Usage

Creating a Database

To create a new database or open an existing one:

from safedb.database import SDB

# Create or open a database
db = SDB("my_database")

Working with Tables

# Create a new table
db.add_table("users")

# List all tables
print(db.tables)

# Remove a table
db.remove_table("users")

Adding and Retrieving Data

# Add data to a table
user = {"id": 1, "username": "john_doe", "email": "john@example.com"}
db.add_content("users", user)

# Get all data from a table
all_users = db.get_table("users")

# Get data by index position
user = db.get_data_from_index("users", 0)  # Get the first user

Working with Indexes

Indexes improve query performance by creating lookup dictionaries for specific fields:

# Create an index on the "username" field
db.add_index("users", "username")

# Retrieve data using an index
user_index = db.get_index_from_index("users", "username", "john_doe")
user = db.get_data_from_index("users", user_index)

Transaction System

SafeDB uses an exchange system (similar to transactions) to ensure data integrity:

# Method 1: Using context manager (recommended)
with SDB("my_database") as db:
    db.add_table("products")
    db.add_content("products", {"id": 1, "name": "Laptop"})
    # Changes are automatically committed if no exceptions occur
    # and rolled back if an exception is raised

# Method 2: Manual transaction management
db = SDB("my_database")
db.start_exchange()  # Begin transaction
try:
    db.add_table("categories")
    db.add_content("categories", {"id": 1, "name": "Electronics"})
    db.commit()  # Commit changes
except Exception as e:
    db.rollback()  # Rollback on error
finally:
    db.save()  # Save changes to disk

Data Persistence

# Save database to disk
db.save()

# Load database from disk
db.load()

# Close the database (removes temporary files)
db.close()

# Delete the database (removes all database files)
db.delete()

Best Practices

  1. Use the context manager when possible to ensure proper transaction handling.
  2. Create indexes for fields you frequently search on to improve performance.
  3. Use consistent data types within a table to avoid issues with indexing.
  4. Save regularly to ensure data persistence.

Data Structure Compatibility

SafeDB can store various Python data types:

  • Dictionaries (recommended)
  • Custom objects with attributes
  • Lists (with numeric indexes)
  • Basic data types (strings, numbers, booleans)
  • Nested data structure

For best performance, use dictionaries with consistent keys across all records in a table.

Internal Workings

SafeDB uses an ordinary list to store data. Although, to fix the speed issue, there are multiple "indexes". Each index is a dictionary, where a key is a value of a column and a value is the index of a certain row in a list.

Please note that a "row" is an item in the table's list, and a "column" is a property/attribute of a row.

Storing multiple data types in a table is not recommended, and doing so will force you to make indexes based on common attributes.

SafeDB also has an implementation of the saving algorithm that includes durability and atomicity for the primary file.

Notes

Warnings

  1. Make sure to define any custom classes before you define the database object
  2. Don't forget to call SDB.close() to delete the temporary files
  3. Make sure all index-keys are valid attributes of every row in the table and are hash-able.

Notes

  1. If you need to store dictionaries in a table, you can create indexes with the index-keys being keys from your dictionaries (just make sure all dictionaries have the keys you use for indexes)

Metadata

Release files for safedb 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for safedb 0.1.0
File Size Uploaded
safedb-0.1.0.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for safedb 0.1.0
File Interpreter ABI Platform
safedb-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / safedb-0.1.0.tar.gz

Download URL safedb-0.1.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9c9c897f12c8e3e23525bd1ad83f003ee4c15c6dfae1da22c54923c8765a0dc7
BLAKE2b-256 checksum
How to use checksums
01b3000083b0024c73cabf835fb4be3f77d73a726eeeafcceec6e8e0be702592
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Windows/11

Release files / safedb-0.1.0-py3-none-any.whl

Download URL safedb-0.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a1a1d739b3cfbff4d216f53b4a0af8a0d7e596c5bd3e49fa93e2e937a7a7263c
BLAKE2b-256 checksum
How to use checksums
88e449a9f9e0197073a741e0b5b51c0f4035486c4d95d6a93ee21d9aef9c69e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Windows/11

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page