Skip to content

Commit

Permalink
fix: export parse* methods + updated docs & README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 19, 2022
1 parent ade74dc commit 3251135
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 43 deletions.
15 changes: 9 additions & 6 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,10 @@ import { Request, Response } from 'express';
import {
applyQueryFields,
applyQueryFilters,
applyQueryRelations,
applyQueryRelationsParseOutput,
applyQueryPagination,
applyQuerySort
applyQuerySort,
parseQueryRelations,
} from 'typeorm-extension';

/**
Expand Down Expand Up @@ -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']
});
Expand All @@ -546,27 +547,29 @@ 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
applyQueryFilters(query, filter, {
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();
Expand Down
65 changes: 34 additions & 31 deletions docs/guide/query-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ check out the [documentation](https://rapiq.tada5hi.net) of the [rapiq](https://
declare function applyQueryFields<T>(
query: SelectQueryBuilder<T>,
data: unknown,
options?: FieldsApplyOptions
) : FieldsApplyOutput;
options?: FieldsApplyOptions<T>
): FieldsApplyOutput;
```

Parse and apply fields of the main entity and optional of included relations passed in as `Record<string, string[]>` or `string[]` and apply them to the `SelectQueryBuilder`, in case they match the allowed fields.
Expand Down 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` | `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**

Expand All @@ -63,7 +63,7 @@ The function returns an array of objects. Each object has the properties `fields
declare function applyQueryFilters<T>(
query: SelectQueryBuilder<T>,
data: unknown,
options?: FiltersApplyOptions
options?: FiltersApplyOptions<T>
): FiltersApplyOutput;
```

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` | `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**

Expand All @@ -114,7 +114,7 @@ The function returns an array of objects. Each object has the properties `key` a
declare function applyQueryRelations<T>(
query: SelectQueryBuilder<T>,
data: unknown,
options?: RelationsApplyOptions
options?: RelationsApplyOptions<T>
): RelationsApplyOutput;
```

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` | `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**

Expand Down Expand Up @@ -215,7 +215,7 @@ The function returns an object. The object might have the properties `limit` and
declare function applyQuerySort<T>(
query: SelectQueryBuilder<T>,
data: unknown,
options?: SortApplyOptions
options?: SortApplyOptions<T>
): SortApplyOutput;
```

Expand Down Expand Up @@ -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**

Expand All @@ -265,26 +265,24 @@ 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<T> = FieldsParseOptions<T>;
```

## `FieldsApplyOutput`

```typescript
import { FieldsParseOutput } from 'rapiq';

export type FieldsApplyOutput = FieldsParseOutput;
export type FieldsApplyOutput = FieldsParseOutput & {
defaultAlias?: string
};
```

## `FiltersApplyOptions`

```typescript
import { FiltersParseOptions } from 'rapiq';

export type FiltersTransformOptions = {
bindingKeyFn?: (key: string) => string,
};

export type FilterTransformOutputElement = {
statement: string,
binding: Record<string, any>
Expand All @@ -293,8 +291,9 @@ export type FiltersTransformOutput = FilterTransformOutputElement[];

// -----------------------------------------

export type FiltersApplyOptions = FiltersParseOptions & {
transform?: FiltersTransformOptions
export type FiltersApplyOptions<T> = FiltersParseOptions<T> & {
defaultAlias?: string,
bindindKey?: (key: string) => string
};
```

Expand Down Expand Up @@ -327,7 +326,9 @@ type PaginationApplyOutput = PaginationParseOutput;
```typescript
import { RelationsParseOptions } from 'rapiq';

export type RelationsApplyOptions = RelationsParseOptions;
export type RelationsApplyOptions<T> = RelationsParseOptions<T> & {
defaultAlias?: string
};
```

## `RelationsApplyOutput`
Expand All @@ -344,7 +345,9 @@ export type RelationsApplyOutput = RelationsParseOutput;

import { SortParseOptions } from 'rapiq';

export type SortApplyOptions = SortParseOptions;
export type SortApplyOptions<T> = SortParseOptions<T> & {
defaultAlias?: string
};
```

## `SortApplyOutput`
Expand Down
15 changes: 9 additions & 6 deletions docs/guide/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ import { Request, Response } from 'express';
import {
applyQueryFields,
applyQueryFilters,
applyQueryRelations,
applyQueryRelationsParseOutput,
applyQueryPagination,
applyQuerySort
applyQuerySort,
parseQueryRelations,
} from 'typeorm-extension';

/**
Expand Down Expand Up @@ -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']
});
Expand All @@ -103,27 +104,29 @@ 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
applyQueryFilters(query, filter, {
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();
Expand Down
9 changes: 9 additions & 0 deletions src/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
export * from './parameter';
export * from './utils';

export {
parseQueryFields,
parseQueryFilters,
parseQueryPagination,
parseQueryParameter,
parseQueryRelations,
parseQuerySort,
} from 'rapiq';

0 comments on commit 3251135

Please sign in to comment.