diff --git a/README.MD b/README.MD index 6858baf2..3b109594 100644 --- a/README.MD +++ b/README.MD @@ -504,9 +504,10 @@ import { Request, Response } from 'express'; import { applyQueryFields, applyQueryFilters, - applyQueryRelations, + applyQueryRelationsParseOutput, applyQueryPagination, - applyQuerySort + applyQuerySort, + parseQueryRelations, } from 'typeorm-extension'; /** @@ -537,7 +538,7 @@ export async function getUsers(req: Request, res: Response) { // ----------------------------------------------------- - const relationsParsed = applyQueryRelations(query, include, { + const relations = parseQueryRelations(include, { defaultAlias: 'user', allowed: ['profile'] }); @@ -546,14 +547,14 @@ export async function getUsers(req: Request, res: Response) { defaultAlias: 'user', allowed: ['id', 'name', 'profile.id'], // profile.id can only be used as sorting key, if the relation 'profile' is included. - relations: relationsParsed + relations }); applyQueryFields(query, fields, { defaultAlias: 'user', allowed: ['id', 'name', 'profile.id', 'profile.avatar'], // porfile fields can only be included, if the relation 'profile' is included. - relations: relationsParsed + relations }) // only allow filtering users by id & name @@ -561,12 +562,14 @@ export async function getUsers(req: Request, res: Response) { defaultAlias: 'user', allowed: ['id', 'name', 'profile.id'], // porfile.id can only be used as a filter, if the relation 'profile' is included. - relations: relationsParsed + relations }); // only allow to select 20 items at maximum. const pagination = applyQueryPagination(query, page, {maxLimit: 20}); + applyQueryRelationsParseOutput(query, relations); + // ----------------------------------------------------- const [entities, total] = await query.getManyAndCount(); diff --git a/docs/guide/query-api-reference.md b/docs/guide/query-api-reference.md index 7398ab6f..8fac5c1b 100644 --- a/docs/guide/query-api-reference.md +++ b/docs/guide/query-api-reference.md @@ -12,8 +12,8 @@ check out the [documentation](https://rapiq.tada5hi.net) of the [rapiq](https:// declare function applyQueryFields( query: SelectQueryBuilder, data: unknown, - options?: FieldsApplyOptions -) : FieldsApplyOutput; + options?: FieldsApplyOptions +): FieldsApplyOutput; ``` Parse and apply fields of the main entity and optional of included relations passed in as `Record` or `string[]` and apply them to the `SelectQueryBuilder`, in case they match the allowed fields. @@ -41,11 +41,11 @@ console.log(fields); **Parameters** -| Name | Type | Description | -|:----------|:--------------------------|:------------------------------------------------------------| -| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. | -| `data` | `unknown` | Fields in raw format. F.e `['name']` or `{user: ['name']}`. | -| `options` | `FieldsApplyOptions` | Options for the fields to select. | +| Name | Type | Description | +|:----------|:---------------------------|:------------------------------------------------------------| +| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. | +| `data` | `unknown` | Fields in raw format. F.e `['name']` or `{user: ['name']}`. | +| `options` | `FieldsApplyOptions`<`T`> | Options for the fields to select. | **Returns** @@ -63,7 +63,7 @@ The function returns an array of objects. Each object has the properties `fields declare function applyQueryFilters( query: SelectQueryBuilder, data: unknown, - options?: FiltersApplyOptions + options?: FiltersApplyOptions ): FiltersApplyOutput; ``` @@ -92,11 +92,11 @@ console.log(filters); **Parameters** -| Name | Type | Description | -|:----------|:--------------------------|:-------------------------------------| -| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. | -| `data` | `unknown` | Fields in raw format. F.e `{id: 1}`. | -| `options` | `FiltersApplyOptions` | Options for the fields to select. | +| Name | Type | Description | +|:----------|:----------------------------|:-------------------------------------| +| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. | +| `data` | `unknown` | Fields in raw format. F.e `{id: 1}`. | +| `options` | `FiltersApplyOptions`<`T`> | Options for the fields to select. | **Returns** @@ -114,7 +114,7 @@ The function returns an array of objects. Each object has the properties `key` a declare function applyQueryRelations( query: SelectQueryBuilder, data: unknown, - options?: RelationsApplyOptions + options?: RelationsApplyOptions ): RelationsApplyOutput; ``` @@ -143,11 +143,11 @@ console.log(includes); **Parameters** -| Name | Type | Description | -|:----------|:--------------------------|:----------------------------------------------------| -| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. | -| `data` | `unknown` | Relations in raw format. F.e `['roles']` or `roles` | -| `options` | `RelationsApplyOptions` | Options for the relations to include. | +| Name | Type | Description | +|:----------|:------------------------------|:----------------------------------------------------| +| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. | +| `data` | `unknown` | Relations in raw format. F.e `['roles']` or `roles` | +| `options` | `RelationsApplyOptions`<`T`> | Options for the relations to include. | **Returns** @@ -215,7 +215,7 @@ The function returns an object. The object might have the properties `limit` and declare function applyQuerySort( query: SelectQueryBuilder, data: unknown, - options?: SortApplyOptions + options?: SortApplyOptions ): SortApplyOutput; ``` @@ -248,7 +248,7 @@ console.log(sort); |:----------|:--------------------------|:--------------------------------------------------------------------------------------------------------------------------| | `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. | | `data` | `unknown` | Sorting Fields in raw format. F.e `['-name']`, `-name` or `{name: 'DESC'}`. The hyphen prefix indicates descending order. | -| `options` | `SortApplyOptions` | Options for the sorting strategy. | +| `options` | `SortApplyOptions`<`T`> | Options for the sorting strategy. | **Returns** @@ -265,7 +265,7 @@ The function returns an objects. Each key-value pair represents a field and the ```typescript import { FieldsParseOptions } from 'rapiq'; -export type FieldsApplyOptions = FieldsParseOptions; +export type FieldsApplyOptions = FieldsParseOptions; ``` ## `FieldsApplyOutput` @@ -273,7 +273,9 @@ export type FieldsApplyOptions = FieldsParseOptions; ```typescript import { FieldsParseOutput } from 'rapiq'; -export type FieldsApplyOutput = FieldsParseOutput; +export type FieldsApplyOutput = FieldsParseOutput & { + defaultAlias?: string +}; ``` ## `FiltersApplyOptions` @@ -281,10 +283,6 @@ export type FieldsApplyOutput = FieldsParseOutput; ```typescript import { FiltersParseOptions } from 'rapiq'; -export type FiltersTransformOptions = { - bindingKeyFn?: (key: string) => string, -}; - export type FilterTransformOutputElement = { statement: string, binding: Record @@ -293,8 +291,9 @@ export type FiltersTransformOutput = FilterTransformOutputElement[]; // ----------------------------------------- -export type FiltersApplyOptions = FiltersParseOptions & { - transform?: FiltersTransformOptions +export type FiltersApplyOptions = FiltersParseOptions & { + defaultAlias?: string, + bindindKey?: (key: string) => string }; ``` @@ -327,7 +326,9 @@ type PaginationApplyOutput = PaginationParseOutput; ```typescript import { RelationsParseOptions } from 'rapiq'; -export type RelationsApplyOptions = RelationsParseOptions; +export type RelationsApplyOptions = RelationsParseOptions & { + defaultAlias?: string +}; ``` ## `RelationsApplyOutput` @@ -344,7 +345,9 @@ export type RelationsApplyOutput = RelationsParseOutput; import { SortParseOptions } from 'rapiq'; -export type SortApplyOptions = SortParseOptions; +export type SortApplyOptions = SortParseOptions & { + defaultAlias?: string +}; ``` ## `SortApplyOutput` diff --git a/docs/guide/query.md b/docs/guide/query.md index 0870a3b8..80128e69 100644 --- a/docs/guide/query.md +++ b/docs/guide/query.md @@ -61,9 +61,10 @@ import { Request, Response } from 'express'; import { applyQueryFields, applyQueryFilters, - applyQueryRelations, + applyQueryRelationsParseOutput, applyQueryPagination, - applyQuerySort + applyQuerySort, + parseQueryRelations, } from 'typeorm-extension'; /** @@ -94,7 +95,7 @@ export async function getUsers(req: Request, res: Response) { // ----------------------------------------------------- - const relationsParsed = applyQueryRelations(query, include, { + const relations = parseQueryRelations(include, { defaultAlias: 'user', allowed: ['profile'] }); @@ -103,14 +104,14 @@ export async function getUsers(req: Request, res: Response) { defaultAlias: 'user', allowed: ['id', 'name', 'profile.id'], // profile.id can only be used as sorting key, if the relation 'profile' is included. - relations: relationsParsed + relations }); applyQueryFields(query, fields, { defaultAlias: 'user', allowed: ['id', 'name', 'profile.id', 'profile.avatar'], // porfile fields can only be included, if the relation 'profile' is included. - relations: relationsParsed + relations }) // only allow filtering users by id & name @@ -118,12 +119,14 @@ export async function getUsers(req: Request, res: Response) { defaultAlias: 'user', allowed: ['id', 'name', 'profile.id'], // porfile.id can only be used as a filter, if the relation 'profile' is included. - relations: relationsParsed + relations }); // only allow to select 20 items at maximum. const pagination = applyQueryPagination(query, page, {maxLimit: 20}); + applyQueryRelationsParseOutput(query, relations); + // ----------------------------------------------------- const [entities, total] = await query.getManyAndCount(); diff --git a/src/query/index.ts b/src/query/index.ts index bdbfcd87..feec3895 100644 --- a/src/query/index.ts +++ b/src/query/index.ts @@ -1,2 +1,11 @@ export * from './parameter'; export * from './utils'; + +export { + parseQueryFields, + parseQueryFilters, + parseQueryPagination, + parseQueryParameter, + parseQueryRelations, + parseQuerySort, +} from 'rapiq';