This repository was archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimporter.ts
More file actions
65 lines (58 loc) · 2.5 KB
/
importer.ts
File metadata and controls
65 lines (58 loc) · 2.5 KB
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
import * as crypto from "crypto";
import * as fs from "fs";
import * as geolib from "geolib";
import { IUser, User } from "./schema";
const csv = require("csv-parser");
(async () => {
await User.collection.drop();
// At Houston's airport
const center: geolib.PositionAsDecimal = { latitude: 29.978999, longitude: -95.336954 };
let userDocs: Partial<IUser>[] = [];
fs.createReadStream("data.csv", {encoding: "utf8"})
.pipe(csv())
.on("data", async (data: any) => {
let locationProximity: number = parseInt(data.location, 10);
let location = geolib.computeDestinationPoint(center, Math.random() * locationProximity * 9000 + 8000 * locationProximity, Math.random() * 360);
userDocs.push({
"name": data.name,
"medicalConditions": parseInt(data.medicalConditions, 10),
"allergies": parseInt(data.allergies, 10),
"medications": parseInt(data.medications, 10),
"weight": parseInt(data.weight, 10),
"height": parseInt(data.height, 10),
"age": parseInt(data.age, 10),
"kids": parseInt(data.kids, 10),
"animals": parseInt(data.animals, 10),
"spouse": data.spouse === "1",
"hasTransportation": data.hasTransportation === "1",
"locationProximity": parseInt(data.location, 10),
"location": [{
"lat": location.latitude,
"long": location.longitude
}],
"authorizationKey": crypto.randomBytes(32).toString("hex")
});
console.log(`${location.latitude},${location.longitude}`);
}).on("end", async () => {
// Add persistent
userDocs.push({
"name" : "Aaron Vontell",
"medicalConditions" : 2,
"allergies" : 2,
"medications" : 2,
"weight" : 260,
"height" : 71,
"age" : 20,
"kids" : 0,
"animals" : 2,
"spouse" : true,
"hasTransportation" : false,
"evacuate" : true,
"authorizationKey" : "5ded7ca370fbe000fd5f0cfcbafb47d1b920bcc063db9511e4be8fe09215241a",
"location" : []
});
await User.insertMany(userDocs);
console.log(`Imported ${userDocs.length} documents`);
process.exit(0);
});
})();