MongoAdapter
MongoAdapter is a namespace that provides types and utility functions for integrating MongoDB with OPRA. It translates OPRA filter expressions, projections, and sort descriptors into MongoDB-native structures, and can parse an HTTP ExecutionContext into a ready-to-use MongoDB operation descriptor.
import { MongoAdapter } from '@opra/mongodb';
Types
MongoAdapter.AnyId
type AnyId = string | number | ObjectId;
Any value that can serve as a MongoDB document identifier.
MongoAdapter.FilterInput<T>
type FilterInput<T = any> =
| OpraFilter.Expression
| mongodb.Filter<T>
| string
| undefined;
Accepted filter forms — an OPRA filter expression object, a raw MongoDB filter, an OPRA filter string, or undefined (no filter).
MongoAdapter.TransformedRequest
Result of parseRequest. Carries the operation name and its parameters ready for service calls.
| Property | Type | Description |
|---|---|---|
method | 'create' | 'delete' | 'deleteMany' | 'get' | 'findMany' | 'replace' | 'update' | 'updateMany' | The resolved operation |
key | any | The document key, if the operation targets a single document |
data | any | The request body, for create / update / replace operations |
options | any | Operation-specific options (filter, projection, sort, limit, skip…) |
Functions
MongoAdapter.prepareFilter
prepareFilter<T>(input: FilterInput<T> | FilterInput<T>[]): mongodb.Filter<T> | undefined
Converts one or more FilterInput values into a single MongoDB filter object. Multiple inputs are combined with $and. Returns undefined when the result would be empty.
MongoAdapter.prepareKeyValues
prepareKeyValues(id: AnyId, fields: string[]): mongodb.Filter<any>
Builds a MongoDB equality filter from an ID value and the field names that form the key. For a single-field key produces { fieldName: id }; for composite keys distributes values across the fields.
MongoAdapter.prepareProjection
prepareProjection(
dataType: ComplexType,
projection: string | string[] | mongodb.Document | '*' | undefined,
scope?: string,
): mongodb.Document | undefined
Translates an OPRA projection descriptor into a MongoDB $project document. Returns undefined when no projection is requested (all fields returned).
MongoAdapter.prepareSort
prepareSort(sort: string[]): mongodb.Sort | undefined
Converts OPRA sort descriptors (e.g. ['+name', '-createdAt']) into a MongoDB sort object. Returns undefined for an empty array.
MongoAdapter.parseRequest
parseRequest(context: ExecutionContext): Promise<TransformedRequest>
Parses an HTTP ExecutionContext produced by an @HttpOperation.Entity.* handler and returns a TransformedRequest ready to pass to a service method. Throws TypeError if the context transport is not 'http', and Error if the operation is not a recognised Entity operation.
const req = await MongoAdapter.parseRequest(ctx);
const result = await service.for(ctx)[req.method](req.key, req.data, req.options);