Skip to content

Commit

Permalink
⚡ Add mongodb create indexes script
Browse files Browse the repository at this point in the history
  • Loading branch information
Androz2091 committed Jul 17, 2020
1 parent 153938f commit 4ec3a31
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/Atlanta.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class Atlanta extends Client {
if(this.databaseCache.members.get(`${memberID}${guildID}`)){
return isLean ? this.databaseCache.members.get(`${memberID}${guildID}`).toJSON() : this.databaseCache.members.get(`${memberID}${guildID}`);
} else {
let memberData = (isLean ? await this.membersData.findOne({ id: memberID, guildID }).lean() : await this.membersData.findOne({ id: memberID, guildID }));
let memberData = (isLean ? await this.membersData.findOne({ guildID, id: memberID }).lean() : await this.membersData.findOne({ guildID, id: memberID }));
if(memberData){
if(!isLean) this.databaseCache.members.set(`${memberID}${guildID}`, memberData);
return memberData;
Expand Down
44 changes: 44 additions & 0 deletions scripts/create-db-indexes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const chalk = require("chalk");
console.log(chalk.blue("Migrating database from v4.6.4 to v4.7.0...\n\n"));

let MongoClient;
try {
MongoClient = require("mongodb").MongoClient;
} catch (e) {
console.log(chalk.red("Cannot find module mongodb. Please install it using \"npm install mongodb\" before executing migration scripts."));
process.exit(1);
}

const config = require("../config.js");
const dbName = config.mongoDB.split("/").pop();
const baseURL = config.mongoDB.substr(0, config.mongoDB.length - dbName.length);
const client = new MongoClient(baseURL, {
useUnifiedTopology: true
});
client.connect().then(async () => {
console.log(chalk.green("Connected successfully to mongoDB database."));

const db = client.db(dbName);
const guilds = db.collection("guilds");
const members = db.collection("members");
const users = db.collection("users");

console.log(chalk.yellow("Creating guilds index..."));
await guilds.createIndex({ id: 1 });
console.log(chalk.green("Guilds index created."));

console.log(chalk.yellow("Creating members index..."));
await members.createIndex({ guildID: 1, id: -1 });
console.log(chalk.green("Members index created."));

console.log(chalk.yellow("Creating users index..."));
await users.createIndex({ id: 1 });
console.log(chalk.green("Users index created."));

console.log(chalk.blue("\n\nIndexes created."));

process.exit(0);
}).catch(() => {
console.log(chalk.red("Couldn't connect to mongoDB database..."));
process.exit(1);
});

0 comments on commit 4ec3a31

Please sign in to comment.