Search code examples
nestjsnest

Nestjs Nested array of object dynamically key


How create a structure dto for this structure of data and CRUD operation?

enter image description here


Solution

  • You can just create user.entity.ts. You didnt specify to what db you use, but ı just think you are using mongodb and in mongodb you can just create models like that. then

    import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
    import mongoose, { Document } from "mongoose";
    
    export type UserDocument = User & Document;
    
    @Schema()
    export class User {
      @Prop({type:mongoose.Types.ObjectId})
      id: string;
    
      @Prop({maxlength:50})
      name: string;
      
      @Prop()
      phone: string;
    
      ...
    }
    
    
    export const UserSchema = SchemaFactory.createForClass(User);