forked from jitsi/rtcstats-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-gridfs-bucket.js
More file actions
51 lines (40 loc) · 1.59 KB
/
reset-gridfs-bucket.js
File metadata and controls
51 lines (40 loc) · 1.59 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
const { MongoClient } = require('mongodb');
const {
RTCSTATS_MONGODB_URI,
RTCSTATS_MONGODB_NAME,
RTCSTATS_GRIDFS_BUCKET
} = process.env;
if (!RTCSTATS_MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) {
console.error(
'Error: RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_GRIDFS_BUCKET '
+ 'environment variables must be set.'
);
process.exit(1);
}
/**
* Resets the MongoDB GridFS bucket by dropping existing collections.
* This ensures the bucket is clean before use.
* @returns {Promise<void>}
*/
async function resetGridFSBucket() {
const client = new MongoClient(RTCSTATS_MONGODB_URI);
try {
await client.connect();
console.log('Successfully connected to MongoDB server.');
const db = client.db(RTCSTATS_MONGODB_NAME);
const collections = await db.listCollections({ name: `${RTCSTATS_GRIDFS_BUCKET}.files` }).toArray();
if (collections.length > 0) {
console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' already exists. Dropping and recreating...`);
await db.collection(`${RTCSTATS_GRIDFS_BUCKET}.files`).drop();
await db.collection(`${RTCSTATS_GRIDFS_BUCKET}.chunks`).drop();
console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' dropped successfully.`);
}
console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' is ready for use.`);
} catch (err) {
console.error('An error occurred during GridFS setup:', err);
} finally {
await client.close();
console.log('MongoDB connection closed.');
}
}
resetGridFSBucket();