API Reference

aiodatalite Module

@aiodatalite.datalite(db_path: str, type_overload: Dict[type | None, str] | None = None, tweaked: bool = True, automarkup: bool = False) Callable

Bind a dataclass to a sqlite3 database. This adds new methods to the class, such as create_entry(), remove_entry() and update_entry().

Parameters:
  • db_path – Path of the database to be bound.

  • type_overload – Type overload dictionary.

  • tweaked – Whether to use pickle type tweaks

  • automarkup – Whether to use automarkup (synchronously)

Returns:

The new dataclass.

aiodatalite.constraints module

aiodatalite.constraints module introduces constraint

types that can be used to hint field variables, that can be used to signal datalite decorator constraints in the database.

exception aiodatalite.constraints.ConstraintFailedError

Bases: Exception

This exception is raised when a Constraint fails.

aiodatalite.fetch module

async aiodatalite.fetch.fetch_all(class_: Type[T], page: int = 0, element_count: int = 10) tuple[T]

Fetch all the records in the bound database.

Parameters:
  • class – Class of the records.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns:

All the records of type class_ in the bound database as a tuple.

async aiodatalite.fetch.fetch_equals(class_: Type[T], field: str, value: Any) T

Fetch a class_ type variable from its bound db.

Parameters:
  • class – Class to fetch.

  • field – Field to check for, by default, object id.

  • value – Value of the field to check for.

Returns:

The object whose data is taken from the database.

async aiodatalite.fetch.fetch_from(class_: Type[T], obj_id: int) T

Fetch a class_ type variable from its bound dv.

Parameters:
  • class – Class to fetch from.

  • obj_id – Unique object id of the object.

Returns:

The fetched object.

async aiodatalite.fetch.fetch_if(class_: Type[T], condition: str, page: int = 0, element_count: int = 10, parameter_values: tuple = None) T

Fetch all class_ type variables from the bound db, provided they fit the given condition

Parameters:
  • class – Class type to fetch.

  • condition – Condition to check for.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

  • parameter_values – If placeholders are used, they will be replaced with these values

Returns:

A tuple of records that fit the given condition of given type class_.

async aiodatalite.fetch.fetch_one(class_: Type[T], field: str, value: Any) T | None

Fetch a class_ type variable from its bound db.

Parameters:
  • class – Class to fetch.

  • field – Field to check for, by default, object id.

  • value – Value of the field to check for.

Returns:

The object whose data is taken from the database or None if not found.

async aiodatalite.fetch.fetch_range(class_: Type[T], range_: range) tuple[T]

Fetch the records in a given range of object ids.

Parameters:
  • class – Class of the records.

  • range – Range of the object ids.

Returns:

A tuple of class_ type objects whose values come from the class_’ bound database.

async aiodatalite.fetch.fetch_where(class_: Type[T], field: str, value: Any, page: int = 0, element_count: int = 10) tuple[T]

Fetch all class_ type variables from the bound db if the field of the records fit the given value.

Parameters:
  • class – Class of the records.

  • field – Field to check.

  • value – Value to check for.

  • page – Which page to retrieve, default all. (0 means closed).

  • element_count – Element count in each page.

Returns:

A tuple of the records.

async aiodatalite.fetch.is_fetchable(class_: type, obj_id: int) bool

Check if a record is fetchable given its obj_id and class_ type.

Parameters:
  • class – Class type of the object.

  • obj_id – Unique obj_id of the object.

Returns:

If the object is fetchable.

aiodatalite.mass_actions module

This module includes functions to insert multiple records to a bound database at one time, with one time open and closing of the database file.

exception aiodatalite.mass_actions.HeterogeneousCollectionError

Bases: Exception

:raiseif the passed collection is not homogeneous.

ie: If a List or Tuple has elements of multiple types.

async aiodatalite.mass_actions.copy_many(objects: List[T] | Tuple[T], db_name: str, protect_memory: bool = True) None

Copy many records to another database, from their original database to a new database, do not delete old records.

Parameters:
  • objects – Objects to copy.

  • db_name – Name of the new database.

  • protect_memory – Whether to protect memory during operation, Setting this to False will quicken the operation, but if the operation is cut short, the database file will corrupt.

Returns:

None

async aiodatalite.mass_actions.create_many(objects: List[T] | Tuple[T], protect_memory: bool = True) None

Insert many records corresponding to objects in a tuple or a list.

Parameters:
  • protect_memory – If False, memory protections are turned off, makes it faster.

  • objects – A tuple or a list of objects decorated with datalite.

Returns:

None.

aiodatalite.migrations module

Migrations module deals with migrating data when the object definitions change. This functions deal with Schema Migrations.

async aiodatalite.migrations.migrate(class_: type, column_transfer: dict = None, do_backup: bool = True, safe_migration_defaults: Dict[str, Any] = None) None

Given a class, compare its previous table, delete the fields that no longer exist, create new columns for new fields. If the column_flow parameter is given, migrate elements from previous column to the new ones. It should be noted that the obj_ids do not persist.

Parameters:
  • class – Datalite class to migrate.

  • column_transfer – A dictionary showing which columns will be copied to new ones.

  • do_backup – Whether to copy a whole database before dropping table

  • safe_migration_defaults – Key-value that will be written to old records in the database during migration so as not to break the schema

Returns:

None.