Hey @all, I am trying to mongo embedded document ...
# orm-help
z
Hey @all, I am trying to mongo embedded document using Prisma with Nest.js , but unable to figure out how I can achieve this. Any help. Thanks
1
n
Hi Zahra 👋 Welcome to Prisma’s Slack community! What’s your use case? Did you have a look at Composite Types? This guide shows example on how you can have embedded documents in MongoDB.
z
Yeah @Nurul, That part helps me in creating schema, Now further I am trying to use that schema with Nest.js.
n
Okay, So in Nest.js are you running into any particular error? For reference here’s an example app of using prisma with Nest.js
z
@Nurul this is how I am implementinng Company Entity
Copy code
import { ObjectType, Field, Int, registerEnumType } from '@nestjs/graphql';
import { WeekStart } from '@prisma/client';
import { CompanySetting } from './company-setting.entity';


registerEnumType (WeekStart,{
  name: 'WeekStart',
  description: 'week start',
})

@ObjectType()
export class Company {
  id: string;

  name: string;

  @Field({nullable: true})
  avatar?: string;

  @Field({nullable: true})
  ownerId: string;

  phone?: string;

  users: string[];

  @Field(() => WeekStart)
  startOfWeek?: WeekStart;

  @Field({nullable: true, defaultValue: "Asia/Karachi"})
  defaultTimezone: string;

  isArchived?: boolean;

  @Field(() => CompanySetting)
  settings?: CompanySetting

  createdAt: Date;

  updatedAt: Date;
  
}
Company Settings Entity
Copy code
import { Field, ObjectType } from "@nestjs/graphql";

@ObjectType()
export class CompanySetting {

    @Field({ nullable: true, defaultValue: 10 })
    captureDuration?: number;

    @Field(type => [String], { nullable: true })
    alerts?: string[];

}
Create Company DTO
Copy code
import { InputType, Int, Field } from '@nestjs/graphql';
import { Setting, WeekStart } from '@prisma/client';
import { IsNotEmpty, Matches, MinLength, Validate} from 'class-validator';
import { CompanySetting } from '../entities/company-setting.entity';
import { CompanyExistsValidator } from '../validators/company-exists.validator';

@InputType()
export class CreateCompanyInput {

  @Field()
  @IsNotEmpty()
  @MinLength(3)
  @Matches(/^[ A-Za-z0-9_ -]*$/)
  @Validate(CompanyExistsValidator)
  name: string;

  @Field({ nullable: true})
  avatar: string;

  @Field({ nullable: true })
  phone: string;

  @Field(() => WeekStart, {defaultValue: WeekStart.MONDAY})
  startOfWeek: WeekStart;
  
  @Field({ nullable: true })
  ownerId: string;

  @Field({ nullable: true })
  defaultTimezone: string;

  @Field(type => [CompanySetting])
  settings: Setting

  @Field()
  isArchived: boolean;
}
n
This seems to be a Nest.js specific error. Did you have a look at this StackOverflow answer? https://stackoverflow.com/questions/66225288/nestjs-make-sure-your-class-is-decorated-with-an-appropriate-decorator
1
z
Perfect. Thanks @Nurul for your support. It really helped me to figure out the issue. Tada issue resolved and it’s working for me now. 🚀
n
Glad to hear that 🙌
🙌🏼 1