Mongoose Adapter
Relevant source files
The following files were used as context for generating this wiki page:
The Mongoose Adapter provides a persistence layer for the nestjs-query framework by implementing the core QueryService interface using Mongoose. It allows developers to perform CRUD operations, complex filtering, sorting, paging, and relationship management on MongoDB databases within a NestJS application.
This adapter integrates with @ptc-org/nestjs-query-core to provide a standardized way to query data, which can be used independently or in conjunction with the GraphQL resolvers provided by @ptc-org/nestjs-query-graphql.
Sources: packages/query-mongoose/README.md, README.md:32-34
Core Architecture
The Mongoose adapter is built around the MongooseQueryService class. This service wraps a Mongoose Model and translates nestjs-query primitives into Mongoose queries and aggregation pipelines.
Components and Dependencies
The package relies on specific peer dependencies to interact with the database and the NestJS framework.
| Dependency | Purpose |
|---|---|
@nestjs/mongoose | Provides NestJS integration for Mongoose |
mongoose | The underlying MongoDB object modeling tool |
@ptc-org/nestjs-query-core | Defines the QueryService interface and query types |
Sources: packages/query-mongoose/package.json:23-27, packages/query-mongoose/README.md
Service Data Flow
The following diagram illustrates how the MongooseQueryService processes a request to retrieve data with filters.
The service translates standardized filter operators (like eq, gt, in) into Mongoose-specific query syntax.
Sources: packages/query-mongoose/tests/services/mongoose-query.service.spec.ts:88-165
Supported Operations
The MongooseQueryService implements the full suite of CRUD and metadata operations defined by the core framework.
Querying and Filtering
The adapter supports standard comparison and logical operators. Key implementations include:
- Basic Comparisons:
eq,neq,gt,gte,lt,lte. - Set Operations:
in,notIn. - Boolean Checks:
is,isNot. - String Matching:
like,notLike,iLike,notILike(translated to RegEx).
Sources: packages/query-mongoose/tests/services/mongoose-query.service.spec.ts:88-165
Aggregations
The adapter uses Mongoose aggregation pipelines to perform calculations across datasets. It supports count, sum, avg, min, max, and groupBy operations.
Relationship Management
The Mongoose adapter handles both document-embedded references and virtual references.
Reference Operations
The service provides methods to manage associations between models:
- findRelation: Retrieves a single related document.
- queryRelations: Retrieves multiple related documents with support for paging and sorting.
- addRelations / removeRelations: Modifies array-based references in a document.
- setRelation / removeRelation: Modifies a single reference field.
Virtual Relations
The adapter specifically supports virtual references, though it restricts certain modifications. For example, addRelations is not supported for virtual relations because they are typically derived from other collections rather than stored as an array of IDs on the document itself.
Sources: packages/query-mongoose/tests/services/mongoose-query.service.spec.ts:405-779
Configuration and Usage
To use the adapter, you must register the NestjsQueryMongooseModule and provide schemas for your documents.
Module Setup
In the application module, you configure the database connection and the features (schemas) you wish to use with the service.
// examples/mongoose/src/app.module.ts:16-29
@Module({
imports: [
MongooseModule.forRoot(uri, options),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: 'examples/mongoose/schema.gql',
// ...
}),
SubTaskModule,
TodoItemModule,
TagModule
]
})
export class AppModule {}
Sources: examples/mongoose/src/app.module.ts:16-29, packages/query-mongoose/tests/services/mongoose-query.service.spec.ts:45-56
Service Implementation
Custom services extend MongooseQueryService and inject the Mongoose model using the @InjectModel decorator.
// Example derived from tests
class TestEntityService extends MongooseQueryService<TestEntity> {
constructor(@InjectModel(TestEntity.name) readonly model: Model<TestEntity>) {
super(model)
}
}
Sources: packages/query-mongoose/tests/services/mongoose-query.service.spec.ts:32-37
Development and Testing
The package includes a comprehensive test suite using mongodb-memory-server for isolated unit and integration testing.
- Test Location:
packages/query-mongoose/__tests__/ - Build Target:
query-mongoose(defined inproject.json) - Linting: Uses
@nx/eslint:lint - Testing: Uses
@nx/jest:jestwith a dedicatedjest.config.ts
Sources: packages/query-mongoose/project.json:7-31, AGENTS.md:14-25
Summary
The Mongoose Adapter acts as a bridge between MongoDB and the nestjs-query ecosystem. It automates the creation of complex MongoDB queries and aggregations, providing a consistent API for data access regardless of the underlying persistence mechanism. By handling both standard Mongoose models and virtual relationships, it allows for flexible schema designs while maintaining type safety and standard query patterns.