Skip to main content

MongoSingletonService

import { MongoSingletonService } from '@opra/mongodb';

Hierarchy: ServiceBase → MongoService → MongoEntityService → MongoSingletonService


Constructor​

new MongoSingletonService<T>(dataType: Type | string, options?: MongoSingletonService.Options)

Properties​

PropertyTypeDescription
_idAnyIdThe 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.

OptionTypeDefaultDescription
_idAnyId655608925cad472b75fc6485The fixed document ID for the singleton record

CreateOptions​

Extends mongodb.InsertOneOptions.

OptionTypeDescription
projectionstring | string[] | Document | '*'Fields to return in the fetched-back document.

ExistsOptions​

Extends mongodb.CommandOperationOptions (minus session).

OptionTypeDescription
filterFilterInput<T>Additional filter criteria for the existence check.

FindOneOptions​

Extends mongodb.AggregateOptions.

OptionTypeDescription
filterFilterInput<T>Additional filter criteria.
projectionstring | string[] | Document | '*'Fields to include. '*' returns all fields.
sortstring[]Sort directives, e.g. ['name', '-createdAt'].
skipnumberNumber of documents to skip.
preStagesmongodb.Document[]Aggregation stages inserted before the filter stage.
postStagesmongodb.Document[]Aggregation stages appended after other stages.

UpdateOneOptions​

Extends mongodb.FindOneAndUpdateOptions (minus projection, returnDocument, includeResultMetadata).

OptionTypeDescription
projectionstring | string[] | Document | '*'Fields to return in the updated document.
filterFilterInput<T>Additional filter criteria for the update.
initArrayFieldsstring[]Array field names to initialize as [] if they don't exist before the update.

DeleteOptions​

Extends mongodb.DeleteOptions.

OptionTypeDescription
filterFilterInput<T>Additional filter criteria for the delete.