-
Notifications
You must be signed in to change notification settings - Fork 0
/
storeInDb.ts
31 lines (27 loc) · 922 Bytes
/
storeInDb.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
/**
* storeInDb
* Created by dcorns on 1/31/21
* Copyright © 2021 Dale Corns
*/
import {MongoClient} from 'mongodb';
const storeInDb = (docs, uri, databaseName, providerName, cb) => {
const client = new MongoClient(uri);
async function run() {
try {
const options = { ordered: true };
await client.connect();
const database = client.db(databaseName);
const collection = database.collection(providerName);
const result = await collection.insertMany(docs, options);
console.log(`Documents added: ${result.insertedCount}`);
const cursor = collection.find();
const cursorCount = await cursor.count();
console.log(`Total Documents: ${cursorCount}`);
} finally {
await client.close();
cb();
}
}
run().catch(console.dir);
}
export default storeInDb