1.0.1 • Published 3 years ago
nestjs-methodset v1.0.1
nestjs-methodset
MethodSet is an abstract controller that when extended will provide:
- All the required
GET/DELETE/POST/UPDATEendpoints 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
Installation
npm i nestjs-methodsetin main.ts add following to the app:
import { ValidationPipe } from '@nestjs/common';
...
app.useGlobalPipes(new ValidationPipe({ transform: true }));Code
- Simply extends
MethodSetin your controller and you done. Make sure to have therepositoryin 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>
- get: GET
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
- separate conditions by
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