Skip to content

Class: ReactiveQueryCollection<T, PK, R, H, M>

Provides a reactive, observable response for a query that returns a collection of models.

This class is used for queries that return multiple results (e.g., .fetch() or .all()), and will automatically re-fetch the collection when relevant model events occur on the event bus.

Example

ts
// Use the query builder's .reactive() method to get a ReactiveQueryCollection
const usersQuery = db.model('users').where(...)
const reactive = await usersQuery.reactive()
const response = await reactive.fetch() // returns a ReactiveQueryCollection
response.on('next', (users) => { ... })
response.on('error', (err) => { ... })
response.on('complete', () => { ... })
// ...
response.unmount()

Extends

Type Parameters

Type ParameterDefault typeDescription
T extends PlainObject-The type of the model's data.
PK extends StringKeyOf<T>-The type of the primary key field.
R extends Record<string, RelationshipConfiguration>-The relationships configuration for the model.
H extends Required<ReactiveDatabaseOptions["hooks"]>--
M extends ReactiveModel<T, PK, R>ReactiveModel<T, PK, R>The model instance type (defaults to ReactiveModel).

Implements

Properties

PropertyTypeDescriptionInherited from
eEventListenerMapThe internal event listener map for the emitter.ReactiveQueryResponseInterface.e ReactiveQueryResponse.e
valueM[]Returns the latest query result value. Throws if the initial query has not yet completed. Throws If the query has not yet run.ReactiveQueryResponse.value

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.

Implementation of

ts
ReactiveQueryResponseInterface.off;

Inherited from

ReactiveQueryResponse.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

Implementation of

ts
ReactiveQueryResponseInterface.on;

Inherited from

ReactiveQueryResponse.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

Implementation of

ts
ReactiveQueryResponseInterface.once;

Inherited from

ReactiveQueryResponse.once


unmount()

ts
unmount(): void;

Returns

void

Inherited from

ReactiveQueryResponse.unmount