-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.js
65 lines (55 loc) · 2.01 KB
/
crud.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { MongoClient } = require("mongodb");
const mongodb = require("mongodb");
var index = require("./audio_scraper");
const fs = require("fs");
async function main() {
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/drivers/node/ for more details
*/
const uri =
"mongodb+srv://akshaynthakur:[email protected]/?retryWrites=true&w=majority";
/**
* The Mongo Client you will use to interact with your database
* See https://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html for more details
* In case: '[MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated...'
* pass option { useUnifiedTopology: true } to the MongoClient constructor.
* const client = new MongoClient(uri, {useUnifiedTopology: true})
*/
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
// Make the appropriate DB calls
let records = await index.pullRecords();
for (const record of records) {
await createAudio(client, record);
}
} finally {
// Close the connection to the MongoDB cluster
await client.close();
}
}
main().catch(console.error);
// Add functions that make DB calls here
// async function createAudio(client, newAudio) {
// const result = await client
// .db("cultural-survival-mobile")
// .collection("audio")
// .insertOne(newAudio);
// console.log(
// `New listing created with the following id: ${result.insertedId}`
// );
// }
async function createAudio(client, metadata_insert) {
const db = client.db("cultural-survival-mobile");
const bucket = new mongodb.GridFSBucket(db, { bucketName: "audio" });
//uploading files
let filename = metadata_insert["mp3filename"];
fs.createReadStream(`./audio/${filename}.mp3`).pipe(
bucket.openUploadStream(`${filename}.mp3`, {
chunkSizeBytes: 1048576,
metadata: metadata_insert,
})
);
}