Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
3 Hrs
55 Min
26 Seg

C贸mo tipar relaciones uno a muchos embebidas

21/24

Reading

In embedded relationships we use the following two forms:

...

Register or login to read the rest of the content.

Contributions 3

Questions 4

Sort by:

Want to see more contributions, questions and answers from the community?

馃憟 En esta lectura est谩 la soluci贸n de como lograr las relaciones embebidas incluyendo el tipado.

Para que Swagger se muestre correctamente se debe agregar:

  @ApiProperty({ type: () => [CreateSubDocDto]}) 
  readonly subDocs: CreateSubDocDto[]; 

De igual manera me parece que los tipos de las propiedades deben ser string en lugar de String, sino Swagger los muestra como objetos.

readonly description: string;

Les dejo el codigo para la solucion de la clase anterior:

// src/users/entities/skills.entity.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

@Schema()
export class Skill {
  @Prop()
  name: string;
  @Prop()
  color: string;
}
export const SkillSchema = SchemaFactory.createForClass(Skill);
// src/users/dtos/skills.dto.ts
import { IsNotEmpty, IsString } from 'class-validator';
import { PartialType } from '@nestjs/swagger';

export class CreateSkillDto {
  @IsString()
  @IsNotEmpty()
  readonly name: string;

  @IsString()
  @IsNotEmpty()
  readonly color: string;
}
export class UpdateSkillDto extends PartialType(CreateSkillDto) {}

Implementacion en el dto de customers

import { CreateSkillDto } from './skills.dto';
export class CreateCustomerDto {
  ...
  @IsArray()
  @IsNotEmpty()
  @ValidateNested({ each: true })
  @Type(() => CreateSkillDto)
  readonly skills: CreateSkillDto[];
}

y en la entidad de customers

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { Skill, SkillSchema } from './skills.entity';

@Schema()
export class Customer extends Document {
  @Prop({ required: true, type: String })
  name: string;
  @Prop({ required: true, type: String })
  email: string;
  @Prop({
    type: [SkillSchema],
  })
  skills: Types.Array<Skill>;
}
export const CustomerSchema = SchemaFactory.createForClass(Customer);