Added dbinit

This commit is contained in:
ultimateplayer1999 2023-04-26 08:02:43 +00:00
parent 294e24058f
commit 0a33e7f4b5
3 changed files with 55 additions and 15 deletions

14
db.js
View File

@ -1,14 +0,0 @@
require('dotenv').config();
const mongoose = require('mongoose');
const uri = `mongodb+srv://${process.env.MONGODBUSER}:${process.env.MONGODBPASS}@${process.env.MONGODBCLUSTER}/${process.env.DATABASE}?retryWrites=true&w=majority`
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`Connected to the database`);
}).catch((error) => {
console.log(`Failed to connect to the database`);
console.log(error);
});

50
dbinit.js Normal file
View File

@ -0,0 +1,50 @@
require('dotenv').config();
const mongoose = require('mongoose');
const Wolfcount = require('./models/wolfcount');
const uri = `mongodb+srv://${process.env.MONGODBUSER}:${process.env.MONGODBPASS}@${process.env.MONGODBCLUSTER}/${process.env.DATABASE}?retryWrites=true&w=majority`
try {
// connect to the database
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log(`Connected to the database`);
}).catch((error) => {
console.log(`Failed to connect to the database`);
console.log(error);
});
// Wait a few seconds bfore execution
setTimeout(function() {
// ensure that the bot stats document exists
const wolfcount = new Wolfcount({
count: 0,
validation: "count"
});
wolfcount.save().then(() => {
console.log('Count document saved successfully into the database!');
Wolfcount.find().then((document) => {
console.log(document);
}).catch((err) => {
console.log('Failed to retrieve document from the database', err)
});
}).catch(err => {
console.log('Error while saving the count document', err);
});
}, 3000);
setTimeout(function() {
try {mongoose.disconnect();}
catch(err){console.log('Error while disconnecting from the database', err);}
}, 3000);
setTimeout(function() {
console.log('Collection and document saved successfully into the database. Succesfully closed the connection to the db!');
}, 4000);
}
catch(err) {
console.log('Error while connecting to the database', err);
}

View File

@ -3,4 +3,8 @@
The configuration is based on a `.env` file. See the example.env file for the configuration options. The configuration is based on a `.env` file. See the example.env file for the configuration options.
It is currently only based on the cloud version of mongodb. It is currently only based on the cloud version of mongodb.
This bot is based on [shrimpBot](https://github.com/numselli/shrimpBot). Converted the DB from postgres to mongodb This bot is based on [shrimpBot](https://github.com/numselli/shrimpBot). Converted the DB from postgres to mongodb.
Use the following command to initialize the database.
`node dbinit.js`
It will create the needed collection and document and return it in the console output.