EffortlessDB

EffortlessDB is the main class for interacting with the Effortless database. It provides methods for initializing, configuring, and performing operations on the database.

Properties

config

An EffortlessConfig object that holds the configuration settings for the database.

encryption_key

The encryption key (password) used to encrypt and decrypt the database.

Methods

__init__

Initializes an EffortlessDB instance with the given database name. If no name is provided, it defaults to "db". For example, passing in "users" will create a database with the file "users.effortless" in the current directory. If an encryption_key is provided, it will be used to encrypt/decrypt the database.

db = EffortlessDB("users")

add

Adds a new entry to the database. The entry must be a JSON-serializable dictionary that follows the configured rules of the database.

db.add({"name": "John", "age": 30})

update

Updates a single entry in the database that matches the given condition. The condition must match only one entry; if you want to update multiple entries at once, use batch(). Returns True if an entry was updated, or False otherwise.

db.update({"age": 31}, Field("id").equals(62824))

remove

Removes a single entry in the database that matches the given condition. The condition must match only one entry; if you want to update multiple entries at once, use erase(). Returns True if an entry was removed, or False otherwise.

db.remove(Field("id").equals(62824))

get_all

Retrieves all entries from the database, returning a list where each item is an entry in the database.

filter

Filters the database records based on a given Field object, returning a list of records that match the field condition.

condition = Field("name").equals("Ben") filtered_records = db.filter(condition)

batch

Updates all entries in the database that match the given condition. Returns the number of entries that were updated.

update_count = db.batch({"adult": True}, Field("age").greater_than(18))

erase

Erases all entries from the database that match the given condition. Returns the number of entries that were removed.

erased_count = db.erase(Field("age").less_than(18))

wipe

Clears all data from the database, resetting it to its initial state. You cannot wipe a read-only database unless you explicitly set override to True.

db.wipe()

configure

Updates the database configuration to reflect a new EffortlessConfig object.

encrypt

Sets a new encryption key and re-encrypts the database if necessary. If the database is already encrypted, this method will attempt to decrypt with both the old and new keys, then re-encrypt with the new key.

unencrypt

Permanently unencrypts the database using the encryption key if the database is currently encrypted. Raises a ValueError if an encryption key is not set.

set_directory

Sets the directory for writing and reading the database file; the directory must exist. The database will be saved to the new directory immediately.

db.set_directory("/path/to/database/directory")

set_storage

Sets the storage filename for the database. The actual file will be named 'db_name.effortless'. You must pass in a valid filename with no special characters. You should probably pass the filename into __init__ instead.

db.set_storage("users")

finish_backup

Stops processing until any ongoing backup operation to complete or the timeout is reached. Returns True if the backup completed (or there was no backup running), or returns False if the timeout was reached before the backup completed.

backup_completed = db.finish_backup(30)
Docs Home EffortlessConfig