UNPKG

sectom

Version:

Sectom is a useful npm package that has multiple easy to use functions.

55 lines (42 loc) 1.95 kB
const { DataBaseValueError } = require("../../CustomErrors/CustomErrors"); /** * Switches two values from the database with one another * @param {object} db - The database required * @param {string} keyOne The first item * @param {string} keyTwo The second item * @example const keyOne = await db.get('keyOne') const keyTwo = await db.get('keyTwo') DB.switch(db, keyOne, keyTwo) @returns {(true | false| null )} - Throws null if both values are the same, Throws true if the function swapped both values around, Throws false if there was an error such as: TypeError, ReferenceError, or any other type of error */ const quickMongoDB_switch = async (db, keyOne, keyTwo) => { try { if (!db) throw new TypeError('You must pass in the "db" parameter!'); if (typeof db.set == "undefined") throw new ReferenceError( "Make sure you have passed in a valid db object such as: quick.db or quickmongo!" ); if (keyOne === keyTwo) return; const doesExistOne = db.all().then((values) => { values.filter((a) => a.ID == keyOne); }); const doesExistTwo = db.all().then((values) => { values.filter((a) => a.ID == keyTwo); }); if ((!doesExistOne || doesExistOne == null) && !db.has(keyOne)) throw new DataBaseValueError(`"${keyOne}"' is not found in the database`); if ((!doesExistTwo || doesExistTwo == null) && !db.has(keyTwo)) throw new DataBaseValueError(`"${keyTwo}"' is not found in the database`); const keyOneValue = await db.get(keyOne); const keyTwoValue = await db.get(keyTwo); db.set(keyOne, keyTwoValue); db.set(keyTwo, keyOneValue); return true; } catch (error) { console.log(error); return false; } }; module.exports = quickMongoDB_switch;