JaBa/scripts/create-db-indexes.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const chalk = require("chalk");
2021-12-16 16:05:15 +05:00
console.log(chalk.blue("Creating database indexes...\n\n"));
2021-12-10 21:39:54 +05:00
let MongoClient;
try {
MongoClient = require("mongodb").MongoClient;
} catch (e) {
2021-12-22 19:13:26 +05:00
console.log(chalk.red("Cannot find module mongodb. Please install it using \"npm install mongodb\" before executing script."));
2021-12-10 21:39:54 +05:00
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..."));
2021-12-26 19:29:37 +05:00
await guilds.createIndex({
id: 1
});
2021-12-10 21:39:54 +05:00
console.log(chalk.green("Guilds index created."));
console.log(chalk.yellow("Creating members index..."));
2021-12-26 19:29:37 +05:00
await members.createIndex({
guildID: 1,
id: -1
});
2021-12-10 21:39:54 +05:00
console.log(chalk.green("Members index created."));
console.log(chalk.yellow("Creating users index..."));
2021-12-26 19:29:37 +05:00
await users.createIndex({
id: 1
});
2021-12-10 21:39:54 +05:00
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);
});