Skip to content

Commit

Permalink
betterment to selection type of tuple.
Browse files Browse the repository at this point in the history
  • Loading branch information
mindler-sasu committed Mar 17, 2024
1 parent 3480f46 commit 8791c75
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
18 changes: 16 additions & 2 deletions src/queryBuilders/getItemQueryBuilder.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,25 @@ describe("GetItemQueryBuilder", () => {
stringTimestamp: "123",
})
.consistentRead(true)
.attributes(["tuplez[0]"])
.attributes(["tuplez[1]"])
.execute();

expect(Object.keys(data!).length).toBe(1);
expect(data?.tuplez?.[0]).toEqual(TEST_DATA[6].tuplez[0]);
expect(data?.tuplez?.[0]).toEqual(TEST_DATA[6].tuplez[1]);
});
it("handles selecting multiple attributes from tuples", async () => {
const data = await tsynamoClient
.getItemFrom("myOtherTable")
.keys({
userId: TEST_DATA[6].userId,
stringTimestamp: "123",
})
.consistentRead(true)
.attributes(["tuplez[0]", "tuplez[1]"])
.execute();

expect(Object.keys(data!).length).toBe(1);
expect(data?.tuplez).toEqual(TEST_DATA[6].tuplez);
});

it("can't await instance directly", async () => {
Expand Down
41 changes: 32 additions & 9 deletions src/typeHelpers.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,41 @@ describe("typeHelpers typecheck", () => {
type selectTuple1 = SelectAttributes<Table, ["key", "tuple[0]"]>;
expectTypeOf<selectTuple1>().toEqualTypeOf<{
key: number;
tuple: {
0: {
tuple: [
{
kissa: "koira";
};
};
}
];
}>();

type selectTuple2 = SelectAttributes<Table, ["key", "tuple[1]"]>;
// TODO Fix this =(
// expectTypeOf<selectTuple2>().toEqualTypeOf<{
// key: number;
// tuple: { 0: number };
// }>();
expectTypeOf<selectTuple2>().toEqualTypeOf<{
key: number;
tuple: [number];
}>();

type selectTuple3 = SelectAttributes<
Table,
["key", "tuple[0]", "tuple[1]"]
>;
// TODO Fix this to be [{kissa: "koira"}, number]
expectTypeOf<selectTuple3>().toEqualTypeOf<{
key: number;
tuple: [
{
kissa: "koira";
}
] &
[number];
}>();

type selectArray = SelectAttributes<Table, ["key", "cats[0]"]>;
expectTypeOf<selectArray>().toEqualTypeOf<{
key: number;
cats: {
name: string;
age: number;
}[];
}>();
});
});
11 changes: 7 additions & 4 deletions src/typeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ type RecursiveSelectAttributes<Table, Properties> = Properties extends [
infer First,
...infer Rest
]
? First extends keyof Table
? { [key in First]: RecursiveSelectAttributes<Table[First], Rest> }
: // When "First" in path is a number, and Table is an Array, Recurse to the inner type of the array
[First, Table] extends [`${number}`, any[]]
? [First, IsTuple<Table>] extends [`${number}`, true]
? First extends keyof Table
? [RecursiveSelectAttributes<Table[First], Rest>]
: Table
: [First, Table] extends [`${number}`, any[]]
? RecursiveSelectAttributes<As<Table, any[]>[number], Rest>[]
: First extends keyof Table
? { [key in First]: RecursiveSelectAttributes<Table[First], Rest> }
: never
: Table;

Expand Down
4 changes: 4 additions & 0 deletions test/testFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ export const TEST_DATA = [
name: "Sylvester J. Pussycat Sr.",
age: 78,
},
{
name: "Tommy the Cat",
age: 12,
},
],
},
{
Expand Down

0 comments on commit 8791c75

Please sign in to comment.