diff --git a/my-server/controllers/products.js b/my-server/controllers/products.js index 15d2801..02763b6 100644 --- a/my-server/controllers/products.js +++ b/my-server/controllers/products.js @@ -1,6 +1,6 @@ const path = require("path"); const { ObjectId } = require("mongodb"); -const { initConfLog, initMongoDb, codes } = require("server-framework"); +const { initConfLog, getDbWithCollectionInit, codes } = require("server-framework"); const configPath = path.join(__dirname, "configs", "config.json"); @@ -10,8 +10,8 @@ const getId = async (req, res) => { const { logger, config } = initConfLog(configPath); const collName = config.get("dbConfig").collName; - const db = await initMongoDb(); - + const db = await getDbWithCollectionInit(); + const dbResponse = await db .collection(collName) .findOne({ _id: new ObjectId(req.params.id) }); @@ -38,7 +38,7 @@ const create = async (req, res) => { const { logger, config } = initConfLog(configPath); const collName = config.get("dbConfig").collName; - const db = await initMongoDb(); + const db = await getDbWithCollectionInit(); const data = req.body; logger.info("data", data); diff --git a/my-server/index.js b/my-server/index.js index 57282a9..d4a7a21 100644 --- a/my-server/index.js +++ b/my-server/index.js @@ -1,7 +1,7 @@ const fs = require("fs"); const path = require("path"); -const { server, initConfLog, codes } = require("server-framework"); +const { server, initConfLog, initMongoDb, codes } = require("server-framework"); const protectedMiddleware = require("./middlewares/protectedMiddleware"); const productRoutes = require("./routes/products"); @@ -11,11 +11,11 @@ const configPath = path.join(__dirname, "configs", "config.json"); const logsFolderPath = path.join(__dirname, "logs"); const start = async () => { - const { logger, config } = initConfLog(configPath, logsFolderPath); const appPort = config.get("appPort"); - + await initMongoDb(); const app = server(); + app.addContentTypeParser("application/json", defaultJsonParser); const router = app.Router; diff --git a/server-framework/ServerGlobal.js b/server-framework/ServerGlobal.js index 4b65ff5..7754782 100644 --- a/server-framework/ServerGlobal.js +++ b/server-framework/ServerGlobal.js @@ -7,6 +7,7 @@ const nconf = require("nconf"); class ServerGlobal { _config; _logger; + _dbConnection; static _instance; @@ -14,7 +15,6 @@ class ServerGlobal { console.log("path", configPathname); this._config = nconf.argv().env().file({ file: configPathname }); - const { errorFile, combinedFile, env } = this._config.get("logger"); if (!fs.existsSync(logsFolderPath)) { @@ -44,18 +44,24 @@ class ServerGlobal { } async initMongo() { - const { mongoUrl, dbName, collName, validatorObj } = - this._config.get("dbConfig"); + const { mongoUrl, dbName } = this._config.get("dbConfig"); const client = new MongoClient(mongoUrl); this._logger.info("initDb"); try { await client.connect(); this._logger.info("Connected successfully to server"); - const db = client.db(dbName); + this._dbConnection = client.db(dbName); + } catch (e) { + this._logger.error(`Error init database collection ${e}`); + } + } + + async getDbWithCollectionInit() { + const { collName, validatorObj } = this._config.get("dbConfig"); - const collection = await client - .db(dbName) + try { + const collection = await this._dbConnection .listCollections({}, { nameOnly: true }) .toArray(); @@ -63,13 +69,13 @@ class ServerGlobal { collection.filter((collectionItem) => collectionItem.name === collName) .length ) { - return db; + return this._dbConnection; } else { - await db.createCollection(collName, validatorObj); - return db; + await this._dbConnection.createCollection(collName, validatorObj); + return this._dbConnection; } } catch (e) { - this._logger.error(`Error init database collection ${e}`); + this._logger.error(`Error collection create ${e}`); } } @@ -89,7 +95,10 @@ class ServerGlobal { get logger() { return this._logger; } -} + get db() { + return this._dbConnection; + } +} module.exports = ServerGlobal; diff --git a/server-framework/server.js b/server-framework/server.js index 2a79d88..152e0ab 100644 --- a/server-framework/server.js +++ b/server-framework/server.js @@ -14,6 +14,7 @@ const ServerGlobal = require("./ServerGlobal"); const initConfLog = (path, logsPath) => ServerGlobal.getInstance(path, logsPath); const initMongoDb = () => ServerGlobal.getInstance().initMongo() +const getDbWithCollectionInit = () => ServerGlobal.getInstance().getDbWithCollectionInit() function createResponse(res) { res.send = (message) => res.end(message); @@ -159,4 +160,4 @@ function customServer() { }; } -module.exports = { server: customServer, initConfLog, initMongoDb, codes }; +module.exports = { server: customServer, initConfLog, initMongoDb, getDbWithCollectionInit, codes };