relation-matcher 1.0.12 → 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/README.md +150 -0
- package/package.json +1 -1
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`.
|