Coder Social home page Coder Social logo

amranwar / nestjs-methodset Goto Github PK

View Code? Open in Web Editor NEW
19.0 1.0 2.0 430 KB

Abstract extendable controller contains all required endpoints (CRUD)

JavaScript 12.12% TypeScript 87.88%
backend backend-api controller controllers crud crud-api nestjs nestjs-backend

nestjs-methodset's Introduction

nestjs-methodset

MethodSet is an abstract controller that when extended will provide:

  • All the required GET/DELETE/POST/UPDATE endpoints for the TypeORM repository.
  • List endpoint contains: pagination, order, search, filter options.
  • filter options contains: =, gt, gte, lt, lte

nestjs-methodSet is influenced by ViewSet in Django Rest Framework

semantic-release

Installation

npm i nestjs-methodset

in main.ts add following to the app:

import { ValidationPipe } from '@nestjs/common';
...
  app.useGlobalPipes(new ValidationPipe({ transform: true }));

Code

  • Simply extends MethodSet in your controller and you done. Make sure to have the repository in your controller dependencies!
import { MethodSet } from 'nestjs-methodset';

@Controller({ path: 'article' })
export class ArticleController extends MethodSet<ArticleEntity> {
	constructor(
		@InjectRepository(ArticleEntity)
		protected readonly repository: Repository<ArticleEntity>
	) {
		super();
	}
}
  • Now your API has the following implemented functions:
    • get: GET /article/<id>
    • list: GET /article
    • post: POST /article
    • update: UPDATE /article/<id>
    • delete: DELETE /article/<id>

List URL Params:

the list endpoint contains different options as following:

  • page & pageSize: ?&page=1&pageSize=3
  • orderBy & sortOrder(default: DESC): ?orderBy=timestamp&sortOrder=ASC
  • search : ?search=test
    • Must specify search fields in the controller, ie:
    searchFields: SearchFields<ArticleEntity> = ['body', 'title'];
  • filter[]:
    • separate conditions by , ie: ?filter=price__lte=10,total=2,
    • Or can be written: ?filter[]=price__lte=10&filter[]=total=2

All Filter Valid Options:

  • <field>=<value>, equals, ie: ?filter=price=10
  • <field>__gt=<value>, greater than, ie: filter=price__lte=10
  • <field>__gte=<value> greater than or equal
  • <field>__lt=<value> less than
  • <field>__lte=<value> less than or equal

nestjs-methodset's People

Contributors

amranwar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.