MongoSingletonService
import { MongoSingletonService } from '@opra/mongodb';
Hierarchy: ServiceBase → MongoService → MongoEntityService → MongoSingletonService
Constructor
new MongoSingletonService<T>(dataType: Type | string, options?: MongoSingletonService.Options)
Properties
| Property | Type | Description |
|---|---|---|
_id | AnyId | The fixed _id used for all operations |
Methods
All methods operate on the singleton document — no _id argument is accepted.
assert
assert(options?: ExistsOptions<T>): Promise<void>
Throws ResourceNotAvailableError if the singleton document does not exist.
options — ExistsOptions<T>
create
create(input: PartialDTO<T>, options: RequiredSome<CreateOptions, 'projection'>): Promise<PartialDTO<T>>
create(input: PartialDTO<T>, options?: CreateOptions): Promise<T>
Inserts the singleton document and returns it.
options — CreateOptions
createOnly
createOnly(input: PartialDTO<T>, options?: CreateOptions): Promise<T>
Inserts the singleton document without fetching it back.
options — CreateOptions
find
find(options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
find(options?: FindOneOptions<T>): Promise<T | undefined>
Fetches the singleton document. Returns undefined if not found.
options — FindOneOptions<T>
get
get(options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T>>
get(options?: FindOneOptions<T>): Promise<T>
Fetches the singleton document. Throws ResourceNotAvailableError if not found.
options — FindOneOptions<T>
exists
exists(options?: ExistsOptions<T>): Promise<boolean>
Returns true if the singleton document exists.
options — ExistsOptions<T>
update
update(input: MongoPatchDTO<T> | UpdateFilter<T>, options: RequiredSome<UpdateOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
update(input: MongoPatchDTO<T> | UpdateFilter<T>, options?: UpdateOneOptions<T>): Promise<T | undefined>
Applies a partial update to the singleton document and returns the updated document. Returns undefined if not found.
options — UpdateOneOptions<T>
updateOnly
updateOnly(input: MongoPatchDTO<T> | UpdateFilter<T>, options?: UpdateOneOptions<T>): Promise<number>
Same as update but does not fetch and return the updated document. Returns the number of matched documents.
options — UpdateOneOptions<T>
delete
delete(options?: DeleteOptions<T>): Promise<number>
Removes the singleton document. Returns the number of deleted documents.
options — DeleteOptions<T>
Interfaces
MongoSingletonService.Options
Extends MongoEntityService.Options → MongoService.Options.
| Option | Type | Default | Description |
|---|---|---|---|
_id | AnyId | 655608925cad472b75fc6485 | The fixed document ID for the singleton record |
CreateOptions
Extends mongodb.InsertOneOptions.
| Option | Type | Description |
|---|---|---|
projection | string | string[] | Document | '*' | Fields to return in the fetched-back document. |
ExistsOptions
Extends mongodb.CommandOperationOptions (minus session).
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Additional filter criteria for the existence check. |
FindOneOptions
Extends mongodb.AggregateOptions.
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Additional filter criteria. |
projection | string | string[] | Document | '*' | Fields to include. '*' returns all fields. |
sort | string[] | Sort directives, e.g. ['name', '-createdAt']. |
skip | number | Number of documents to skip. |
preStages | mongodb.Document[] | Aggregation stages inserted before the filter stage. |
postStages | mongodb.Document[] | Aggregation stages appended after other stages. |
UpdateOneOptions
Extends mongodb.FindOneAndUpdateOptions (minus projection, returnDocument, includeResultMetadata).
| Option | Type | Description |
|---|---|---|
projection | string | string[] | Document | '*' | Fields to return in the updated document. |
filter | FilterInput<T> | Additional filter criteria for the update. |
initArrayFields | string[] | Array field names to initialize as [] if they don't exist before the update. |
DeleteOptions
Extends mongodb.DeleteOptions.
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Additional filter criteria for the delete. |