@travetto/rest-model-query v4.0.7
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, SortClause, 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';
const convert = <T>(k?: string) => k && typeof k === 'string' && /^[\{\[]/.test(k) ? JSON.parse(k) as T : k;
@Controller('/user')
class UserQueryController {
@Inject()
service: ModelQuerySupport;
@Get('')
async getAllUser(query: RestModelQuery) {
return this.service.query(User, {
limit: query.limit,
offset: query.offset,
sort: convert(query.sort) as SortClause<User>[],
where: convert(query.where)
});
}
@Get('/suggest/:field')
async suggest(field: ValidStringFields<User>, suggest: RestModelSuggestQuery) {
if (isQuerySuggestSupported(this.service)) {
return this.service.suggest(User, field, suggest.q, suggest);
}
}
}
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
4 months ago
6 months ago
6 months ago
7 months ago
8 months ago
8 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
7 months ago
6 months ago
7 months ago
9 months ago
9 months ago
8 months ago
8 months ago
8 months ago
9 months ago
11 months ago
11 months ago
11 months ago
12 months ago
12 months ago
12 months ago
12 months ago
11 months ago
11 months ago
11 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago