Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#439): add missing TypeScript types for attributes property on scan/find/match methods #470

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,8 @@ All notable changes to this project will be documented in this file. Breaking ch
- [Issue #464](https://github.com/tywalch/electrodb/issues/464); When specifing return attributes on retrieval methods, ElectroDB would unexpectly return null or missing values if the options chosen resulted in an empty object being returned. This behavor could be confused with no results being found. ElectroDB now returns the empty object in these cases.

### Added
- ElectroDB Error objects no contain a `params()` method. If your operation resulted in an error thrown by the DynamoDB client, you can call the `params()` method to get the compiled parameters sent to DynamoDB. This can be helpful for debugging. Note, that if the error was thrown prior to parameter creation (validation errors, invalid query errors, etc) then the `params()` method will return the value `null`.
- ElectroDB Error objects no contain a `params()` method. If your operation resulted in an error thrown by the DynamoDB client, you can call the `params()` method to get the compiled parameters sent to DynamoDB. This can be helpful for debugging. Note, that if the error was thrown prior to parameter creation (validation errors, invalid query errors, etc) then the `params()` method will return the value `null`.

## [3.1.1]
### Fixed
- [Issue #439](https://github.com/tywalch/electrodb/issues/439); Fixed missing TypeScript types for `attributes` property on `scan`, `find`, and `match` methods.
25 changes: 15 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,18 +1236,18 @@ export interface RecordsActionOptions<
F extends string,
C extends string,
S extends Schema<A, F, C>,
Items,
ResponseItem,
IndexCompositeAttributes,
> {
go: QueryRecordsGo<Items>;
go: QueryRecordsGo<ResponseItem>;
params: ParamRecord;
where: WhereClause<
A,
F,
C,
S,
Item<A, F, C, S, S["attributes"]>,
RecordsActionOptions<A, F, C, S, Items, IndexCompositeAttributes>
RecordsActionOptions<A, F, C, S, ResponseItem, IndexCompositeAttributes>
>;
}

Expand Down Expand Up @@ -2855,11 +2855,16 @@ export type ServiceQueryRecordsGo<
options?: Options,
) => Promise<{ data: T; cursor: string | null }>;

export type QueryRecordsGo<ResponseType, Options = QueryOptions> = <
T = ResponseType,
>(
export type QueryRecordsGo<Item> = <Options extends GoQueryTerminalOptions<keyof Item>>(
options?: Options,
) => Promise<{ data: T; cursor: string | null }>;
) => Options extends GoQueryTerminalOptions<infer Attr>
? Promise<{
data: Array<{
[Name in keyof Item as Name extends Attr ? Name : never]: Item[Name];
}>;
cursor: string | null;
}>
: Promise<{ data: Array<Item>; cursor: string | null }>;

export type UpdateRecordGo<ResponseType, Keys> = <
T = ResponseType,
Expand Down Expand Up @@ -5247,7 +5252,7 @@ export class Entity<
F,
C,
S,
ResponseItem<A, F, C, S>[],
ResponseItem<A, F, C, S>,
AllTableIndexCompositeAttributes<A, F, C, S>
>;

Expand All @@ -5258,7 +5263,7 @@ export class Entity<
F,
C,
S,
ResponseItem<A, F, C, S>[],
ResponseItem<A, F, C, S>,
AllTableIndexCompositeAttributes<A, F, C, S>
>;

Expand All @@ -5267,7 +5272,7 @@ export class Entity<
F,
C,
S,
ResponseItem<A, F, C, S>[],
ResponseItem<A, F, C, S>,
TableIndexCompositeAttributes<A, F, C, S>
>;
query: Queries<A, F, C, S>;
Expand Down
17 changes: 17 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,23 @@ expectAssignable<"paramtest">(
expectAssignable<"paramtest">(
entityWithoutSK.get({ attr1: "abc" }).params<"paramtest">(),
);
expectAssignable<Promise<{ attr1: string; attr2: string }[]>>(
entityWithSK.scan
.go({ attributes: ["attr1", "attr2"] })
.then((res) => res.data),
);
expectAssignable<Promise<{ attr1: string; attr7?: any; attr6?: number }[]>>(
entityWithSK
.find({ attr10: true })
.go({ attributes: ["attr1", "attr7", "attr6"] })
.then((res) => res.data),
);
expectAssignable<Promise<{ attr4: "abc" | "ghi"; attr5?: string }[]>>(
entityWithSK
.match({ attr3: "def" })
.go({ attributes: ["attr4", "attr5"] })
.then((res) => res.data),
);
entityWithSK
.get([{ attr1: "abc", attr2: "def" }])
.go()
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electrodb",
"version": "3.1.0",
"version": "3.1.1",
"description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
"main": "index.js",
"scripts": {
Expand Down
37 changes: 37 additions & 0 deletions test/queries.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GoQueryTerminal,
// PageQueryTerminal,
Queries,
QueryRecordsGo,
} from "../";
import { expectType, expectError, expectNotType } from "tsd";

Expand Down Expand Up @@ -48,6 +49,10 @@ class MockEntity<
return {} as GoQueryTerminal<A, F, C, S, ResponseItem<A, F, C, S>>;
}

getScanTerminal(): QueryRecordsGo<ResponseItem<A, F, C, S>> {
return {} as QueryRecordsGo<ResponseItem<A, F, C, S>>;
}

// getPageQueryTerminal(): PageQueryTerminal<A,F,C,S, ResponseItem<A,F,C,S>, {abc: string}> {
// return {} as PageQueryTerminal<A,F,C,S, ResponseItem<A,F,C,S>, {abc: string}>;
// }
Expand Down Expand Up @@ -440,6 +445,8 @@ const entityWithoutSKE = new Entity({
});

const entityWithSKGo = entityWithSK.getGoQueryTerminal();
const entityWithSKScan = entityWithSK.getScanTerminal();

entityWithSKGo({
attributes: ["attr2", "attr3", "attr4", "attr6", "attr8"],
}).then((results) => {
Expand All @@ -454,6 +461,20 @@ entityWithSKGo({
>(results.data);
});

entityWithSKScan({
attributes: ["attr2", "attr3", "attr4", "attr6", "attr8"],
}).then((results) => {
expectType<
{
attr2: string;
attr3?: "123" | "def" | "ghi" | undefined;
attr4: "abc" | "ghi";
attr6?: number | undefined;
attr8: boolean;
}[]
>(results.data);
});

// const entityWithSKPage = entityWithSK.getPageQueryTerminal();
// entityWithSKPage(null, {attributes: ['attr2', 'attr3', 'attr4', 'attr6', 'attr8']}).then(data => {
// const [page, results] = data;
Expand All @@ -472,6 +493,8 @@ entityWithSKGo({
// });

const entityWithoutSKGo = entityWithoutSK.getGoQueryTerminal();
const entityWithoutSKScan = entityWithoutSK.getScanTerminal();

entityWithoutSKGo({
attributes: ["attr2", "attr3", "attr4", "attr6", "attr8"],
}).then((results) => {
Expand All @@ -486,6 +509,20 @@ entityWithoutSKGo({
>(magnify(results.data));
});

entityWithoutSKScan({
attributes: ["attr2", "attr3", "attr4", "attr6", "attr8"],
}).then((results) => {
expectType<
{
attr2?: string | undefined;
attr3?: "123" | "def" | "ghi" | undefined;
attr4: "abc" | "def";
attr6?: number | undefined;
attr8: boolean;
}[]
>(magnify(results.data));
});

// const entityWithoutSKPage = entityWithoutSK.getPageQueryTerminal();
// entityWithoutSKPage(null, {attributes: ['attr2', 'attr3', 'attr4', 'attr6', 'attr8']}).then(data => {
// const [page, results] = data;
Expand Down
53 changes: 53 additions & 0 deletions test/ts_connected.crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4339,6 +4339,59 @@ describe("attributes query option", () => {
attr10: item.attr10,
},
]);

const scanItem = await entityWithSK.scan
.go({
attributes: ["attr2", "attr9", "attr5", "attr10"],
})
.then((res) => res.data);

expect(scanItem).to.deep.equal([
{
attr2: item.attr2,
attr9: item.attr9,
attr5: item.attr5,
attr10: item.attr10,
},
]);

const matchItem = await entityWithSK
.match({
attr1: item.attr1,
attr2: item.attr2,
})
.go({
attributes: ["attr2", "attr9", "attr5", "attr10"],
})
.then((res) => res.data);

expect(matchItem).to.deep.equal([
{
attr2: item.attr2,
attr9: item.attr9,
attr5: item.attr5,
attr10: item.attr10,
},
]);

const findItem = await entityWithSK
.find({
attr1: item.attr1,
attr2: item.attr2,
})
.go({
attributes: ["attr2", "attr9", "attr5", "attr10"],
})
.then((res) => res.data);

expect(findItem).to.deep.equal([
{
attr2: item.attr2,
attr9: item.attr9,
attr5: item.attr5,
attr10: item.attr10,
},
]);
});

it("should not add entity identifiers", async () => {
Expand Down
Loading