Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add array operators #754

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/crud-request/src/request-query.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export class RequestQueryParser implements ParsedRequestParams {
'$between',
'$inL',
'$notinL',
'$contArr',
'$intersectsArr',
];
const isEmptyValue = ['isnull', 'notnull', '$isnull', '$notnull'];
const param = data.split(this._options.delim);
Expand Down
2 changes: 2 additions & 0 deletions packages/crud-request/src/request-query.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const deprecatedComparisonOperatorsList = [
'isnull',
'notnull',
'between',
'contArr',
'intersectsArr',
];
export const comparisonOperatorsList = [
...deprecatedComparisonOperatorsList,
Expand Down
58 changes: 33 additions & 25 deletions packages/crud-request/src/types/request-query.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ type DeprecatedCondOperator =
| 'notin'
| 'isnull'
| 'notnull'
| 'between';
| 'between'
| 'contArr'
| 'intersectsArr';

export enum CondOperator {
EQUALS = '$eq',
Expand All @@ -65,39 +67,45 @@ export enum CondOperator {
EXCLUDES_LOW = '$exclL',
IN_LOW = '$inL',
NOT_IN_LOW = '$notinL',
CONTAINS_ARRAY = '$contArr',
INTERSECTS_ARRAY = '$intersectsArr',
}

export type ComparisonOperator = DeprecatedCondOperator | keyof SFieldOperator;

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

export type SFiledValues = SPrimitivesVal | Array<SPrimitivesVal>;
export type SFieldValues = SPrimitivesVal | Array<SPrimitivesVal>;
// DEPRECATED: remove before next major release (or other breaking change)
export type SFiledValues = SFieldValues;

export type SFieldOperator = {
$eq?: SFiledValues;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this block ($eq?:... to notinL) marked deleted? Looks like an indent issue with your code editor of choice...?

$ne?: SFiledValues;
$gt?: SFiledValues;
$lt?: SFiledValues;
$gte?: SFiledValues;
$lte?: SFiledValues;
$starts?: SFiledValues;
$ends?: SFiledValues;
$cont?: SFiledValues;
$excl?: SFiledValues;
$in?: SFiledValues;
$notin?: SFiledValues;
$between?: SFiledValues;
$isnull?: SFiledValues;
$notnull?: SFiledValues;
$eqL?: SFiledValues;
$neL?: SFiledValues;
$startsL?: SFiledValues;
$endsL?: SFiledValues;
$contL?: SFiledValues;
$exclL?: SFiledValues;
$inL?: SFiledValues;
$notinL?: SFiledValues;
$eq?: SFieldValues;
$ne?: SFieldValues;
$gt?: SFieldValues;
$lt?: SFieldValues;
$gte?: SFieldValues;
$lte?: SFieldValues;
$starts?: SFieldValues;
$ends?: SFieldValues;
$cont?: SFieldValues;
$excl?: SFieldValues;
$in?: SFieldValues;
$notin?: SFieldValues;
$between?: SFieldValues;
$isnull?: SFieldValues;
$notnull?: SFieldValues;
$eqL?: SFieldValues;
$neL?: SFieldValues;
$startsL?: SFieldValues;
$endsL?: SFieldValues;
$contL?: SFieldValues;
$exclL?: SFieldValues;
$inL?: SFieldValues;
$notinL?: SFieldValues;
$contArr?: Array<SPrimitivesVal>;
$intersectsArr?: Array<SPrimitivesVal>;
$or?: SFieldOperator;
$and?: never;
};
Expand Down
17 changes: 12 additions & 5 deletions packages/crud-typeorm/src/typeorm-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
const likeOperator =
this.dbName === 'postgres' ? 'ILIKE' : /* istanbul ignore next */ 'LIKE';
let str: string;
let params: ObjectLiteral;
// NOTE: may be overridden by specific operators
let params: ObjectLiteral = { [param]: cond.value };

if (cond.operator[0] !== '$') {
cond.operator = ('$' + cond.operator) as ComparisonOperator;
Expand Down Expand Up @@ -1030,16 +1031,22 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
str = `LOWER(${field}) NOT IN (:...${param})`;
break;

case '$contArr':
this.checkFilterIsArray(cond);
str = `${field} @> ARRAY[:...${param}]`;
break;

case '$intersectsArr':
this.checkFilterIsArray(cond);
str = `${field} && ARRAY[:...${param}]`;
break;

/* istanbul ignore next */
default:
str = `${field} = :${param}`;
break;
}

if (typeof params === 'undefined') {
params = { [param]: cond.value };
}

return { str, params };
}

Expand Down