Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sksmta authored Feb 10, 2021
1 parent a26747c commit 6bed0f8
Show file tree
Hide file tree
Showing 25 changed files with 2,153 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./interactions/index.js");
4 changes: 4 additions & 0 deletions interactions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports=({
mongo: require("../mongo/index.js"),
sqlite:require("../sqlite/src/index.js")
})
11 changes: 11 additions & 0 deletions mongo/esm/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import HiveMongo from "../index.js";

export default HiveMongoMongo;
export const {
base,
db,
HiveErr,
schema,
util,
version
} = HiveMongo;
7 changes: 7 additions & 0 deletions mongo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
base: require("./source/base"),
database: require("./source/index"),
HiveErr: require("./source/err"),
schema: require("./source/schema"),
util: require("./source/util")
};
133 changes: 133 additions & 0 deletions mongo/source/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
const EventEmitter = require("events").EventEmitter;
const mongoose = require("mongoose");
const Error = require("./err");
const chalk = require('chalk');

class Base extends EventEmitter {

/**
*Mongoose connection base.
* @param {string} mongodbURL Mongodb Database URL.
* @param {object} connectionOptions Mongodb connection options
*/
constructor(mongodbURL, connectionOptions={}) {
super();
if (!mongodbURL || !mongodbURL.startsWith("mongodb")) throw new Error("No mongodb url was provided!");
if (typeof mongodbURL !== "string") throw new Error(`Expected a string for mongodbURL, received ${typeof mongodbURL}`);
if (connectionOptions && typeof connectionOptions !== "object") throw new Error(`Expected Object for connectionOptions, received ${typeof connectionOptions}`);

/**
* @typedef {string} dbURL
* Current database url
*/
Object.defineProperty(this, "dbURL", {
value: mongodbURL
});

/**
* Mongoose connection options
* @type {ConnectionOptions}
*/
this.options = connectionOptions;

/**
* Returns mongodb connection
* @type {MongooseConnection}
*/
this.connection = this._create();

this.connection.on("error", (e) => {
this.emit("error", e);
});
this.connection.on("open", () => {
/**
* Timestamp when database became ready
* @type {Date}
*/
this.readyAt = new Date();
this.emit("ready");
});
}

/**
* Creates mongodb connection
* @returns {MongooseConnection}
* @ignore
*/
_create(url) {
this.emit("debug", "Creating database connection...");

if (url && typeof url === "string") this.dbURL = url;
if (!this.dbURL || typeof this.dbURL !== "string") throw new Error("Database url was not provided!", "MongoError");

delete this.options["useUnique"];

return mongoose.createConnection(this.dbURL, {
...this.options,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
}

/**
* Destroys database
* @ignore
*/
_destroyDatabase() {
this.connection.close(true);
this.readyAt = undefined;
this.dbURL = null;
this.emit("debug", "Database disconnected!");
}

/**
* Current database url
* @type {string}
*/
get url() {
return this.dbURL;
}

/**
* Returns database connection state
* @type {("DISCONNECTED"|"CONNECTED"|"CONNECTING"|"DISCONNECTING")}
*/
get state() {
if (!this.connection || typeof this.connection.readyState !== "number") return "DISCONNECTED";
switch(this.connection.readyState) {
case 0:
return "DISCONNECTED";
case 1:
return "CONNECTED";
case 2:
return "CONNECTING";
case 3:
return "DISCONNECTING";
}
}
}

/**
* Emitted when database creates connection
* @event Base#ready
* @example db.on("ready", () => {
* console.log("Successfully connected to the database!");
* });
*/

/**
* Emitted when database encounters error
* @event Base#error
* @param {Error} Error Error Message
* @example db.on("error", console.error);
*/

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

module.exports = Base;
12 changes: 12 additions & 0 deletions mongo/source/err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class HiveErr extends Error {

constructor(message, name = null) {
super();
Error.captureStackTrace(this, this.constructor);
this.message = message;
this.name = name || "TypeError";
}

}

module.exports = HiveErr;
Loading

0 comments on commit 6bed0f8

Please sign in to comment.