项目作者: nartc

项目描述 :
NestJs Abstraction Helper
高级语言: TypeScript
项目地址: git://github.com/nartc/nest-abstract.git
创建时间: 2018-11-27T04:14:56Z
项目社区:https://github.com/nartc/nest-abstract

开源协议:MIT License

下载


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 NestJS RESTfulAPI.
  • Includes: AbstractModule, AbstractService, and AbstractControllerFactory along with AbstractModel (mongoose) and AbstractEntity (typeorm).
  • 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

  1. Import AbstractModule in your AppModule

    1. import { Module } from '@nestjs/common';
    2. import { AbstractModule } from 'nest-abstract';
    3. @Module({
    4. imports: [AbstractModule.forRoot()],
    5. })
    6. export class AppModule {}

    By default, AbstractModule will use Mongoose. You can pass a value of ObjectMapping to the forRoot() method.

    1. import { Module } from '@nestjs/common';
    2. import { AbstractModule, ObjectMapping } from 'nest-abstract';
    3. @Module({
    4. imports: [AbstractModule.forRoot(ObjectMapping.TypeOrm)],
    5. })
    6. export class AppModule {}

    Note: ObjectMapping.Mongoose will require you to install mongoose and @nestjs/mongoose while ObjectMapping.TypeOrm will require you to install typeorm and @nestjs/typeorm.

    Note: I will list the usage for Mongoose from step 2 on.

  2. Create your MongooseSchema and create an interface that will extend AbstractModel. AbstractModel is an interface that has: createdAt, updatedAt and id.

    1. import { Schema } from 'mongoose';
    2. import { AbstractModel } from 'nest-abstract';
    3. const todoSchema = new Schema({
    4. content: {
    5. type: String
    6. }
    7. }, { timestamps: true });
    8. interface Todo extends AbstractModel {
    9. content: string;
    10. }

    Use your schema to initialize your Model with @nestjs/mongoose.

  3. Create your Service with AbstractMongooseService.

    1. import { Injectable } from '@nestjs/common';
    2. import { InjectModel } from '@nestjs/mongoose';
    3. import { AbstractMongooseService } from 'nest-abstract';
    4. import { Model } from 'mongoose';
    5. @Injectable()
    6. export class TodoService extends AbstractMongooseService<Todo> {
    7. constructor(@InjectModel('todo') private readonly _todoModel: Model<Todo>) {
    8. super(_todoModel);
    9. }
    10. }

    Use @InjectModel() from @nestjs/mongoose to inject your MongooseModel and pass that to the abstract constructor.

  4. Create your Controller with abstractControllerFactory

    1. import { Controller } from '@nestjs/common';
    2. import { abstractControllerFactory } from 'nest-abstract';
    3. import { Todo } from './todo.model';
    4. import { TodoService } from './todo.service';
    5. const BaseController = abstractControllerFactory<Todo>({model: TodoService.model});
    6. @Controller('todo')
    7. export class TodoController extends BaseController {
    8. constructor(private readonly _todoService: TodoService) {
    9. super(_todoService);
    10. }
    11. }
  5. 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 and delete

With Authentication

To enable Authenticate on your Controllers with Passport, abstractControllerWithAuth.

You need to install passport and @nestjs/passport if you want to enable Authentication.
```typescript
import { abstractControllerWithAuth } from ‘nest-abstract’;

const BaseController = abstractControllerWithAuth({model: TodoService.model});
```

By default, auth is enabled by on all 5 CRUDs operations.

With Swagger

To enable Swagger on your Controller, use abstractControllerWithSwagger.

Authentication is “mandatory” with Swagger 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 use abstractControllerWithSwagger without auth, please pass in an object of type DefaultAuthObj and set all the properties to false.

Plans

Credit