2022-01-04 02:18:28 +05:00
|
|
|
const chalk = require("chalk");
|
|
|
|
console.log(chalk.blue("Creating database indexes...\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 script."));
|
|
|
|
process.exit(1);
|
2022-01-13 00:26:23 +05:00
|
|
|
}
|
2022-01-04 02:18:28 +05:00
|
|
|
|
2022-01-13 00:56:24 +05:00
|
|
|
const config = require("../config");
|
2022-01-04 02:18:28 +05:00
|
|
|
const dbName = config.mongoDB.split("/").pop();
|
|
|
|
const baseURL = config.mongoDB.substr(0, config.mongoDB.length - dbName.length);
|
|
|
|
const client = new MongoClient(baseURL, {
|
2023-06-27 21:19:55 +05:00
|
|
|
useNewUrlParser: true,
|
2022-12-15 21:02:38 +05:00
|
|
|
useUnifiedTopology: true,
|
2022-01-04 02:18:28 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
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..."));
|
2023-06-27 21:19:55 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
await guilds.createIndex({
|
2022-12-15 21:02:38 +05:00
|
|
|
id: 1,
|
2022-01-04 02:18:28 +05:00
|
|
|
});
|
2023-06-27 21:19:55 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
console.log(chalk.green("Guilds index created."));
|
|
|
|
|
|
|
|
console.log(chalk.yellow("Creating members index..."));
|
2023-06-27 21:19:55 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
await members.createIndex({
|
|
|
|
guildID: 1,
|
2022-12-15 21:02:38 +05:00
|
|
|
id: -1,
|
2022-01-04 02:18:28 +05:00
|
|
|
});
|
2023-06-27 21:19:55 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
console.log(chalk.green("Members index created."));
|
|
|
|
|
|
|
|
console.log(chalk.yellow("Creating users index..."));
|
2023-06-27 21:19:55 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
await users.createIndex({
|
2022-12-15 21:02:38 +05:00
|
|
|
id: 1,
|
2022-01-04 02:18:28 +05:00
|
|
|
});
|
2023-06-27 21:19:55 +05:00
|
|
|
|
2022-01-04 02:18:28 +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..."));
|
2023-06-27 21:19:55 +05:00
|
|
|
|
2022-01-04 02:18:28 +05:00
|
|
|
process.exit(1);
|
|
|
|
});
|