Skip to content

Commit 69ff149

Browse files
committed
feat: support one to many & many to many relations
1 parent a53a379 commit 69ff149

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

index.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ type Check = CheckBase & {
1212
place: PlaceBase;
1313
};
1414

15+
type Place = PlaceBase & {
16+
/** checks relation */
17+
checks: Check[];
18+
};
19+
1520
test("some fields, no relations & no omissions", () => {
1621
const shape = getShape<Check>()({
1722
id: true,
@@ -190,3 +195,19 @@ test("foreign fields & relations", () => {
190195
"id,place_id,user_id,place:place_id(id,title),user:user_id(id,name)"
191196
);
192197
});
198+
199+
test("1:m and n:m relations", () => {
200+
const shape = getShape<Place>()({
201+
id: true,
202+
checks: [{ id: true }],
203+
});
204+
205+
expectType<{
206+
id: string;
207+
checks: { id: string }[];
208+
}>(shape);
209+
210+
const fields = getFields(shape);
211+
212+
expect(fields).toBe("id,checks(id)");
213+
});

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type MakePropTypesBoolean<T, A = T> = T extends object
22
? { [K in keyof T]: MakePropTypesBoolean<Partial<T[K]>, T> } & {
33
/** which field to join by */
4-
_: keyof A;
4+
_?: keyof A;
55
/** wheter to get all the fields */
66
"*"?: true;
77
}
@@ -13,6 +13,8 @@ type OmitFalseKeys<T> = {
1313

1414
export type ParseReturnType<T, U> = U extends { "*": true }
1515
? T
16+
: U extends Array<infer UE>
17+
? ParseReturnType<T extends Array<infer TE> ? TE : T, UE>[]
1618
: U extends Record<string, unknown>
1719
? Omit<
1820
{
@@ -76,7 +78,7 @@ export const getFields = (shape: Record<string, any>) => {
7678
7779
return undefined;
7880
})
79-
.replace(/[": \n]/g, "")
81+
.replace(/[(": \n)(\[)(\])]/g, "")
8082
.replace(/\{/g, "(")
8183
.replace(/\}/g, ")")
8284
.slice(1, -1)}`;

0 commit comments

Comments
 (0)