Skip to content

CRUD Router

Generate and register CRUD routes for a :class:~jetio.orm.JetioModel.

CrudRouter builds standard REST endpoints for a given model and attaches them to a Jetio application via :meth:register_routes.

The router is designed to be low-boilerplate while still supporting: relationship eager-loading, optional authentication/authorization, and server-controlled audit/ownership fields.

Examples:

Basic usage:

app = Jetio()
CrudRouter(User).register_routes(app)

Load relationships on GET routes:

CrudRouter(User, load_relationships=["posts", "profile"]).register_routes(app)

Secure all routes with a single dependency:

CrudRouter(
    User,
    secure=True,
    auth_dependency=get_current_user,
).register_routes(app)

Method-specific security (policy):

CrudRouter(
    User,
    secure=True,
    policy={
        "GET": get_current_user,
        "POST": get_current_user,
        "PUT": owner_or_admin,
        "DELETE": owner_or_admin,
    },
).register_routes(app)
See Also
  • :class:~jetio.orm.JetioModel
  • :func:~jetio.security.resolve_audit_field

__init__(model: JetioModel, load_relationships: Optional[List[str]] = None, exclude_methods: Optional[List[str]] = None, secure: bool = False, auth_dependency: Optional[Callable] = None, audit_fields: Optional[List[str]] = None, policy: Optional[Dict[str, Callable]] = None)

Create a CRUD router for a model.

Parameters:

Name Type Description Default
model JetioModel

A model class inheriting from :class:~jetio.orm.JetioModel. The model must expose Pydantic schema attributes (e.g. __pydantic_read_model__ and __pydantic_create_model__) as expected by Jetio.

required
load_relationships Optional[List[str]]

Relationship attribute names to eager-load on GET routes using SQLAlchemy selectinload.

None
exclude_methods Optional[List[str]]

HTTP methods to skip, e.g. ["DELETE"] or ["POST", "PUT"]. Values are normalized to uppercase.

None
secure bool

If True, the registered routes will require authentication via dependency injection.

False
auth_dependency Optional[Callable]

Base dependency used when secure=True and no method-level policy exists for a request method (e.g. get_current_user).

None
audit_fields Optional[List[str]]

Ordered list of field names to treat as audit/ownership fields. When secure, these fields are removed from the create schema and set server-side to user.id for the resolved audit field. Defaults to :data:~jetio.security.DEFAULT_AUDIT_FIELDS.

None
policy Optional[Dict[str, Callable]]

Optional mapping of HTTP method -> dependency callable. This overrides auth_dependency on a per-method basis. Example: {"PUT": owner_check, "DELETE": owner_check}.

None

Raises:

Type Description
ValueError

If secure=True and neither auth_dependency is provided nor the policy covers all enabled methods (after exclude_methods).

register_routes(app, prefix: str = '')

Register CRUD routes on a Jetio application.

The base path is computed as::

/{prefix}/{model.__tablename__}

where prefix is optional and normalized to avoid double slashes.

Routes registered (unless excluded): - GET {base} - POST {base} - GET {base}/{item_id:int} - PUT {base}/{item_id:int} - DELETE {base}/{item_id:int}

Security behavior
  • If secure=False (default), routes are public.
  • If secure=True, each handler uses dependency injection: Depends(_dep_for(METHOD)).
  • If the resolved dependency returns a falsy value, the route returns 401 {"error": "Authentication required"}.

Parameters:

Name Type Description Default
app

Jetio application instance (must expose route(...) decorator).

required
prefix str

Optional URL prefix (e.g. "/v1").

''

Returns:

Type Description

None