Skip to main content

Class: Query<IS, OS, CTX>

Defined in: repo/query.ts:221

Extends

Type Parameters

Type Parameter
IS extends Schema
OS extends IS
CTX extends ReadonlyJSONValue

Constructors

Constructor

new Query<IS, OS, CTX>(config): Query<IS, OS, CTX>

Defined in: repo/query.ts:276

Creates a new Query instance.

Parameters

ParameterTypeDescription
configQueryConfig<IS, OS, CTX>The query configuration object containing:

Returns

Query<IS, OS, CTX>

Overrides

Emitter.constructor

Properties

PropertyModifierTypeDefault valueDescriptionInherited fromDefined in
alwaysActivereadonlybooleanundefined-Emitter.alwaysActivebase/emitter.ts:10
contextreadonlyCTXundefinedOptional context data passed to predicate and sort functions-repo/query.ts:233
dbreadonlyGoatDB<Schema>undefinedDatabase instance this query operates on-repo/query.ts:230
idreadonlystringundefinedUnique identifier for this query-repo/query.ts:227
limitreadonlynumber0Maximum number of results to return, 0 means unlimited-repo/query.ts:239
scheme?readonlyISundefinedSchema type for items in this query-repo/query.ts:236

Accessors

age

Get Signature

get age(): number

Defined in: repo/query.ts:369

Gets the age (generation) of the query results, which is a monotonically increasing number. The age is incremented each time the query results are updated due to changes in the underlying data source. This allows tracking whether cached results are stale.

Returns

number

The current generation number of the query results, which only increases over time.


count

Get Signature

get count(): number

Defined in: repo/query.ts:345

Gets the number of items in the query results. This is more efficient than calling results().length since it directly returns the cached path count rather than constructing the results array first.

Returns

number

The number of items in the query results.


isActive

Get Signature

get isActive(): boolean

Defined in: base/emitter.ts:57

Returns

boolean

Inherited from

Emitter.isActive


loading

Get Signature

get loading(): boolean

Defined in: repo/query.ts:382

Gets the loading status of the query. Checking this status allows building more responsive interfaces by showing intermediate results while the full query loads. See () for waiting until loading completes.

Returns

boolean

true if the query is still loading results, false if loading is complete.


repo

Get Signature

get repo(): Repository

Defined in: repo/query.ts:331

Gets the repository associated with this query's data source.

Returns

Repository

The repository that this query operates on.


scanTimeMs

Get Signature

get scanTimeMs(): number

Defined in: repo/query.ts:356

Gets the total time spent scanning items during query execution.

Returns

number

The total scan time in milliseconds.

Methods

attach()

attach<C, E>(e, c): () => void

Defined in: base/emitter.ts:116

Type Parameters

Type Parameter
C extends Function
E extends QueryEvent | EmitterEvent

Parameters

ParameterType
eE
cC

Returns

() => void

Inherited from

Emitter.attach


close()

close(): void

Defined in: repo/query.ts:634

Closes this query and cleans up its resources. This:

  • Emits a 'Closed' event
  • Unregisters from query persistence to stop caching
  • Removes source change listeners
  • Marks the query as closed

Once closed, a query cannot be reopened. Create a new query instance instead.

Callers MUST call close() when a query is no longer needed to release its event listeners. A FinalizationRegistry safety net exists for the live-updates listener, but GC timing is non-deterministic.

Returns

void


detach()

detach<C, E>(e, c): void

Defined in: base/emitter.ts:149

Type Parameters

Type Parameter
C extends Function
E extends QueryEvent | EmitterEvent

Parameters

ParameterType
eE
cC

Returns

void

Inherited from

Emitter.detach


detachAll()

detachAll<E>(e?): void

Defined in: base/emitter.ts:188

Type Parameters

Type Parameter
E extends QueryEvent | EmitterEvent

Parameters

ParameterType
e?E

Returns

void

Inherited from

Emitter.detachAll


emit()

emit<E>(e, a0?, a1?): void

Defined in: base/emitter.ts:65

Emits an event to all registered listeners in registration (FIFO) order. This ordering is a stable contract -- chained query listeners depend on it.

