import * as knex from 'knex' ; import { AuthDatabaseFacade } from '../facades/AuthDatabaseFacade' ; import { ShardDatabaseFacade } from '../facades/ShardDatabaseFacade' ; import { AuthDbConfig } from '../interfaces/AuthDbConfig' ; import { ShardDbConfig } from '../interfaces/ShardDbConfig' ; export class DatabaseFacade { private authDatabaseFacade : AuthDatabaseFacade ; private shardDatabaseFacade : ShardDatabaseFacade ; public constructor ( authDbConfig , allShardDatabaseConfig , shardDatabaseNamePrefix ) { this . authDatabaseFacade = new AuthDatabaseFacade ( authDbConfig ) ; this . shardDatabaseFacade = new ShardDatabaseFacade ( allShardDatabaseConfig , shardDatabaseNamePrefix ) ; } public checkAuthDatabaseTables ( ) { return this . authDatabaseFacade . checkAuthDatabaseTables ( ) ; } public checkAllShardDatabaseTables ( ) { return this . shardDatabaseFacade . checkAllShardDatabaseTables ( ) ; } public getDb ( authOrShardId ) { if ( authOrShardId === 'auth' ) { return this . authDatabaseFacade . getDb ( ) ; } else { return this . shardDatabaseFacade . getDb ( authOrShardId ) ; } } public getRandomShardId ( ) { return this . shardDatabaseFacade . getRandomShardId ( ) ; } public checkTableExists ( db , tableName ) { return new Promise ( async ( resolve , reject ) : Promise < void > => { try { const result = await db . schema . hasTable ( tableName ) ; resolve ( result ) ; } catch ( error ) { reject ( error ) ; } } ) ; } public async closeAllDatabases ( ) { await this . authDatabaseFacade . closeAuthDatabase ( ) ; await this . shardDatabaseFacade . closeAllShardDatabases ( ) ; } }