Socket
Socket
Sign inDemoInstall

nestjs-typegoose

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-typegoose

A nestjs module wrapper for typegoose


Version published
Weekly downloads
1.5K
decreased by-51.22%
Maintainers
1
Weekly downloads
 
Created
Source

nestjs-typegoose

NPM npm version Build Status Coverage Status

Description

Injects typegoose models for nest components and controllers. Typegoose equivalant for @nestjs/mongoose.

Using Typegoose removes the need for having an Model interface.

Installation

npm install --save nestjs-typegoose

Basic usage

app.module.ts

import { Module } from '@nestjs/common';
import { TypegooseModule } from 'nestjs-typegoose';

@Module({
  imports: [TypegooseModule.forRoot('mongodb://localhost:27017/nest', { useNewUrlParser: true }), 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 { 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 'nestjs-typegoose';
import { Cat } from './cat.model';
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[] | 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';

@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);
    }
}

Mongoose Schema Options

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 {}

Async Mongoose Schema Options

To provide asynchronous mongoose schema options (similar to nestjs mongoose implementation) you can use the TypegooseModule.forRootAsync

@Module({
  imports: [
    TypegooseModule.forAsyncRoot({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        uri: configService.getString('MONGODB_URI'),
      }),
      inject: [ConfigService]
    })
  ]
})
export class CatsModule {}

You can also use a class with useClass

import { TypegooseOptionsFactory, TypegooseModuleOptions } from 'nestjs-typegoose';

class TypegooseConfigService extends TypegooseOptionsFactory {
  createTypegooseOptions(): Promise<TypegooseModuleOptions> | TypegooseModuleOptions {
    return {
      uri: 'mongodb://localhost/nest'
    }
  };
}

@Module({
  imports: [
    TypegooseModule.forAsyncRoot({
      useClass: TypegooseConfigService
    })
  ]
})
export class CatsModule {}

Or if you want to prevent creating another TypegooseConfigService class and want to use it from another imported module then use useExisting

@Module({
  imports: [
    TypegooseModule.forAsyncRoot({
      imports: [ConfigModule],
      useExisting: ConfigService,
    })
  ]
})
export class CatsModule {}

Requirements

  1. Typegoose +5.2.1
  2. @nestjs/common +5.0.0
  3. @nestjs/core +5.0.0
  4. mongoose +5.1.1

License

nestjs-typegoose is MIT licensed.

FAQs

Package last updated on 30 Nov 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc