Socket
Socket
Sign inDemoInstall

@nestjs/mongoose

Package Overview
Dependencies
Maintainers
2
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/mongoose

Nest - modern, fast, powerful node.js web framework (@mongoose)


Version published
Weekly downloads
419K
increased by0.03%
Maintainers
2
Weekly downloads
 
Created

What is @nestjs/mongoose?

@nestjs/mongoose is an integration package for NestJS that allows you to use Mongoose, a popular MongoDB object modeling tool, within your NestJS applications. It provides decorators and utilities to easily define and manage MongoDB schemas and models, and integrates seamlessly with NestJS's dependency injection system.

What are @nestjs/mongoose's main functionalities?

Schema Definition

This feature allows you to define MongoDB schemas using Mongoose. The code sample demonstrates how to define a simple schema for a 'Cat' model with fields for name, age, and breed.

const mongoose = require('mongoose');
const { Schema } = mongoose;

const CatSchema = new Schema({
  name: String,
  age: Number,
  breed: String
});

module.exports = mongoose.model('Cat', CatSchema);

Model Injection

This feature allows you to inject Mongoose models into your NestJS services and controllers. The code sample shows how to create a CatsModule that imports the Cat model and makes it available for dependency injection.

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CatSchema } from './schemas/cat.schema';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';

@Module({
  imports: [MongooseModule.forFeature([{ name: 'Cat', schema: CatSchema }])],
  controllers: [CatsController],
  providers: [CatsService],
})
export class CatsModule {}

CRUD Operations

This feature allows you to perform CRUD operations on your MongoDB collections. The code sample demonstrates how to create a CatsService that can create and find all Cat documents in the MongoDB collection.

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Cat } from './interfaces/cat.interface';
import { CreateCatDto } from './dto/create-cat.dto';

@Injectable()
export class CatsService {
  constructor(@InjectModel('Cat') private readonly catModel: Model<Cat>) {}

  async create(createCatDto: CreateCatDto): Promise<Cat> {
    const createdCat = new this.catModel(createCatDto);
    return createdCat.save();
  }

  async findAll(): Promise<Cat[]> {
    return this.catModel.find().exec();
  }
}

Other packages similar to @nestjs/mongoose

FAQs

Package last updated on 16 Aug 2021

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