-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetl.js
68 lines (62 loc) · 1.77 KB
/
etl.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
const csv = require("csv-parser");
const fs = require("fs");
const results = [];
const SEED_FILE = "knex/seed.sql";
try {
fs.unlinkSync(SEED_FILE);
} catch (err) {
// console.error(err);
}
const wstream = fs.createWriteStream(SEED_FILE);
const knex = require("knex");
const knexPostgis = require("knex-postgis");
const environment = process.env.ENVIRONMENT || "development";
const config = require("./knexfile.js")[environment];
const db = knex(config);
const st = knexPostgis(db);
// Check if table exists if not create
db.schema.dropTable("stores");
db.schema.hasTable("stores").then(function(exists) {
if (!exists) {
return db.schema.createTable("stores", function(t) {
t.increments("id").primary();
t.string("store_name", 100);
t.string("store_location", 100);
t.text("address");
t.string("city", 100);
t.string("state", 100);
t.string("zip_code", 100);
t.specificType("geom", "geometry(point, 4326)");
t.string("latitude", 100);
t.string("longitude", 100);
t.string("county", 100);
t.index(["store_name", "address", "geom"]);
});
}
});
fs.createReadStream("seed/store-locations.csv")
.pipe(
csv({
mapHeaders: ({ header, index }) => header.toLowerCase().replace(" ", "_")
})
)
.on("data", row => {
// console.log(row)
let data = Object.assign({}, row);
// data.geom = `POINT(${data.latitude}, ${data.longitude})`
data.geom = st.geomFromText(
`POINT(${data.latitude} ${data.longitude})`,
4326
);
results.push(row);
let sql = db
.insert(data)
.into("stores")
.toString();
wstream.write(sql + ";\n");
return sql;
})
.on("end", () => {
console.log("This many results finished:", results.length);
wstream.end();
});