Skip to content

Commit f3a9855

Browse files
authored
feat: add sample scripts for initializing MongoDB (#129)
* feat: add sample scripts to initialize MongoDB * refactor: rename environment variables for clarity * refactor: rename setup scripts to clarify reset behavior * refactor: rename environment variables
1 parent 7241646 commit f3a9855

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

infra-samples/mongodb/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Samples for MongoDB
2+
3+
MongoDB collection and GridFS bucket are created automatically on first use, so it is not necessary to run these scripts before the initial application startup. Their primary purpose is to reset the environment to a clean state.
4+
5+
## GridFS
6+
WebRTC statistics are stored as ZIP files on GridFS bucket.
7+
8+
### Run
9+
```
10+
$ node ./infra-samples/mongodb/reset-gridfs-bucket.js
11+
```
12+
13+
## MongoDB
14+
MongoDB manages meeting metadata. You can see the schema in [API.md](../../API.md#identity-request)
15+
16+
### Run
17+
```
18+
$ node ./infra-samples/mongodb/reset-mongodb-indexes.js
19+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const { MongoClient } = require('mongodb');
2+
3+
const {
4+
RTCSTATS_MONGODB_URI,
5+
RTCSTATS_MONGODB_NAME,
6+
RTCSTATS_GRIDFS_BUCKET
7+
} = process.env;
8+
9+
10+
if (!RTCSTATS_MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_GRIDFS_BUCKET) {
11+
console.error(
12+
'Error: RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_GRIDFS_BUCKET '
13+
+ 'environment variables must be set.'
14+
);
15+
process.exit(1);
16+
}
17+
18+
/**
19+
* Resets the MongoDB GridFS bucket by dropping existing collections.
20+
* This ensures the bucket is clean before use.
21+
* @returns {Promise<void>}
22+
*/
23+
async function resetGridFSBucket() {
24+
const client = new MongoClient(RTCSTATS_MONGODB_URI);
25+
26+
try {
27+
await client.connect();
28+
console.log('Successfully connected to MongoDB server.');
29+
30+
const db = client.db(RTCSTATS_MONGODB_NAME);
31+
32+
const collections = await db.listCollections({ name: `${RTCSTATS_GRIDFS_BUCKET}.files` }).toArray();
33+
34+
if (collections.length > 0) {
35+
console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' already exists. Dropping and recreating...`);
36+
await db.collection(`${RTCSTATS_GRIDFS_BUCKET}.files`).drop();
37+
await db.collection(`${RTCSTATS_GRIDFS_BUCKET}.chunks`).drop();
38+
console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' dropped successfully.`);
39+
}
40+
41+
console.log(`Bucket '${RTCSTATS_GRIDFS_BUCKET}' is ready for use.`);
42+
43+
} catch (err) {
44+
console.error('An error occurred during GridFS setup:', err);
45+
} finally {
46+
await client.close();
47+
console.log('MongoDB connection closed.');
48+
}
49+
}
50+
51+
resetGridFSBucket();
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const { MongoClient } = require('mongodb');
2+
3+
const {
4+
RTCSTATS_MONGODB_URI,
5+
RTCSTATS_MONGODB_NAME,
6+
RTCSTATS_METADATA_COLLECTION
7+
} = process.env;
8+
9+
10+
if (!RTCSTATS_MONGODB_URI || !RTCSTATS_MONGODB_NAME || !RTCSTATS_METADATA_COLLECTION) {
11+
console.error(
12+
'Error: RTCSTATS_MONGODB_URI, RTCSTATS_MONGODB_NAME, and RTCSTATS_METADATA_COLLECTION '
13+
+ 'environment variables must be set.'
14+
);
15+
process.exit(1);
16+
}
17+
18+
const collectionName = RTCSTATS_METADATA_COLLECTION;
19+
20+
const indexDefinitions = [
21+
{
22+
key: {
23+
conferenceId: 1,
24+
dumpId: 1
25+
},
26+
name: 'PK_conferenceId_dumpId',
27+
unique: true
28+
},
29+
{
30+
key: {
31+
conferenceUrl: 1,
32+
startDate: 1
33+
},
34+
name: 'GSI_conferenceUrl_startDate'
35+
},
36+
{
37+
key: { conferenceId: 1,
38+
startDate: 1
39+
},
40+
name: 'GSI_conferenceId_startDate'
41+
},
42+
{
43+
key: { sessionId: 1,
44+
startDate: 1
45+
},
46+
name: 'GSI_sessionId_startDate'
47+
}
48+
];
49+
50+
51+
/**
52+
* Resets the MongoDB collection and applies the required indexes.
53+
* It drops the collection if it already exists to ensure a clean state.
54+
* @returns {Promise<void>}
55+
*/
56+
async function resetMongoDB() {
57+
const client = new MongoClient(RTCSTATS_MONGODB_URI);
58+
59+
try {
60+
await client.connect();
61+
console.log('Successfully connected to MongoDB server.');
62+
63+
const db = client.db(RTCSTATS_MONGODB_NAME);
64+
const collection = db.collection(collectionName);
65+
66+
const collections = await db.listCollections({ name: collectionName }).toArray();
67+
68+
if (collections.length > 0) {
69+
console.log(`Collection '${collectionName}' already exists. Dropping and recreating...`);
70+
await collection.drop();
71+
console.log(`Collection '${collectionName}' dropped successfully.`);
72+
}
73+
74+
console.log(`Creating new collection '${collectionName}' and applying indexes...`);
75+
const result = await collection.createIndexes(indexDefinitions);
76+
77+
console.log('Successfully created indexes:', result);
78+
79+
} catch (err) {
80+
console.error('An error occurred during MongoDB setup:', err);
81+
} finally {
82+
await client.close();
83+
console.log('MongoDB connection closed.');
84+
}
85+
}
86+
87+
resetMongoDB();

0 commit comments

Comments
 (0)