Skip to content

service

A generic service object implementation.

Service object is generic on the domain model type.

RepositoryService object is generic on the domain model type which should be a SQLAlchemy model.

RepositoryService

RepositoryService(**repo_kwargs)

Bases: Service[ModelT], Generic[ModelT]

Service object that operates on a repository object.

Parameters:

Name Type Description Default
**repo_kwargs Any

passed as keyword args to repo instantiation.

{}

create async

create(data)

Wrap repository instance creation.

Parameters:

Name Type Description Default
data ModelT

Representation to be created.

required

Returns:

Type Description
ModelT

Representation of created instance.

delete async

delete(id_)

Wrap repository delete operation.

Parameters:

Name Type Description Default
id_ Any

Identifier of instance to be deleted.

required

Returns:

Type Description
ModelT

Representation of the deleted instance.

get async

get(id_)

Wrap repository scalar operation.

Parameters:

Name Type Description Default
id_ Any

Identifier of instance to be retrieved.

required

Returns:

Type Description
ModelT

Representation of instance with identifier id_.

list async

list(*filters, **kwargs)

Wrap repository scalars operation.

Parameters:

Name Type Description Default
*filters FilterTypes

Collection route filters.

()
**kwargs Any

Keyword arguments for attribute based filtering.

{}

Returns:

Type Description
list[ModelT]

The list of instances retrieved from the repository.

new classmethod async

new()

Context manager that returns instance of service object.

Handles construction of the database session.

Returns:

Type Description
AsyncIterator[RepoServiceT]

The service object instance.

update async

update(id_, data)

Wrap repository update operation.

Parameters:

Name Type Description Default
id_ Any

Identifier of item to be updated.

required
data ModelT

Representation to be updated.

required

Returns:

Type Description
ModelT

Updated representation.

upsert async

upsert(id_, data)

Wrap repository upsert operation.

Parameters:

Name Type Description Default
id_ Any

Identifier of the object for upsert.

required
data ModelT

Representation for upsert.

required

Returns:

Type Description
ModelT

Updated or created representation.

Service

Bases: Generic[T]

Generic Service object.

__init_subclass__

__init_subclass__(*_, **__)

Map the service object to a unique identifier.

Important that the id is deterministic across running application instances, e.g., using something like hash() or id() won't work as those would be different on different instances of the running application. So we use the full import path to the object.

create async

create(data)

Create an instance of T.

Parameters:

Name Type Description Default
data T

Representation to be created.

required

Returns:

Type Description
T

Representation of created instance.

delete async

delete(id_)

Delete T that is identified by id_.

Parameters:

Name Type Description Default
id_ Any

Identifier of instance to be deleted.

required

Returns:

Type Description
T

Representation of the deleted instance.

enqueue_background_task async

enqueue_background_task(method_name, job_config=None, **kwargs)

Enqueue an async callback for the operation and data.

Parameters:

Name Type Description Default
method_name str

Method on the service object that should be called by the async worker.

required
job_config JobConfig | None

Configuration object to control the job that is enqueued.

None
**kwargs Any

Arguments to be passed to the method when called. Must be JSON serializable.

{}

get async

get(id_)

Retrieve a representation of T with that is identified by id_

Parameters:

Name Type Description Default
id_ Any

Identifier of instance to be retrieved.

required

Returns:

Type Description
T

Representation of instance with identifier id_.

list async

list(**kwargs)

Return view of the collection of T.

Parameters:

Name Type Description Default
**kwargs Any

Keyword arguments for filtering.

{}

Returns:

Type Description
list[T]

The list of instances retrieved from the repository.

new classmethod async

new()

Context manager that returns instance of service object.

Returns:

Type Description
AsyncIterator[ServiceT]

The service object instance.

update async

update(id_, data)

Update existing instance of T with data.

Parameters:

Name Type Description Default
id_ Any

Identifier of item to be updated.

required
data T

Representation to be updated.

required

Returns:

Type Description
T

Updated representation.

upsert async

upsert(id_, data)

Create or update an instance of T with data.

Parameters:

Name Type Description Default
id_ Any

Identifier of the object for upsert.

required
data T

Representation for upsert.

required

Returns:

Type Description
T

Updated or created representation.

make_service_callback async

make_service_callback(_ctx, *, service_type_id, service_method_name, **kwargs)

Make an async service callback.

Parameters:

Name Type Description Default
_ctx Context

the SAQ context

required
service_type_id str

Value of __id__ class var on service type.

required
service_method_name str

Method to be called on the service object.

required
**kwargs Any

Unpacked into the service method call as keyword arguments.

{}