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;

View file

@ -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;
};
}