# nest-abstract

> NestJS Abstraction Helper

Latest version **1.0.0-beta.10** (published 2018-11-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install nest-abstract
pnpm add nest-abstract
yarn add nest-abstract
bun add nest-abstract
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-beta.10 |
| Published | 2018-11-30 |
| First published | 2018-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 87.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 39 |
| Author | Chau Tran |
| Maintainers | nartc |
| Keywords | nestjs, abstraction, base |

## Links

- npm: https://www.npmjs.com/package/nest-abstract
- Repository: https://github.com/nartc/nest-abstract
- Homepage: https://github.com/nartc/nest-abstract#readme
- Issues: https://github.com/nartc/nest-abstract/issues
- npm.io page: https://npm.io/package/nest-abstract

## Dependencies (2)

- [rxjs](https://npm.io/package/rxjs.md) ^6.3.3
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.12

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 1.0.0-beta.10 (latest) — 2018-11-30
- 1.0.0-beta.5 (next) — 2018-11-30
- 1.0.0-beta.8 — 2018-11-30
- 1.0.0-beta.6 — 2018-11-30
- 1.0.0-beta.4 — 2018-11-30
- 1.0.0-beta.3 — 2018-11-29
- 1.0.0-beta.2 — 2018-11-29
- 1.0.0-beta.1 — 2018-11-29
- 1.0.0-alpha.12 — 2018-11-29
- 1.0.0-alpha.11 — 2018-11-29
- 1.0.0-alpha.10 — 2018-11-28
- 1.0.0-alpha.9 — 2018-11-28
- 1.0.0-alpha.8 — 2018-11-28
- 1.0.0-alpha.7 — 2018-11-28
- 1.0.0-alpha.6 — 2018-11-28
- … 5 more at https://npm.io/package/nest-abstract/versions

## README

<h1 align="center">Nestjs Abstract</h1>

<p align="center">Abstraction component for NestJs.</p>

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`).
- [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

1. Import `AbstractModule` in your `AppModule`
   ```typescript
    import { Module } from '@nestjs/common';
    import { AbstractModule } from 'nest-abstract';

    @Module({
        imports: [AbstractModule.forRoot()],
    })
    export class AppModule {}
   ```

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

    ```typescript
    import { Module } from '@nestjs/common';
    import { AbstractModule, ObjectMapping } from 'nest-abstract';

    @Module({
        imports: [AbstractModule.forRoot(ObjectMapping.TypeOrm)],
    })
    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`.
   
   ```typescript
   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 your `Model` with `@nestjs/mongoose`.

3. Create your `Service` with `AbstractMongooseService`.
   ```typescript
   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.

4. Create your `Controller` with `abstractControllerFactory`
    ```typescript
    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);
        }
    }
    ```

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<Todo>({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

- [x] Might break `abstractControllerFactory` out 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 `BaseController` on my `nest-mean` repository and came up with his/her `BaseController`.

---
_Source: https://npm.io/package/nest-abstract · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
