Skip to content

Commit

Permalink
betterment output type
Browse files Browse the repository at this point in the history
  • Loading branch information
mindler-olli committed Mar 17, 2024
1 parent 754773d commit a919fd4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/queryBuilders/getItemQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { GetNode } from "../nodes/getNode";
import { QueryCompiler } from "../queryCompiler";
import {
DeepPartial,
ExecuteOutput,
ObjectFullPaths,
PickPk,
PickSkRequired,
SelectAttributes,
StripKeys,
} from "../typeHelpers";
import { preventAwait } from "../util/preventAwait";

Expand All @@ -22,7 +21,7 @@ export interface GetQueryBuilderInterface<DDB, Table extends keyof DDB, O> {
attributes: A
): GetQueryBuilderInterface<DDB, Table, SelectAttributes<DDB[Table], A>>;

execute(): Promise<StripKeys<DeepPartial<O>> | undefined>;
execute(): Promise<ExecuteOutput<O> | undefined>;
}

export class GetQueryBuilder<DDB, Table extends keyof DDB, O extends DDB[Table]>
Expand Down Expand Up @@ -77,10 +76,10 @@ export class GetQueryBuilder<DDB, Table extends keyof DDB, O extends DDB[Table]>
}) as any;
}

execute = async (): Promise<StripKeys<DeepPartial<O>> | undefined> => {
execute = async (): Promise<ExecuteOutput<O> | undefined> => {
const command = this.#props.queryCompiler.compile(this.#props.node);
const item = await this.#props.ddbClient.send(command);
return (item.Item as StripKeys<DeepPartial<O>>) ?? undefined;
return (item.Item as ExecuteOutput<O>) ?? undefined;
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/queryBuilders/queryQueryBuilder.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,22 @@ describe("QueryQueryBuilder", () => {
});

it("handles a FilterExpression that uses attribute_exists and attribute_not_exists", async () => {
let data = await tsynamoClient
const data = await tsynamoClient
.query("myTable")
.keyCondition("userId", "=", "123")
.filterExpression("nested.nestedString", "attribute_exists")
.execute();

expect(data).toMatchSnapshot();

data = await tsynamoClient
const data2 = await tsynamoClient
.query("myTable")
.keyCondition("userId", "=", "123")
.filterExpression("nested.nestedString", "attribute_not_exists")
.attributes(["userId", "nested"])
.execute();

expect(data).toMatchSnapshot();
expect(data2).toMatchSnapshot();
});

it("handles a FilterExpression that uses begins_with", async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/queryBuilders/queryQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { QueryNode } from "../nodes/queryNode";
import { QueryCompiler } from "../queryCompiler";
import {
DeepPartial,
ExecuteOutput,
GetFromPath,
ObjectFullPaths,
ObjectKeyPaths,
Expand All @@ -28,7 +28,7 @@ import {
import { preventAwait } from "../util/preventAwait";

export interface QueryQueryBuilderInterface<DDB, Table extends keyof DDB, O> {
execute(): Promise<StripKeys<DeepPartial<O>>[] | undefined>;
execute(): Promise<ExecuteOutput<O>[] | undefined>;

/**
* keyCondition methods
Expand Down Expand Up @@ -694,10 +694,10 @@ export class QueryQueryBuilder<
}) as any;
}

execute = async (): Promise<StripKeys<DeepPartial<O>>[] | undefined> => {
execute = async (): Promise<ExecuteOutput<O>[] | undefined> => {
const command = this.#props.queryCompiler.compile(this.#props.node);
const result = await this.#props.ddbClient.send(command);
return (result.Items as StripKeys<DeepPartial<O>>[]) ?? undefined;
return (result.Items as ExecuteOutput<O>[]) ?? undefined;
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/typeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ export type DeepPartial<T> = {
: T[P];
};

export type ExecuteOutput<O> = StripKeys<PickPk<O> & DeepPartial<O>>;
export type ExecuteOutput<O> = StripKeys<
PickPk<O> & PickSkRequired<O> & DeepPartial<O>
>;

type IntersectionToSingleObject<T> = T extends infer U
? { [K in keyof U]: U[K] }
Expand Down

0 comments on commit a919fd4

Please sign in to comment.