-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_to_json.js
85 lines (69 loc) · 2.23 KB
/
sql_to_json.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
// EXTERNAL
import { writeFileSync } from "fs";
// INTERNAL
import { connectToDb } from "./functions/sql_queries/connect_to_db.js";
import { getAlpacaDetails } from "./functions/sql_queries/get_alpacas.js";
import fileTransformer from "./functions/fileTransformer.js";
import { farmsFromAlpacas } from "./functions/farmsFromAlpacas.js";
const now = Date.now().toString();
/******** SQL -> JSON ********/
console.log(`[LOG] START SQL -> JSON`);
const connection = await connectToDb();
const [alpacaDetailsArray] = await getAlpacaDetails(connection);
console.log(
`[LOG] Retrieving ${alpacaDetailsArray.length} alpaca details from database`
);
await connection.end();
const enrichedAlpacaDetailsArray = await fileTransformer(alpacaDetailsArray, {
geoDecodeEnrich: true,
});
const farms_ALL_WithAlpacaCountArray = farmsFromAlpacas(
enrichedAlpacaDetailsArray,
{ publicFarmsOnly: false }
);
const farms_PUBLIC_WithAlpacaCountArray = farmsFromAlpacas(
enrichedAlpacaDetailsArray,
{ publicFarmsOnly: true }
);
const companies_all = farmsFromAlpacas(enrichedAlpacaDetailsArray, {
publicFarmsOnly: false,
includeAlpacas: true,
});
console.log(`[LOG] END SQL -> JSON`);
/******** JSON -> FILE ********/
console.log(`[LOG] START JSON -> FILE`);
// Write to file which will write as one long line
writeFileSync(
`./data/animals-ALL-from-alpacas-from-sql-${now}.json`,
JSON.stringify(enrichedAlpacaDetailsArray)
);
console.log(
"[LOG] See file: ",
`./data/animals-ALL-from-alpacas-from-sql-${now}.json`
);
writeFileSync(
`./data/farms-ALL-from-alpacas-from-sql-${now}.json`,
JSON.stringify(farms_ALL_WithAlpacaCountArray)
);
console.log(
"[LOG] See file: ",
`./data/farms-ALL-from-alpacas-from-sql-${now}.json`
);
writeFileSync(
`./data/farms-PUBLIC-from-alpacas-from-sql-${now}.json`,
JSON.stringify(farms_PUBLIC_WithAlpacaCountArray)
);
console.log(
"[LOG] See file: ",
`./data/farms-PUBLIC-from-alpacas-from-sql-${now}.json`
);
writeFileSync(
`./data/companies-from-alpacas-from-sql-${now}.json`,
JSON.stringify(companies_all)
);
console.log(
"[LOG] See file: ",
`./data/companies-from-alpacas-from-sql-${now}.json`
);
console.log(`[LOG] END JSON -> FILE`);
// For readability could JSON.parse and write to file with line breaks