Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

nestjs mongoose schema nested

// user.schema.ts
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type UserDocument = User & Document;

@Schema()
class NestedData {
  @Prop({ type: String })
  foo: string;
  
  @Prop({ type: Number })
  bar: number;
}

@Schema()
export class User {
  @Prop({ type: String, unique: true })
  username: string;
  
  @Prop({ type: String })
  password: string;

  @Prop({ type: NestedData })
  data: NestedData;
}

export const UserSchema = SchemaFactory.createForClass(User);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #nestjs #mongoose #schema #nested
ADD COMMENT
Topic
Name
2+3 =