uploaded files to manage MongoDB database

This commit is contained in:
buzz-lightsnack-2007 2025-04-06 15:08:18 +08:00
parent c0c0085294
commit 8809c06912
2 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,33 @@
const Mongoose = require(`mongoose`);
const CustomErrors = require(`../utilities/errors.JS`);
class DBManagement {
/*
Begin the connection.
@param {string} DOMAIN the domain
@param {string} DBNAME the database name
*/
constructor (DOMAIN, DBNAME) {
const throwError = (ERROR) => {throw ERROR;};
this[`state`] = Mongoose.connect(`mongodb://${DOMAIN}/${(DBNAME) ? DBNAME : ""}`, { useNewUrlParser: true, useUnifiedTopology: true }).then((CONNECTION) => {
this[`connection`] = CONNECTION.connection;
console.log(`Connection successful.`);
}).catch(throwError);
};
/*
Establish the schema and collection. The result is stored in `this.#status`.
@param {string} COLLECTION the name of the collection
@param {object} SCHEMA the scehma
*/
configure (COLLECTION, SCHEMA) {
this[`schema`] = new Mongoose.Schema(SCHEMA);
this[`collection`] = COLLECTION;
this.state.then(() => {this.model = this[`connection`].model(`URL`, this[`schema`]);})
}
}
module.exports = DBManagement;