import { Schema, Prop, NestSchema } from'@nestgram/mongo';import { Document } from'mongoose';@Schema()exportclassUser { @Prop() email:string; // email field (string type) @Prop() name:string; @Prop() age:number; // age field (number type) @Prop({ type: [Object], default: [] }) articles:any[]; // params for field}exporttypeUserDocument=User&Document; // export user documentNestSchema.reg(User); // reg schema
Import schema
app.module.ts
import { Module } from'nestgram';import { UseMongoConnection, UseMongo } from'@nestgram/mongo';import { User } from'./user.schema.ts';import { AppController } from'./app.controller';import { AppService } from'./app.service';@Module({ controllers: [AppController], services: [AppService], modules: [UseMongoConnection('uri'),// pass uri hereUseMongo(User),// pass schemas here that will be passed to your service ],})exportclassAppModule {}
Get the schema in the service
app.service.ts
import { Service } from'nestgram';import { Model } from'mongoose';import { User } from'./user.schema.ts';@Service()exportclassAppService {constructor(privatereadonly User:Model<User>) {}}
Then
You can then use the model in service methods and call it in controllers