Skip to content

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

ts
// 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

Extended by

Type Parameters

Type ParameterDefault typeDescription
TanyThe type of the query result value.

Properties

PropertyTypeDescriptionInherited from
eEventListenerMapThe internal event listener map for the emitter.TypedEventEmitter.e
valueTReturns the latest query result value. Throws if the initial query has not yet completed. Throws If the query has not yet run.-

Methods

off()

ts
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

ParameterTypeDescription
eventstring | symbol | KThe 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 : neverOptional. 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

ts
TypedEventEmitter.off;

on()

ts
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

ParameterTypeDescription
eventstring | symbol | KThe 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 : neverThe listener function.
ctx?anyThe this context to apply to the listener when it is called

Returns

this

the current instance for chaining

Inherited from

ts
TypedEventEmitter.on;

once()

ts
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

ParameterTypeDescription
eventstring | symbol | KThe 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 : neverThe listener function.
ctx?anyThe this context to apply to the listener when it is called

Returns

this

the current instance for chaining

Inherited from

ts
TypedEventEmitter.once;

unmount()

ts
unmount(): void;

Returns

void