From 2eba54d48a2d03f1b9fb4353497cc09294a6e362 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:41:58 +0800 Subject: [PATCH] add model search --- scripts/database/management/management.JS | 24 +++++++++++++++++ .../database/management/management_user.JS | 26 +------------------ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/scripts/database/management/management.JS b/scripts/database/management/management.JS index e03c7c1..25e2d8a 100644 --- a/scripts/database/management/management.JS +++ b/scripts/database/management/management.JS @@ -28,6 +28,30 @@ class DBManagement { this[`collection`] = COLLECTION; this.state.then(() => {this.model = this[`connection`].model(`URL`, this[`schema`]);}) } + + /* + Find based on corresponding details. + + @param {object} CRITERIA the search criteria + @param {number} SIZE the size of the search + @return {object} the search results + */ + async search(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; + }; } 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 index c357201..2c39c51 100644 --- a/scripts/database/management/management_user.JS +++ b/scripts/database/management/management_user.JS @@ -27,7 +27,7 @@ class UsersManagement extends DBManagement { @return {object} the found user */ const checkMatch = async () => { - let RESULTS = await this.match({"name": USERNAME}, 1); + let RESULTS = await this.search({"name": USERNAME}, 1); return ((RESULTS.length) ? RESULTS[0] : false) } @@ -52,28 +52,4 @@ class UsersManagement extends DBManagement { 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