From 8791c755b3ce77144b3ed6c676206b9ace69e4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sasu=20M=C3=A4kinen?= Date: Sun, 17 Mar 2024 23:26:34 +0200 Subject: [PATCH] betterment to selection type of tuple. --- .../getItemQueryBuilder.integration.test.ts | 18 +++++++- src/typeHelpers.test-d.ts | 41 +++++++++++++++---- src/typeHelpers.ts | 11 +++-- test/testFixture.ts | 4 ++ 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/queryBuilders/getItemQueryBuilder.integration.test.ts b/src/queryBuilders/getItemQueryBuilder.integration.test.ts index 48223e9..b64551c 100644 --- a/src/queryBuilders/getItemQueryBuilder.integration.test.ts +++ b/src/queryBuilders/getItemQueryBuilder.integration.test.ts @@ -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 () => { diff --git a/src/typeHelpers.test-d.ts b/src/typeHelpers.test-d.ts index 9c87799..3afeb18 100644 --- a/src/typeHelpers.test-d.ts +++ b/src/typeHelpers.test-d.ts @@ -116,18 +116,41 @@ describe("typeHelpers typecheck", () => { type selectTuple1 = SelectAttributes; expectTypeOf().toEqualTypeOf<{ key: number; - tuple: { - 0: { + tuple: [ + { kissa: "koira"; - }; - }; + } + ]; }>(); type selectTuple2 = SelectAttributes; - // TODO Fix this =( - // expectTypeOf().toEqualTypeOf<{ - // key: number; - // tuple: { 0: number }; - // }>(); + expectTypeOf().toEqualTypeOf<{ + key: number; + tuple: [number]; + }>(); + + type selectTuple3 = SelectAttributes< + Table, + ["key", "tuple[0]", "tuple[1]"] + >; + // TODO Fix this to be [{kissa: "koira"}, number] + expectTypeOf().toEqualTypeOf<{ + key: number; + tuple: [ + { + kissa: "koira"; + } + ] & + [number]; + }>(); + + type selectArray = SelectAttributes; + expectTypeOf().toEqualTypeOf<{ + key: number; + cats: { + name: string; + age: number; + }[]; + }>(); }); }); diff --git a/src/typeHelpers.ts b/src/typeHelpers.ts index caffd73..e94bada 100644 --- a/src/typeHelpers.ts +++ b/src/typeHelpers.ts @@ -75,11 +75,14 @@ type RecursiveSelectAttributes = Properties extends [ infer First, ...infer Rest ] - ? First extends keyof Table - ? { [key in First]: RecursiveSelectAttributes } - : // 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] extends [`${number}`, true] + ? First extends keyof Table + ? [RecursiveSelectAttributes] + : Table + : [First, Table] extends [`${number}`, any[]] ? RecursiveSelectAttributes[number], Rest>[] + : First extends keyof Table + ? { [key in First]: RecursiveSelectAttributes } : never : Table; diff --git a/test/testFixture.ts b/test/testFixture.ts index afd72a4..cc9d621 100644 --- a/test/testFixture.ts +++ b/test/testFixture.ts @@ -191,6 +191,10 @@ export const TEST_DATA = [ name: "Sylvester J. Pussycat Sr.", age: 78, }, + { + name: "Tommy the Cat", + age: 12, + }, ], }, {