SqbCollectionService
import { SqbCollectionService } from '@opra/sqb';
Hierarchy: ServiceBase → SqbServiceBase → SqbEntityService → SqbCollectionService
Constructor
new SqbCollectionService<T>(dataType: Type<T> | string, options?: SqbCollectionService.Options)
Properties
| Property | Type | Description |
|---|---|---|
defaultLimit | number | Maximum records returned by findMany when no limit option is given (default: 100) |
Methods
assert
assert(id: SQBAdapter.IdOrIds, options?: ExistsOptions): Promise<void>
Throws ResourceNotAvailableError if no record with the given ID exists.
options — ExistsOptions
create
create(input: PartialDTO<T>, options: RequiredSome<CreateOptions, 'projection'>): Promise<PartialDTO<T>>
create(input: PartialDTO<T>, options?: CreateOptions): Promise<T>
Inserts a new record and fetches it back. If projection is supplied the return type narrows to PartialDTO<T>.
options — CreateOptions
createOnly
createOnly(input: PartialDTO<T>, options?: CreateOptions): Promise<void>
Inserts a new record without returning it.
options — CreateOptions
count
count(options?: CountOptions): Promise<number>
Returns the number of records matching the filter.
options — CountOptions
delete
delete(id: SQBAdapter.IdOrIds, options?: DeleteOptions): Promise<number>
Removes the record with the given ID. Returns the number of deleted records.
options — DeleteOptions
deleteMany
deleteMany(options?: DeleteManyOptions): Promise<number>
Removes all records matching the filter. Returns the number of deleted records.
options — DeleteManyOptions
exists
exists(id: SQBAdapter.IdOrIds, options?: ExistsOptions): Promise<boolean>
Returns true if a record with the given ID exists.
options — ExistsOptions
existsOne
existsOne(options?: ExistsOptions): Promise<boolean>
Returns true if at least one record matches the filter.
options — ExistsOptions
findById
findById(id: SQBAdapter.IdOrIds, options: RequiredSome<FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>
findById(id: SQBAdapter.IdOrIds, options?: FindOneOptions): Promise<T | undefined>
Fetches a record by ID. Returns undefined if not found.
options — FindOneOptions
findOne
findOne(options: RequiredSome<FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>
findOne(options?: FindOneOptions): Promise<T | undefined>
Fetches the first record matching the filter. Returns undefined if not found.
options — FindOneOptions
findMany
findMany(options: RequiredSome<FindManyOptions, 'projection'>): Promise<PartialDTO<T>[]>
findMany(options?: FindManyOptions): Promise<T[]>
Fetches a list of records. Applies defaultLimit when options.limit is not specified.
options — FindManyOptions
findManyWithCount
findManyWithCount(options: RequiredSome<FindManyOptions, 'projection'>): Promise<{ count: number; items: PartialDTO<T>[] }>
findManyWithCount(options?: FindManyOptions): Promise<{ count: number; items: T[] }>
Same as findMany but also returns the total count of matching records before pagination.
options — FindManyOptions
get
get(id: SQBAdapter.IdOrIds, options: RequiredSome<FindOneOptions, 'projection'>): Promise<PartialDTO<T>>
get(id: SQBAdapter.IdOrIds, options?: FindOneOptions): Promise<T>
Fetches a record by ID. Throws ResourceNotAvailableError if not found.
options — FindOneOptions
update
update(id: SQBAdapter.IdOrIds, input: PatchDTO<T, SqlElement>, options: RequiredSome<UpdateOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>
update(id: SQBAdapter.IdOrIds, input: PatchDTO<T, SqlElement>, options?: UpdateOneOptions): Promise<T | undefined>
Applies a partial update to the record with the given ID and returns the updated record. Returns undefined if not found.
options — UpdateOneOptions
updateOnly
updateOnly(id: SQBAdapter.IdOrIds, input: PatchDTO<T, SqlElement>, options?: UpdateOneOptions): Promise<number>
Same as update but does not fetch and return the updated record. Returns the number of matched records.
options — UpdateOneOptions
updateMany
updateMany(input: PatchDTO<T, SqlElement>, options?: UpdateManyOptions): Promise<number>
Applies a partial update to all records matching the filter. Returns the number of matched records.
options — UpdateManyOptions
Interfaces
SqbCollectionService.Options
Extends SqbEntityService.Options.
| Option | Type | Default | Description |
|---|---|---|---|
defaultLimit | number | 100 | Default maximum number of records returned by findMany |
CreateOptions
Extends Repository.CreateOptions.
| Option | Type | Description |
|---|---|---|
projection | string | string[] | Fields to return in the fetched-back record. Omit for all fields. |
CountOptions
| Option | Type | Description |
|---|---|---|
filter | SQBAdapter.FilterInput | Filter expression — operator function, plain object, or OPRA filter string. |
DeleteOptions
| Option | Type | Description |
|---|---|---|
filter | SQBAdapter.FilterInput | Additional filter expression. |
DeleteManyOptions
| Option | Type | Description |
|---|---|---|
filter | SQBAdapter.FilterInput | Filter expression — only matching records are deleted. |
ExistsOptions
| Option | Type | Description |
|---|---|---|
filter | SQBAdapter.FilterInput | Additional filter expression for the existence check. |
FindOneOptions
Same as FindManyOptions without the limit and skip properties.
FindManyOptions
| Option | Type | Description |
|---|---|---|
filter | SQBAdapter.FilterInput | Filter expression. |
projection | string | string[] | Fields to include. |
sort | string[] | Sort directives, e.g. ['name', '-createdAt'] (prefix - for descending). |
limit | number | Maximum number of records to return. Defaults to defaultLimit. |
skip | number | Number of records to skip before returning results. |
UpdateOneOptions
| Option | Type | Description |
|---|---|---|
projection | string | string[] | Fields to return in the updated record. |
filter | SQBAdapter.FilterInput | Additional filter criteria for the update. |
UpdateManyOptions
| Option | Type | Description |
|---|---|---|
filter | SQBAdapter.FilterInput | Filter expression — only matching records are updated. |