/
load-json-to-db.ts
51 lines (45 loc) · 1.7 KB
/
load-json-to-db.ts
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
import * as fs from 'fs';
import * as program from 'commander';
import { AssetQuote } from '../types/asset-quote';
import { QuotesDbClient } from '../db/quotes-db-client';
async function loadJsonToDb(filename: string, options: any) {
const assetQuotes: AssetQuote[] = JSON.parse(fs.readFileSync(filename, 'utf-8'))
.map((entry: AssetQuote) => ({
...entry,
date: new Date(entry.date)
}));
console.log('%o entries read from json', assetQuotes.length);
const dbClient = await QuotesDbClient.getInstance(!options.uri ? undefined : {
uri: options.uri,
dbName: options.dbName,
collName: options.collection
});
if (options.emptify) {
console.log('docs in db before cleanup: %o', await dbClient.countDocuments());
await dbClient.remove({});
}
await dbClient.insertMany(assetQuotes);
return dbClient.countDocuments().then(res => dbClient.disconnect().then(() => res));
}
const options = program
.version('1.0.0')
.description('Loads quotes data from json file to MongoDB')
.usage('[options] <data.json>')
.option('-e, --emptify', 'emptify collection before load', false)
.option('-u, --uri <uri>', 'mongo connection string', false)
.option('-d, --db-name <dbName>', 'database name', false)
.option('-c, --collection <collName>', 'collection name', false)
.parse(process.argv);
if (!program.args.length) {
program.help();
process.exit(1);
}
loadJsonToDb(program.args[0], options).then(
(docsCount) => {
console.log('Data has been imported; now there are %o docs in db', docsCount);
},
(e) => {
console.error('Error: ', e.message);
process.exit(1);
}
);