relation-matcher 1.0.3

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.
Files changed (50) hide show
  1. package/.prettierrc +4 -0
  2. package/.vscode/settings.json +3 -0
  3. package/.yarn/install-state.gz +0 -0
  4. package/.yarnrc.yml +1 -0
  5. package/dist/dist/jest.config.js +10 -0
  6. package/dist/dist/src/index.js +107 -0
  7. package/dist/dist/src/index.test.js +81 -0
  8. package/dist/dist/src/testing/file-output.js +9 -0
  9. package/dist/dist/src/testing/test-data.js +164 -0
  10. package/dist/dist/src/types/generic-bases.js +1 -0
  11. package/dist/dist/src/types/generic-less.js +1 -0
  12. package/dist/dist/src/types/inputs.js +1 -0
  13. package/dist/dist/src/types/return.js +1 -0
  14. package/dist/dist/src/types/typetest.js +6 -0
  15. package/dist/dist/src/types/utils.js +1 -0
  16. package/dist/dist/src/utils/invertInput.js +17 -0
  17. package/dist/dist/src/utils/keys.js +1 -0
  18. package/dist/dist/src/utils/matcher.js +1 -0
  19. package/dist/jest.config.js +10 -0
  20. package/dist/src/index.js +107 -0
  21. package/dist/src/index.test.js +81 -0
  22. package/dist/src/testing/file-output.js +9 -0
  23. package/dist/src/testing/test-data.js +164 -0
  24. package/dist/src/types/generic-bases.js +1 -0
  25. package/dist/src/types/generic-less.js +1 -0
  26. package/dist/src/types/inputs.js +1 -0
  27. package/dist/src/types/return.js +1 -0
  28. package/dist/src/types/typetest.js +6 -0
  29. package/dist/src/types/utils.js +1 -0
  30. package/dist/src/utils/invertInput.js +17 -0
  31. package/dist/src/utils/keys.js +1 -0
  32. package/dist/src/utils/matcher.js +1 -0
  33. package/dist/tsconfig.tsbuildinfo +1 -0
  34. package/jest.config.js +12 -0
  35. package/package.json +25 -0
  36. package/result.json +136 -0
  37. package/src/index.test.ts +88 -0
  38. package/src/index.ts +184 -0
  39. package/src/testing/file-output.ts +15 -0
  40. package/src/testing/test-data.ts +171 -0
  41. package/src/types/generic-bases.ts +9 -0
  42. package/src/types/generic-less.ts +14 -0
  43. package/src/types/inputs.ts +29 -0
  44. package/src/types/return.ts +37 -0
  45. package/src/types/typetest.ts +46 -0
  46. package/src/types/utils.ts +16 -0
  47. package/src/utils/invertInput.ts +28 -0
  48. package/src/utils/keys.ts +1 -0
  49. package/src/utils/matcher.ts +8 -0
  50. package/tsconfig.json +36 -0
