From 0a33e7f4b577029f128a4bdd4985b55a7bbfbbdf Mon Sep 17 00:00:00 2001 From: ultimateplayer1999 Date: Wed, 26 Apr 2023 08:02:43 +0000 Subject: [PATCH] Added dbinit --- db.js | 14 -------------- dbinit.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 6 +++++- 3 files changed, 55 insertions(+), 15 deletions(-) delete mode 100644 db.js create mode 100644 dbinit.js diff --git a/db.js b/db.js deleted file mode 100644 index b470407..0000000 --- a/db.js +++ /dev/null @@ -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); -}); \ No newline at end of file diff --git a/dbinit.js b/dbinit.js new file mode 100644 index 0000000..b31866d --- /dev/null +++ b/dbinit.js @@ -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); +} \ No newline at end of file diff --git a/readme.md b/readme.md index 15d53b0..8c7d3ea 100644 --- a/readme.md +++ b/readme.md @@ -3,4 +3,8 @@ 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. -This bot is based on [shrimpBot](https://github.com/numselli/shrimpBot). Converted the DB from postgres to mongodb \ No newline at end of file +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. \ No newline at end of file