relation-matcher 1.0.11 → 1.0.13

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/.yarnrc.yml CHANGED
@@ -1 +1,3 @@
1
1
  nodeLinker: node-modules
2
+
3
+ yarnPath: .yarn/releases/yarn-4.13.0.cjs
package/README.md ADDED
@@ -0,0 +1,150 @@
1
+ # Relation Matcher
2
+
3
+ This is a utility to convert unstructured data from a database into structured relational json data.
4
+
5
+ ## Contents
6
+
7
+ - [Installation](#installation)
8
+ - [Usage](#usage)
9
+ - [Data](#data)
10
+ - [Schema](#schema)
11
+ - [Output](#output)
12
+
13
+ ## Installation
14
+
15
+ | | Installation Command |
16
+ | ---- | --------------------------- |
17
+ | npm | `npm i relation-matcher` |
18
+ | yarn | `yarn add relation-matcher` |
19
+ | pnpm | `pnpm add relation-matcher` |
20
+
21
+ ## Usage
22
+
23
+ To use the relation matcher you feed in your `data` from your database and a `schema`. For example with drizzle:
24
+
25
+ ```ts
26
+ import relationMatcher from "relation-matcher";
27
+ import db from "~/db/client";
28
+
29
+ /* Get your data from the database */
30
+ const dbData = await db.select().fro...
31
+
32
+ const data /* Output */ = relationMatcher(
33
+ dbData /* Data */,
34
+ {...} /* Schema */
35
+ );
36
+
37
+ return data;
38
+ ```
39
+
40
+ ### `data`
41
+
42
+ The `data`'s shape should extend:
43
+
44
+ ```ts
45
+ Array<Record<string, Record<string, unknown> | null>>;
46
+ ```
47
+
48
+ #### Example
49
+
50
+ The data could have the following type:
51
+
52
+ ```ts
53
+ {
54
+ user: {
55
+ id: string;
56
+ email: string;
57
+ password: string;
58
+ ...otherColumns
59
+ };
60
+
61
+ post: {
62
+ id: string;
63
+ title: string;
64
+ content: string;
65
+
66
+ author_id: string; // < Same as user.id
67
+ }
68
+ }[]
69
+ ```
70
+
71
+ ### `schema`
72
+
73
+ The `schema`'s shape should be:
74
+
75
+ ```ts
76
+ {
77
+ base: "name of table",
78
+ id: "distinct column in above table",
79
+
80
+ _yourJoinedTableName: {
81
+ base: "name of table to join",
82
+ id: "distinct column of above table",
83
+ joinsFrom: "column of parent table",
84
+ joinsTo: "column of current table",
85
+ joinType: "single" | "array"
86
+ }
87
+ }
88
+ ```
89
+
90
+ > [!NOTE]
91
+ > Keys for joins start with an underscore; the final property key will have the leading underscore removed.
92
+
93
+ #### Example
94
+
95
+ With the above `data`, the `schema`'s shape could be:
96
+
97
+ ```ts
98
+ {
99
+ base: "user",
100
+ id: "id",
101
+
102
+ _posts: {
103
+ base: "post",
104
+ id: "id",
105
+ joinsFrom: "id",
106
+ joinsTo: "author_id",
107
+ joinType: "array",
108
+ }
109
+ }
110
+ ```
111
+
112
+ ### `output`
113
+
114
+ The outputted data will take the shape of your schema.
115
+
116
+ #### Example
117
+
118
+ For the `data` and `schema` provided above, the `output` will have the following type:
119
+
120
+ ```ts
121
+ Record<
122
+ string, // The id specified in the root of your schema.
123
+ {
124
+ id: string;
125
+ email: string;
126
+ password: string;
127
+ ...otherColumns;
128
+
129
+ posts: Array<
130
+ {
131
+ id: string;
132
+ title: string;
133
+ content: string;
134
+
135
+ author_id: string;
136
+ }
137
+ >;
138
+ }
139
+ >;
140
+ ```
141
+
142
+ ## Changelog
143
+
144
+ ### 1.0.13
145
+
146
+ - Added readme.
147
+
148
+ ### 1.0.12
149
+
150
+ - Fixed type issue where return type would be repeated union of expected `output`.
@@ -1,7 +1,6 @@
1
1
  import type { TInputBase } from "./types/generic-bases";
2
2
  import type { RelationMapRoot } from "./types/inputs";
3
3
  import type { RelationMapperReturnRoot } from "./types/return";
4
- export declare const relationMatcher: <TInput extends TInputBase, TOutputRoot extends RelationMapRoot<TInput>>(inputs: TInput[], output: TOutputRoot) => Record<string, RelationMapperReturnRoot<TInput, TOutputRoot>>;
5
- /** @deprecated Use `relationMatcher`. */
6
4
  export declare const relationMatcherRoot: <TInput extends TInputBase, TOutputRoot extends RelationMapRoot<TInput>>(inputs: TInput[], output: TOutputRoot) => Record<string, RelationMapperReturnRoot<TInput, TOutputRoot>>;
7
- export default relationMatcher;
5
+ export declare const relationMatcher: <TInput extends TInputBase, TOutputRoot extends RelationMapRoot<TInput>>(inputs: TInput[], output: TOutputRoot) => Record<string, RelationMapperReturnRoot<TInput, TOutputRoot>>;
6
+ export default relationMatcherRoot;
package/dist/src/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import invertInput, {} from "./utils/invertInput";
2
2
  import { getJoinFinalKey } from "./utils/keys";
3
3
  import { matcherFunc } from "./utils/matcher";
4
- export const relationMatcher = (inputs, output) => {
4
+ export const relationMatcherRoot = (inputs, output) => {
5
5
  if (!inputs.length)
6
6
  return {};
7
7
  if (!Array.isArray(inputs)) {
@@ -44,9 +44,8 @@ export const relationMatcher = (inputs, output) => {
44
44
  }, {});
45
45
  return baseItems;
46
46
  };
47
- /** @deprecated Use `relationMatcher`. */
48
- export const relationMatcherRoot = relationMatcher;
49
- export default relationMatcher;
47
+ export const relationMatcher = relationMatcherRoot;
48
+ export default relationMatcherRoot;
50
49
  const relationMatcherJoiner = (input, output, joiningFrom) => {
51
50
  return Object.entries(output).reduce((final, [key, value]) => {
52
51
  if (typeof value === "object") {
@@ -1,4 +1,3 @@
1
- import type { RelationMapRoot } from "~/types/inputs";
2
1
  export declare const testData: ({
3
2
  users: {
4
3
  id: string;
@@ -69,4 +68,35 @@ export declare const testData: ({
69
68
  posts: null;
70
69
  comments: null;
71
70
  })[];
72
- export declare const testSchema: RelationMapRoot<(typeof testData)[number]>;
71
+ export declare const testSchema: {
72
+ base: "users";
73
+ id: "id";
74
+ _teamToUsers: {
75
+ base: "teamToUser";
76
+ id: "teamId";
77
+ joinsFrom: "id";
78
+ joinsTo: "userId";
79
+ joinType: "array";
80
+ _team: {
81
+ base: "teams";
82
+ id: "id";
83
+ joinsFrom: "teamId";
84
+ joinsTo: "id";
85
+ joinType: "single";
86
+ };
87
+ };
88
+ _posts: {
89
+ base: "posts";
90
+ id: "id";
91
+ joinsFrom: "id";
92
+ joinsTo: "userId";
93
+ joinType: "array";
94
+ _comments: {
95
+ base: "comments";
96
+ id: "id";
97
+ joinsFrom: "id";
98
+ joinsTo: "postId";
99
+ joinType: "array";
100
+ };
101
+ };
102
+ };
@@ -1,11 +1,9 @@
1
1
  import type { TInputBase, TJoinedFromBase, TOutputBase } from "./generic-bases";
2
2
  import type { RelationMapRoot } from "./inputs";
3
3
  import type { ExtendsNull, NoUnderscore, Simplify } from "./utils";
4
- export type RelationMapperReturnRoot<TInput extends TInputBase, TOutput extends RelationMapRoot<TInput>> = Simplify<{
5
- [inputKey in keyof TInput]: ExtendsNull<TInput[TOutput["base"]]> extends true ? Simplify<TInput[TOutput["base"]] & RelationMapperReturnJoinRoot<TInput, TOutput, inputKey>> | null : Simplify<TInput[TOutput["base"]] & RelationMapperReturnJoinRoot<TInput, TOutput, inputKey>>;
6
- }[keyof TInput]>;
4
+ export type RelationMapperReturnRoot<TInput extends TInputBase, TOutput extends RelationMapRoot<TInput>> = Simplify<ExtendsNull<TInput[TOutput["base"]]> extends true ? (TInput[TOutput["base"]] & RelationMapperReturnJoinRoot<TInput, TOutput, TOutput["base"]>) | null : TInput[TOutput["base"]] & RelationMapperReturnJoinRoot<TInput, TOutput, TOutput["base"]>>;
7
5
  export type RelationMapperReturnJoinRoot<TInput extends TInputBase, TOutput extends RelationMapRoot<TInput>, TInputKey extends keyof TInput> = {
8
- [outputKey in NoUnderscore<keyof TOutput>]: Simplify<RelationMapperReturn<TInput, TInput[TInputKey], TOutput[`_${outputKey}`]>>;
6
+ [outputKey in NoUnderscore<keyof TOutput>]: RelationMapperReturn<TInput, TInput[TInputKey], TOutput[`_${outputKey}`]>;
9
7
  };
10
8
  /** Fill current object with value specified in base */
11
9
  export type RelationMapperReturn<TInput extends TInputBase, TJoinedFrom extends TJoinedFromBase, TOutput extends TOutputBase<TInput, TJoinedFrom>> = TOutput["joinType"] extends "array" ? Array<Simplify<TInput[TOutput["base"]] & RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>>> : ExtendsNull<TInput[TOutput["base"]]> extends true ? Simplify<TInput[TOutput["base"]] & RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>> | null : Simplify<TInput[TOutput["base"]] & RelationMapperReturnJoin<TInput, TJoinedFrom, TOutput>>;