A helper class for managing collections with incremental updates,
efficient querying, and automatic index maintenance.
by_id
The main source of truth, the full collection keyed by Uuid.
type SvelteMap<Uuid, T>
readonly
values
type Array<T>
readonly
keys
type Array<Uuid>
readonly
size
Get the current count of items.
type number
readonly
indexes
Stores all index values in a reactive object.
type Record<string, any>
readonly
constructor
type new <T extends IndexedItem, TKeySingle extends string = string, TKeyMulti extends string = string, TKeyDerived extends string = string, TKeyDynamic extends string = string>(options?: IndexedCollectionOptions<T, TKeySingle, TKeyMulti, TKeyDerived, TKeyDynamic> | undefined): IndexedCollection<...>
options?
type IndexedCollectionOptions<T, TKeySingle, TKeyMulti, TKeyDerived, TKeyDynamic> | undefined
optional
toJSON
type (): readonly any[]
returns readonly any[]
get_index
Get a typed index value by key.
type <TResult = any>(key: TKeySingle | TKeyMulti | TKeyDerived | TKeyDynamic): TResult
key
type TKeySingle | TKeyMulti | TKeyDerived | TKeyDynamic
returns TResult
single_index
Get a single-value index with proper typing.
type (key: TKeySingle): SvelteMap<any, T>
returns SvelteMap<any, T>
multi_index
Get a multi-value index with proper typing.
type (key: TKeyMulti): SvelteMap<any, T[]>
returns SvelteMap<any, T[]>
derived_index
Get a derived index with proper typing.
type (key: TKeyDerived): T[]
returns T[]
dynamic_index
Get a dynamic (function) index with proper typing.
type <Q = any>(key: TKeyDynamic): (query: Q) => T
returns (query: Q) => T
query
Query an index with parameters.
This method is type-aware when the index has a query_schema that defines TQuery.
type <TResult = any, TQuery = any>(key: TKeySingle | TKeyMulti | TKeyDerived | TKeyDynamic, query: TQuery): TResult
key
type TKeySingle | TKeyMulti | TKeyDerived | TKeyDynamic
returns TResult
add
Add an item to the collection and update all indexes.
type (item: T): void
returns void
add_many
Add multiple items to the collection at once with improved performance.
type (items: T[]): void
returns void
remove
Remove an item by its id and update all indexes.
type (id: string & $brand<"Uuid">): boolean
id
type string & $brand<"Uuid">
returns boolean
remove_many
Remove multiple items efficiently.
type (ids: (string & $brand<"Uuid">)[]): number
ids
type (string & $brand<"Uuid">)[]
returns number
get
type (id: string & $brand<"Uuid">): T | undefined
id
type string & $brand<"Uuid">
returns T | undefined
has
Check if the collection has an item with the given id.
type (id: string & $brand<"Uuid">): boolean
id
type string & $brand<"Uuid">
returns boolean
clear
Clear all items and reset indexes.
type (): void
returns void
where
Get all items matching a multi-indexed property value.
Type-safe version that uses the TKeyMulti generic parameter.
type <V = any>(index_key: TKeyMulti, value: V): T[]
returns T[]
first
Get the first N items matching a multi-indexed property value.
Type-safe version that uses the TKeyMulti generic parameter.
type <V = any>(index_key: TKeyMulti, value: V, limit: number): T[]
returns T[]
latest
Get the latest N items matching a multi-indexed property value.
Type-safe version that uses the TKeyMulti generic parameter.
type <V = any>(index_key: TKeyMulti, value: V, limit: number): T[]
returns T[]
by
Get an item by a single-value index.
Returns the item or throws if no item is found.
Type-safe version that uses the TKeySingle generic parameter.
type <V = any>(index_key: TKeySingle, value: V): T
index_key
type TKeySingle
returns T
by_optional
Get an item by a single-value index, returning undefined if not found.
Type-safe version that uses the TKeySingle generic parameter.
type <V = any>(index_key: TKeySingle, value: V): T | undefined
index_key
type TKeySingle
returns T | undefined