Skip to content

Commit 073b4dd

Browse files
committed
docs(typegoose): add section about discriminators
1 parent ab29f94 commit 073b4dd

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

documentation/docs/persistence/typegoose/relations.mdx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,85 @@ export class TagDTO {
401401
</TabItem>
402402
</Tabs>
403403

404+
405+
## Discriminators
406+
407+
Typegoose supports mongoose [discriminators](https://mongoosejs.com/docs/discriminators.html). `nestjs-query` provides support for them through the `NestjsQueryTypegooseModule`.
408+
409+
To use discriminators you need to define them in your `NestjsQueryTypegooseModule.forFeature` call.
410+
411+
When working with discriminators, `nestjs-query` requires you to provide a service class for each discriminated entity. This is necessary for the auto-generated resolvers to be created correctly. These service classes are also the perfect place to add any custom business logic for your discriminated entities.
412+
413+
```ts title="todo-item/todo-item.module.ts"
414+
import { NestjsQueryGraphQLModule } from '@ptc-org/nestjs-query-graphql';
415+
import { NestjsQueryTypegooseModule, TypegooseQueryService } from '@ptc-org/nestjs-query-typegoose';
416+
import { Module } from '@nestjs/common';
417+
import { TodoItemEntity } from './entities/todo-item.entity';
418+
import { TodoTaskEntity } from './entities/todo-task.entity';
419+
import { TodoAppointmentEntity } from './entities/todo-appointment.entity';
420+
import { TodoItemDTO } from './dto/todo-item.dto';
421+
import { TodoTaskDTO } from './dto/todo-task.dto';
422+
import { TodoAppointmentDTO } from './dto/todo-appointment.dto';
423+
import { CreateTodoTaskInput } from './dto/create-todo-task.input';
424+
import { CreateTodoAppointmentInput } from './dto/create-todo-appointment.input';
425+
import { InjectModel } from '@m8a/nestjs-typegoose';
426+
import { ReturnModelType } from '@typegoose/typegoose';
427+
428+
const typegooseModule = NestjsQueryTypegooseModule.forFeature([
429+
{
430+
typegooseClass: TodoItemEntity,
431+
discriminators: [
432+
{ typegooseClass: TodoTaskEntity },
433+
{ typegooseClass: TodoAppointmentEntity }
434+
]
435+
}
436+
]);
437+
438+
439+
class TodoTaskEntityService extends TypegooseQueryService<TodoTaskEntity> {
440+
constructor(@InjectModel(TodoTaskEntity) readonly model: ReturnModelType<typeof TodoTaskEntity>) {
441+
super(model)
442+
}
443+
}
444+
445+
class TodoAppointmentService extends TypegooseQueryService<TodoAppointmentEntity> {
446+
constructor(@InjectModel(TodoAppointmentEntity) readonly model: ReturnModelType<typeof TodoAppointmentEntity>) {
447+
super(model)
448+
}
449+
}
450+
451+
@Module({
452+
imports: [
453+
NestjsQueryGraphQLModule.forFeature({
454+
imports: [typegooseModule],
455+
resolvers: [
456+
{
457+
DTOClass: TodoItemDTO,
458+
EntityClass: TodoItemEntity,
459+
create: {disabled: true}, // Disable create for the base entity
460+
update: {disabled: true} // Disable update for the base entity
461+
},
462+
{
463+
DTOClass: TodoTaskDTO,
464+
EntityClass: TodoTaskEntity,
465+
// Tell the resolver to use our custom input for the create operation
466+
CreateDTOClass: CreateTodoTaskInput,
467+
ServiceClass: TodoTaskEntityService
468+
},
469+
{
470+
DTOClass: TodoAppointmentDTO,
471+
EntityClass: TodoAppointmentEntity,
472+
// Tell the resolver to use our custom input for the create operation
473+
CreateDTOClass: CreateTodoAppointmentInput,
474+
ServiceClass: TodoAppointmentService
475+
}
476+
],
477+
services: [
478+
TodoTaskEntityService,
479+
TodoAppointmentService
480+
]
481+
})
482+
]
483+
})
484+
export class TodoItemModule {}
485+
```

0 commit comments

Comments
 (0)