Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
nestjs-typegoose
Advanced tools
Injects typegoose models for nest components and controllers. Typegoose equivalant for @nestjs/mongoose.
Using Typegoose removes the need for having an Model interface.
npm install --save nestjs-typegoose
app.module.ts
import { Module } from '@nestjs/common';
import { TypegooseModule } from 'nestjs-typegoose';
@Module({
imports: [TypegooseModule.forRoot('mongodb://localhost/nest'), CatsModule],
})
export class ApplicationModule {}
Create class that extends Typegoose
cat.model.ts
import { prop, Typegoose } from 'typegoose';
import { IsString } from 'class-validator';
export class Cat extends Typegoose {
@IsString()
@prop({ required: true })
name: string;
}
Inject Cat for CatsModule
cat.module.ts
import { Module } from '@nestjs/common';
import { TypegooseModule } from 'nestjs-typegoose';
import { Cat } from './cat.model';
import { CatsService } from './cat.service';
@Module({
imports: [TypegooseModule.forFeature(Cat)],
providers: [CatsService]
})
export class CatsModule {}
Get the cat model in a service
cats.service.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Cat } from './interfaces/cat.interface';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatSchema } from './schemas/cat.schema';
import { ModelType } from 'typegoose';
@Injectable()
export class CatsService {
constructor(@InjectModel(Cat) private readonly catModel: ModelType<Cat>) {}
async create(createCatDto: {name: string}): Promise<Cat> {
const createdCat = new this.catModel(createCatDto);
return await createdCat.save();
}
async findAll(): Promise<Cat[]> {
return await this.catModel.find().exec();
}
}
To add custom mongoose schema options
you can simply change Typegoose.forFeature
to the following format:
@Module({
imports: [
TypegooseModule.forFeature({
typegooseClass: Cat,
schemaOptions: {
collection: 'ADifferentCollectionNameForCats'
}
})
]
})
export class CatsModule {}
nestjs-typegoose is MIT licensed.
FAQs
A nestjs module wrapper for typegoose
The npm package nestjs-typegoose receives a total of 1,104 weekly downloads. As such, nestjs-typegoose popularity was classified as popular.
We found that nestjs-typegoose demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.