babacarbasse / nestjs-mongo-crud-base

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NestJs MongoDB Base Crud Api

styled with prettier npm MIT licensed Coverage Status Build Status dependencies Status Dev Dependencies

Nest Powered

Usage

Installation

npm i nestjs-mongo-crud-base

Importing library

In your service you can import the BaseRepositoryService and extends your custom service

import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { BaseRepositoryService } from 'nestjs-mongo-crud-base';
import { Hero } from "../schemas/hero.schema";


@Injectable()
export class HeroService extends BaseRepositoryService<Hero> {
  constructor(
    @InjectModel(Hero.name) private heroModel: Model<Hero>
  ) {
    super(heroModel);
  }
}

In your controller you can import the BaseController and extends your custom controller

import {
  BaseController
} from 'nestjs-mongo-crud-base';

import { Controller } from '@nestjs/common';
import { BaseController } from 'nestjs-mongo-crud-base';
import { ApiTags } from '@nestjs/swagger';
import { Hero } from "../schemas/hero.schema";
import { HeroService } from "../providers/document-file.service";

@ApiTags('heros')
@Controller('heros')
export class HeroController extends BaseController<Hero> {
  constructor(private readonly heroService: HeroService) {
    super(heroService)
  }
}
IBaseService<T> {
  findAll(
    paginateOpts?: {
      page: number;
      limit: number;
    },
    ...args: any[]
  ): Promise<T[]>;

  findOne(id: any, ...args: any[]): Promise<T>;

  findOneBy(query: object, ...args: any[]): Promise<T>;

  findBy(
    query: object,
    paginateOpts?: {
      page: number;
      limit: number;
    },
    ...args: any[]
  ): Promise<T[]>;

  update(id: Types.ObjectId, payload: any, ...args: any[]): Promise<T>;

  create(payload: any, ...args: any[]): Promise<any>;

  delete(id: any, ...args: any[]): Promise<any>;
}

Service functions

Function Description
findAll Return all collection data with pagination if provided.
findOne Return a collection data by id.
findOneBy Return one corresponding collection data with the query provided.
findBy Return all corresponding collection data with the query provided.
update Update data by id.
create Create new data.
delete Remove data by id.

Examples Hero Endpoins

endpoint method description
/heros OR /heros?page=1&limit=10 GET Return all heros with pagination if provided.
/heros/:id GET Return a hero by id.
/heros/:id put update a hero by id.
/heros post Create new data.
/heros/:id delete Remove data by id.

About

License:MIT License


Languages

Language:TypeScript 100.0%