import logger from "../utilities/Logger" ; import { Connection , getConnection } from "typeorm" ; import { ConnectionNotFoundError } from "typeorm/error/ConnectionNotFoundError" ; export const getCurrentConnection = ( ) : Connection | null => { try { return getConnection ( ) ; } catch ( err ) { if ( err instanceof ConnectionNotFoundError ) { return null ; } else { throw err ; } } } ; const toExecuteQueries = [ ] ; export const query = ( query , ... args ) : Promise < DatabaseResultSet > => { return new Promise ( ( resolve , reject ) => { const currentConnection = getCurrentConnection ( ) ; if ( ( query . match ( /\?/g ) || [ ] ) . length !== args . length ) { reject ( "Amount of arguments mismatch" ) ; } else if ( currentConnection == null ) { toExecuteQueries . push ( { query , resolve , args , } ) ; } else if ( toExecuteQueries . length > 0 ) { for ( const queryobj of toExecuteQueries ) { currentConnection . query ( queryobj . query , ... queryobj . args ) . then ( ( data ) => { queryobj . resolve ( data ) ; } ) ; } } else { currentConnection . query ( query , args ) . then ( ( value ) => { resolve ( new DatabaseResultSet ( value , value . insertId ) ) ; } ) . catch ( ( reason ) => { logger . error ( reason ) ; } ) ; } } ) ; } ; export class DatabaseResultSet implements Iterable < DatabaseResultRow > { constructor ( private rows , private insertId ) { } private static getAnyFromDB ( name , obj ) { if ( Array . isArray ( obj ) ) { obj = obj [ 0 ] ; } const currObj = obj [ name . trim ( ) ] ; try { return currObj ; } catch ( err ) { return null ; } } public static getStringFromDB ( name , obj ) { return this . getAnyFromDB ( name , obj ) as string ; } public static getBlobFromDB ( name , obj ) { return this . getAnyFromDB ( name , obj ) as Buffer ; } public static getNumberFromDB ( name , obj ) { return this . getAnyFromDB ( name , obj ) as number ; } public getInsertId ( ) { return this . insertId ; } public getLength ( ) { return this . rows . length ; } public getStringFromDB ( name , index = 0 ) { return DatabaseResultSet . getStringFromDB ( name , this . rows [ index ] ) ; } public getBlobFromDB ( name , index = 0 ) { return DatabaseResultSet . getBlobFromDB ( name , this . rows [ index ] ) ; } public getNumberFromDB ( name , index = 0 ) { return DatabaseResultSet . getNumberFromDB ( name , this . rows [ index ] ) ; } public getRows ( ) { return this . rows ; } public convertRowsToResultObjects ( ) { const convertedRows = [ ] ; for ( const row of this . rows ) { convertedRows . push ( new DatabaseResultRow ( row ) ) ; } return convertedRows ; } [ Symbol . iterator ] ( ) { const rows = this . rows ; let index = 0 ; return { next ( ) { return { done : ! ( index < rows . length ) , value : new DatabaseResultRow ( rows [ index ++ ] ) , } ; } , } ; } } export class DatabaseResultRow { constructor ( private row ) { } public getStringFromDB ( name ) { return DatabaseResultSet . getStringFromDB ( name , this . row ) ; } public getNumberFromDB ( name ) { return DatabaseResultSet . getNumberFromDB ( name , this . row ) ; } }