Typegoose Adapter
Relevant source files
The following files were used as context for generating this wiki page:
- packages/query-typegoose/package.json
- packages/query-typegoose/tests/services/typegoose-query.service.spec.ts
- packages/query-typegoose/tests/services/typegoose-query-descriminated.service.spec.ts
- packages/query-typegoose/README.md
- examples/typegoose/src/app.module.ts
- examples/typegoose-discriminators/src/app.module.ts
- packages/query-typegoose/project.json
The Typegoose Adapter provides an implementation of the @ptc-org/nestjs-query-core interfaces using Typegoose and Mongoose. It allows developers to use Typegoose classes as the source of truth for database schemas while benefiting from the CRUD, filtering, and sorting capabilities of the nestjs-query ecosystem.
This adapter integrates with NestJS through the @m8a/nestjs-typegoose module to manage database connections and model injection. It supports standard CRUD operations, complex filtering, relation management, and MongoDB discriminators.
Sources: packages/query-typegoose/package.json, packages/query-typegoose/README.md, examples/typegoose/src/app.module.ts
Architecture and Core Service
The primary component is the TypegooseQueryService. This class extends the core query service logic to interact with Typegoose models. It handles the conversion of nestjs-query filters into Mongoose-compatible query objects.
Data Flow Overview
The following diagram shows how a query request moves from the service layer to the MongoDB database.
The service consumes a Typegoose model through dependency injection, typically using the @InjectModel decorator.
Sources: packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:32-42, packages/query-typegoose/package.json:28-33
CRUD Operations
The adapter supports the full suite of CRUD operations defined by the core library.
Query and Filtering
The service supports various operators to filter data. It converts these operators into Mongoose queries.
| Operator | Mongoose Equivalent | Description |
|---|---|---|
eq | $eq | Matches values that are equal to a specified value. |
neq | $ne | Matches all values that are not equal to a specified value. |
gt | $gt | Matches values that are greater than a specified value. |
gte | $gte | Matches values that are greater than or equal to a specified value. |
in | $in | Matches any of the values specified in an array. |
like | $regex | Performs a regex match (converted from SQL-like % syntax). |
is | Boolean check | Checks if a value is strictly true/false. |
Sources: packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:114-188
Creation and Updates
The service provides methods for single and bulk operations. It validates that IDs are not manually specified during creation to prevent conflicts with MongoDB's _id generation.
createOne(entity): Saves a single document.createMany(entities): Saves multiple documents in bulk.updateOne(id, update): Updates a document by its ID.updateMany(update, filter): Updates all documents matching the filter.
Sources: packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:318-364, packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:400-440
Relation Management
The Typegoose Adapter manages relations through both Mongoose references (ref) and virtuals.
Relation Operations Flow
This sequence diagram illustrates how the service handles adding a relation between two entities.
The service supports findRelation, queryRelations, addRelations, removeRelations, and setRelations. It explicitly rejects modification operations (add/set/remove) on virtual relations, as these must be managed via the underlying foreign key or reference field.
Sources: packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:480-530, packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:804-814
Support for Discriminators
The adapter handles MongoDB discriminators, allowing for inheritance patterns where multiple document types reside in the same collection.
Configuration
You configure discriminators using the NestjsQueryTypegooseModule.forFeature method by providing a typegooseClass and an array of discriminators.
NestjsQueryTypegooseModule.forFeature([
{
typegooseClass: TestEntity,
discriminators: [TestDiscriminatedEntity]
},
TestReference
])
Sources: packages/query-typegoose/tests/services/typegoose-query-descriminated.service.spec.ts:50-58, examples/typegoose-discriminators/src/app.module.ts:14-22
Discriminator Logic
The TypegooseQueryService correctly identifies the specific model for a discriminated type. Queries performed through a discriminated service automatically include the discriminator key (usually __t) to ensure only relevant documents are returned.
Sources: packages/query-typegoose/tests/services/typegoose-query-descriminated.service.spec.ts:114-125
Aggregations
The adapter supports Mongoose aggregations, including count, sum, avg, min, max, and groupBy.
| Feature | Description |
|---|---|
| Field Aggregation | Computes stats like avg or sum for specific numeric fields. |
| GroupBy | Groups results by a specific field before performing aggregations. |
| Relation Aggregation | Performs aggregations on related documents (e.g., counting sub-tasks for a todo item). |
Sources: packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:193-240, packages/query-typegoose/tests/services/typegoose-query.service.spec.ts:700-750
Conclusion
The Typegoose Adapter bridges the gap between Typegoose's class-based Mongoose modeling and the core query capabilities of nestjs-query. It provides a type-safe way to perform complex database operations, manage document relations, and handle advanced MongoDB features like discriminators and aggregations within a NestJS application.