-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
72 lines (62 loc) · 2.18 KB
/
Copy pathcommand.ts
File metadata and controls
72 lines (62 loc) · 2.18 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
68
69
70
71
72
import type { Configuration } from "log4js";
import type { Db } from "mongodb";
import { program } from "commander";
import has from "lodash.has";
import Log4js from "log4js";
import config from "./config";
import mongoConnection from "./infra/mongo";
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { version } = require("./package.json") as { version: string };
if (has(config, "logging")) {
Log4js.configure(config.logging as Configuration);
global.logger = Log4js.getLogger();
}
function increaseVerbosity(_v: string, total: number): number {
return total + 1;
}
program
.version(version)
.option(
"-v, --verbose",
"A value that can be increased",
increaseVerbosity,
0,
)
.option("-l, --log", "Log to a file")
.description("GeoName Commands");
program
.command("populate:db")
.description("Populate DB with Brazilian States and Cities")
.option("--drop", "Drop collections before inserting")
.action(async function populateDB(options: { drop?: boolean }) {
global.logger.info("Starting");
const conn = await mongoConnection(global.logger, config.db);
const db = conn.db(config.db.dbName) as Db;
if (options.drop) {
global.logger.info("dropping");
await Promise.all([
db.collection("states").deleteMany({}),
db.collection("cities").deleteMany({}),
]);
}
global.logger.info("Inserting states and cities");
await Promise.all([
// eslint-disable-next-line @typescript-eslint/no-require-imports
db
.collection("states")
.insertMany(require("./infra/db-seed/states.json")),
// eslint-disable-next-line @typescript-eslint/no-require-imports
db
.collection("cities")
.insertMany(require("./infra/db-seed/cities.json")),
]);
global.logger.info("Creating indexes");
await db
.collection("states")
.createIndex({ "$**": "text", name: 1, shortName: 1 });
await db.collection("states").createIndex({ shortName: 1 });
await db.collection("cities").createIndex({ name: 1 });
await db.collection("cities").createIndex({ "$**": "text", name: 1 });
global.logger.info("Done.");
});
program.parse(process.argv);