From b621b639152d7cfd0b6f640f4af7d18f268ae906 Mon Sep 17 00:00:00 2001 From: Olli Warro Date: Mon, 18 Mar 2024 23:02:46 +0200 Subject: [PATCH] fix nested expressions with keys --- src/queryBuilders/expressionBuilder.ts | 248 ++++++++++++++++++++----- src/queryBuilders/queryQueryBuilder.ts | 8 +- 2 files changed, 203 insertions(+), 53 deletions(-) diff --git a/src/queryBuilders/expressionBuilder.ts b/src/queryBuilders/expressionBuilder.ts index c9ba5b7..9cb0a3e 100644 --- a/src/queryBuilders/expressionBuilder.ts +++ b/src/queryBuilders/expressionBuilder.ts @@ -20,7 +20,7 @@ import { export interface ExpressionBuilderInterface { // Regular operand - expression>>( + expression>( key: Exclude, operation: Key extends NotExpression ? never @@ -29,7 +29,7 @@ export interface ExpressionBuilderInterface { ): ExpressionBuilderInterface; // function expression for functions that only take path as param - expression>>( + expression>( key: Exclude, func: Extract< FunctionExpression, @@ -39,7 +39,7 @@ export interface ExpressionBuilderInterface { // CONTAINS function expression expression< - Key extends ObjectKeyPaths>, + Key extends ObjectKeyPaths, Property extends GetFromPath & unknown[] >( key: Key, @@ -48,14 +48,14 @@ export interface ExpressionBuilderInterface { ): ExpressionBuilderInterface; // BEGINS_WITH function expression - expression>>( + expression>( key: Key, expr: Extract, substr: string ): ExpressionBuilderInterface; // BETWEEN expression - expression>>( + expression>( key: Key, expr: BetweenExpression, left: StripKeys>, @@ -78,13 +78,13 @@ export interface ExpressionBuilderInterface { ): ExpressionBuilderInterface; // Or expressions - orExpression>>( + orExpression>( key: Key, operation: ExpressionConditionComparators, val: StripKeys> ): ExpressionBuilderInterface; - orExpression>>( + orExpression>( key: Exclude, func: Extract< FunctionExpression, @@ -92,14 +92,14 @@ export interface ExpressionBuilderInterface { > ): ExpressionBuilderInterface; - orExpression>>( + orExpression>( key: Key, func: Extract, substr: string ): ExpressionBuilderInterface; orExpression< - Key extends ObjectKeyPaths>, + Key extends ObjectKeyPaths, Property extends GetFromPath & unknown[] >( key: Key, @@ -107,7 +107,7 @@ export interface ExpressionBuilderInterface { value: StripKeys[number] ): ExpressionBuilderInterface; - orExpression>>( + orExpression>( key: Key, expr: BetweenExpression, left: StripKeys>, @@ -133,111 +133,258 @@ export interface ExpressionBuilderInterface { export interface ExpressionBuilderInterfaceWithOnlyExpressionOperations< DDB, Table extends keyof DDB, - O + O, + AllowKeys = true > { /** * expression methods */ - expression>>( + expression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Key, operation: ExpressionConditionComparators, val: StripKeys> - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; - expression>>( + expression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Exclude, func: Extract< FunctionExpression, "attribute_exists" | "attribute_not_exists" > - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; - expression>>( + expression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Key, func: Extract, substr: string - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; expression< - Key extends ObjectKeyPaths>, + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + >, Property extends GetFromPath & unknown[] >( key: Key, expr: Extract, value: StripKeys[number] - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; - expression>>( + expression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Key, expr: BetweenExpression, left: StripKeys>, right: StripKeys> - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; expression( not: NotExpression, builder: ( - qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations - ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; expression( builder: ( - qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations - ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; /** * orExpression methods */ - orExpression>>( + orExpression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Key, operation: ExpressionConditionComparators, val: StripKeys> - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; - orExpression>>( + orExpression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Exclude, func: Extract< FunctionExpression, "attribute_exists" | "attribute_not_exists" > - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; - orExpression>>( + orExpression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Key, func: Extract, substr: string - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; orExpression< - Key extends ObjectKeyPaths>, + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + >, Property extends GetFromPath & unknown[] >( key: Key, expr: Extract, value: StripKeys[number] - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; - orExpression>>( + orExpression< + Key extends ObjectKeyPaths< + AllowKeys extends true ? DDB[Table] : PickNonKeys + > + >( key: Key, expr: BetweenExpression, left: StripKeys>, right: StripKeys> - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; orExpression( not: NotExpression, builder: ( - qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations - ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; orExpression( builder: ( - qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations - ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations - ): ExpressionBuilderInterfaceWithOnlyExpressionOperations; + qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + > + ): ExpressionBuilderInterfaceWithOnlyExpressionOperations< + DDB, + Table, + O, + AllowKeys + >; _getNode(): ExpressionNode; } @@ -246,7 +393,8 @@ export type ExprArgs< DDB, Table extends keyof DDB, O, - Key extends ObjectKeyPaths> + Key, + AllowKeysInExpression = true > = | [ key: Key, @@ -280,7 +428,8 @@ export type ExprArgs< qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations< DDB, Table, - O + O, + AllowKeysInExpression > ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations ] @@ -289,7 +438,8 @@ export type ExprArgs< qb: ExpressionBuilderInterfaceWithOnlyExpressionOperations< DDB, Table, - O + O, + AllowKeysInExpression > ) => ExpressionBuilderInterfaceWithOnlyExpressionOperations ]; @@ -306,7 +456,7 @@ export class ExpressionBuilder< this.#props = props; } - _expression>>( + _expression>( joinType: JoinType, ...args: ExprArgs ): ExpressionBuilderInterface { @@ -466,13 +616,13 @@ export class ExpressionBuilder< throw new Error("Invalid arguments given to expression builder"); } - expression>>( + expression>( ...args: ExprArgs ): ExpressionBuilderInterface { return this._expression("AND", ...args); } - orExpression>>( + orExpression>( ...args: ExprArgs ): ExpressionBuilderInterface { return this._expression("OR", ...args); diff --git a/src/queryBuilders/queryQueryBuilder.ts b/src/queryBuilders/queryQueryBuilder.ts index d7fb667..6375ff0 100644 --- a/src/queryBuilders/queryQueryBuilder.ts +++ b/src/queryBuilders/queryQueryBuilder.ts @@ -21,11 +21,11 @@ import { ExprArgs, ExpressionBuilder } from "./expressionBuilder"; export interface QueryQueryBuilderInterface { filterExpression>>( - ...args: ExprArgs + ...args: ExprArgs ): QueryQueryBuilderInterface; orFilterExpression>>( - ...args: ExprArgs + ...args: ExprArgs ): QueryQueryBuilderInterface; /** @@ -156,7 +156,7 @@ export class QueryQueryBuilder< } filterExpression>>( - ...args: ExprArgs + ...args: ExprArgs ): QueryQueryBuilderInterface { const eB = new ExpressionBuilder({ node: { ...this.#props.node.filterExpression }, @@ -174,7 +174,7 @@ export class QueryQueryBuilder< } orFilterExpression>>( - ...args: ExprArgs + ...args: ExprArgs ): QueryQueryBuilderInterface { const eB = new ExpressionBuilder({ node: { ...this.#props.node.filterExpression },