Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Abstract extendable controller contains all required endpoints (CRUD)

Notifications You must be signed in to change notification settings

AmrAnwar/nestjs-methodset

Repository files navigation

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

About

Abstract extendable controller contains all required endpoints (CRUD)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /