Class: abstract ReactiveQueryResponse<T>
Provides a reactive, observable response to a query, automatically re-fetching results when relevant model events occur on the provided UnifiedEventBus.
This class listens for model save and delete events, and only re-fetches query results if the event is relevant to the current query (e.g., the model/table matches and the row matches the query's where clause). Consumers can subscribe to value changes, errors, and completion events. The response can be unmounted to clean up all resources and event listeners.
Example
// Use the query builder's .reactive() method to get a reactive response
const usersQuery = db.model('users').where(...)
const reactive = await usersQuery.reactive()
const response = await reactive.fetch() // or .first(), .last(), etc.
response.on('next', (value) => { ... })
response.on('error', (err) => { ... })
response.on('complete', () => { ... })
// ...
response.unmount()Extends
TypedEventEmitter<ReactiveQueryResponseEventMap<T>>
Extended by
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
T | any | The type of the query result value. |
Properties
Methods
off()
off<K>(event: string | symbol | K, listener?:
| (...args: any[]) => void
| K extends keyof ReactiveQueryResponseEvents<T> ? Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>] extends unknown[] ? (...args: Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>]) => void : never : never): this;Unsubscribe from an event.
Type Parameters
| Type Parameter |
|---|
K |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | symbol | K | The event name. |
listener? | | (...args: any[]) => void | K extends keyof ReactiveQueryResponseEvents<T> ? Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>] extends unknown[] ? (...args: Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>]) => void : never : never | Optional. The listener function to remove. |
Returns
this
the current instance for chaining
Remarks
When no listener is provided, all listeners for the event will be removed, otherwise only the specified listener will be removed.
Inherited from
TypedEventEmitter.off;on()
on<K>(
event: string | symbol | K,
listener:
| (...args: any[]) => void
| K extends keyof ReactiveQueryResponseEvents<T> ? Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>] extends unknown[] ? (...args: Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>]) => void : never : never,
ctx?: any): this;Subscribe to an event with a typed listener.
Type Parameters
| Type Parameter |
|---|
K |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | symbol | K | The event name. |
listener | | (...args: any[]) => void | K extends keyof ReactiveQueryResponseEvents<T> ? Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>] extends unknown[] ? (...args: Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>]) => void : never : never | The listener function. |
ctx? | any | The this context to apply to the listener when it is called |
Returns
this
the current instance for chaining
Inherited from
TypedEventEmitter.on;once()
once<K>(
event: string | symbol | K,
listener:
| (...args: any[]) => void
| K extends keyof ReactiveQueryResponseEvents<T> ? Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>] extends unknown[] ? (...args: Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>]) => void : never : never,
ctx?: any): this;Subscribe to an event once with a typed listener.
Type Parameters
| Type Parameter |
|---|
K |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | symbol | K | The event name. |
listener | | (...args: any[]) => void | K extends keyof ReactiveQueryResponseEvents<T> ? Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>] extends unknown[] ? (...args: Record<keyof ReactiveQueryResponseEvents<T>, any[]>[K<K>]) => void : never : never | The listener function. |
ctx? | any | The this context to apply to the listener when it is called |
Returns
this
the current instance for chaining
Inherited from
TypedEventEmitter.once;unmount()
unmount(): void;Returns
void