-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
2,153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./interactions/index.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.