-
Notifications
You must be signed in to change notification settings - Fork 42
/
convertData.js
318 lines (287 loc) · 11.9 KB
/
convertData.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
global.__base = __dirname + "/"
const cds = require('@sap/cds')
const path = require('path')
const fs = require("fs")
let routesDir = path.join(global.__base, '../oldPython/resources/fixtures/')
const uuid = require('uuid')
async function clearDB(db) {
console.log(`Clearing existing DB tables`)
//New DB Referential Integrity Checks means you can't delete parallel any longer
await db.run(DELETE.from(db.entities.Planet2People))
await db.run(DELETE.from(db.entities.Film2People))
await db.run(DELETE.from(db.entities.Film2Planets))
await db.run(DELETE.from(db.entities.Film2Starships))
await db.run(DELETE.from(db.entities.Film2Vehicles))
await db.run(DELETE.from(db.entities.Film2Species))
await db.run(DELETE.from(db.entities.Species2People))
await db.run(DELETE.from(db.entities.Starship2Pilot))
await db.run(DELETE.from(db.entities.Vehicle2Pilot))
await db.run(DELETE.from(db.entities.People))
await db.run(DELETE.from(db.entities.Starship))
await db.run(DELETE.from(db.entities.Vehicles))
await db.run(DELETE.from(db.entities.Species))
await db.run(DELETE.from(db.entities.Film))
await db.run(DELETE.from(db.entities.Planet))
console.log(`DB Tables Cleared`)
}
async function readFile(fileName) {
const fullPath = path.join(routesDir + fileName)
console.log(`Reading ${fileName}`)
let importData = fs.readFileSync(fullPath)
let data = JSON.parse(importData)
let output = []
for (let item of data) {
item.ID = uuid.v4()
output.push(item)
}
return output
}
async function peopleLoad(db, people, planets) {
let personsNew = []
let Planet2PeopleNew = []
for (let person of people) {
let planet = planets.find(x => x.pk == person.fields.homeworld)
person.fields.homeworld = planet.ID
let insert = {}
let p2p = {}
p2p.people_ID = person.ID
insert.ID = person.ID
insert.homeworld_ID = person.fields.homeworld
p2p.planet_ID = person.fields.homeworld
insert.name = person.fields.name
insert.height = person.fields.height
insert.mass = person.fields.mass
insert.hair_color = person.fields.hair_color
insert.skin_color = person.fields.skin_color
insert.eye_color = person.fields.eye_color
insert.birth_year = person.fields.birth_year
insert.gender = person.fields.gender
personsNew.push(insert)
Planet2PeopleNew.push(p2p)
}
await db.run(INSERT.into(db.entities.People).entries(personsNew))
console.log(`Insert into People`)
await db.run(INSERT.into(db.entities.Planet2People).entries(Planet2PeopleNew))
console.log(`Insert into Planet2People`)
}
async function planetsLoad(db, planets) {
let planetsNew = []
for (let planet of planets) {
let insert = {}
insert.ID = planet.ID
insert.name = planet.fields.name
insert.diameter = planet.fields.diameter
insert.rotation_period = planet.fields.rotation_period
insert.orbital_period = planet.fields.orbital_period
insert.gravity = planet.fields.gravity
insert.population = planet.fields.population
insert.climate = planet.fields.climate
insert.terrain = planet.fields.terrain
insert.surface_water = planet.fields.surface_water
planetsNew.push(insert)
}
await db.run(INSERT.into(db.entities.Planet).entries(planetsNew))
console.log(`Insert into Planet`)
}
async function starshipLoad(db, starships, transports, people) {
let starshipsNew = []
let s2p = []
for (let ship of starships) {
let insert = {}
let transport = transports.find(x => x.pk == ship.pk)
insert.ID = ship.ID
insert.name = transport.fields.name
insert.model = transport.fields.model
insert.starship_class = ship.fields.starship_class
insert.manufacturer = transport.fields.manufacturer
insert.cost_in_credits = transport.fields.cost_in_credits
insert.length = transport.fields.length
insert.crew = transport.fields.crew
insert.passengers = transport.fields.passengers
insert.max_atmosphering_speed = transport.fields.max_atmosphering_speed
insert.hyperdrive_rating = ship.fields.hyperdrive_rating
insert.MGLT = ship.fields.MGLT
insert.cargo_capacity = transport.fields.cargo_capacity
insert.consumables = transport.fields.consumables
starshipsNew.push(insert)
for (let pilot of ship.fields.pilots) {
let pilotDetails = people.find(x => x.pk == pilot)
let insert = {}
insert.starship_ID = ship.ID
insert.pilot_ID = pilotDetails.ID
s2p.push(insert)
}
}
await db.run(INSERT.into(db.entities.Starship).entries(starshipsNew))
console.log(`Insert into Starship`)
await db.run(INSERT.into(db.entities.Starship2Pilot).entries(s2p))
console.log(`Insert into Starship2Pilot`)
}
async function vehiclesLoad(db, vehicles, transports, people) {
let vehiclesNew = []
let v2p = []
for (let vehicle of vehicles) {
let insert = {}
let transport = transports.find(x => x.pk == vehicle.pk)
insert.ID = vehicle.ID
insert.name = transport.fields.name
insert.model = transport.fields.model
insert.vehicle_class = vehicle.fields.vehicle_class
insert.manufacturer = transport.fields.manufacturer
insert.cost_in_credits = transport.fields.cost_in_credits
insert.length = transport.fields.length
insert.crew = transport.fields.crew
insert.passengers = transport.fields.passengers
insert.max_atmosphering_speed = transport.fields.max_atmosphering_speed
insert.cargo_capacity = transport.fields.cargo_capacity
insert.consumables = transport.fields.consumables
vehiclesNew.push(insert)
for (let pilot of vehicle.fields.pilots) {
let pilotDetails = people.find(x => x.pk == pilot)
let insert = {}
insert.vehicle_ID = vehicle.ID
insert.pilot_ID = pilotDetails.ID
v2p.push(insert)
}
}
await db.run(INSERT.into(db.entities.Vehicles).entries(vehiclesNew))
console.log(`Insert into Vehicles`)
await db.run(INSERT.into(db.entities.Vehicle2Pilot).entries(v2p))
console.log(`Insert into Vehicle2Pilot`)
}
async function speciesLoad(db, species, planets, people) {
let speciesNew = []
let Species2PeopleNew = []
for (let specie of species) {
let insert = {}
insert.ID = specie.ID
insert.name = specie.fields.name
insert.classification = specie.fields.classification
insert.designation = specie.fields.designation
insert.eye_colors = specie.fields.eye_colors
insert.skin_colors = specie.fields.skin_colors
insert.language = specie.fields.language
insert.hair_colors = specie.fields.hair_colors
insert.average_lifespan = specie.fields.average_lifespan
insert.average_height = specie.fields.average_height
if (specie.fields.homeworld) {
let planet = planets.find(x => x.pk == specie.fields.homeworld)
if (planet) {
insert.homeworld_ID = planet.ID
}
}
speciesNew.push(insert)
for (let person of specie.fields.people) {
let personDetails = people.find(x => x.pk == person)
let insert = {}
insert.species_ID = specie.ID
insert.people_ID = personDetails.ID
Species2PeopleNew.push(insert)
}
}
await db.run(INSERT.into(db.entities.Species).entries(speciesNew))
console.log(`Insert into Species`)
await db.run(INSERT.into(db.entities.Species2People).entries(Species2PeopleNew))
console.log(`Insert into Species2People`)
}
async function filmsLoad(db, films, people, planets, species, starships, vehicles) {
let filmNew = []
let Film2PeopleNew = []
let Film2PlanetsNew = []
let Film2StarshipsNew = []
let Film2VehiclesNew = []
let Film2SpeciesNew = []
for (let film of films) {
let insert = {}
insert.ID = film.ID
insert.producer = film.fields.producer
insert.title = film.fields.title
insert.episode_id = film.fields.episode_id
insert.director = film.fields.director
insert.release_date = film.fields.release_date
insert.opening_crawl = film.fields.opening_crawl
filmNew.push(insert)
for (let starship of film.fields.starships) {
let starshipDetails = starships.find(x => x.pk == starship)
let insert = {}
insert.film_ID = film.ID
insert.starship_ID = starshipDetails.ID
Film2StarshipsNew.push(insert)
}
for (let vehicle of film.fields.vehicles) {
let vehicleDetails = vehicles.find(x => x.pk == vehicle)
let insert = {}
insert.film_ID = film.ID
insert.vehicle_ID = vehicleDetails.ID
Film2VehiclesNew.push(insert)
}
for (let planet of film.fields.planets) {
let planetDetails = planets.find(x => x.pk == planet)
let insert = {}
insert.film_ID = film.ID
insert.planet_ID = planetDetails.ID
Film2PlanetsNew.push(insert)
}
for (let person of film.fields.characters) {
let personDetails = people.find(x => x.pk == person)
let insert = {}
insert.film_ID = film.ID
insert.people_ID = personDetails.ID
Film2PeopleNew.push(insert)
}
for (let specie of film.fields.species) {
let specieDetails = species.find(x => x.pk == specie)
let insert = {}
insert.film_ID = film.ID
insert.specie_ID = specieDetails.ID
Film2SpeciesNew.push(insert)
}
}
await db.run(INSERT.into(db.entities.Film).entries(filmNew))
console.log(`Insert into Film`)
await Promise.all([
db.run(INSERT.into(db.entities.Film2People).entries(Film2PeopleNew)),
db.run(INSERT.into(db.entities.Film2Planets).entries(Film2PlanetsNew)),
db.run(INSERT.into(db.entities.Film2Starships).entries(Film2StarshipsNew)),
db.run(INSERT.into(db.entities.Film2Vehicles).entries(Film2VehiclesNew)),
db.run(INSERT.into(db.entities.Film2Species).entries(Film2SpeciesNew))
])
console.log(`Insert into Film2People`)
console.log(`Insert into Film2Planets`)
console.log(`Insert into Film2Starships`)
console.log(`Insert into Film2Vehicles`)
console.log(`Insert into Film2Species`)
}
async function init() {
try {
const modelPath = path.join(global.__base, '/gen/srv/srv/csn.json')
cds.log(`Model Location - CDS Log: ${modelPath}`)
console.log(`Model Location: ${modelPath}`)
const db = await cds.connect.to('db', { model: modelPath, })
await clearDB(db)
// eslint-disable-next-line no-unused-vars
let [people, planets, films, species, starships, vehicles, transports]
= await Promise.all([
readFile('people.json'),
readFile('planets.json'),
readFile('films.json'),
readFile('species.json'),
readFile('starships.json'),
readFile('vehicles.json'),
readFile('transport.json')
])
//New DB Referential Integrity Checks means you can't load parallel any longer
await planetsLoad(db, planets)
await peopleLoad(db, people, planets)
await starshipLoad(db, starships, transports, people)
await vehiclesLoad(db, vehicles, transports, people)
await speciesLoad(db, species, planets, people)
await filmsLoad(db, films, people, planets, species, starships, vehicles)
console.log(`Done`)
// process.exit()
} catch (error) {
console.error(error)
process.exit()
}
}
init()