import CSVDatabase from './CSVDatabase' ; import CaseInformation from '../CaseInformation' ; import FacilityInformation from '../FacilityInformation' ; import MongoDBDatabase from './MongoDBDatabase' ; /**
 * Handles Database
 */ class DatabaseAdapter { private static instance : DatabaseAdapter ; private db ! : MongoDBDatabase ; /**
     * return {DatabaseAdapter} instance of the database adapter
     */ async init ( ) { if ( ! DatabaseAdapter . instance ) { DatabaseAdapter . instance = this ; console . log ( 'Connecting to database' ) ; let msg ; console . log ( `Database Type: ${ process . env . DATABASE_TYPE }` ) ; if ( String ( process . env . DATABASE_TYPE ) . toLowerCase ( ) === 'nosql' ) { msg = await this . connect ( new MongoDBDatabase ( ) ) ; } else if ( String ( process . env . DATABASE_TYPE ) . toLowerCase ( ) === 'mysql' ) { throw Error ( 'MYSQL Database is deprecated' ) ; } else { throw Error ( 'Please specify "DATABASE_TYPE" in environment variables' ) ; } console . log ( msg ) ; } return DatabaseAdapter . instance ; } param { * } database return { Promise < String > } connect ( database ) { this . db = database ; return this . db . connect ( ) ; } param queries param { String } queries . month param { String } queries . day param { int } queries . page param { int } queries . limit param { int } queries . maxLimit param { Object | undefined } queries . filters return { Promise < * > } get ( queries ) { return this . db . get ( queries ) as Promise < any > ; } param { String } dbName param { Object | null } objFilters return { Promise } count ( dbName , objFilters = null ) { return this . db . count ( dbName , objFilters ) ; } param { String } field param { String | Number } value return { Promise } filter ( field , value ) { return this . db . filter ( field , value ) ; } param { String } region return { Promise < Object > } return { Promise < Object > } return { Promise < Object > } return { Promise < Object > } getSummary ( region = null ) { return this . db . getSummary ( region ) ; } param { Object } queries return { Promise } getTimeline ( queries ) { return this . db . getTimeline ( queries ) ; } return { Promise } getTopRegions ( ) { return this . db . getTopRegions ( ) ; } param { Object } queries return { Promise } getFacilities ( queries ) { return this . db . getFacilities ( queries ) ; } param { Object } queries return { Promise } getFacilitiesSummary ( queries ) { return this . db . getFacilitiesSummary ( queries ) ; } param { String } field param { String } dataset return { Promise } getListOf ( field , dataset ) { return this . db . getListOf ( field , dataset ) ; } return { Promise < String > } getLatestFolderID ( ) { return this . db . getLatestFolderID ( ) ; } return { Promise < String > } getLastUpdateDate ( ) { return this . db . getLastUpdateDate ( ) ; } param { String } tableName return { Promise < void > } truncate ( tableName ) { return this . db . truncate ( tableName ) ; } param { String } tableName param { Object } fieldValueObj return { Promise < String > } insert ( tableName , fieldValueObj ) { return this . db . insert ( tableName , fieldValueObj ) ; } /**
     * End Database connection
     */ endConnection ( ) { this . db . endConnection ( ) ; } return { Promise < boolean > } async updateDatabaseFromCSV ( ) { let result = true ; let lowMemory = true ; let res = await this . db . updateDatabaseFromCSV ( ( await new CSVDatabase ( ) . init ( new CaseInformation ( ) ) ) , 'case_informations' ) ; if ( ! res ) result = false ; while ( lowMemory ) { const memUsed = process . memoryUsage ( ) . heapTotal / 1000000 ; if ( memUsed < 150 ) lowMemory = false ; global . gc ( ) ; } res = await this . db . updateDatabaseFromCSV ( ( await new CSVDatabase ( ) . init ( new FacilityInformation ( ) ) ) , 'facility_informations' ) ; if ( ! res ) result = false ; global . gc ( ) ; lowMemory = true ; while ( lowMemory ) { const memUsed = process . memoryUsage ( ) . heapTotal / 1000000 ; if ( memUsed < 150 ) lowMemory = false ; global . gc ( ) ; } return result ; } } export default DatabaseAdapter ;