djs-systems
Version:
The simplest way to build complex Discord bots.
58 lines (57 loc) • 2.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.connect = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const error_1 = require("./error");
const misc_1 = require("./misc");
const simplydjs_1 = require("../simplydjs");
// ------------------------------
// ------ F U N C T I O N -------
// ------------------------------
/**
* Connect to a mongo database to access some of the simply-djs functions ! *Requires* [MongoDB](https://mongodb.com/)
*
* @param db - The MongoDB URI string that is used to connect to the database.
* @param options
*
* @returns A Promise that resolves to a boolean value indicating whether the database connection was
* successful or not.
*
* @link `Documentation:` https://simplyd.js.org/docs/general/connect
* @example simplydjs.connect('mongoURI')
*/
async function connect(db, options = { strict: false }) {
return new Promise(async (resolve, reject) => {
if (!db) {
if (options?.strict)
throw new error_1.SimplyError({
function: 'connect',
title: 'Provide an valid mongodb uri string.',
tip: `Expected an MongoDB URI. Received ${db || 'undefined'}`
});
else
console.log(`SimplyError - connect | Provide an valid mongodb uri string.\n\n` +
`Expected an MongoDB URI. Received ${db || 'undefined'}`);
}
mongoose_1.default
.connect(db)
.then(async () => {
if (options?.notify !== false) {
const json = await (0, misc_1.https)('registry.npmjs.org/simply-djs');
const v = json['dist-tags'].latest;
if (v.toString() != simplydjs_1.version) {
console.log(`\n\t\tUpdate available | ${simplydjs_1.version} >>> ${v}\n\t\tRun 'npm i simply-djs@latest' to update\n`);
}
console.log('[Simply-DJS] Database connected ✨');
}
resolve(true);
})
.catch((err) => {
reject(err.stack);
});
});
}
exports.connect = connect;