Skip to content

Commit

Permalink
Merge pull request #4 from mindler-sasu/main
Browse files Browse the repository at this point in the history
prevent await on instance
  • Loading branch information
woltsu authored Mar 14, 2024
2 parents 4f6b586 + 1acc74d commit a64daac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/queryBuilders/getItemQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SelectAttributes,
StripKeys,
} from "../typeHelpers";
import { preventAwait } from "../util/preventAwait";

export interface GetQueryBuilderInterface<DDB, Table extends keyof DDB, O> {
keys<Keys extends PickPk<DDB[Table]> & PickSkRequired<DDB[Table]>>(
Expand Down Expand Up @@ -89,6 +90,11 @@ export class GetQueryBuilder<DDB, Table extends keyof DDB, O extends DDB[Table]>
};
}

preventAwait(
GetQueryBuilder,
"Don't await GetQueryBuilder instances directly. To execute the query you need to call the `execute` method"
);

interface GetQueryBuilderProps {
readonly node: GetNode;
readonly ddbClient: DynamoDBDocumentClient;
Expand Down
15 changes: 15 additions & 0 deletions src/tsynamo.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ describe("tsynamo", () => {
expect(data?.someBoolean).toBe(TEST_ITEM_1.someBoolean);
expect(Object.keys(data!).length).toBe(2);
});
it("can't await instance directly", async () => {
expect(
async () =>
await tsynamoClient
.getItemFrom("myTable")
.keys({
userId: TEST_ITEM_1.userId,
timestamp: TEST_ITEM_1.timestamp,
})
.consistentRead(true)
.attributes(["somethingElse", "someBoolean"])
).rejects.toThrowError(
"Don't await GetQueryBuilder instances directly. To execute the query you need to call the `execute` method"
);
});
});

describe("query", () => {
Expand Down
10 changes: 10 additions & 0 deletions src/util/preventAwait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const preventAwait = (c: Function, message: string): void => {
Object.defineProperties(c.prototype, {
then: {
enumerable: false,
value: () => {
throw new Error(message);
},
},
});
};

0 comments on commit a64daac

Please sign in to comment.