import * as knex from 'knex' ; export class Store { type { string } public static MODE_SINGLE = 'single' ; type { string } public static MODE_POOL = 'pool' ; type { string } public static MODE_REPLICATION = 'replication' ; type { string } public static ROLE_MASTER = 'master' ; type { string } public static ROLE_SLAVE = 'slave' ; type { string } private client : string ; type { string } private name : string ; type { string } private mode : string = Store . MODE_SINGLE ; type { { } } private connections : Object = { [ Store . ROLE_MASTER ] : [ ] , [ Store . ROLE_SLAVE ] : [ ] , } ; type { { } } private pointers : Object = { [ Store . ROLE_MASTER ] : 0 , [ Store . ROLE_SLAVE ] : 0 , } ; param { string } name param { { } } config public constructor ( name , config ? ) { this . name = name ; if ( config ) { this . register ( config ) ; } } returns { string } public getName ( ) { return this . name ; } param { PoolConfig | ReplicationConfig | SingleConfig } config returns { Store } public register ( config ) { if ( Array . isArray ( ( config as PoolConfig ) . connections ) ) { this . registerPool ( config as PoolConfig ) ; } else if ( typeof ( config as ReplicationConfig ) . connections === 'object' ) { this . registerReplication ( config as ReplicationConfig ) ; } else if ( typeof ( config as SingleConfig ) . connection === 'object' ) { this . registerConnection ( config as SingleConfig ) ; } return this ; } param { SingleConfig } config param { string } role returns { Store } public registerConnection ( config , role = null ) { if ( [ Store . ROLE_MASTER , Store . ROLE_SLAVE , null ] . indexOf ( role ) === - 1 ) { throw new Error ( `Trying to register using invalid role. Expected "${ Store . ROLE_MASTER }" or "${ Store . ROLE_SLAVE }".` ) ; } const connection = knex ( config ) ; if ( role === Store . ROLE_MASTER || role === null ) { this . connections [ Store . ROLE_MASTER ] . push ( connection ) ; } if ( role === Store . ROLE_SLAVE || role === null ) { this . connections [ Store . ROLE_SLAVE ] . push ( connection ) ; } this . client = config . client ; return this ; } returns { string } public getClient ( ) { return this . client ; } returns { Object } public getConnections ( ) { return this . connections ; } param { string } role returns { knex } public getConnection ( role = Store . ROLE_SLAVE ) { if ( this . mode === Store . MODE_SINGLE ) { return this . connections [ role ] [ 0 ] ; } const connection = this . connections [ role ] [ this . pointers [ role ] ++ ] ; if ( this . pointers [ role ] >= this . connections [ role ] . length ) { this . pointers [ role ] = 0 ; } return connection ; } param { PoolConfig } config returns public registerPool ( config ) { this . mode = Store . MODE_POOL ; config . connections . forEach ( connection => { this . registerConnection ( this . makeConnectionConfig ( config , connection ) ) ; } ) ; return this ; } param { ReplicationConfig } config returns public registerReplication ( config ) { this . mode = Store . MODE_REPLICATION ; Object . getOwnPropertyNames ( config . connections ) . forEach ( role => { config . connections [ role ] . forEach ( connection => { this . registerConnection ( this . makeConnectionConfig ( config , connection ) , role ) ; } ) ; } ) ; return this ; } param { { } } config param { { } } connection returns { SingleConfig } private makeConnectionConfig ( config , connection ) { const connectionConfig = { connection } ; Object . getOwnPropertyNames ( config ) . forEach ( key => { if ( key === 'connection' || key === 'connections' ) { return ; } connectionConfig [ key ] = config [ key ] ; } ) ; return connectionConfig ; } } export interface Connection { [ key ] : any ; } export interface SingleConfig { debug ? : boolean ; connection : Connection ; client ? : string ; } export interface PoolConfig { debug ? : boolean ; client : string ; connections : Array < Connection > ; [ key ] : any ; } export interface ReplicationConfig { debug ? : boolean ; client : string ; connections : { master ? : Array < Connection > ; slave ? : Array < Connection > ; } ; [ key ] : any ; }