relation-matcher 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,18 @@
1
1
  {
2
2
  "name": "relation-matcher",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "A utility to convert table data (such as out of a SQL query) into structured JSON.",
5
5
  "license": "ISC",
6
6
  "author": "",
7
7
  "type": "module",
8
8
  "main": "dist/src/index.js",
9
9
  "types": "dist/src/index.d.ts",
10
+ "exports": {
11
+ ".": "./dist/src/index.js",
12
+ "./types": "./dist/src/types/index.js",
13
+ "./source": "./src/index.ts",
14
+ "./source/types": "./src/types/index.ts"
15
+ },
10
16
  "private": false,
11
17
  "scripts": {
12
18
  "test": "jest",
package/src/index.ts CHANGED
@@ -99,7 +99,7 @@ export const relationMatcherRoot = <
99
99
  return final;
100
100
  }, {});
101
101
 
102
- return baseItems as Record<
102
+ return baseItems as unknown as Record<
103
103
  string,
104
104
  RelationMapperReturnRoot<TInput, TOutputRoot>
105
105
  >;
@@ -1,4 +1,5 @@
1
1
  import type { RelationMapRoot } from "~/types/inputs";
2
+ import type { Simplify } from "~/types/utils";
2
3
 
3
4
  export const testData = [
4
5
  {
@@ -0,0 +1,5 @@
1
+ export type * from "./generic-bases";
2
+ export type * from "./generic-less";
3
+ export type * from "./return";
4
+ export type * from "./typetest";
5
+ export type * from "./utils";
@@ -3,8 +3,8 @@ import type { TInputBase, TJoinedFromBase } from "./generic-bases";
3
3
  export type RelationMapRoot<TInput extends TInputBase> = {
4
4
  [k in keyof TInput]: {
5
5
  base: k;
6
- id: keyof TInput[k];
7
- } & RelationMapJoiner<TInput, TInput[k]>;
6
+ id: keyof NonNullable<TInput[k]>;
7
+ } & RelationMapJoiner<TInput, NonNullable<TInput[k]>>;
8
8
  }[keyof TInput];
9
9
 
10
10
  export type RelationMapBase<
@@ -13,9 +13,10 @@ export type RelationMapBase<
13
13
  > = {
14
14
  [k in keyof TInput]: {
15
15
  base: k;
16
- id: keyof TInput[k];
17
- joinsTo: keyof TInput[k];
18
- joinsFrom: keyof TJoinedFrom;
16
+ /** The id that uniqueness is checked against. Either the primary key of the table, or the id you are joining **to** on a join table. */
17
+ id: keyof NonNullable<TInput[k]>;
18
+ joinsTo: keyof NonNullable<TInput[k]>;
19
+ joinsFrom: keyof NonNullable<TJoinedFrom>;
19
20
  joinType: "single" | "array";
20
21
  } & RelationMapJoiner<TInput, TInput[k]>;
21
22
  }[keyof TInput];
@@ -24,6 +25,6 @@ export type RelationMapJoiner<
24
25
  TInput extends TInputBase,
25
26
  TJoinedFrom extends TJoinedFromBase,
26
27
  > = {
27
- [k: `_${string}`]: RelationMapJoiner<TInput, TJoinedFrom> &
28
- RelationMapBase<TInput, TJoinedFrom>;
28
+ [k: `_${string}`]: RelationMapBase<TInput, TJoinedFrom> &
29
+ RelationMapJoiner<TInput, TJoinedFrom>;
29
30
  };
@@ -1,27 +1,61 @@
1
1
  import type { TInputBase, TJoinedFromBase, TOutputBase } from "./generic-bases";
2
2
  import type { RelationMapRoot } from "./inputs";
3
- import type { NoUnderscore } from "./utils";
3
+ import type { ExtendsNull, NoUnderscore, Simplify } from "./utils";
4
4
 
5
5
  export type RelationMapperReturnRoot<
6
6
  TInput extends TInputBase,
7
7
  TOutput extends RelationMapRoot<TInput>,
8
+ > = Simplify<
9
+ {
10
+ [inputKey in keyof TInput]: ExtendsNull<
11
+ TInput[TOutput["base"]]
12
+ > extends true
13
+ ? Simplify<
14
+ TInput[TOutput["base"]] &
15
+ RelationMapperReturnJoinRoot<TInput, TOutput, inputKey>
16
+ > | null
17
+ : Simplify<
18
+ TInput[TOutput["base"]] &
19
+ RelationMapperReturnJoinRoot<TInput, TOutput, inputKey>
20
+ >;
21
+ }[keyof TInput]
22
+ >;
23
+
24
+ export type RelationMapperReturnJoinRoot<
25
+ TInput extends TInputBase,
26
+ TOutput extends RelationMapRoot<TInput>,
27
+ TInputKey extends keyof TInput,
8
28
  > = {
9
- [inputKey in keyof TInput]: TInput[TOutput["base"]] & {
10
- [outputKey in NoUnderscore<keyof TOutput>]: RelationMapperReturn<
29
+ [outputKey in NoUnderscore<keyof TOutput>]: Simplify<
30
+ RelationMapperReturn<
11
31
  TInput,
12
- TInput[inputKey],
32
+ TInput[TInputKey],
13
33
  TOutput[`_${outputKey}`]
14
- >;
15
- };
16
- }[keyof TInput];
34
+ >
35
+ >;
36
+ };
17
37
 
18
38
  /** Fill current object with value specified in base */
19
39
  export type RelationMapperReturn<
