New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

quickmongo

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

quickmongo - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

src/Util.js

8

index.js
module.exports = {
Base: require("./src/Base"),
Database: require("./src/Main"),
version: require("./package.json").version,
MemoryStorage: require("./src/Cache")
Error: require("./src/Error"),
MemoryStorage: require("./src/Cache"),
Schema: require("./src/Schema"),
Util: require("./src/Util"),
version: require("./package.json").version
};
{
"name": "quickmongo",
"version": "1.0.2",
"version": "1.0.3",
"description": "Quick mongodb wrapper for beginners.",

@@ -28,3 +28,3 @@ "main": "index.js",

"dependencies": {
"mongoose": "^5.9.20"
"mongoose": "^5.9.25"
},

@@ -31,0 +31,0 @@ "devDependencies": {

@@ -11,2 +11,3 @@ const EventEmitter = require("events").EventEmitter;

* @param {String} mongodbURL Mongodb Database URL.
* @param {Object} connectionOptions Mongodb connection options
* @returns {Base}

@@ -19,3 +20,3 @@ * @example const db = new Base("mongodb://localhost/mydb");

if (typeof mongodbURL !== "string") throw new Error(`Expected a string for mongodbURL, received ${typeof mongodbURL}`);
if (typeof connectionOptions !== "object") throw new Error(`Expected Object for connectionOptions, received ${typeof connectionOptions}`);
if (connectionOptions && typeof connectionOptions !== "object") throw new Error(`Expected Object for connectionOptions, received ${typeof connectionOptions}`);

@@ -50,6 +51,7 @@ /**

/**
* Creates connection
* Creates mongodb connection
* @private
*/
_create() {
this.emit("Creating database connection...");
mongoose.connect(this.dbURL, {

@@ -68,2 +70,4 @@ useNewUrlParser: true,

this.readyAt = undefined;
this.dbURL = null;
this.emit("debug", "Database disconnected!");
}

@@ -87,2 +91,9 @@ }

/**
* Emitted on debug mode
* @event Base#debug
* @param {String} Message Debug message
* @example db.on("debug", console.log);
*/
module.exports = Base;

@@ -1,5 +0,6 @@

let obj = {};
const Error = require("./Error");
const Util = require("./Util");
const fs = require("fs");
class Cache {
class MemoryStorage {

@@ -15,3 +16,4 @@ /**

constructor() {
console.warn("You are using MemoryStorage! Data will disappear on restart.");
console.warn("[QuickMongo] You are using MemoryStorage! Data will disappear on restart.");
this.obj = [];
}

@@ -23,8 +25,14 @@

* @param {any} value Value
* @returns {Object}
* @returns {any}
*/
set(key, value) {
if (!key || !value) throw new Error("No key or value provided!");
obj[String(key)] = value;
return obj;
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
if (!Util.isValue(value)) throw new Error("Invalid value specified!", "ValueError");
if (this.has(key)) {
this.obj.find(i => i.ID === key).data = value;
} else {
this.obj.push({ ID: key, data: value });
}
return this.get(key);
}

@@ -35,8 +43,9 @@

* @param {String} key Key
* @returns {Object}
* @returns {any}
*/
delete(key) {
if (!key) throw new Error("No key was specified!");
if (!!obj[String(key)]) delete obj[String(key)];
return obj;
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
if (!this.has(key)) return false;
this.obj = this.obj.filter(r => !r.ID === key);
return this.obj;
}

@@ -50,7 +59,16 @@

exists(key) {
if (!key) throw new Error("No key was specified!");
return !!obj[String(key)];
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
return !!(this.get(key));
}
/**
/**
* Checks if the key exists in cache or not
* @param {String} key Key
* @returns {Boolean}
*/
has(key) {
return this.exists(key);
}
/**
* Returns the data of the matching key

@@ -61,12 +79,24 @@ * @param {String} key Key

get(key) {
if (!key) throw new Error("No key was specified!");
return obj[String(key)];
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
let data = this.obj.find(r => r.ID === key);
if (!data) return null;
return data.data;
}
/**
* Returns the data of the matching key
* @param {String} key Key
* @returns {any}
*/
fetch(key) {
return this.get(key);
}
/**
* Returns everything from the cache
* @returns {Object}
* @returns {any}
*/
all() {
return obj;
return this.obj;
}

@@ -76,11 +106,35 @@

* Deletes everything from the cache
* @returns {Object}
* @returns {any}
*/
deleteAll() {
obj = {};
return obj;
this.obj = [];
return this.obj;
}
/**
* Exports the data to json file
* @param {String} fileName File name
* @param {String} path File path
* @returns {Promise<String>}
* @example db.export("database", "./").then(path => {
* console.log(`File exported to ${path}`);
* });
*/
export(fileName="database", path="./") {
if (typeof fileName !== "string") throw new Error("File name must be a string!");
if (typeof path !== "string") throw new Error("File path must be a string!");
return new Promise((resolve, reject) => {
const filePath = `${path}${fileName}.json`;
try {
fs.writeFileSync(filePath, JSON.stringify(this.all()));
resolve(filePath);
} catch(e) {
reject(e);
}
});
}
}
module.exports = Cache;
module.exports = MemoryStorage;

@@ -11,3 +11,3 @@ /**

this.message = message;
this.name = name || "Error";
this.name = name || "QuickMongoError";
}

@@ -14,0 +14,0 @@

@@ -5,2 +5,3 @@ const Base = require("./Base");

const fs = require("fs");
const Util = require("./Util");

@@ -10,3 +11,3 @@ /**

*/
class db extends Base {
class Database extends Base {

@@ -20,4 +21,4 @@ /**

*/
constructor(mongodbURL, name) {
super(mongodbURL);
constructor(mongodbURL, name, connectionOptions={}) {
super(mongodbURL, connectionOptions);

@@ -38,5 +39,4 @@ /**

async set(key, value) {
if (!key) throw new Error("No key specified!", "KeyError");
if (typeof key !== "string") throw new Error("Key must be a string.", "SyntaxError");
if (value === Infinity || value === -Infinity) throw new Error("Value may not be Infinity.");
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
if (!Util.isValue(value)) throw new Error("Invalid value specified!", "ValueError");
let raw = await this.schema.findOne({

@@ -71,4 +71,3 @@ ID: key

async delete(key) {
if (!key) throw new Error("No key specified!", "KeyError");
if (typeof key !== "string") throw new Error("Key must be a string.", "SyntaxError");
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
let data = await this.schema.findOneAndDelete({ ID: key })

@@ -87,4 +86,3 @@ .catch(e => {

async exists(key) {
if (!key) throw new Error("No key specified!", "KeyError");
if (typeof key !== "string") throw new Error("Key must be a string.", "SyntaxError");
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
let get = await this.get(key);

@@ -109,4 +107,3 @@ return !!get;

async get(key) {
if (!key) throw new Error("No key specified!", "KeyError");
if (typeof key !== "string") throw new Error("Key must be a string.", "SyntaxError");
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
let get = await this.schema.findOne({ ID: key })

@@ -165,7 +162,5 @@ .catch(e => {

async math(key, operator, value) {
if (!key) throw new Error("No key specified!", "KeyError");
if (typeof key !== "string") throw new Error("Key must be a string.", "SyntaxError");
if (!operator) throw new Error("No operator was provided!");
if (!value) throw new Error("No value was provided!");
if (typeof value !== "number") throw new Error("Value must be a number.", "SyntaxError");
if (!Util.isKey(key)) throw new Error("Invalid key specified!", "KeyError");
if (!operator) throw new Error("No operator provided!");
if (!Util.isValue(value)) throw new Error("Invalid value specified!", "ValueError");

@@ -292,3 +287,3 @@ switch(operator) {

async import(data=[]) {
if (!Array.isArray(data)) throw new Error("Data type must be Array.");
if (!Array.isArray(data)) throw new Error("Data type must be Array.", "DataTypeError");
if (data.length < 1) return [];

@@ -307,6 +302,7 @@ let start = Date.now();

this.set(item.ID, item.data);
this.emit("debug", `Successfully migrated ${item.ID} @${index}!`);
this.emit("debug", `Successfully migrated ${item.ID} @${index}!`);
});
this.emit("debug", `Successfully migrated ${data.length} entries to mongodb! Took ${Date.now() - start}ms!`);
return true;
this.set(item.ID, item.data);
this.emit("debug", `Successfully migrated ${data.length}. Took ${Date.now() - start}ms!`);
return;
}

@@ -323,4 +319,47 @@

/**
* Returns current schema name
* @readonly
*/
get name() {
return this.schema.name;
}
/**
* Read latency
* @ignore
*/
async _read() {
let start = Date.now();
await this.get("LQ==");
return Date.now() - start;
}
/**
* Write latency
* @ignore
*/
async _write() {
let start = Date.now();
await this.set("LQ==", Buffer.from(start.toString()).toString("base64"));
return Date.now() - start;
}
/**
* Fetches read and write latency of the database in ms
* @example const ping = await db.fetchLatency();
* console.log("Read: ", ping.read);
* console.log("Write: ", ping.write);
* console.log("Average: ", ping.average);
*/
async fetchLatency() {
let read = await this._read();
let write = await this._write();
let average = (read + write) / 2;
this.delete("LQ==").catch(e => {});
return { read, write, average };
}
}
module.exports = db;
module.exports = Database;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc