Skip to main content

SqbEntityService

SqbEntityService<T> is an abstract class that sits between SqbServiceBase and the concrete service classes. It adds the CRUD database primitives, the interceptor-aware command execution loop, and the lifecycle hooks used by SqbCollectionService and SqbSingletonService.

You do not extend it directly. Extend SqbCollectionService or SqbSingletonService instead.

import { SqbEntityService } from '@opra/sqb';

Hierarchy: ServiceBaseSqbServiceBaseSqbEntityService


Lifecycle hooks

Override these in SqbCollectionService or SqbSingletonService subclasses. The base implementations are no-ops.

HookSignature
_beforeCreate(command)(CreateCommand<T>) => Promise<void>
_afterCreate(command, result)(CreateCommand<T>, PartialDTO<T>) => Promise<void>
_beforeUpdate(command)(UpdateOneCommand<T>) => Promise<void>
_afterUpdate(command, result)(UpdateOneCommand<T>, PartialDTO<T> | undefined) => Promise<void>
_beforeUpdateMany(command)(UpdateManyCommand<T>) => Promise<void>
_afterUpdateMany(command, affected)(UpdateManyCommand<T>, number) => Promise<void>
_beforeDelete(command)(DeleteOneCommand) => Promise<void>
_afterDelete(command, affected)(DeleteOneCommand, number) => Promise<void>
_beforeDeleteMany(command)(DeleteManyCommand) => Promise<void>
_afterDeleteMany(command, affected)(DeleteManyCommand, number) => Promise<void>

Interfaces

SqbEntityService.Options

Extends SqbServiceBase.Options.

OptionTypeDescription
resourceNamestring | ((_this) => string)Resource name used in error messages and API metadata
scopestringComma-delimited scopes used to filter the API document
commonFilterCommonFilter | CommonFilter[]Filter(s) automatically applied to every read and write operation
interceptor(next, command, _this) => Promise<any>Wraps every operation — useful for logging, tracing, or access control
onError(error, command, _this) => void | Promise<void>Called whenever a command throws

SqbEntityService.CrudOp

type CrudOp = 'create' | 'read' | 'update' | 'delete';

SqbEntityService.CommandInfo

Passed to every lifecycle hook and interceptor.

PropertyTypeDescription
crudCrudOpThe CRUD category of the operation
methodstringThe method name (e.g. 'findMany', 'update')
byIdbooleanWhether the operation targets a specific record by ID
documentIdIdOrIdsThe record identifier, if applicable
inputanyThe input payload, if applicable
optionsanyThe operation options

SqbEntityService.CommonFilter

type CommonFilter =
| SQBAdapter.FilterInput
| ((args: CommandInfo, _this: SqbEntityService<any>) =>
SQBAdapter.FilterInput | Promise<SQBAdapter.FilterInput> | undefined);

A static filter or a function computed per request. Applied automatically to every operation alongside any method-level filter option.


Command interfaces

Command objects are passed to lifecycle hooks. Each interface extends CommandInfo and narrows the crud discriminant.

CreateCommand<T>

PropertyType
crud'create'
inputPatchDTO<T>
optionsCreateOptions

CountCommand

PropertyType
crud'read'
optionsCountOptions

DeleteOneCommand

PropertyType
crud'delete'
documentIdIdOrIds
optionsDeleteOptions

DeleteManyCommand

PropertyType
crud'delete'
optionsDeleteManyOptions

ExistsCommand

PropertyType
crud'read'
documentIdIdOrIds (optional)
optionsExistsOptions

FindOneCommand

PropertyType
crud'read'
documentIdIdOrIds (optional)
optionsFindOneOptions

FindManyCommand

PropertyType
crud'read'
optionsFindManyOptions

UpdateOneCommand<T>

PropertyType
crud'update'
documentIdIdOrIds (optional)
inputPatchDTO<T, SqlElement>
optionsUpdateOneOptions

UpdateManyCommand<T>

Same shape as UpdateOneCommand<T> but without documentId.