20
40
  TInput extends TInputBase,
21
41
  TJoinedFrom extends TJoinedFromBase,
22
42
  TOutput extends TOutputBase<TInput, TJoinedFrom>,
23
- > = TInput[TOutput["base"]] &
24
- RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>;
43
+ > = TOutput["joinType"] extends "array"
44
+ ? Array<
45
+ Simplify<
46
+ TInput[TOutput["base"]] &
47
+ RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
48
+ >
49
+ >
50
+ : ExtendsNull<TInput[TOutput["base"]]> extends true
51
+ ? Simplify<
52
+ TInput[TOutput["base"]] &
53
+ RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
54
+ > | null
55
+ : Simplify<
56
+ TInput[TOutput["base"]] &
57
+ RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>
58
+ >;
25
59
 
26
60
  /** Takes underscored values (joins) and replaces their values with the actual output. */
27
61
  export type RelationMapperReturnJoin<
@@ -1,46 +1,76 @@
1
1
  import type { RelationMapperReturnRoot } from "./return";
2
+ import type { DeepSimplify, ExtendsNull, Simplify } from "./utils";
2
3
 
3
- export type A = RelationMapperReturnRoot<
4
+ export type A = DeepSimplify<
5
+ RelationMapperReturnRoot<
6
+ {
7
+ team: {
8
+ created_at: Date;
9
+ updated_at: Date | null;
10
+ id: string;
11
+ team_name: string;
12
+ club_name: string;
13
+ club_postcode: string;
14
+ };
15
+ team_to_user: {
16
+ created_at: Date;
17
+ updated_at: Date | null;
18
+ user_clerk_id: string;
19
+ team_id: string;
20
+ } | null;
21
+ },
22
+ {
23
+ base: "team";
24
+ id: "id";
25
+
26
+ _teamToUser: {
27
+ base: "team_to_user";
28
+ id: "user_clerk_id";
29
+ joinsTo: "team_id";
30
+ joinsFrom: "id";
31
+ joinType: "single";
32
+
33
+ _team: {
34
+ base: "team";
35
+ id: "id";
36
+ joinsFrom: "team_id";
37
+ joinsTo: "id";
38
+ joinType: "array";
39
+ };
40
+ };
41
+ }
42
+ >,
43
+ Date | null | number
44
+ >;
45
+
46
+ type B = RelationMapperReturnRoot<
4
47
  {
5
- team: {
6
- created_at: Date;
7
- updated_at: Date | null;
8
- id: string;
9
- team_name: string;
10
- club_name: string;
11
- club_postcode: string;
48
+ user: {
49
+ clerk_id: string;
12
50
  };
13
51
  team_to_user: {
14
- created_at: Date;
15
- updated_at: Date | null;
16
- user_clerk_id: string;
17
52
  team_id: string;
18
- };
53
+ user_clerk_id: string;
54
+ } | null;
19
55
  },
20
56
  {
21
- base: "team";
22
- id: "id";
57
+ base: "user";
58
+ id: "clerk_id";
23
59
 
24
60
  _teamToUser: {
25
61
  base: "team_to_user";
26
- id: "user_clerk_id";
27
- joinsTo: "team_id";
28
- joinsFrom: "id";
62
+ id: "team_id";
63
+ joinsFrom: "clerk_id";
64
+ joinsTo: "user_clerk_id";
29
65
  joinType: "single";
30
-
31
- _sar: {
32
- base: "team";
33
- id: "id";
34
- joinsFrom: "team_id";
35
- joinsTo: "id";
36
- joinType: "array";
37
- };
38
66
  };
39
67
  }
40
68
  >;
41
69
 
42
- // type B = {
43
- // team: Schema["Team"]["$inferSelect"][];
44
- // team_to_user: Schema["TeamToUser"]["$inferSelect"][];
45
- // };
70
+ type C = Simplify<B>;
46
71
  // ^?
72
+
73
+ type D = ExtendsNull<{
74
+ team_id: string;
75
+ user_clerk_id: string;
76
+ } | null>;
@@ -2,15 +2,27 @@ export type Simplify<T> = {
2
2
  [k in keyof T]: T[k];
3
3
  } & {};
4
4
 
5
- export type DeepSimplify<T> = {
6
- [k in keyof T]: T[k] extends Record<
7
- string | number | symbol,
8
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
- (...args: any[]) => any
10
- >
11
- ? T[k]
12
- : DeepSimplify<T[k]>;
5
+ export type DeepSimplify<TInput, TDontSimplify> = {
6
+ [k in keyof TInput]: TInput[k] extends TDontSimplify
7
+ ? TInput[k]
8
+ : DeepSimplify<TInput[k], TDontSimplify>;
13
9
  } & {};
14
10
 
15
11
  export type NoUnderscore<T extends string | number | symbol> =
16
12
  T extends `_${infer Result}` ? Result : never;
13
+
14
+ export type DeepNonNullable<T extends object> = {
15
+ [k in keyof T]: T[k] extends infer NonNull | null ? NonNull : T[k];
16
+ };
17
+
18
+ export type ArrayOrSingle<
19
+ T,
20
+ TType extends "single" | "array",
21
+ > = TType extends "array" ? Array<T> : T;
22
+
23
+ export type IsNullIfNull<
24
+ TIsNull extends object | null,
25
+ TReturn extends object | never = never,
26
+ > = TIsNull extends infer T ? (T & TReturn) | null : TIsNull & TReturn;
27
+
28
+ export type ExtendsNull<T> = null extends T ? true : false;