nest-abstract
nest-abstract copied to clipboard
NestJs Abstraction Helper
Nestjs Abstract
Abstraction component for NestJs.
Fair warning: This package is still in early development stage. Please give me any feedbacks if you decide to try it out and find any problems/area-for-improvements. Thank you!
Features
- Provides Abstractions for your
NestJSRESTfulAPI. - Includes:
AbstractModule,AbstractService, andAbstractControllerFactoryalong withAbstractModel(mongoose) andAbstractEntity(typeorm). - [x] Supports
@nestjs/swagger
Motivations
I am a big fan of TypeScript and abstraction overall. One of the biggest motivations is to create a BaseController to work with Swagger's decorators that @nestjs/swagger provides which is on the todo list. Main reason is I want to roll out a version of the package that will make it work with non-swagger applications first as this is my first attempt at an npm package.
Installation
npm i nest-abstract
Usage
-
Import
AbstractModulein yourAppModuleimport { Module } from '@nestjs/common'; import { AbstractModule } from 'nest-abstract'; @Module({ imports: [AbstractModule.forRoot()], }) export class AppModule {}By default,
AbstractModulewill useMongoose. You can pass a value ofObjectMappingto theforRoot()method.import { Module } from '@nestjs/common'; import { AbstractModule, ObjectMapping } from 'nest-abstract'; @Module({ imports: [AbstractModule.forRoot(ObjectMapping.TypeOrm)], }) export class AppModule {}Note:
ObjectMapping.Mongoosewill require you to installmongooseand@nestjs/mongoosewhileObjectMapping.TypeOrmwill require you to installtypeormand@nestjs/typeorm.Note: I will list the usage for Mongoose from step 2 on.
-
Create your
MongooseSchemaand create an interface that will extendAbstractModel.AbstractModelis an interface that has:createdAt,updatedAtandid.import { Schema } from 'mongoose'; import { AbstractModel } from 'nest-abstract'; const todoSchema = new Schema({ content: { type: String } }, { timestamps: true }); interface Todo extends AbstractModel { content: string; }Use your
schemato initialize yourModelwith@nestjs/mongoose. -
Create your
ServicewithAbstractMongooseService.import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { AbstractMongooseService } from 'nest-abstract'; import { Model } from 'mongoose'; @Injectable() export class TodoService extends AbstractMongooseService<Todo> { constructor(@InjectModel('todo') private readonly _todoModel: Model<Todo>) { super(_todoModel); } }Use
@InjectModel()from@nestjs/mongooseto inject your MongooseModel and pass that to the abstract constructor. -
Create your
ControllerwithabstractControllerFactoryimport { Controller } from '@nestjs/common'; import { abstractControllerFactory } from 'nest-abstract'; import { Todo } from './todo.model'; import { TodoService } from './todo.service'; const BaseController = abstractControllerFactory<Todo>({model: TodoService.model}); @Controller('todo') export class TodoController extends BaseController { constructor(private readonly _todoService: TodoService) { super(_todoService); } } -
Make sure you put your
Serviceinprovidersarray inModuleand yourControllerincontrollersarray inModule.Now your
TodoControllershould have 5 pre-defined route handlers:find,findById,create,updateanddelete
With Authentication
To enable Authenticate on your Controllers with Passport, abstractControllerWithAuth.
You need to install
passportand@nestjs/passportif you want to enableAuthentication.
import { abstractControllerWithAuth } from 'nest-abstract';
const BaseController = abstractControllerWithAuth<Todo>({model: TodoService.model});
By default,
authis enabled by on all 5 CRUDs operations.
With Swagger
To enable Swagger on your Controller, use abstractControllerWithSwagger.
Authenticationis "mandatory" withSwaggerso you will have to have:passport,@nestjs/passportand@nestjs/swaggerinstalled. By default,authis enabled by on all 5 CRUDs operations. If you wish to useabstractControllerWithSwaggerwithoutauth, please pass in an object of typeDefaultAuthObjand set all the properties tofalse.
Plans
- [x] Might break
abstractControllerFactoryout to 3 separate factories: normal, swagger and withAuth - Supports
Serialization(https://docs.nestjs.com/techniques/serialization)? - anything?
Credit
- @rcanessa89 and his/her repository: https://github.com/rcanessa89/nest-js-starter. rcanessa89 first raised an issue regarding a
BaseControlleron mynest-meanrepository and came up with his/herBaseController.