-
Notifications
You must be signed in to change notification settings - Fork 39
/
remove_db.ts
43 lines (35 loc) · 1.21 KB
/
remove_db.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
32
33
34
35
36
37
38
39
40
41
42
43
import mongoose, { Connection } from 'mongoose';
// Pass URL of your MongoDB instance as the first argument
const userArgs: string[] = process.argv.slice(2);
const mongoDB: string = userArgs[0];
// Connect to MongoDB
mongoose.connect(mongoDB)
.then(() => {
const db: Connection = mongoose.connection;
// Handle connection errors
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// Function to clear the database
const clearDatabase = async () => {
try {
// Clear each collection
await db.dropDatabase();
console.log('Database cleared');
} catch (err) {
console.log('ERROR when dropping: ' + err);
} finally {
// Close the database connection
db.close();
}
}
// Run the function to clear the database
clearDatabase().catch((err) => {
console.log('ERROR when clearing: ' + err);
db.close();
process.exit(1);
});
})
.catch(err => {
console.log('ERROR when connecting: ' + err);
process.exit(1);
});
console.log('Processing ...');