Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
@m8a/nestjs-typegoose
Advanced tools
NOTE: this is a fork of nestjs-typegoose. We and another dependency we use needed this package to be up-to-date and since the original maintainer hadn't kept up with the updating for over a year, we've decided to take over the package. We are very grateful for the work put into the original package and give kpfromer full credit for the work.
What does this package do? It injects typegoose models for nest components (mainly services). This module is very similar to @nestjs/mongoose. however, Typegoose goes much further in terms of TypeScript support for Mongoose.
Using Typegoose removes the need for having a Model interface and more.
npm install --save @m8a/nestjs-typegoose
or
yarn add @m8a/nestjs-typegoose
Here is the full documentation describing all basic and advanced features.
You can checkout the example
project for more details.
app.module.ts
import { Module } from "@nestjs/common";
import { TypegooseModule } from "@m8a/nestjs-typegoose";
import { CatsModule } from "./cat.module.ts";
@Module({
imports: [
TypegooseModule.forRoot("mongodb://localhost:27017/nest", {
useNewUrlParser: true,
}),
CatsModule,
],
})
export class ApplicationModule {}
Create class that describes your schema
cat.model.ts
import { prop } from "@typegoose/typegoose";
import { IsString } from "class-validator";
export class Cat {
@IsString()
@prop({ required: true })
name: string;
}
Inject Cat for CatsModule
cat.module.ts
import { Module } from "@nestjs/common";
import { TypegooseModule } from "@m8a/nestjs-typegoose";
import { Cat } from "./cat.model";
import { CatsController } from "./cats.controller";
import { CatsService } from "./cats.service";
@Module({
imports: [TypegooseModule.forFeature([Cat])],
controllers: [CatsController],
providers: [CatsService],
})
export class CatsModule {}
Get the cat model in a service
cats.service.ts
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@m8a/nestjs-typegoose";
import { Cat } from "./cat.model";
import { ReturnModelType } from "@typegoose/typegoose";
@Injectable()
export class CatsService {
constructor(
@InjectModel(Cat) private readonly catModel: ReturnModelType<typeof Cat>
) {}
async create(createCatDto: { name: string }): Promise<Cat> {
const createdCat = new this.catModel(createCatDto);
return await createdCat.save();
}
async findAll(): Promise<Cat[] | null> {
return await this.catModel.find().exec();
}
}
Finally, use the service in a controller!
cats.controller.ts
import { Controller, Get, Post, Body } from "@nestjs/common";
import { CatsService } from "./cats.service";
import { Cat } from "./cats.model.ts";
@Controller("cats")
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get()
async getCats(): Promise<Cat[] | null> {
return await this.catsService.findAll();
}
@Post()
async create(@Body() cat: Cat): Promise<Cat> {
return await this.catsService.create(cat);
}
}
You can now inject a connection inside of your services, when needed.
import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@m8a/nestjs-typegoose';
import { Connection } from 'mongoose';
@Injectable()
export class CatsService {
constructor(@InjectConnection() private connection: Connection) {}
}
@m8a/nestjs-typegoose is MIT licensed.
FAQs
A nestjs module wrapper for typegoose
We found that @m8a/nestjs-typegoose demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.