bootstack.data.DataSourceProtocol#

class bootstack.data.DataSourceProtocol(*args, **kwargs)#

Bases: Protocol

Protocol defining the interface for data source implementations.

This protocol establishes a contract that all datasource backends must implement, ensuring consistent behavior across different storage mechanisms (memory, database, web API, etc.).

All datasource implementations must support:
  • Pagination with configurable page size

  • Filtering with col-based conditions (see bootstack.data.query)

  • Sorting by one or more columns

  • Full CRUD operations on records

  • Selection tracking for multi-select scenarios

  • CSV export capabilities

  • Direct index-based data access

Notes

  • Records are represented as Dict[str, Any] with at least ‘id’ and ‘selected’ fields

  • Filtering and sorting are expressed with the col expression API, not SQL

  • All methods preserve immutability - operations return new data or modify in-place

property count: int#

Total number of records matching the current filter.

property id_field: str#

Name of the record field that holds the stable row identity.

page_size: int#

Number of records per page.

property selected_count: int#

Number of selected records.

close()#

Release any resources held by the source (a connection, file handle).

A no-op for in-memory sources. Sources are also context managers, so a with block closes automatically. Safe to call more than once.

delete(record_id)#

Delete record by ID.

Parameters:

record_id (Any) – Unique identifier of the record

Returns:

True if record was deleted, False if not found

Return type:

bool

deselect(record_id)#

Mark record as unselected.

Parameters:

record_id (Any) – Unique identifier of the record

Returns:

True if record was deselected, False if not found

Return type:

bool

deselect_all(current_page_only=False)#

Deselect all records (optionally only current page).

Parameters:

current_page_only (bool) – If True, deselect only records on current page

Returns:

Number of records deselected

Return type:

int

export_csv(filepath, include_all=True)#

Export records to CSV file.

Parameters:
  • filepath (str) – Path to output CSV file

  • include_all (bool) – If True, export all records; if False, export only selected

get(record_id)#

Retrieve single record by ID.

Parameters:

record_id (Any) – Unique identifier of the record

Returns:

Record dictionary or None if not found

Return type:

Record | None

has_next_page()#

Check if more pages exist after current page.

Returns:

True if next page exists, False otherwise

Return type:

bool

insert(record)#

Create new record and return its ID.

Parameters:

record (Dict[str, Any]) – Dictionary with record data

Returns:

The ID assigned to the new record

Return type:

int

is_selected(record_id)#

Check whether a record is currently selected.

Parameters:

record_id (Any) – Unique identifier of the record

Returns:

True if the record is selected, False otherwise (including missing records)

Return type:

bool

load(records)#

Load data records into the datasource.

Parameters:

records (Sequence[Primitive] | Sequence[Mapping[str, Any]]) – Sequence of records (dicts) or primitives (auto-wrapped)

Returns:

Self for method chaining

Return type:

DataSourceProtocol

move(record_id, target_index)#

Reorder a record to a new position.

Parameters:
  • record_id (Any) – Unique identifier of the record to move

  • target_index (int) – Zero-based destination index (clamped to valid range)

Returns:

True if the record was moved, False if not supported or not found

Return type:

bool

next_page()#

Advance to next page and return its records.

Returns:

List of record dictionaries for the new page

Return type:

List[Record]

observe(condition=None, *order)#

Observe a live result set for a where/order query. Returns a Stream of result sets that re-emits on relevant changes.

on_change(handler=None)#

Subscribe to changes. Returns a Stream (no handler) or a cancellable subscription (with handler). The handler receives a DataChangeEvent.

order(*keys)#

Sort rows by one or more keys.

Parameters:

keys (str | Column | SortKey) – Column names ("name"), descending names ("-name"), or col(...) specs (col("salary").desc()). With no arguments, clears sorting.

Returns:

Self for method chaining

Return type:

DataSourceProtocol

page(page=None)#

Get records for specified page (or current page if None).

Parameters:

page (int | None) – Page number (0-indexed); updates current page if provided

Returns:

List of record dictionaries for the page

Return type:

List[Record]

page_slice(start_index, count)#

Get records by start index and count (respects filter/sort).

Parameters:
  • start_index (int) – Starting record index

  • count (int) – Number of records to retrieve

Returns:

List of record dictionaries

Return type:

List[Record]

prev_page()#

Move to previous page and return its records.

Returns:

List of record dictionaries for the new page

Return type:

List[Record]

reload()#

Re-read data from the underlying source.

For in-memory implementations this is typically a no-op. For file- or database-backed sources, this re-queries or re-reads.

save(path, *, selected_only=False, format=None, config=None)#

Export records to a file, format chosen by the path extension (or format).

Records are streamed into the writer; the active where/order view is respected. Built-in formats: CSV, TSV, JSON, JSONL, XML — plus Parquet, Feather, and HDF5 with the matching extra installed.

select(record_id)#

Mark record as selected.

Parameters:

record_id (Any) – Unique identifier of the record

Returns:

True if record was selected, False if not found

Return type:

bool

select_all(current_page_only=False)#

Select all records (optionally only current page).

Parameters:

current_page_only (bool) – If True, select only records on current page

Returns:

Number of records selected

Return type:

int

selected(page=None)#

Get selected records, optionally paginated.

Parameters:

page (int | None) – Optional page number for paginating selected records

Returns:

List of selected record dictionaries

Return type:

List[Record]

update(record_id, updates)#

Update record fields by ID.

Parameters:
  • record_id (Any) – Unique identifier of the record

  • updates (Dict[str, Any]) – Dictionary with fields to update

Returns:

True if record was updated, False if not found

Return type:

bool

where(condition=None)#

Filter rows by a condition built with col.

Parameters:

condition (Condition | None) – A filter condition (e.g. col("age") >= 25), or None / no argument to clear the filter.

Returns:

Self for method chaining

Return type:

DataSourceProtocol