Skip to content

Commit

Permalink
Merge 299c0ae into 8ef174f
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelyali committed Dec 7, 2019
2 parents 8ef174f + 299c0ae commit a1ed2c1
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 239 deletions.
Expand Up @@ -4,7 +4,6 @@ import { QueryFields, QueryFilter, QueryJoin, QuerySort, SCondition } from '../t
export interface ParsedRequestParams {
fields: QueryFields;
paramsFilter: QueryFilter[];
authFilter: ObjectLiteral;
authPersist: ObjectLiteral;
search: SCondition;
filter: QueryFilter[];
Expand Down
27 changes: 20 additions & 7 deletions packages/crud-request/src/request-query.parser.ts
Expand Up @@ -34,13 +34,14 @@ import {
QueryJoin,
QuerySort,
SCondition,
SConditionAND,
SFields,
} from './types';

// tslint:disable:variable-name ban-types
export class RequestQueryParser implements ParsedRequestParams {
public fields: QueryFields = [];
public paramsFilter: QueryFilter[] = [];
public authFilter: ObjectLiteral = undefined;
public authPersist: ObjectLiteral = undefined;
public search: SCondition;
public filter: QueryFilter[] = [];
Expand Down Expand Up @@ -69,7 +70,6 @@ export class RequestQueryParser implements ParsedRequestParams {
return {
fields: this.fields,
paramsFilter: this.paramsFilter,
authFilter: this.authFilter,
authPersist: this.authPersist,
search: this.search,
filter: this.filter,
Expand Down Expand Up @@ -142,12 +142,25 @@ export class RequestQueryParser implements ParsedRequestParams {
return this;
}

setAuthFilter(filter: ObjectLiteral = {}) {
this.authFilter = filter;
setAuthPersist(persist: ObjectLiteral = {}) {
this.authPersist = persist || {};
}

setAuthPersist(persist: ObjectLiteral = {}) {
this.authPersist = persist;
convertFilterToSearch(filter: QueryFilter): SFields | SConditionAND {
const isEmptyValue = {
isnull: true,
notnull: true,
};

return filter
? {
[filter.field]: {
[filter.operator]: isEmptyValue[filter.operator]
? isEmptyValue[filter.operator]
: filter.value,
},
}
: {};
}

private getParamNames(
Expand Down Expand Up @@ -320,6 +333,6 @@ export class RequestQueryParser implements ParsedRequestParams {
break;
}

return { field: option.field, operator: 'eq', value };
return { field: option.field, operator: '$eq', value };
}
}
60 changes: 41 additions & 19 deletions packages/crud-request/src/types/request-query.types.ts
Expand Up @@ -22,7 +22,9 @@ export type QuerySort = {

export type QuerySortArr = [string, QuerySortOperator];

export type ComparisonOperator =
export type QuerySortOperator = 'ASC' | 'DESC';

type DeprecatedCondOperator =
| 'eq'
| 'ne'
| 'gt'
Expand All @@ -37,31 +39,41 @@ export type ComparisonOperator =
| 'notin'
| 'isnull'
| 'notnull'
| 'between'
| keyof SFieldOperator;
export type QuerySortOperator = 'ASC' | 'DESC';
| 'between';

export enum CondOperator {
EQUALS = 'eq',
NOT_EQUALS = 'ne',
GREATER_THAN = 'gt',
LOWER_THAN = 'lt',
GREATER_THAN_EQUALS = 'gte',
LOWER_THAN_EQUALS = 'lte',
STARTS = 'starts',
ENDS = 'ends',
CONTAINS = 'cont',
EXCLUDES = 'excl',
IN = 'in',
NOT_IN = 'notin',
IS_NULL = 'isnull',
NOT_NULL = 'notnull',
BETWEEN = 'between',
EQUALS = '$eq',
NOT_EQUALS = '$ne',
GREATER_THAN = '$gt',
LOWER_THAN = '$lt',
GREATER_THAN_EQUALS = '$gte',
LOWER_THAN_EQUALS = '$lte',
STARTS = '$starts',
ENDS = '$ends',
CONTAINS = '$cont',
EXCLUDES = '$excl',
IN = '$in',
NOT_IN = '$notin',
IS_NULL = '$isnull',
NOT_NULL = '$notnull',
BETWEEN = '$between',
EQUALS_LOW = '$eqL',
NOT_EQUALS_LOW = '$neL',
STARTS_LOW = '$startsL',
ENDS_LOW = '$endsL',
CONTAINS_LOW = '$contL',
EXCLUDES_LOW = '$exclL',
IN_LOW = '$inL',
NOT_IN_LOW = '$betweenL',
}

export type ComparisonOperator = DeprecatedCondOperator | keyof SFieldOperator;

// new search
export type SPrimitivesVal = string | number | boolean;

export type SFiledValues = SPrimitivesVal | Array<SPrimitivesVal>;

export type SFieldOperator = {
$eq?: SFiledValues;
$ne?: SFiledValues;
Expand All @@ -78,9 +90,19 @@ export type SFieldOperator = {
$between?: SFiledValues;
$isnull?: SFiledValues;
$notnull?: SFiledValues;
$eqL?: SFiledValues;
$neL?: SFiledValues;
$startsL?: SFiledValues;
$endsL?: SFiledValues;
$contL?: SFiledValues;
$exclL?: SFiledValues;
$inL?: SFiledValues;
$notinL?: SFiledValues;
$betweenL?: SFiledValues;
$or?: SFieldOperator;
$and?: never;
};

export type SField = SPrimitivesVal | SFieldOperator;

export type SFields = {
Expand Down
28 changes: 9 additions & 19 deletions packages/crud-request/test/request-query.parser.spec.ts
Expand Up @@ -451,10 +451,14 @@ describe('#request-query', () => {
};
const test = qp.parseParams(params, options);
const expected = [
{ field: 'foo', operator: 'eq', value: 'cb1751fd-7fcf-4eb5-b38e-86428b1fd88d' },
{ field: 'bb', operator: 'eq', value: 1 },
{ field: 'buz', operator: 'eq', value: 'string' },
{ field: 'bigInt', operator: 'eq', value: '9007199254740999' },
{
field: 'foo',
operator: '$eq',
value: 'cb1751fd-7fcf-4eb5-b38e-86428b1fd88d',
},
{ field: 'bb', operator: '$eq', value: 1 },
{ field: 'buz', operator: '$eq', value: 'string' },
{ field: 'bigInt', operator: '$eq', value: '9007199254740999' },
];
expect(test.paramsFilter).toMatchObject(expected);
});
Expand All @@ -468,24 +472,11 @@ describe('#request-query', () => {
bar: { field: 'bar', type: 'number' },
};
const test = qp.parseParams(params, options);
const expected = [{ field: 'bar', operator: 'eq', value: 123 }];
const expected = [{ field: 'bar', operator: '$eq', value: 123 }];
expect(test.paramsFilter).toMatchObject(expected);
});
});

describe('#setAuthFilter', () => {
it('it should set authFilter, 1', () => {
qp.setAuthFilter();
expect(qp.authFilter).toMatchObject({});
});
it('it should set authFilter, 2', () => {
const test = { foo: 'bar' };
qp.setAuthFilter(test);
const parsed = qp.getParsed();
expect(parsed.authFilter).toMatchObject(test);
});
});

describe('#setAuthPersist', () => {
it('it should set authPersist, 1', () => {
qp.setAuthPersist();
Expand All @@ -505,7 +496,6 @@ describe('#request-query', () => {
fields: [],
paramsFilter: [],
search: undefined,
authFilter: undefined,
authPersist: undefined,
filter: [],
or: [],
Expand Down
3 changes: 3 additions & 0 deletions packages/crud-typeorm/package.json
Expand Up @@ -37,5 +37,8 @@
},
"scripts": {
"build": "npx tsc -b"
},
"dependencies": {
"@zmotivat0r/o0": "^1.0.2"
}
}

0 comments on commit a1ed2c1

Please sign in to comment.