How to write down nested schemas for mongoose using NestJS nomenclature

Viewed 2130

I am learning NestJS, and Mongoose. I was wondering how to write down/code nested schemas in Mongoose using NextJS nomenclature.

Incoming data structure looks like this -

{
    something: {
        info: {
            title: string,
            score: number,
            description: string,
            time: string,
            DateOfCreation: string
        },
        Store: {
            item: {
                question: string,
                options: {
                    item: {
                        answer: string,
                        description: string,
                        id: string,
                        key: string,
                        option: string
                    }
                }
            }
        }
    }
}

Challenge: writing down that using NestJS internal APIs.

I am using NestJS and Mongoose. I want to write a schema for the data structure given above. I can't find examples for nested schemas. Any insigth is welcome.

I am a beginner in all NestJS, Mongoose and MongoDB. So please don't assume that I know something. Thus, any insight on Mongoose as well is welcome.

Thanks a lot.

Edit - Here's something I came up with after following this SO post - Mongoose Subdocuments in Nest.js . But I am just throwing stones in the dark.

import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";

@Schema()
export class Cat {
    @Prop()
    name: string
}


export const CatSchema = SchemaFactory.createForClass(Cat);


@Schema()
class testInfo {
    @Prop()
    title: string;
    @Prop()
    score: number;
    @Prop()
    description: string;
    @Prop()
    time: string;
    @Prop()
    DateOfCreation: string;
}

const testInfoSchema = SchemaFactory.createForClass(testInfo);

@Schema()
class OptionContent {
    @Prop()
    answer: string;
    @Prop()
    description: string;
    @Prop()
    id: string;
    @Prop()
    key: string;
    @Prop()
    option: string
}
const OptionContentSchema = SchemaFactory.createForClass(OptionContent);

@Schema({ strict: false })
class Option {
    @Prop({ type: OptionContentSchema })
    item: OptionContent;
}

const OptionSchema = SchemaFactory.createForClass(Option);

@Schema({ strict: false })
class Page {
    @Prop()
    question: string;
    @Prop({ type: OptionSchema })
    options: Option;
}

const PageSchema = SchemaFactory.createForClass(Page);

@Schema({ strict: false })
class McqStore {
    @Prop({ type: PageSchema })
    item: Page;
}

const McqStoreSchema = SchemaFactory.createForClass(McqStore);

@Schema()
export class Test {
    @Prop({ type: testInfoSchema })
    info: testInfo

    @Prop({ type: McqStoreSchema })
    McqStore: McqStore
}

const TestSchema = SchemaFactory.createForClass(Test);

@Schema()
export class TestContainer {
    @Prop({ type: TestSchema })
    name: Test
}

export const TestContainerSchema = SchemaFactory.createForClass(TestContainer);

export type userDocument = TestContainer & Document;
1 Answers

nice question, it sparked my curiosity!

I am not new to Mongoose, but new to NestJS, learning to use. I was able to make this run properly.

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type CatDocument = Cat & Document;


@Schema()
export class Owners {
  @Prop()
  names: [string];
}

@Schema()
export class Cat {
  @Prop()
  name: string;

  @Prop()
  age: number;

  @Prop()
  breed: string;

  @Prop()
  owners: Owners;//schema for owner
}

export const CatSchema = SchemaFactory.createForClass(Cat);

Result! It seems to solve your problem, if I understand properly. Please, you need to adapt, and I am not sure about all this levels you added, you will have to experiment!

On your sample, you "compiled" the subschemas, if my memory serves me well, you do not do that even in Mongoose using express! I have to double check that! For NestJS, it seems, no need for that!

enter image description here

If you want something like: Mongoose Subdocuments in Nest.js, that is another story. My concern on this approach is using populate, I know how to use on Express, but no idea on NestJS.

I have found their documentation quite rich, and they have a repository as well. See here: https://docs.nestjs.com/techniques/mongodb

Update

user3399180 8 nicely sent me his final answer by e-mail!!

import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { Document } from "mongoose";

@Schema()
class testInfo {
    @Prop()
    title: string;
    @Prop()
    score: number;
    @Prop()
    description: string;
    @Prop()
    time: string;
    @Prop()
    dateOfCreation: string;
}

@Schema()
class OptionContent {
    @Prop()
    answer: string;
    @Prop()
    description: string;
    @Prop()
    id: string;
    @Prop()
    key: string;
    @Prop()
    option: string
}

@Schema()
class Option {
    @Prop()
    option: OptionContent;
}

@Schema()
class Page {
    @Prop()
    question: string;
    @Prop()
    options: Option;
}

@Schema()
class McqStore {
    @Prop()
    page: Page;
}

@Schema()
export class Test {
    @Prop()
    info: testInfo

    @Prop()
    McqStore: McqStore
}

@Schema()
export class TestContainer {
    @Prop({ type: Map })
    name: Test
}

export const TestContainerSchema = SchemaFactory.createForClass(TestContainer);

export type userDocument = Test & Document;

Related:

  1. NestJS - How to create nested schema with decorators
Related