@travetto/rest-model-query
v5.0.5
Published
RESTful support for generating query APIs from Model classes.
Downloads
180
Maintainers
Readme
RESTful Model Query Routes
RESTful support for generating query APIs from Model classes.
Install: @travetto/rest-model-query
npm install @travetto/rest-model-query
# or
yarn add @travetto/rest-model-query
Data Model Querying support can also be added support in the form of ModelQueryRoutes. This provides listing by query as well as an endpoint to facilitate suggestion behaviors.
Code: ModelQueryRoutes example
import { Inject } from '@travetto/di';
import { ModelQuerySupport } from '@travetto/model-query';
import { Controller } from '@travetto/rest';
import { ModelQueryRoutes } from '@travetto/rest-model-query';
import { User } from './user';
@Controller('/user')
@ModelQueryRoutes(User)
class UserQueryController {
@Inject()
source: ModelQuerySupport;
}
is a shorthand that is equal to:
Code: Comparable UserController, built manually
import { Inject } from '@travetto/di';
import { ModelQuerySupport, ValidStringFields } from '@travetto/model-query';
import { isQuerySuggestSupported } from '@travetto/model-query/src/internal/service/common';
import { Controller, Get } from '@travetto/rest';
import { RestModelQuery, RestModelSuggestQuery } from '@travetto/rest-model-query';
import { User } from './user';
@Controller('/user')
class UserQueryController {
@Inject()
service: ModelQuerySupport;
@Get('')
async getAllUser(query: RestModelQuery) {
return this.service.query(User, query.finalize());
}
@Get('/suggest/:field')
async suggest(field: ValidStringFields<User>, suggest: RestModelSuggestQuery) {
if (isQuerySuggestSupported(this.service)) {
return this.service.suggest(User, field, suggest.q, suggest);
}
}
}