-
Notifications
You must be signed in to change notification settings - Fork 36
/
app.js
172 lines (151 loc) · 3.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const jsonServer = require("json-server")
const films = require("./films.json")
const people = require("./people.json")
const planets = require("./planets.json")
const species = require("./species.json")
const starships = require("./starships.json")
const transport = require("./transport.json")
const vehicles = require("./vehicles.json")
const db = {
films,
people,
planets,
species,
starships,
transport,
vehicles,
}
/**
* Lifting the "fields" to the root of the object
* Using the "pk" as the "id"
*/
const fdb = Object.keys(db).reduce((acc, current) => {
acc[current] = db[current].map(item => {
return Object.assign(item.fields, {
id: item.pk,
})
})
return acc
}, {})
/**
* Vehicles and Starships "extend" Transport...
*/
const xdb = Object.keys(fdb).reduce((acc, current) => {
if (current === "starships" || current === "vehicles") {
acc[current] = acc[current].map(item => {
const transport = acc["transport"].find(elm => {
return elm.id === item.id
})
if (transport) {
return Object.assign(item, transport)
}
return item
})
}
return acc
}, fdb)
/**
* Relation definitions to match them with swapi.co
*/
const relations = [
{
name: "people",
relation: [
{
alias: "vehicles",
table: "vehicles",
name: "pilots",
type: Array,
},
{
alias: "starships",
table: "starships",
name: "pilots",
type: Array,
},
{
alias: "films",
table: "films",
name: "characters",
type: Array,
},
],
},
{
name: "planets",
relation: [
{
alias: "residents",
table: "people",
name: "homeworld",
type: Number,
},
{
alias: "films",
table: "films",
name: "planets",
type: Array,
},
],
},
{
name: "starships",
relation: [
{
alias: "films",
table: "films",
name: "starships",
type: Array,
},
],
},
]
function addRelation(db, host, relations) {
return relations.reduce((acc, relation) => {
const alias = relation.alias
const table = relation.table
const name = relation.name
if (relation.type === Number) {
acc[alias] = db[table]
.filter(item => item[name] === host.id)
.map(item => item.id)
return acc
}
acc[alias] = db[table]
.filter(item => item[name].indexOf(host.id) > -1)
.map(item => item.id)
return acc
}, {})
}
const relationDb = relations.reduce((acc, current) => {
acc[current.name] = acc[current.name].map(item => {
return Object.assign(
item,
addRelation(acc, item, current.relation)
)
})
return acc
}, xdb)
const server = jsonServer.create()
const serveStatic = require("serve-static")
const path = require("path")
server.use(
serveStatic(path.join(__dirname, "public"), {
maxAge: "1d",
})
)
server.use((req, res, next) => {
const { delay } = req.query
if (delay) {
setTimeout(() => {
next()
}, Number(delay))
} else {
next()
}
})
server.use(jsonServer.defaults())
const router = jsonServer.router(relationDb)
server.use(router)
server.listen(process.env.PORT || 3000)
console.log(`Server started on port 3000`)