Skip to content

Commit

Permalink
refactor: refactor the framework connector components and connections…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
simplymichael committed Jun 23, 2024
1 parent 98ac405 commit 22ba496
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ SESSION_SAME_SITE=none
## Currently supported drivers include "memory" and "redis".
SESSION_STORE_DRIVER=memory

## Remote logging (LogTail)
LOGTAIL_SOURCE_TOKEN=

# CORS settings

## Allowed HTTP Headers
Expand All @@ -99,3 +96,6 @@ ALLOWED_METHODS="GET, POST put|DelEte"
## Allowed origins
## (separate multiple origins by spaces, comma, semicolon, or pipe(|))
ALLOWED_ORIGINS="http://localhost http://localhost:3000"

## Remote logging (LogTail)
LOGTAIL_SOURCE_TOKEN=
25 changes: 17 additions & 8 deletions src/framework/component/connector/mongoose.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const util = require("node:util");
const mongoose = require("mongoose");
const debug = require("../../lib/debug");

Expand Down Expand Up @@ -63,22 +64,30 @@ module.exports = class MongooseStore {

mongoose.set("debug", enableDebugging);

debug("Connecting to MongoDB...");
try {
debug("Connecting to MongoDB...");

this.#db = await mongoose.connect(dsn, {});
this.#db = await mongoose.connect(dsn, {});

debug("MongoDB connection established");
debug("MongoDB connection established");

return this.#db;
return this.#db;
} catch(e) {
debug(`Mongoose connection error: ${util.inspect(e)}`);
}
}

async disconnect() {
debug("Disconnecting from MongoDB");
try {
debug("Disconnecting from MongoDB");

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

debug("MongoDB disconnection complete");
debug("MongoDB disconnection complete");
} catch(e) {
debug(`Mongoose disconnection error: ${util.inspect(e)}`);
}
}

connected() {
Expand Down
20 changes: 14 additions & 6 deletions src/framework/component/connector/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,27 @@ module.exports = class RedisStore {
);
}

debug("Connecting to Redis...");
try {
debug("Connecting to Redis...");

await this.getClient()?.connect();
await this.getClient()?.connect();

debug("Redis connection established.");
debug("Redis connection established.");
} catch(e) {
debug(`Redis connection error: ${util.inspect(e)}`);
}
}

async disconnect() {
debug("Disconnecting from Redis...");
try {
debug("Disconnecting from Redis...");

await this.getClient().disconnect();
await this.getClient().disconnect();

debug("Redis disconnection complete.");
debug("Redis disconnection complete.");
} catch(e) {
debug(`Redis disconnection error: ${util.inspect(e)}`);
}
}

/**
Expand Down
25 changes: 17 additions & 8 deletions src/framework/component/connector/sequelize.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const util = require("node:util");
const { Sequelize } = require("sequelize");
const debug = require("../../lib/debug");

Expand Down Expand Up @@ -88,13 +89,17 @@ module.exports = class SequelizeStore {
async connect() {
const dbEngine = this.#dbEngine;

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

await this.#db.authenticate();
await this.#db.authenticate();

this.#connected = true;
this.#connected = true;

debug(`${dbEngine} connection established`);
debug(`${dbEngine} connection established`);
} catch(e) {
debug(`Sequelize connection error: ${util.inspect(e)}`);
}

return this.#db;
}
Expand All @@ -111,13 +116,17 @@ module.exports = class SequelizeStore {
async disconnect() {
const dbEngine = this.#dbEngine;

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

await this.#db.close();
await this.#db.close();

this.#connected = false;
this.#connected = false;

debug(`${dbEngine} disconnection complete`);
debug(`${dbEngine} disconnection complete`);
} catch(e) {
debug(`Sequelize disconnection error: ${util.inspect(e)}`);
}
}

connected() {
Expand Down
43 changes: 16 additions & 27 deletions src/framework/connections/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const util = require("node:util");
const RedisStore = require("../component/connector/redis");
const MongooseStore = require("../component/connector/mongoose");
const createObjectStore = require("../component/registry");
Expand All @@ -25,39 +24,29 @@ module.exports = class Connections {
}

static #connectToMongoDb(dbCreds) {
try {
const mongooseStore = new MongooseStore(dbCreds);
const mongooseClient = mongooseStore.getClient();
const mongooseStore = new MongooseStore(dbCreds);
const mongooseClient = mongooseStore.getClient();

setTimeout(async function() {
if(!mongooseStore.connecting() && !mongooseStore.connected()) {
await mongooseStore.connect();
}
}, 1000);
setTimeout(async function() {
if(!mongooseStore.connecting() && !mongooseStore.connected()) {
await mongooseStore.connect();
}
}, 1000);

return mongooseClient;
} catch(e) {
// TO DO: Use a proper log service for this.
Connections.#log("Mongoose error", util.inspect(e));
}
return mongooseClient;
}

static #connectToRedis(redisCreds) {
try {
const redisStore = new RedisStore(redisCreds);
const redisClient = redisStore.getClient();
const redisStore = new RedisStore(redisCreds);
const redisClient = redisStore.getClient();

setTimeout(async function() {
if(!redisStore.connecting() && !redisStore.connected()) {
await redisStore.connect();
}
}, 1000);
setTimeout(async function() {
if(!redisStore.connecting() && !redisStore.connected()) {
await redisStore.connect();
}
}, 1000);

return redisClient;
} catch(e) {
// TO DO: Use a proper log service for this.
Connections.#log("Redis error", util.inspect(e));
}
return redisClient;
}

static #log(message) {
Expand Down
4 changes: 2 additions & 2 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const request = require("supertest");
const { create } = require("./framework/application");

const { StatusCodes, StatusTexts } = require("./framework/component/http");
const { STATUS_CODES, STATUS_TEXTS } = require("./framework/component/http");
const { chai } = require("./lib/test-helper");

module.exports = {
Expand Down Expand Up @@ -69,7 +69,7 @@ module.exports = {

expect(res.body).to.be.an("object");
expect(res.body).to.have.property("success", true);
expect(res.body).to.have.property("message", StatusTexts[StatusCodes.HTTP_OK]);
expect(res.body).to.have.property("message", STATUS_TEXTS[STATUS_CODES.HTTP_OK]);
done();
});
});
Expand Down

0 comments on commit 22ba496

Please sign in to comment.