Skip to content

mikemajesty/nestjs-mongoose-generic-repository

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nestjs Mongoose Generic Repository

$ npm i nestjs-mongoose-generic-repository

Usage

├── cats
        ├── controller.ts
        ├── entity.ts
        ├── module.ts
        ├── repository.ts
        ├── schema.ts
--repository.ts

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Repository } from 'nestjs-mongoose-generic-repository';

import { CatDocument, Cats } from './schema';

@Injectable()
export class CatsRepository extends Repository<CatDocument> {
  constructor(@InjectModel(Cats.name) private entity: Model<CatDocument>) {
    super(entity);
  }
}
--controller.ts

import { Body, Controller, Post } from '@nestjs/common';
import { CreatedModel } from 'nestjs-mongoose-generic-repository';

import { CatsDTO } from './entity';
import { CatsRepository } from './repository';

@Controller('cats')
export class CatsController {
  constructor(private readonly catRepository: CatsRepository) {}

  @Post()
  async save(@Body() model: CatsDTO): Promise<CreatedModel> {
    const saved = await this.catRepository.create(model);
    return saved;
  }
}

Operators

  create
  find
  findById
  findAll
  remove
  updateOne
  updateMany

Lib types

import { ObjectId } from 'mongoose';

export type UpdatedModel = {
  matchedCount: number;
  modifiedCount: number;
  acknowledged: boolean;
  upsertedId: unknown | ObjectId;
  upsertedCount: number;
};

export type RemovedModel = {
  deletedCount: number;
  deleted: boolean;
};

export type CreatedModel = {
  id: string;
  created: boolean;
};

The following is a list of all the people that have contributed to nestjs-mongoose-generic-repository. Thanks for your contributions!

mikemajesty

License

It is available under the MIT license. License

Releases

No releases published

Packages

No packages published