Created
March 9, 2018 09:16
-
-
Save jimmiehansson/1c33f25c1af494f4f3024452ece5026f to your computer and use it in GitHub Desktop.
Construct
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
'use strict'; | |
/** | |
* This notation was generated by templates. | |
* // ------------------------------------------------- | |
* @description Universal database factory | |
* @file index.js | |
* @author: Anton Myrberg <anton> 2018-02-26T11:10:55+01:00 | |
* // ------------------------------------------------- | |
* Make sure this file is part of its proper namespace | |
* and project before moving on. | |
* // ------------------------------------------------- | |
* Code-tags conventionally should be used (See below) : | |
* TODO - Something that someone need to do. | |
* DOING - Self remind for what you are doing. | |
* CONSIDER - Reminder to consider a change or addition. | |
* BUG - The below section of a code cause a bug. | |
* FIXME - The below section of code need to be fixed. | |
* HACK - The below section of code is a workaround. | |
* XXX - Any notation important enough to consider implementing. | |
* CLARIFY - Very incomprehensible section of code below. | |
* | |
* Created by Anton Myrberg <anton> 2018-02-26T11:10:55+01:00 | |
* Last modified by Anton Myrberg <anton> 2018-02-26T11:10:55+01:00 | |
* | |
*/ | |
/** | |
* @description | |
* | |
* DOING: Should add node_modules and files required | |
* to separate them from the rest. | |
*/ | |
const Sequelize = require('sequelize'); | |
const Mongoose = require('mongoose'); | |
/** | |
* @description | |
* | |
* DOING: Should add librarires and files required | |
* to separate them from the rest. | |
*/ | |
const language = require('./lib/locale/default'); | |
/** | |
* @description | |
* | |
* DOING: Should return boolean of | |
* whether instance is truthy. | |
* @returns {Boolean} | |
*/ | |
const isConstruct = (instance, construct) => (!!instance instanceof construct); | |
/** | |
* @description | |
* | |
* DOING: Should return the default | |
* handler to the callee. | |
* | |
* @returns {Object|Function|*} | |
*/ | |
const defaultHandler = Mongoose; | |
/** | |
* @function databaseFactory | |
* | |
* @description | |
* | |
* DOING: Should export the correct factory as | |
* to the callee as a factory object. | |
* | |
* @param {Object} config | |
* @param {Object} instance | |
* @returns {Object|Function|Promise|*} | |
*/ | |
module.exports = config => instance => { | |
/** | |
* @description | |
* | |
* DOING: Should return a factory global | |
* instance of the SuperClass prototype. | |
*/ | |
const Db = new SuperDatabase(config.construct); | |
const factory = { | |
/** | |
* @method | |
* | |
* @description | |
* | |
* DOING: Should return a construct | |
* of the sequelize module as a new instance. | |
*/ | |
'superdb-someMethod' : () => { | |
return Db.someMethod(); | |
}, | |
/** | |
* @method sequelize | |
* | |
* @description | |
* | |
* DOING: Should return a construct | |
* of the sequelize module as a new instance. | |
*/ | |
'sequelize' : () => { | |
const { database, user, pass, extraConf } = config.config; | |
const db = new Sequelize(database, user, pass, extraConf); | |
return db; | |
}, | |
/** | |
* @method mongoose | |
* | |
* @description | |
* | |
* DOING: Should return a construct | |
* of the mongoose module as a new Promise instance. | |
* | |
* @returns {Object|Promise.<void>} | |
*/ | |
'mongoose' : () => new Promise((resolve, reject) => { | |
/** | |
* @description | |
* | |
* CLARIFY: Should connect to the proper | |
* MongoDB instance from the passed configuration | |
* | |
* @returns {Object|Function|*} | |
*/ | |
const connectionObj = Mongoose.createConnection(`${config.config.host}${config.config.database}`); | |
connectionObj.on('open', () => { | |
var Model = connectionObj.model(config.config.model, config.config.schema); | |
var returnModel = new Model({}); | |
return resolve(returnModel); | |
}); | |
connectionObj.on('error', (e) => { | |
return reject(language.ERROR_CONNECTING, error); | |
}) | |
}) | |
}; | |
return factory[config.handler]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment