Skip to content
ArteMerlow edited this page May 28, 2025 · 1 revision

Events

When creating a database query using the library, ModularORM generates events that you can handle directly in the methods of your Module. This can be done using the EventHandler annotation, which creates routing to your method. When an event is triggered, the QueryHandler interface will be passed to your method. Events (like any other user-defined functions supported by the library) can be asynchronous.

@Table()
@NamedTable('ur_table_name')
class TestTable extends Module {

    @Column(DefaultColumn.AUTOINCREMENT_ID)
    public someParam!: number;

    @EventHandler
    public logger(params: QueryHandler): void {
        // type is QueryType
        console.log(`Query type: ${params.type}`)
        // table is string
        console.log(`In table: ${params.table}`)
        // sql is string
        console.log(`SQL query: ${params.sql}`)
        // params is any[]
        console.log(`With params: ${params.params}`)
    }
    
}

const repository = new Repository(TestTable)
// Query type: QueryType.SELECT
// In table: ur_table_name
// SQL query: SELECT * FROM ur_table_name
// With params: []
await repository.find();

// Query type: QueryType.SELECT
// In table: ur_table_name
// SQL query: SELECT * FROM ur_table_name WHERE someParam = ?
// With params: [123]
await repository.find({ someParam: 123 })

Clone this wiki locally