Skip to content

Commit

Permalink
filterExpression -> expression
Browse files Browse the repository at this point in the history
  • Loading branch information
mindler-olli committed Mar 18, 2024
1 parent ecd274d commit 01e95e1
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 62 deletions.
8 changes: 8 additions & 0 deletions src/nodes/expressionComparatorExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ExpressionConditionComparators } from "./operands";

export type ExpressionComparatorExpressions = {
readonly kind: "ExpressionComparatorExpressions";
readonly key: string;
readonly operation: ExpressionConditionComparators;
readonly value: unknown;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { AttributeNotExistsFunctionExpression } from "./attributeNotExistsFuncti
import { BeginsWithFunctionExpression } from "./beginsWithFunctionExpression";
import { BetweenConditionExpression } from "./betweenConditionExpression";
import { ContainsFunctionExpression } from "./containsFunctionExpression";
import { FilterExpressionComparatorExpressions } from "./filterExpressionComparatorExpression";
import { FilterExpressionNode } from "./filterExpressionNode";
import { FilterExpressionNotExpression } from "./filterExpressionNotExpression";
import { ExpressionComparatorExpressions } from "./expressionComparatorExpression";
import { ExpressionNode } from "./expressionNode";
import { ExpressionNotExpression } from "./expressionNotExpression";

export type JoinType = "AND" | "OR";

export type FilterExpressionJoinTypeNode = {
readonly kind: "FilterExpressionJoinTypeNode";
export type ExpressionJoinTypeNode = {
readonly kind: "ExpressionJoinTypeNode";
readonly expr:
| FilterExpressionNode
| FilterExpressionComparatorExpressions
| FilterExpressionNotExpression
| ExpressionNode
| ExpressionComparatorExpressions
| ExpressionNotExpression
| AttributeExistsFunctionExpression
| AttributeNotExistsFunctionExpression
| BetweenConditionExpression
Expand Down
6 changes: 6 additions & 0 deletions src/nodes/expressionNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ExpressionJoinTypeNode } from "./expressionJoinTypeNode";

export type ExpressionNode = {
readonly kind: "ExpressionNode";
readonly expressions: ExpressionJoinTypeNode[];
};
6 changes: 6 additions & 0 deletions src/nodes/expressionNotExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ExpressionNode } from "./expressionNode";

export type ExpressionNotExpression = {
readonly kind: "ExpressionNotExpression";
readonly expr: ExpressionNode;
};
8 changes: 0 additions & 8 deletions src/nodes/filterExpressionComparatorExpression.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/nodes/filterExpressionNode.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/nodes/filterExpressionNotExpression.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/nodes/operands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export type FunctionExpression =
export type NotExpression = "NOT";

export type KeyConditionComparators = "=" | "<" | "<=" | ">" | ">=";
export type FilterConditionComparators = KeyConditionComparators | "<>";
export type ExpressionConditionComparators = KeyConditionComparators | "<>";
4 changes: 2 additions & 2 deletions src/nodes/queryNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AttributesNode } from "./attributesNode";
import { ConsistentReadNode } from "./consistentReadNode";
import { FilterExpressionNode } from "./filterExpressionNode";
import { ExpressionNode } from "./expressionNode";
import { KeyConditionNode } from "./keyConditionNode";
import { LimitNode } from "./limitNode";
import { ScanIndexForwardNode } from "./scanIndexForwardNode";
Expand All @@ -10,7 +10,7 @@ export type QueryNode = {
readonly kind: "QueryNode";
readonly table: TableNode;
readonly keyConditions: KeyConditionNode[];
readonly filterExpression: FilterExpressionNode;
readonly filterExpression: ExpressionNode;
readonly consistentRead?: ConsistentReadNode;
readonly scanIndexForward?: ScanIndexForwardNode;
readonly limit?: LimitNode;
Expand Down
37 changes: 18 additions & 19 deletions src/queryBuilders/queryQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { AttributeExistsFunctionExpression } from "../nodes/attributeExistsFunctionExpression";
import { AttributeNotExistsFunctionExpression } from "../nodes/attributeNotExistsFunctionExpression";
import {
FilterExpressionJoinTypeNode,
ExpressionJoinTypeNode,
JoinType,
} from "../nodes/filterExpressionJoinTypeNode";
} from "../nodes/expressionJoinTypeNode";
import {
BetweenExpression,
FilterConditionComparators,
ExpressionConditionComparators,
FunctionExpression,
KeyConditionComparators,
NotExpression,
Expand Down Expand Up @@ -63,7 +63,7 @@ export interface QueryQueryBuilderInterface<DDB, Table extends keyof DDB, O> {
// Regular operand
filterExpression<Key extends ObjectKeyPaths<PickNonKeys<DDB[Table]>>>(
key: Exclude<Key, "NOT">,
operation: Key extends NotExpression ? never : FilterConditionComparators,
operation: Key extends NotExpression ? never : ExpressionConditionComparators,
val: StripKeys<GetFromPath<DDB[Table], Key>>
): QueryQueryBuilderInterface<DDB, Table, O>;

Expand Down Expand Up @@ -123,7 +123,7 @@ export interface QueryQueryBuilderInterface<DDB, Table extends keyof DDB, O> {
// Regular operand
orFilterExpression<Key extends ObjectKeyPaths<PickNonKeys<DDB[Table]>>>(
key: Key,
operation: FilterConditionComparators,
operation: ExpressionConditionComparators,
val: StripKeys<GetFromPath<DDB[Table], Key>>
): QueryQueryBuilderInterface<DDB, Table, O>;

Expand Down Expand Up @@ -203,7 +203,7 @@ export interface QueryQueryBuilderInterfaceWithOnlyFilterOperations<
*/
filterExpression<Key extends ObjectKeyPaths<PickNonKeys<DDB[Table]>>>(
key: Key,
operation: FilterConditionComparators,
operation: ExpressionConditionComparators,
val: StripKeys<GetFromPath<DDB[Table], Key>>
): QueryQueryBuilderInterfaceWithOnlyFilterOperations<DDB, Table, O>;

Expand Down Expand Up @@ -255,7 +255,7 @@ export interface QueryQueryBuilderInterfaceWithOnlyFilterOperations<
*/
orFilterExpression<Key extends ObjectKeyPaths<PickNonKeys<DDB[Table]>>>(
key: Key,
operation: FilterConditionComparators,
operation: ExpressionConditionComparators,
val: StripKeys<GetFromPath<DDB[Table], Key>>
): QueryQueryBuilderInterfaceWithOnlyFilterOperations<DDB, Table, O>;

Expand Down Expand Up @@ -313,7 +313,7 @@ type FilterExprArgs<
> =
| [
key: Key,
operation: FilterConditionComparators,
operation: ExpressionConditionComparators,
value: StripKeys<GetFromPath<DDB[Table], Key>>
]
| [
Expand Down Expand Up @@ -452,7 +452,7 @@ export class QueryQueryBuilder<
filterExpression: {
...this.#props.node.filterExpression,
expressions: this.#props.node.filterExpression.expressions.concat({
kind: "FilterExpressionJoinTypeNode",
kind: "ExpressionJoinTypeNode",
expr: {
kind: "BeginsWithFunctionExpression",
key,
Expand All @@ -473,7 +473,7 @@ export class QueryQueryBuilder<
filterExpression: {
...this.#props.node.filterExpression,
expressions: this.#props.node.filterExpression.expressions.concat({
kind: "FilterExpressionJoinTypeNode",
kind: "ExpressionJoinTypeNode",
expr: {
kind: "ContainsFunctionExpression",
key,
Expand Down Expand Up @@ -506,7 +506,7 @@ export class QueryQueryBuilder<
filterExpression: {
...this.#props.node.filterExpression,
expressions: this.#props.node.filterExpression.expressions.concat({
kind: "FilterExpressionJoinTypeNode",
kind: "ExpressionJoinTypeNode",
expr: resultExpr,
joinType,
}),
Expand All @@ -523,7 +523,7 @@ export class QueryQueryBuilder<
filterExpression: {
...this.#props.node.filterExpression,
expressions: this.#props.node.filterExpression.expressions.concat({
kind: "FilterExpressionJoinTypeNode",
kind: "ExpressionJoinTypeNode",
expr: {
kind: "BetweenConditionExpression",
key,
Expand Down Expand Up @@ -551,10 +551,10 @@ export class QueryQueryBuilder<
filterExpression: {
...this.#props.node.filterExpression,
expressions: this.#props.node.filterExpression.expressions.concat({
kind: "FilterExpressionJoinTypeNode",
kind: "ExpressionJoinTypeNode",
joinType,
expr: {
kind: "FilterExpressionComparatorExpressions",
kind: "ExpressionComparatorExpressions",
key,
operation,
value,
Expand All @@ -580,7 +580,7 @@ export class QueryQueryBuilder<
...this.#props.node,
filterExpression: {
expressions: [],
kind: "FilterExpressionNode",
kind: "ExpressionNode",
},
},
});
Expand All @@ -589,8 +589,8 @@ export class QueryQueryBuilder<

const { filterExpression } = result._getNode();

let resultNode: FilterExpressionJoinTypeNode = {
kind: "FilterExpressionJoinTypeNode",
let resultNode: ExpressionJoinTypeNode = {
kind: "ExpressionJoinTypeNode",
expr: filterExpression,
joinType,
};
Expand All @@ -599,7 +599,7 @@ export class QueryQueryBuilder<
resultNode = {
...resultNode,
expr: {
kind: "FilterExpressionNotExpression",
kind: "ExpressionNotExpression",
expr: filterExpression,
},
};
Expand All @@ -621,7 +621,6 @@ export class QueryQueryBuilder<
throw new Error("Invalid arguments given to filterExpression");
}

// TODO: Add support for all operations from here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html#Expressions.OperatorsAndFunctions.Syntax
filterExpression<Key extends ObjectKeyPaths<PickNonKeys<DDB[Table]>>>(
...args: FilterExprArgs<DDB, Table, O, Key>
): QueryQueryBuilderInterface<DDB, Table, O> {
Expand Down
22 changes: 11 additions & 11 deletions src/queryCompiler/queryCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetCommand, PutCommand, QueryCommand } from "@aws-sdk/lib-dynamodb";
import { FilterExpressionJoinTypeNode } from "../nodes/filterExpressionJoinTypeNode";
import { FilterExpressionNode } from "../nodes/filterExpressionNode";
import { ExpressionJoinTypeNode } from "../nodes/expressionJoinTypeNode";
import { ExpressionNode } from "../nodes/expressionNode";
import { GetNode } from "../nodes/getNode";
import { KeyConditionNode } from "../nodes/keyConditionNode";
import { QueryNode } from "../nodes/queryNode";
Expand Down Expand Up @@ -68,7 +68,7 @@ export class QueryCompiler {
attributeNames
);

const compiledFilterExpression = this.compileFilterExpression(
const compiledFilterExpression = this.compileExpression(
filterExpressionNode,
filterExpressionAttributeValues,
attributeNames
Expand Down Expand Up @@ -151,8 +151,8 @@ export class QueryCompiler {
};
}

compileFilterExpression = (
expression: FilterExpressionNode,
compileExpression = (
expression: ExpressionNode,
filterExpressionAttributeValues: Map<string, unknown>,
attributeNames: Map<string, string>
) => {
Expand All @@ -174,7 +174,7 @@ export class QueryCompiler {
};

compileFilterExpressionJoinNodes = (
{ expr }: FilterExpressionJoinTypeNode,
{ expr }: ExpressionJoinTypeNode,
filterExpressionAttributeValues: Map<string, unknown>,
attributeNames: Map<string, string>
) => {
Expand All @@ -193,9 +193,9 @@ export class QueryCompiler {
}

switch (expr.kind) {
case "FilterExpressionNode": {
case "ExpressionNode": {
res += "(";
res += this.compileFilterExpression(
res += this.compileExpression(
expr,
filterExpressionAttributeValues,
attributeNames
Expand All @@ -204,15 +204,15 @@ export class QueryCompiler {
break;
}

case "FilterExpressionComparatorExpressions": {
case "ExpressionComparatorExpressions": {
res += `${attributeName} ${expr.operation} ${attributeValue}`;
filterExpressionAttributeValues.set(attributeValue, expr.value);
break;
}

case "FilterExpressionNotExpression": {
case "ExpressionNotExpression": {
res += "NOT (";
res += this.compileFilterExpression(
res += this.compileExpression(
expr.expr,
filterExpressionAttributeValues,
attributeNames
Expand Down
2 changes: 1 addition & 1 deletion src/queryCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class QueryCreator<DDB> {
},
keyConditions: [],
filterExpression: {
kind: "FilterExpressionNode",
kind: "ExpressionNode",
expressions: [],
},
},
Expand Down

0 comments on commit 01e95e1

Please sign in to comment.