ZCore LogoZCore
Api reference

BaseRouter

Comprehensive API reference for the BaseRouter class, RouteKey enumeration, lookup projections, and endpoint overrides.

BaseRouter automatically scaffolds 8 standard security-aware CRUD, search, and field-projected lookup endpoints (POST, GET, GET_ALL, SEARCH, LOOKUP, UPDATE, PATCH, DELETE) with primary key reflection, dependency injection, and specificity route sorting.

Class Definition

from zcore import BaseRouter, RouteKey

class BaseRouter(Generic[CreateSchemaType, UpdateSchemaType]):
    def __init__(self) -> None:
        ...

Class Attributes

These attributes configure the scaffolded endpoints in your subclass:

Prop

Type


RouteKey Enumeration

The RouteKey enum identifies standard HTTP endpoints managed by BaseRouter:

from zcore import RouteKey

Prop

Type


Dependency Methods

get_route_action

Retrieves the database/permission action name for a given route key using model.actions().

def get_route_action(self, route_key: RouteKey) -> str: ...
  • RouteKey.POST $\rightarrow$ model.actions().CREATE
  • RouteKey.GET $\rightarrow$ model.actions().VIEW
  • RouteKey.GET_ALL / RouteKey.SEARCH $\rightarrow$ model.actions().LISTVIEW
  • RouteKey.LOOKUP $\rightarrow$ model.actions().LOOKUP
  • RouteKey.UPDATE / RouteKey.PATCH $\rightarrow$ model.actions().UPDATE
  • RouteKey.DELETE $\rightarrow$ model.actions().DELETE

get_route_dependencies

Generates default route dependencies (authentication, authorization, logging, etc.). Subclasses can override this method to inject dynamic, runtime dependencies per operation.

def get_route_dependencies(self, route_key: RouteKey, action: str) -> list[Any]: ...

Prop

Type


Overridable Endpoint Handlers

Subclasses can override the core endpoint execution methods to customize response envelopes, inject custom query parameters, or set specialized HTTP status codes:

create_endpoint

async def create_endpoint(self, data_in: Any, service: BaseService) -> ResponseWrapper: ...

get_endpoint

async def get_endpoint(self, id: Any, service: BaseService) -> ResponseWrapper: ...

get_all_endpoint

async def get_all_endpoint(self, service: BaseService, pagination: Any = None) -> ResponseWrapper: ...

search_endpoint

async def search_endpoint(self, search_in: SearchRequest, service: BaseService) -> ResponseWrapper: ...

lookup_endpoint

async def lookup_endpoint(self, search_in: SearchRequest, service: BaseService) -> ResponseWrapper: ...

update_endpoint

async def update_endpoint(self, id: Any, data_in: Any, service: BaseService) -> ResponseWrapper: ...

patch_endpoint

async def patch_endpoint(self, id: Any, data_in: Any, service: BaseService) -> ResponseWrapper: ...

delete_endpoint

async def delete_endpoint(self, id: Any, service: BaseService, force: bool = False) -> ResponseWrapper: ...

Architectural Mechanisms

Intelligent Projection (_resolve_lookup_projections): When processing /lookup queries, BaseRouter introspects lookup_schema and maps declared properties directly to model column attributes. It builds a database load_only(*fields) projection, automatically incorporates the primary key, and configures relationship loaders (selectinload for collections, joinedload for scalars). Only attributes declared in the lookup schema are loaded from the database.

Route Specificity Sorting (_sort_routes): BaseRouter sorts registered routes using hierarchical specificity scoring. Static path segments (such as /search and /lookup) are prioritized and evaluated before dynamic parameterized paths (such as /{id}), preventing FastAPI route shadowing collisions.

On this page