From 8809c06912f46085f1295ff99eee5cb315f79b24 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sun, 6 Apr 2025 15:08:18 +0800 Subject: [PATCH] uploaded files to manage MongoDB database --- scripts/database/management/management.JS | 33 ++++++++ .../database/management/management_user.JS | 79 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 scripts/database/management/management.JS create mode 100644 scripts/database/management/management_user.JS diff --git a/scripts/database/management/management.JS b/scripts/database/management/management.JS new file mode 100644 index 0000000..e03c7c1 --- /dev/null +++ b/scripts/database/management/management.JS @@ -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; \ No newline at end of file diff --git a/scripts/database/management/management_user.JS b/scripts/database/management/management_user.JS new file mode 100644 index 0000000..c357201 --- /dev/null +++ b/scripts/database/management/management_user.JS @@ -0,0 +1,79 @@ +import CustomErrors from "../../utilities/errors.JS"; + +const DBManagement = require(`./management.JS`); +const User = require(`../entry/user.JS`); +const Hash = require(`../../utilities/hash.JS`); +const Errors = require(`../../utilities/errors.JS`); + +class UsersManagement extends DBManagement { + constructor() { + super(`localhost:27017`, `Activity-Tracker`); + this.configure(`Users`, User.generateTemplate()); + }; + + /* + Create a user. The ID will be automatically generated. + + @param {string} USERNAME the user name + @param {object} OPTIONS the account options + @return {object} the user data + */ + async create(USERNAME, OPTIONS, done) { + await this.state; + + /* + Check if a user has been created. + + @return {object} the found user + */ + const checkMatch = async () => { + let RESULTS = await this.match({"name": USERNAME}, 1); + return ((RESULTS.length) ? RESULTS[0] : false) + } + + const createUser = async () => { + let ID = await (new Hash(USERNAME)).convert(`String`); + let CREATED = new User({'name': USERNAME.trim(), 'ID': ID}); + + let ENTRY = new this.model(CREATED); + try { + await ENTRY.save(); + } catch(ERROR) { + throw new CustomErrors.DBProblem(ERROR.message); + } + + return (CREATED); + } + + if ((USERNAME) ? USERNAME.trim() : false) { + let CURRENT = !(await checkMatch()) + CURRENT = (!CURRENT) ? createUser() : CURRENT; + + return (CURRENT); + }; + }; + + /* + Find a user based on their corresponding details. + + @param {object} CRITERIA the search criteria + @param {number} SIZE the size of the search + @return {object} the search results + */ + async match(CRITERIA = {}, SIZE) { + await this.state; + let RESULT = []; + + if ((CRITERIA && CRITERIA instanceof Object) ? !Object.keys(CRITERIA).length : true) { + RESULT = await this.model.find(); + } else if (CRITERIA instanceof Object) { + RESULT = await this.model.find(CRITERIA); + } + + if (RESULT.length && (SIZE) ? (SIZE > 0 && SIZE < RESULT.length) : false) { + RESULT = RESULT.slice(0,n); + } + + return RESULT; + }; +} \ No newline at end of file