Last active
August 22, 2019 07:36
-
-
Save mohsen89z/71955af5bf6d55cb21843c5970a23696 to your computer and use it in GitHub Desktop.
Database Connection Manager
This file contains hidden or 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
import { MongoDBConfig } from './Config' | |
import * as mongoose from 'mongoose' | |
import * as bluebird from 'bluebird' | |
import { logger } from './Logger' | |
export enum ConnectionStatus { | |
DISCONNECTED = 'DISCONNECTED', | |
CONNECTED = 'CONNECTED', | |
ERROR = 'ERROR', | |
} | |
export interface ConnectionManager { | |
connection: any | |
connectionStatus: string | |
label: string | |
connect: () => void | |
disconnect: () => void | |
} | |
export class MongoDBConnectionManager implements ConnectionManager { | |
label = 'MongoDB' | |
connectionStatus = ConnectionStatus.DISCONNECTED | |
connection: mongoose.Connection | |
private connected: () => void | |
private error: (err: any) => void | |
private disconnected: (err: any) => void | |
constructor( | |
connected = () => {}, | |
error = err => {}, | |
disconnected = err => {}, | |
) { | |
this.connected = connected | |
this.error = error | |
this.disconnected = disconnected | |
} | |
connect() { | |
;(mongoose as any).Promise = bluebird | |
mongoose.set('debug', true) | |
mongoose.connect(MongoDBConfig.url, MongoDBConfig.options) | |
mongoose.connection.on('connected', () => { | |
logger.info('MongoDB Connected') | |
this.connectionStatus = ConnectionStatus.CONNECTED | |
this.connected() | |
}) | |
mongoose.connection.on('error', err => { | |
logger.error('MongoDB Error') | |
this.connectionStatus = ConnectionStatus.ERROR | |
this.error(err) | |
}) | |
mongoose.connection.on('disconnected', err => { | |
logger.info('MongoDB Disconnected') | |
this.connectionStatus = ConnectionStatus.DISCONNECTED | |
this.disconnected(err) | |
}) | |
this.connection = mongoose.connection | |
} | |
disconnect() { | |
this.connection.close() | |
} | |
} | |
export const ApplicationDependencies: { | |
[index: string]: ConnectionManager | |
} = { | |
MongoDB: new MongoDBConnectionManager(), | |
} |
This file contains hidden or 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
import { format, Logger, transports, createLogger } from 'winston' | |
import { ServiceConfig } from './Config' | |
const { combine, timestamp, label, printf } = format | |
const serviceLogFormat = printf(info => { | |
return `${info.timestamp} [${info.label}] ${info.level}: ${ | |
typeof info.message === 'object' | |
? JSON.stringify(info.message) | |
: info.message | |
}` | |
}) | |
export const logger: Logger = createLogger({ | |
transports: [ | |
// Console Logger Settings | |
new transports.Console({ | |
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug', | |
handleExceptions: true, | |
format: combine( | |
format.colorize(), | |
label({ label: ServiceConfig.label }), | |
timestamp(), | |
serviceLogFormat, | |
), | |
}), | |
], | |
exitOnError: false, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment