MikroORM Adapter
Relevant source files
The following files were used as context for generating this wiki page:
The MikroORM Adapter provides persistence integration between @ptc-org/nestjs-query-core and the MikroORM library. It implements the standard QueryService interface, enabling automated CRUD operations, complex filtering, and relation management for MikroORM entities within a NestJS application.
Sources: packages/query-mikro-orm/package.json:3-5, packages/query-mikro-orm/src/services/mikro-orm-query.service.ts:18-20
Architecture and Components
The adapter consists of two primary components: the NestjsQueryMikroOrmModule for dependency injection and the MikroOrmQueryService for data persistence logic.
Module Configuration
NestjsQueryMikroOrmModule handles the registration of entities and their corresponding query services. The forFeature method accepts an array of entities or entity-DTO pairs to register services in the NestJS container. It exports MikroOrmModule to ensure entity repositories are available.
Sources: packages/query-mikro-orm/src/module.ts:11-20, packages/query-mikro-orm/tests/module.spec.ts:47-50
Query Service Logic
MikroOrmQueryService wraps a MikroORM EntityRepository. It translates generic Query objects (containing filters, sorting, and paging) into MikroORM-compatible options.
The diagram shows the data flow from a core query request through the service translation layer to the MikroORM repository.
Sources: packages/query-mikro-orm/src/services/mikro-orm-query.service.ts:25-30, packages/query-mikro-orm/tests/services/mikro-orm-query.service.spec.ts:31-35
Key Operations
The adapter supports standard persistence operations as defined by the core interfaces.
Retrieval Operations
- query: Retrieves multiple entities based on filters, sorting, and paging.
- getById: Retrieves a single entity by its primary key or throws an error if not found.
- findById: Returns an entity by ID or
undefinedif absent. - count: Returns the number of entities matching a filter.
Sources: packages/query-mikro-orm/src/services/mikro-orm-query.service.ts:33-70, packages/query-mikro-orm/tests/services/mikro-orm-query.service.spec.ts:127-145
Relation Management
The service provides methods to query and modify relations between entities.
| Method | Description |
|---|---|
findRelation | Finds a single related entity for one or more host entities. |
queryRelations | Queries a collection of related entities for one or more host entities. |
countRelations | Counts related entities based on a filter. |
addRelations | Adds many-to-many or one-to-many relations. |
setRelations | Overwrites existing relations with a new set. |
removeRelations | Removes specific relations. |
Sources: packages/query-mikro-orm/src/services/mikro-orm-query.service.ts:72-130, packages/query-mikro-orm/tests/services/mikro-orm-query.service.spec.ts:170-195
Filter Translation
The adapter translates core filter operators into MikroORM query syntax. It supports logical operators (and, or) and field-level comparisons.
| Core Operator | MikroORM Equivalent |
|---|---|
eq | $eq |
neq | $ne |
gt / gte | $gt / $gte |
lt / lte | $lt / $lte |
in / notIn | $in / $nin |
like / notLike | $like / $notLike |
is | null or value check |
Sources: packages/query-mikro-orm/tests/services/mikro-orm-query.service.spec.ts:40-85
Translation Sequence
When a query is executed, the service performs the following steps:
The sequence illustrates the internal translation process before calling the MikroORM repository.
Sources: packages/query-mikro-orm/src/services/mikro-orm-query.service.ts:33-45
Configuration and Dependencies
The package requires specific peer dependencies to function within a NestJS environment.
| Dependency | Version Requirement |
|---|---|
@mikro-orm/core | ^6.0.0 |
@mikro-orm/nestjs | ^6.0.0 |
@nestjs/common | ^10.0.0 or ^11.0.0 |
@ptc-org/nestjs-query-core | ^9.0.0 |
Sources: packages/query-mikro-orm/package.json:23-28
Feature Registration
Registration is performed using NestjsQueryMikroOrmModule.forFeature. If a DTO is provided, the service maps the repository results to the DTO type.
// Example registration
NestjsQueryMikroOrmModule.forFeature([
TestEntity,
{ entity: TestEntity, dto: TestEntityDTO }
])
Sources: packages/query-mikro-orm/tests/module.spec.ts:27-45
Implementation Details
Sorting
The adapter supports ASC and DESC directions. It also handles null sorting through SortNulls (e.g., NULLS_FIRST, NULLS_LAST), translating these into MikroORM order options.
Sources: packages/query-mikro-orm/tests/services/mikro-orm-query.service.spec.ts:110-125
Constraints
- count: The
countmethod does not support thewithDeletedoption in this implementation; attempting to use it throws an error. - Filter Structure: A filter object must contain either logical operators (
and/or) or field properties, but not both at the same level.
Sources: packages/query-mikro-orm/tests/services/mikro-orm-query.service.spec.ts:162-166, packages/query-mikro-orm/tests/services/mikro-orm-query.service.spec.ts:258-265
Summary
The MikroORM Adapter enables seamless integration of MikroORM entities into the nestjs-query ecosystem. It automates the translation of complex DTO-based queries into database-level MikroORM operations, providing a consistent API for CRUD and relationship management while maintaining compatibility with NestJS dependency injection patterns.