loom.core.repository.abc¶
- class loom.core.repository.abc.CursorResult(*, items, next_cursor, has_next)[source]¶
Bases:
Struct,Generic[OutputT]Result of a cursor-paginated query.
- class loom.core.repository.abc.FilterGroup(filters, op='AND')[source]¶
Bases:
objectA group of filter conditions combined with AND or OR logic.
- Parameters:
filters (tuple[FilterSpec, ...]) – Filter conditions to combine.
op (Literal['AND', 'OR']) – Logical operator:
"AND"(default) or"OR".
Example:
FilterGroup( filters=( FilterSpec("price", FilterOp.GTE, 10.0), FilterSpec("price", FilterOp.LTE, 100.0), ), op="AND", )
- class loom.core.repository.abc.FilterOp(value)[source]¶
Bases:
StrEnumFilter operator applied to a single field.
- EQ¶
Exact equality.
- NE¶
Inequality.
- GT¶
Greater than.
- GTE¶
Greater than or equal.
- LT¶
Less than.
- LTE¶
Less than or equal.
- IN¶
Value is in a collection.
- LIKE¶
SQL LIKE pattern (case-sensitive).
- ILIKE¶
SQL LIKE pattern (case-insensitive).
- IS_NULL¶
Field is NULL.
- EXISTS¶
Related collection is non-empty (subquery).
- NOT_EXISTS¶
Related collection is empty (subquery).
- class loom.core.repository.abc.FilterParams(*, filters=<factory>)[source]¶
Bases:
StructGeneric filter container for list queries.
- class loom.core.repository.abc.FilterSpec(field, op, value=None)[source]¶
Bases:
objectA single field filter condition.
- Parameters:
Example:
FilterSpec(field="price", op=FilterOp.GTE, value=10.0)
- class loom.core.repository.abc.PageParams(*, page=1, limit=50)[source]¶
Bases:
StructPagination parameters for list queries.
- class loom.core.repository.abc.PageResult(*, items, total_count, page, limit, has_next)[source]¶
Bases:
Struct,Generic[OutputT]Paginated result set returned by list queries.
- class loom.core.repository.abc.PaginationMode(value)[source]¶
Bases:
StrEnumPagination strategy for list and query operations.
- OFFSET¶
Classic page+limit pagination. Compatible with all backends.
- CURSOR¶
Keyset (cursor) pagination. Performant at scale; requires a stable sort order with a tie-breaker.
- class loom.core.repository.abc.QuerySpec(filters=None, sort=<factory>, pagination=PaginationMode.OFFSET, limit=50, page=1, cursor=None)[source]¶
Bases:
objectStructured query contract for list operations.
Replaces the flat
FilterParamsdict with an explicit, type-safe representation. The repository implementation compiles this into backend-specific clauses at query time.- Parameters:
filters (FilterGroup | None) – Optional filter group applied to the query.
sort (tuple[SortSpec, ...]) – Ordered tuple of sort directives.
pagination (PaginationMode) – Pagination strategy. Defaults to
OFFSET.limit (int) – Maximum number of items per page (1-1000).
page (int) – 1-based page number (only for
OFFSETmode).cursor (str | None) – Opaque cursor token (only for
CURSORmode).
Example:
QuerySpec( filters=FilterGroup( filters=(FilterSpec("price", FilterOp.GTE, 10.0),), ), sort=(SortSpec("name"),), pagination=PaginationMode.OFFSET, limit=20, page=1, )
- class loom.core.repository.abc.RepoFor(*args, **kwargs)[source]¶
Bases:
Protocol[ModelT]Model-centric repository protocol used for UseCase dependency injection.
This protocol intentionally focuses on the generic CRUD surface used by most UseCases. Infrastructure bindings may provide a
RepositorySQLAlchemyinstance or any compatible implementation.- async list_paginated(page_params, filter_params=None, profile='default')[source]¶
Fetch entities with pagination.
- Parameters:
page_params (PageParams)
filter_params (FilterParams | None)
profile (str)
- Return type:
PageResult[ModelT]
- async list_with_query(query, profile='default')[source]¶
Fetch entities using a structured query (offset or cursor mode).
- Parameters:
- Return type:
PageResult[ModelT] | CursorResult[ModelT]
- class loom.core.repository.abc.Repository(*args, **kwargs)[source]¶
Bases:
RepositoryRead[OutputT,IdT],RepositoryWrite[OutputT,CreateT,UpdateT,IdT],Protocol[OutputT,CreateT,UpdateT,IdT]Combined read-write repository protocol.
- class loom.core.repository.abc.RepositoryRead(*args, **kwargs)[source]¶
Bases:
Protocol[OutputT,IdT]Protocol for read-only repository operations (get by id and paginated listing).
- async get_by_id(obj_id, profile='default')[source]¶
Fetch a single entity by its primary key.
- Parameters:
obj_id (IdT) – Primary key of the entity.
profile (str) – Loading profile name for eager-load options.
- Returns:
The entity output struct, or
Noneif not found.- Return type:
OutputT | None
- async count()[source]¶
Return the total number of entities in the repository.
- Returns:
Total row count as an integer.
- Return type:
- async list_paginated(page_params, filter_params=None, profile='default')[source]¶
Fetch a paginated list of entities.
- Parameters:
page_params (PageParams) – Pagination parameters (page and limit).
filter_params (FilterParams | None) – Optional filter criteria.
profile (str) – Loading profile name for eager-load options.
- Returns:
A
PageResultwith the matching items and pagination metadata.- Return type:
PageResult[OutputT]
- async list_with_query(query, profile='default')[source]¶
Fetch entities using a structured
QuerySpec.Supports both offset and cursor pagination, structured filters, and explicit sort directives. The concrete return type depends on
query.pagination:PaginationMode.OFFSET→PageResultPaginationMode.CURSOR→CursorResult
- Parameters:
- Returns:
A
PageResultfor offset queries or aCursorResultfor cursor queries.- Return type:
PageResult[OutputT] | CursorResult[OutputT]
- class loom.core.repository.abc.RepositoryWrite(*args, **kwargs)[source]¶
Bases:
Protocol[OutputT,CreateT,UpdateT,IdT]Protocol for write repository operations (create, update, delete).
- async create(data)[source]¶
Persist a new entity.
- Parameters:
data (CreateT) – Creation payload struct.
- Returns:
The newly created entity output struct.
- Return type:
OutputT
- class loom.core.repository.abc.SortSpec(field, direction='ASC')[source]¶
Bases:
objectA single sort directive applied to a query.
- Parameters:
Example:
SortSpec(field="created_at", direction="DESC")
- loom.core.repository.abc.build_page_result(items, total_count, page_params)[source]¶
Construct a
PageResultfrom a list of items and pagination metadata.- Parameters:
items (list[OutputT]) – Entity output structs for the current page.
total_count (int) – Total number of matching entities across all pages.
page_params (PageParams) – The pagination parameters used for this query.
- Returns:
A populated
PageResultinstance.- Return type:
PageResult[OutputT]