NestJs Abstraction Helper
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!
NestJS
RESTfulAPI.AbstractModule
, AbstractService
, and AbstractControllerFactory
along with AbstractModel
(mongoose
) and AbstractEntity
(typeorm
).@nestjs/swagger
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.
npm i nest-abstract
Import AbstractModule
in your AppModule
By default,
AbstractModule
will useMongoose
. You can pass a value ofObjectMapping
to theforRoot()
method.
Note:
ObjectMapping.Mongoose
will require you to installmongoose
and@nestjs/mongoose
whileObjectMapping.TypeOrm
will require you to installtypeorm
and@nestjs/typeorm
.
Note: I will list the usage for Mongoose from step 2 on.
Create your MongooseSchema
and create an interface that will extend AbstractModel
. AbstractModel
is an interface that has: createdAt
, updatedAt
and id
.
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
schema
to initialize yourModel
with@nestjs/mongoose
.
Create your Service
with AbstractMongooseService
.
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/mongoose
to inject your MongooseModel and pass that to the abstract constructor.
Create your Controller
with abstractControllerFactory
import { 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 Service
in providers
array in Module
and your Controller
in controllers
array in Module
.
Now your
TodoController
should have 5 pre-defined route handlers:find
,findById
,create
,update
anddelete
To enable Authenticate
on your Controllers
with Passport
, abstractControllerWithAuth
.
You need to install
passport
and@nestjs/passport
if you want to enableAuthentication
.
```typescript
import { abstractControllerWithAuth } from ‘nest-abstract’;
const BaseController = abstractControllerWithAuth
```
By default,
auth
is enabled by on all 5 CRUDs operations.
To enable Swagger
on your Controller
, use abstractControllerWithSwagger
.
Authentication
is “mandatory” withSwagger
so you will have to have:passport
,@nestjs/passport
and@nestjs/swagger
installed.
By default,auth
is enabled by on all 5 CRUDs operations. If you wish to useabstractControllerWithSwagger
withoutauth
, please pass in an object of typeDefaultAuthObj
and set all the properties tofalse
.
abstractControllerFactory
out to 3 separate factories: normal, swagger and withAuthSerialization
(https://docs.nestjs.com/techniques/serialization)?BaseController
on my nest-mean
repository and came up with his/her BaseController
.