@@ -0,0 +1,37 @@
1
+ import type { TInputBase, TJoinedFromBase, TOutputBase } from "./generic-bases";
2
+ import type { RelationMapRoot } from "./inputs";
3
+ import type { NoUnderscore, Simplify } from "./utils";
4
+
5
+ export type RelationMapperReturnRoot<
6
+ TInput extends TInputBase,
7
+ TOutput extends RelationMapRoot<TInput>,
8
+ > = {
9
+ [inputKey in keyof TInput]: TInput[TOutput["base"]] & {
10
+ [outputKey in NoUnderscore<keyof TOutput>]: RelationMapperReturn<
11
+ TInput,
12
+ TInput[inputKey],
13
+ TOutput[`_${outputKey}`]
14
+ >;
15
+ };
16
+ }[keyof TInput];
17
+
18
+ /** Fill current object with value specified in base */
19
+ export type RelationMapperReturn<
20
+ TInput extends TInputBase,
21
+ TJoinedFrom extends TJoinedFromBase,
22
+ TOutput extends TOutputBase<TInput, TJoinedFrom>,
23
+ > = TInput[TOutput["base"]] &
24
+ Simplify<RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>>;
25
+
26
+ /** Takes underscored values (joins) and replaces their values with the actual output. */
27
+ export type RelationMapperReturnJoin<
28
+ TInput extends TInputBase,
29
+ TJoinedFrom extends TJoinedFromBase,
30
+ TOutput extends TOutputBase<TInput, TJoinedFrom>,
31
+ > = {
32
+ [k in NoUnderscore<keyof TOutput>]: RelationMapperReturn<
33
+ TInput,
34
+ TJoinedFrom,
35
+ TOutput[`_${k}`]
36
+ >;
37
+ };
@@ -0,0 +1,46 @@
1
+ import type { RelationMapperReturnRoot } from "./return";
2
+
3
+ export type A = RelationMapperReturnRoot<
4
+ {
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;
12
+ };
13
+ team_to_user: {
14
+ created_at: Date;
15
+ updated_at: Date | null;
16
+ user_clerk_id: string;
17
+ team_id: string;
18
+ };
19
+ },
20
+ {
21
+ base: "team";
22
+ id: "id";
23
+
24
+ _teamToUser: {
25
+ base: "team_to_user";
26
+ id: "user_clerk_id";
27
+ joinsTo: "team_id";
28
+ joinsFrom: "id";
29
+ joinType: "single";
30
+
31
+ _sar: {
32
+ base: "team";
33
+ id: "id";
34
+ joinsFrom: "team_id";
35
+ joinsTo: "id";
36
+ joinType: "array";
37
+ };
38
+ };
39
+ }
40
+ >;
41
+
42
+ // type B = {
43
+ // team: Schema["Team"]["$inferSelect"][];
44
+ // team_to_user: Schema["TeamToUser"]["$inferSelect"][];
45
+ // };
46
+ // ^?
@@ -0,0 +1,16 @@
1
+ export type Simplify<T> = {
2
+ [k in keyof T]: T[k];
3
+ } & {};
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]>;
13
+ } & {};
14
+
15
+ export type NoUnderscore<T extends string | number | symbol> =
16
+ T extends `_${infer Result}` ? Result : never;
@@ -0,0 +1,28 @@
1
+ import type { TInputBase } from "~/types/generic-bases";
2
+
3
+ export type InvertedInput<TInput extends TInputBase = TInputBase> = Record<
4
+ keyof TInput,
5
+ TInput[keyof TInput][]
6
+ >;
7
+
8
+ const invertInput = <TInput extends TInputBase>(
9
+ inputs: TInputBase[],
10
+ ): InvertedInput<TInput> => {
11
+ const invertedInput = inputs.reduce<InvertedInput<TInput>>((final, row) => {
12
+ Object.entries(row).forEach(([header, value]) => {
13
+ if (!value) return;
14
+
15
+ if (final[header]) {
16
+ final[header].push(value as TInput[keyof TInput]);
17
+ } else {
18
+ final[header as keyof TInput] = [value as TInput[keyof TInput]];
19
+ }
20
+ });
21
+
22
+ return final;
23
+ }, {} as InvertedInput<TInput>);
24
+
25
+ return invertedInput;
26
+ };
27
+
28
+ export default invertInput;
@@ -0,0 +1 @@
1
+ export const getJoinFinalKey = (key: string) => key.substring(1);
@@ -0,0 +1,8 @@
1
+ import type { TInputBase } from "~/types/generic-bases";
2
+ import type { InvertedInput } from "./invertInput";
3
+ import type { Output } from "~/types/generic-less";
4
+
5
+ export const matcherFunc =
6
+ <TInput extends TInputBase>(row: TInput[keyof TInput], value: Output) =>
7
+ (item: InvertedInput[keyof InvertedInput][number]) =>
8
+ row?.[value.joinsFrom as string] === item?.[value.joinsTo as string];
package/tsconfig.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Base Options: */
4
+ "esModuleInterop": true,
5
+ "skipLibCheck": true,
6
+ "target": "es2022",
7
+ "allowJs": true,
8
+ "resolveJsonModule": true,
9
+ "moduleDetection": "force",
10
+ "isolatedModules": true,
11
+ "verbatimModuleSyntax": true,
12
+
13
+ /* Strictness */
14
+ "strict": true,
15
+ "noUncheckedIndexedAccess": true,
16
+ "checkJs": true,
17
+
18
+ /* Bundled projects */
19
+ "lib": ["dom", "dom.iterable", "ES2022"],
20
+ // "noEmit": true,
21
+ "module": "ESNext",
22
+ "moduleResolution": "Bundler",
23
+ "jsx": "preserve",
24
+ "plugins": [{ "name": "next" }],
25
+ "incremental": true,
26
+ "outDir": "./dist",
27
+
28
+ /* Path Aliases */
29
+ "baseUrl": ".",
30
+ "paths": {
31
+ "~/*": ["./src/*"]
32
+ }
33
+ },
34
+ "include": ["**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.js"],
35
+ "exclude": ["node_modules", "dist"]
36
+ }