add model search

This commit is contained in:
buzz-lightsnack-2007 2025-04-06 15:41:58 +08:00
parent 8ff4092ebc
commit 2eba54d48a
2 changed files with 25 additions and 25 deletions

View file

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

View file

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