-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
67 lines (56 loc) · 1.91 KB
/
index.ts
File metadata and controls
67 lines (56 loc) · 1.91 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
66
67
/* eslint-disable no-console */
import mongoose from "mongoose";
import { hydrateClassifiersExtendedMeta } from "./classifiers";
import { rehydrateRecHHF } from "./recHHF";
import { hydrateScores } from "./scores";
import { hydrateShooters } from "./shooters";
import { hydrateStats } from "./stats";
export const connect = async () => {
const { LOCAL_DEV, MONGO_URL, MONGO_URL_LOCAL } = process.env;
if (!LOCAL_DEV && !MONGO_URL) {
throw new Error(
`Environment Variable MONGO_URL must be specified in top-level environment variables in sandbox mode.`,
);
}
const url = !LOCAL_DEV ? MONGO_URL : MONGO_URL_LOCAL;
const publicLogsDBHost = url!.split("@")[1]?.split(".")[0] || "local";
const dbName = url!.split("?")[0]?.split("/").reverse()[0] || "root";
const _connect = () => {
console.error("DB: connecting");
return mongoose.connect(url!);
};
mongoose.connection.on("error", e => {
console.error("Mongo connection error:");
if (e) {
console.error(e);
}
});
mongoose.connection.on("disconnected", async () => {
console.error("DB: lost connection");
await _connect();
});
mongoose.connection.on("connected", () => {
console.error(`DB: connected to ${publicLogsDBHost} ${dbName}`);
});
mongoose.connection.on("reconnected", () => {
console.error("DB: reconnected");
});
// Close the Mongoose connection, when receiving SIGINT
process.on("SIGINT", async () => {
await mongoose.connection.close();
process.exit(0);
});
await _connect();
};
// legacy hydration from USPSA json files, probably doesn't work anymore
export const hydrate = async () => {
console.log("hydrating everything");
console.time("full hydration");
await hydrateScores();
await rehydrateRecHHF();
await hydrateShooters();
await hydrateClassifiersExtendedMeta();
await hydrateStats();
console.timeEnd("full hydration");
console.log("hydration done");
};