Add query executed event; forward model events to global event bus
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
61731c4ebd
commit
c264d45927
@ -0,0 +1,67 @@
|
||||
import {Event} from '../../../event/Event'
|
||||
import {Inject, Injectable} from '../../../di'
|
||||
import {InvalidJSONStateError, JSONState} from '../../../util'
|
||||
import {Connection} from '../Connection'
|
||||
import {DatabaseService} from '../../DatabaseService'
|
||||
|
||||
/**
|
||||
* Event fired when a query is executed.
|
||||
*/
|
||||
@Injectable()
|
||||
export class QueryExecutedEvent extends Event {
|
||||
@Inject()
|
||||
protected database!: DatabaseService
|
||||
|
||||
/**
|
||||
* The name of the connection where the query was executed.
|
||||
* @protected
|
||||
*/
|
||||
public connectionName!: string
|
||||
|
||||
/**
|
||||
* The connection where the query was executed.
|
||||
*/
|
||||
public connection!: Connection
|
||||
|
||||
/**
|
||||
* The query that was executed.
|
||||
*/
|
||||
public query!: string
|
||||
|
||||
constructor(
|
||||
connectionName?: string,
|
||||
connection?: Connection,
|
||||
query?: string,
|
||||
) {
|
||||
super()
|
||||
if ( connectionName ) {
|
||||
this.connectionName = connectionName
|
||||
}
|
||||
|
||||
if ( connection ) {
|
||||
this.connection = connection
|
||||
}
|
||||
|
||||
if ( query ) {
|
||||
this.query = query
|
||||
}
|
||||
}
|
||||
|
||||
async dehydrate(): Promise<JSONState> {
|
||||
return {
|
||||
connectionName: this.connectionName,
|
||||
query: this.query,
|
||||
}
|
||||
}
|
||||
|
||||
rehydrate(state: JSONState): void {
|
||||
if ( !state.connectionName || !state.query ) {
|
||||
throw new InvalidJSONStateError('Missing connectionName or query from QueryExecutedEvent state.')
|
||||
}
|
||||
|
||||
this.query = String(state.query)
|
||||
this.connectionName = String(state.connectionName)
|
||||
this.connection = this.database.get(this.connectionName)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue