-
Notifications
You must be signed in to change notification settings - Fork 19
/
localDevTester.js
41 lines (36 loc) · 1.24 KB
/
localDevTester.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
import fs from 'fs';
import MongoDB from 'mongodb';
import JSONStream from 'JSONStream';
import { streamToMongoDB } from '../index';
import sampleDataGenerator from './sampleDataGenerator';
// feel free to change these
const numberOfDataItems = 100;
const sampleDataOutputFile = 'src/Dev/testDataDev.json';
const config = {
dbURL: 'mongodb://localhost:27017/streamToMongoDB',
collection: 'dev',
batchSize: 1
};
const runTester = async () => {
try {
await clearDB();
await sampleDataGenerator.insertSampleData(numberOfDataItems, sampleDataOutputFile);
const inputDataStream = fs.createReadStream(sampleDataOutputFile).pipe(JSONStream.parse('*'));
const streamer = await streamToMongoDB(config);
console.log(`Streaming data to MongoDB [ count: ${numberOfDataItems} | batchSize : ${config.batchSize} ]`);
inputDataStream.pipe(streamer);
inputDataStream.on('end', () => console.log('-> DONE'));
} catch (error) {
console.log(error);
}
};
const clearDB = async () => {
try {
const dbConnection = await MongoDB.MongoClient.connect(config.dbURL);
await dbConnection.dropDatabase();
await dbConnection.close();
} catch (error) {
console.log(error);
}
};
runTester();