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 { 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) } }