Socket
Book a DemoInstallSign in
Socket

@nestjsbase/mongoose

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjsbase/mongoose

This is a library includes inheritable classes that can help you to save your time when create CRUD module in NestJS. This classes are supporting for mongoose. If you want to use typeorm instead, you can go [here](https://www.npmjs.com/package/@nestjsbase

1.0.1
latest
npmnpm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

NestJS Base

This is a library includes inheritable classes that can help you to save your time when create CRUD module in NestJS. This classes are supporting for mongoose. If you want to use typeorm instead, you can go here

Installation

npm install @nestjsbase/mongoose

Usage

BaseModel

First, you have create your own entity class and inherit BaseModel of @nestjsbase/mongoose

import { Schema, Prop } from '@nestjs/mongoose';
import { BaseModel } from '@nestjsbase/mongoose';

@Schema({ timestamp: true })
export class UserModel extends BaseModel {
    @Prop()
    name!: string;
}

You have to enable timestamp if you want to use createdAt and updatedAt

This is BaseModel actually do in background

export class BaseModel {
  @Prop({ type: Date })
  createdAt!: Date;

  @Prop({ type: Date })
  updatedAt!: Date;

  @Prop({ type: Date })
  deletedAt!: Date;
}

BaseService

Now, make your service inherit BaseService

import { Model } from 'mongoose';
import { BaseService } from '@nestjsbase/mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { UserModel } from '../user.model';

@Injectable()
export class UserService extends BaseService {
    constructor(@InjectModel(UserModel.name) private userModel: Model<UserModel>){
        super(userModel);
    }
}

This BaseService already have methods:

export class IBaseService {
  create(...);
  createMany(...);

  getOne(...);
  getOneOrFail(...);

  getOneById(...);
  getOneByIdOrFail(...);

  getAll(...);
  getAllWithPagination(...);

  update(...);
  updateById(...);

  remove(...);
  removeById(...);

  softRemove(...);
  softRemoveById(...);
}

FindOptions and FindOrFailOptions

  • where: usage same as filter of mongoose
  • relations: usage same as populate of mongoose
  • errorMessage: This message will be thrown when entity not be found. Default is: "Document not found"

FindWithPaginationOptions

  • limit: number of items in one page
  • page: page number

IPagination

export interface IPagination<T> {
  data: T[];
  pagination: {
    limit: number;
    page: number;
    total: number;
  };
}

FAQs

Package last updated on 09 Aug 2023

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.