Skip to content

Commit

Permalink
feat(core): improve logical expression input parser
Browse files Browse the repository at this point in the history
  • Loading branch information
whimzyLive committed Aug 7, 2021
1 parent 1f6cd8c commit e713ffc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
24 changes: 10 additions & 14 deletions packages/core/src/classes/expression/condition-options-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
RequireOnlyOne,
ConditionType,
ATTRIBUTE_TYPE,
RequireAtLeastOne,
ResolveScalarType,
} from '@typedorm/common';

Expand Down Expand Up @@ -64,26 +63,23 @@ type AttributeConditionOptions<Entity> =

type RecursiveConditionOptions<Entity> = {
// for `AND` and `OR` logical operators require at least one of defined options or other self
[key in Extract<
ConditionType.LogicalOperator,
'OR' | 'AND'
>]: RequireAtLeastOne<
[key in Extract<ConditionType.LogicalOperator, 'OR' | 'AND'>]: Partial<
AttributeConditionOptions<Entity> &
// manually infer recursive type
RecursiveConditionOptions<Entity> extends infer R
? R
: never
>;
RecursiveConditionOptions<Entity>
> extends infer R
? R
: never;
} &
// for `NOT` logical operators require one from defined options or other self
{
[key in Extract<ConditionType.LogicalOperator, 'NOT'>]: RequireOnlyOne<
[key in Extract<ConditionType.LogicalOperator, 'NOT'>]: Partial<
AttributeConditionOptions<Entity> &
// manually infer recursive type
RecursiveConditionOptions<Entity> extends infer R
? R
: never
>;
RecursiveConditionOptions<Entity>
> extends infer R
? R
: never;
} &
// require attribute filter
AttributeConditionOptions<Entity>;
Expand Down
13 changes: 4 additions & 9 deletions packages/core/src/classes/expression/expression-input-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,23 @@ export class ExpressionInputParser {
);
const base = parsedExpList.shift();
if (!base) {
throw new Error(
`Value for operator "${operatorOrAttr}" can not be empty`
);
return new ExpClass();
}
switch (operatorOrAttr) {
case 'AND': {
if (!parsedExpList?.length) {
throw new Error(
`Value for operator "${operatorOrAttr}" must contain more that 1 attributes.`
);
return base;
}
return base.mergeMany(parsedExpList as T[], MERGE_STRATEGY.AND);
}
case 'OR': {
if (!parsedExpList?.length) {
throw new Error(
`Value for operator "${operatorOrAttr}" must contain more that 1 attributes.`
);
return base;
}
return base.mergeMany(parsedExpList as T[], MERGE_STRATEGY.OR);
}
case 'NOT': {
// not can not contain more than one items
if (parsedExpList?.length) {
throw new Error(
`Value for operator "${operatorOrAttr}" can not contain more than 1 attributes.`
Expand Down
21 changes: 10 additions & 11 deletions packages/core/src/classes/expression/filter-options-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ATTRIBUTE_TYPE,
FilterType,
NonKeyAttributesWithReturnType,
RequireAtLeastOne,
RequireOnlyOne,
ResolveScalarType,
} from '@typedorm/common';
Expand Down Expand Up @@ -60,23 +59,23 @@ type AttributeFilterOptions<Entity, PrimaryKey> =

type RecursiveFilterOptions<Entity, PrimaryKey> = {
// for `AND` and `OR` logical operators require at least one of defined options or other self
[key in Extract<FilterType.LogicalOperator, 'OR' | 'AND'>]: RequireAtLeastOne<
[key in Extract<FilterType.LogicalOperator, 'OR' | 'AND'>]: Partial<
AttributeFilterOptions<Entity, PrimaryKey> &
// manually infer recursive type
RecursiveFilterOptions<Entity, PrimaryKey> extends infer R
? R
: never
>;
RecursiveFilterOptions<Entity, PrimaryKey>
> extends infer R
? R
: never;
} &
// for `NOT` logical operators require one from defined options or other self
{
[key in Extract<FilterType.LogicalOperator, 'NOT'>]: RequireOnlyOne<
[key in Extract<FilterType.LogicalOperator, 'NOT'>]: Partial<
AttributeFilterOptions<Entity, PrimaryKey> &
// manually infer recursive type
RecursiveFilterOptions<Entity, PrimaryKey> extends infer R
? R
: never
>;
RecursiveFilterOptions<Entity, PrimaryKey>
> extends infer R
? R
: never;
} &
// require attribute filter
AttributeFilterOptions<Entity, PrimaryKey>;
Expand Down

0 comments on commit e713ffc

Please sign in to comment.