Skip to content

Commit

Permalink
fix: param order in apply-query-parse-output
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 21, 2022
1 parent 4ec6155 commit 4fff8cd
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 134 deletions.
70 changes: 35 additions & 35 deletions docs/guide/query-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | `QueryFieldsApplyOptions`<`T`> | 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` | `QueryFieldsApplyOptions`<`T`> | Options for the fields to select. |

**Returns**

Expand All @@ -54,8 +54,8 @@ console.log(fields);
The function returns an array of objects. Each object has the properties `fields` and optional `alias` and `addFields`.

**References**
- [QueryFieldsApplyOptions](#fieldsapplyoptions)
- [QueryFieldsApplyOutput](#fieldsapplyoutput)
- [QueryFieldsApplyOptions](#queryfieldsapplyoptions)
- [QueryFieldsApplyOutput](#queryfieldsapplyoutput)

## `applyQueryFilters`

Expand Down Expand Up @@ -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` | `QueryFiltersApplyOptions`<`T`> | 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` | `QueryFiltersApplyOptions`<`T`> | Options for the fields to select. |

**Returns**

Expand All @@ -105,8 +105,8 @@ console.log(filters);
The function returns an array of objects. Each object has the properties `key` and `value`.

**References**
- [QueryFiltersApplyOptions](#filtersapplyoptions)
- [QueryFiltersApplyOutput](#filtersapplyoutput)
- [QueryFiltersApplyOptions](#queryfiltersapplyoptions)
- [QueryFiltersApplyOutput](#queryfiltersapplyoutput)

## `applyQueryRelations`

Expand Down Expand Up @@ -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` | `QueryRelationsApplyOptions`<`T`> | 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` | `QueryRelationsApplyOptions`<`T`> | Options for the relations to include. |

**Returns**

Expand All @@ -156,8 +156,8 @@ console.log(includes);
The function returns an array of objects. Each object has the properties `property` and `alias`.

**References**
- [QueryRelationsApplyOptions](#relationsapplyoptions)
- [QueryRelationsApplyOutput](#relationsapplyoutput)
- [QueryRelationsApplyOptions](#queryrelationsapplyoptions)
- [QueryRelationsApplyOutput](#queryrelationsapplyoutput)

## `applyQueryPagination`

Expand Down Expand Up @@ -193,11 +193,11 @@ console.log(pagination);

**Parameters**

| Name | Type | Description |
|:----------|:--------------------------|:--------------------------------------------------------------|
| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. |
| `data` | `unknown` | Pagination data in raw format. F.e `{limit: 20, offset: 10}`. |
| `options` | `QueryPaginationApplyOptions` | Options for the pagination to select. |
| Name | Type | Description |
|:----------|:---------------------------------|:--------------------------------------------------------------|
| `query` | `SelectQueryBuilder`<`T`> | Typeorm SelectQueryBuilder Class. |
| `data` | `unknown` | Pagination data in raw format. F.e `{limit: 20, offset: 10}`. |
| `options` | `QueryPaginationApplyOptions` | Options for the pagination to select. |

**Returns**

Expand All @@ -206,8 +206,8 @@ console.log(pagination);
The function returns an object. The object might have the properties `limit` and `offset`.

**References**
- [QueryPaginationApplyOptions](#paginationapplyoptions)
- [QueryPaginationApplyOutput](#paginationapplyoutput)
- [QueryPaginationApplyOptions](#querypaginationapplyoptions)
- [QueryPaginationApplyOutput](#querypaginationapplyoutput)

### applyQuerySort

Expand Down Expand Up @@ -244,11 +244,11 @@ console.log(sort);

**Parameters**

| Name | Type | Description |
|:----------|:--------------------------|:--------------------------------------------------------------------------------------------------------------------------|
| `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` | `QuerySortApplyOptions`<`T`> | Options for the sorting strategy. |
| Name | Type | Description |
|:----------|:-----------------------------|:--------------------------------------------------------------------------------------------------------------------------|
| `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` | `QuerySortApplyOptions`<`T`> | Options for the sorting strategy. |

**Returns**

Expand All @@ -257,8 +257,8 @@ console.log(sort);
The function returns an objects. Each key-value pair represents a field and the corresponding sorting direction.

**References**
- [QuerySortApplyOptions](#sortapplyoptions)
- [QuerySortApplyOutput](#sortapplyoutput)
- [QuerySortApplyOptions](#querysortapplyoptions)
- [QuerySortApplyOutput](#querysortapplyoutput)

## `QueryFieldsApplyOptions`

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dependencies": {
"@faker-js/faker": "^7.6.0",
"locter": "^0.3.2",
"rapiq": "^0.2.8",
"rapiq": "^0.3.0",
"reflect-metadata": "^0.1.13",
"yargs": "^17.6.0"
},
Expand Down
54 changes: 25 additions & 29 deletions src/query/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Parameter, ParseOutput, parseQuery } from 'rapiq';
import { ParseOutput, parseQuery } from 'rapiq';
import { ObjectLiteral, SelectQueryBuilder } from 'typeorm';
import {
applyQueryFieldsParseOutput,
Expand All @@ -13,36 +13,32 @@ export function applyQueryParseOutput<T extends ObjectLiteral = ObjectLiteral>(
query: SelectQueryBuilder<T>,
context: ParseOutput,
): ParseOutput {
const keys = Object.keys(context);
if (context.fields) {
applyQueryFieldsParseOutput(query, context.fields, {
defaultAlias: context.defaultPath,
relations: context.relations,
});
}

if (context.filters) {
applyQueryFiltersParseOutput(query, context.filters, {
defaultAlias: context.defaultPath,
relations: context.relations,
});
}

for (let i = 0; i < keys.length; i++) {
const key = keys[i] as `${Parameter}`;
if (context.pagination) {
applyQueryPaginationParseOutput(query, context.pagination);
}

if (context.relations) {
applyQueryRelationsParseOutput(query, context.relations, {
defaultAlias: context.defaultPath,
});
}

switch (key) {
case Parameter.FIELDS:
applyQueryFieldsParseOutput(query, context[key], {
defaultAlias: context.defaultPath,
relations: context.relations,
});
break;
case Parameter.FILTERS:
applyQueryFiltersParseOutput(query, context[key], {
defaultAlias: context.defaultPath,
relations: context.relations,
});
break;
case Parameter.PAGINATION:
applyQueryPaginationParseOutput(query, context[key]);
break;
case Parameter.RELATIONS:
applyQueryRelationsParseOutput(query, context[key], {
defaultAlias: context.defaultPath,
});
break;
case Parameter.SORT:
applyQuerySortParseOutput(query, context[key]);
break;
}
if (context.sort) {
applyQuerySortParseOutput(query, context.sort);
}

return context;
Expand Down
Loading

0 comments on commit 4fff8cd

Please sign in to comment.