Skip to content

Commit

Permalink
refactor: refactor the mongoose and sequelize connector
Browse files Browse the repository at this point in the history
  • Loading branch information
simplymichael committed Jun 19, 2024
1 parent 78e9d93 commit 6bb3e6b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/framework/component/connector/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module.exports = class MongooseStore {
debug("Disconnecting from MongoDB");

await this.#db.disconnect();
this.#db = null;

debug("MongoDB disconnection complete");
}
Expand Down
32 changes: 30 additions & 2 deletions src/framework/component/connector/sequelize.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { Sequelize } = require("sequelize");
const debug = require("../../lib/debug");


module.exports = class SequelizeStore {
#db = null;
#dbEngine = "memory";
#options = null;
#connected = false;

/**
* @param {Object} options object with properties:
Expand All @@ -23,14 +25,16 @@ module.exports = class SequelizeStore {
* in the URL string.
*/
constructor(options) {
debug("Creating SequelizeStore Instance...");

const {
dsn = "",
host = "0.0.0.0",
port = 3006,
username = "",
password = "",
dbEngine = "memory",
dbName = "orders",
dbName = "frameworkdb",
} = options;

this.#options = { dsn, host, port, username, password, dbEngine, dbName };
Expand All @@ -41,11 +45,13 @@ module.exports = class SequelizeStore {
}

createDbObject() {
debug("Creating Sequelize object...");

let dsn;
let sequelize;
const options = this.#options;
const { host, port, username, password, dbEngine, dbName } = options;
//const connOpts = { logging: logger.debug.bind(logger) };
const connOpts = {}; //{ logging: logger.debug.bind(logger) };

if(dbEngine.toLowerCase() === "memory") {
sequelize = new Sequelize("sqlite::memory:", connOpts);
Expand All @@ -70,6 +76,8 @@ module.exports = class SequelizeStore {
}

this.#db = sequelize;

debug("Sequelize object created.");
}

/**
Expand All @@ -78,8 +86,16 @@ module.exports = class SequelizeStore {
* @return {resource} a (mongoose) connection instance
*/
async connect() {
const dbEngine = this.#dbEngine;

debug(`Connecting to ${dbEngine}...`);

await this.#db.authenticate();

this.#connected = true;

debug(`${dbEngine} connection established`);

return this.#db;
}

Expand All @@ -93,7 +109,19 @@ module.exports = class SequelizeStore {
* Cf. https://sequelize.org/docs/v6/getting-started/#closing-the-connection
*/
async disconnect() {
const dbEngine = this.#dbEngine;

debug(`Disconnecting from ${dbEngine}`);

await this.#db.close();

this.#connected = false;

debug(`${dbEngine} disconnection complete`);
}

connected() {
return this.#connected;
}

getClient() {
Expand Down

0 comments on commit 6bb3e6b

Please sign in to comment.