@travetto/rest-model
v5.0.5
Published
RESTful support for generating APIs from Model classes.
Downloads
160
Readme
RESTful Model Routes
RESTful support for generating APIs from Model classes.
Install: @travetto/rest-model
npm install @travetto/rest-model
# or
yarn add @travetto/rest-model
To facilitate common RESTful patterns, the module exposes Data Modeling Support support in the form of ModelRoutes.
Code: ModelRoutes example
import { Inject } from '@travetto/di';
import { ModelCrudSupport } from '@travetto/model';
import { Controller } from '@travetto/rest';
import { ModelRoutes } from '@travetto/rest-model';
import { User } from './user';
@Controller('/user')
@ModelRoutes(User)
class UserController {
@Inject()
source: ModelCrudSupport;
}
is a shorthand that is equal to:
Code: Comparable UserController, built manually
import { Inject } from '@travetto/di';
import { ModelCrudSupport } from '@travetto/model';
import { Path, Controller, Body, Get, Request, Delete, Post, Put } from '@travetto/rest';
import { User } from './user';
@Controller('/user')
class UserController {
@Inject()
service: ModelCrudSupport;
@Get('')
async getAllUser(req: Request) {
return await this.service.list(User);
}
@Get(':id')
async getUser(@Path() id: string) {
return await this.service.get(User, id);
}
@Delete(':id')
async deleteUser(@Path() id: string) {
return await this.service.delete(User, id);
}
@Post('')
async saveUser(@Body() user: User) {
return await this.service.create(User, user);
}
@Put('')
async updateUser(@Body() user: User) {
return await this.service.update(User, user);
}
}