Type Parameters

Type Parameter
E extends QueryEvent | EmitterEvent

Parameters

ParameterType
eE
a0?unknown
a1?unknown

Returns

void

Inherited from

Emitter.emit


entries()

entries(): Generator<Entry<OS>>

Defined in: repo/query.ts:493

Returns a generator that yields key-value pairs for all items in the query results. Each entry contains the item's path key and its corresponding value.

Returns

Generator<Entry<OS>>

A generator yielding [key, value] tuples for each item in the query


find()

find(fieldName, value): ManagedItem<OS, Schema>

Defined in: repo/query.ts:555

Finds the first item in the query results where the specified field matches the given value. If the field is the sort field, uses binary search for O(log n) lookup. Otherwise performs a linear scan.

Parameters

ParameterTypeDescription
fieldNamekeyof OS["fields"]The name of the field to search on
valueSchemaDataType<OS>[keyof OS["fields"]]The value to search for

Returns

ManagedItem<OS, Schema>

The first matching item, or undefined if no match found


has()

has(path): boolean

Defined in: repo/query.ts:393

Returns true if path matches this query's predicate, regardless of the limit setting. Use results() to get the bounded result set.

Parameters

ParameterTypeDescription
pathstringThe path to check.

Returns

boolean

true if the path is included in the query results, false otherwise.


loadingFinished()

loadingFinished(): Promise<true>

Defined in: repo/query.ts:537

Returns a promise that resolves to true when the query finishes loading its initial results. If loading is already finished, the promise resolves on the next event loop cycle.

Returns

Promise<true>

A promise that resolves to true when loading is finished


mute()

mute(): void

Defined in: base/emitter.ts:288

Returns

void

Inherited from

Emitter.mute


once()

once<C, E>(e, c): () => void

Defined in: base/emitter.ts:248

Type Parameters

Type Parameter
C extends Function
E extends QueryEvent | EmitterEvent

Parameters

ParameterType
eE
cC

Returns

() => void

Inherited from

Emitter.once


onLoadingFinished()

onLoadingFinished(handler): () => void

Defined in: repo/query.ts:521

Registers a callback to be invoked when the query finishes loading its initial results. If loading is already finished, the callback will be scheduled to run on the next event loop cycle.

Parameters

ParameterTypeDescription
handler() => voidThe callback function to execute when loading finishes

Returns

A cleanup function that removes the event listener when called

() => void


onResultsChanged()

onResultsChanged(handler): () => void

Defined in: repo/query.ts:506

Registers a callback to be invoked when the query results change due to updates in the underlying data source.

Parameters

ParameterTypeDescription
handler() => voidThe callback function to execute when results change

Returns

A cleanup function that removes the event listener when called

() => void


paths()

paths(): Iterable<string>

Defined in: repo/query.ts:406

Gets an iterable of all paths included in the query results.

Returns

Iterable<string>

An iterable containing all paths that match the query criteria.

Remarks

The returned paths may change during iteration if the underlying data source changes. Consider using () for a stable snapshot if consistency is needed during iteration.


results()

results(): readonly ManagedItem<OS, Schema>[]

Defined in: repo/query.ts:417

Gets the results of the query as an array of managed items. The returned items are mutable - any changes made to them will automatically trigger the query to update its results.

Returns

readonly ManagedItem<OS, Schema>[]

An array of managed items that match the query criteria.


resume()

protected resume(): Promise<void>

Defined in: repo/query.ts:585

Returns

Promise<void>

Overrides

Emitter.resume


suspend()

protected suspend(): void

Defined in: repo/query.ts:653

Returns

void

Overrides

Emitter.suspend


unmute()

unmute(): void

Defined in: base/emitter.ts:292

Returns

void

Inherited from

Emitter.unmute


valueForPath()

valueForPath(path): Item<OS>

Defined in: repo/query.ts:477

Gets the item value for a given path key. The value is retrieved from the repository's committed head or temporary records.

Parameters

ParameterTypeDescription
pathstringThe path key to look up in the repository

Returns

Item<OS>

The item value if found, undefined otherwise