Skip to content

Commit

Permalink
Merge pull request #9 from mindler-sasu/main
Browse files Browse the repository at this point in the history
add support for tuples in attributes
  • Loading branch information
woltsu authored Mar 16, 2024
2 parents d447cf4 + 20e8145 commit b6cb471
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/queryBuilders/getItemQueryBuilder.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe("GetItemQueryBuilder", () => {
expect(Object.keys(data!).length).toBe(2);
});

it("handles selecting attributes from arrays and tuples", async () => {
it("handles selecting attributes from arrays", async () => {
const data = await tsynamoClient
.getItemFrom("myOtherTable")
.keys({
Expand All @@ -91,6 +91,20 @@ describe("GetItemQueryBuilder", () => {
expect(data?.cats?.length).toEqual(1);
expect(data?.cats?.[0].age).toEqual(TEST_DATA[6].cats[1].age);
});
it("handles selecting attributes from tuples", async () => {
const data = await tsynamoClient
.getItemFrom("myOtherTable")
.keys({
userId: TEST_DATA[6].userId,
stringTimestamp: "123",
})
.consistentRead(true)
.attributes(["tuplez[0]"])
.execute();

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

it("can't await instance directly", async () => {
expect(
Expand Down
30 changes: 26 additions & 4 deletions src/typeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ export type ObjectKeyPaths<T> =
: // Leaf value reached, don't return anything
never;

type IsTuple<T> = T extends [any, ...any] ? true : false;

/**
* Generate union from 0 to N
* RangeToN<3> => 0 | 1 | 2 | 3
*/
type RangeToN<
N extends number,
Result extends any[] = []
> = Result["length"] extends N
? Result[number]
: RangeToN<N, [...Result, Result["length"]]>;

// T extends (number extends T['length'] ? [] : any[])
export type ObjectFullPaths<T> =
// if `T` is an object or array
T extends Record<PropertyKey, unknown>
Expand All @@ -194,10 +208,18 @@ export type ObjectFullPaths<T> =
`${Key}` | `${Key}.${ObjectFullPaths<T[Key]>}`
: // If it's not an object, check if its an array
T[Key] extends (infer A)[]
? // If it's an array concatenate the key, array accessors, and the rest of the path recursively
| `${Key}`
| `${Key}[${number}]`
| `${Key}[${number}].${ObjectFullPaths<A[][number]>}`
? IsTuple<T[Key]> extends true
? // If tuple create array concatenate the key, specific array accessor for each index of the tuple, and the rest of the path recursively
| `${Key}`
| `${Key}${ObjectFullPaths<{
[SpecificKey in RangeToN<
T[Key]["length"]
> as `[${SpecificKey}]`]: T[Key][SpecificKey];
}>}`
: // If it's an array concatenate the key, array accessors, and the rest of the path recursively
| `${Key}`
| `${Key}[${number}]`
| `${Key}[${number}].${ObjectFullPaths<A[][number]>}`
: `${Key}`
: // unreachable branch (if key is symbol)
never
Expand Down
4 changes: 2 additions & 2 deletions test/testFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface DDB {
stringTimestamp: SortKey<string>;
somethingElse: number;
someBoolean: boolean;
tuple: [
tuplez: [
{
beer: string;
},
Expand Down Expand Up @@ -174,7 +174,7 @@ export const TEST_DATA = [
stringTimestamp: "123",
somethingElse: -5,
someBoolean: true,
tuple: [
tuplez: [
{
beer: "karhu",
},
Expand Down

0 comments on commit b6cb471

Please sign in to comment.