This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.js
127 lines (117 loc) · 7.15 KB
/
backup.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
const {info, log} = require("./logger.mjs");
const fs = require("fs");
const {addData} = require("./db.mjs");
const {core} = require('express');
module.exports = {
/**
* Adds the /clone path to the
* @param {core.Express} app The app instance to add the get method to.
*/
addClone: (app) => {
app.get('/clone', async (req, res) => {
info("Cloning data from data.json to Database...");
const now = new Date().toJSON().slice(0, 19).replace('T', ' ');
const jsonData = fs.readFileSync('data.json');
const data = JSON.parse(jsonData);
for (let areaKey in data)
if (data.hasOwnProperty(areaKey)) {
const area = data[areaKey];
info(`Inserting Area \"${areaKey}\"...`);
const aCreation = new Date(area.created.value._seconds * 1000).toJSON().slice(0, 19).replace('T', ' ');
const a = await addData('Areas', [areaKey, aCreation, now, area.displayName, area.image, area.kmz, area.webURL]);
log("Area SQL:", a);
const zones = area.__collections__.Zones;
for (let zoneKey in zones)
if (zones.hasOwnProperty(zoneKey)) {
const zone = zones[zoneKey];
info(` Inserting Zone \"${zoneKey}\"...`);
const zCreation = new Date(zone.created.value._seconds * 1000).toJSON().slice(0, 19).replace('T', ' ');
const zPos = zone.location.value;
const z = await addData(
'Zones',
[
zoneKey,
zCreation,
now,
zone.displayName,
zone.image,
zone.kmz,
zPos._latitude,
zPos._longitude,
zone.webURL,
areaKey,
],
);
log("Zone SQL:", z);
const sectors = zone.__collections__.Sectors;
for (let sectorKey in sectors)
if (sectors.hasOwnProperty(sectorKey)) {
const sector = sectors[sectorKey];
info(` Inserting Sector \"${sectorKey}\"...`);
const sCreation = new Date(zone.created.value._seconds * 1000).toJSON().slice(0, 19).replace('T', ' ');
const sPos = zone.location.value;
const sun = sector.sunTime;
const s = await addData(
'Sectors',
[
sectorKey,
sCreation,
now,
sector.displayName,
sector.image,
sector.kidsApt,
sPos._latitude,
sPos._longitude,
sun === 0 ? 'day' : sun === 1 ? 'morning' : sun === 2 ? 'afternoon' : 'none',
sector.walkingTime,
sector.weight,
areaKey,
],
);
log("Sector SQL:", s);
const paths = sector.__collections__.Paths;
for (let pathKey in paths)
if (paths.hasOwnProperty(pathKey)) {
const path = paths[pathKey];
info(` Inserting Path \"${pathKey}\"...`);
const pCreation = new Date(path.created.value._seconds * 1000).toJSON().slice(0, 19).replace('T', ' ');
const p = await addData(
'Paths',
[
pathKey,
pCreation,
now,
path.displayName,
path.sketchId,
path.grade,
!!path.height ? path.height.join(';') : 'NULL',
!!path.builtBy && path.builtBy.length > 0 ? path.builtBy : 'NULL',
!!path.rebuiltBy && !!path.rebuiltBy.length > 0 ? path.rebuiltBy.join(';') : 'NULL',
path.description && path.description.length > 0 ? path.description : 'NULL',
path.description && path.description.length > 0 ? path.showDescription : false,
path.stringCount,
path.paraboltCount,
path.burilCount,
path.pitonCount,
path.spitCount,
path.tensorCount,
path.crackerRequired,
path.friendRequired,
path.lanyardRequired,
path.nailRequired,
path.pitonRequired,
path.stripsRequired,
!!path.ending && path.ending.length > 0 ? path.ending.join(';') : 'NULL',
!!path.ending_artifo && path.ending_artifo.length > 0 ? path.ending_artifo.replaceAll('\r', '').replaceAll('\n', ';') : 'NULL',
sectorKey,
],
);
log("Path SQL:", p);
}
}
}
}
res.status(200).type("application/json").send(data);
});
},
};