indexed_collection.svelte.ts

Declarations
#

4 declarations

view source

IndexDefinition
#

indexed_collection.svelte.ts view source

IndexDefinition<T, TResult, TQuery>

Generic index definition with full flexibility.

generics

T

constraint IndexedItem

TResult

default any

TQuery

default any

key

Unique identifier for this index.

type string

type

Optional index type for simpler creation.

extractor

Optional extractor function for single/multi indexes.

type (item: T) => any

compute

Function to compute the index value from scratch.

type (collection: IndexedCollection<T>) => TResult

query_schema

Schema for validating query parameters.

type z.ZodType<TQuery>

matches

Optional predicate to determine if an item is relevant to this index.

type (item: T) => boolean

onadd

Optional function to update the index when an item is added.

type (result: TResult, item: T, collection: IndexedCollection<T>) => TResult

onremove

Optional function to update the index when an item is removed.

type (result: TResult, item: T, collection: IndexedCollection<T>) => TResult

IndexedCollection
#

indexed_collection.svelte.ts view source

A helper class for managing collections with incremental updates, efficient querying, and automatic index maintenance.

generics

T

constraint IndexedItem

TKeySingle

constraint string
default string

TKeyMulti

constraint string
default string

TKeyDerived

constraint string
default string

TKeyDynamic

constraint string
default string

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>

key
type TKeySingle
returns SvelteMap<any, T>

multi_index

Get a multi-value index with proper typing.

type (key: TKeyMulti): SvelteMap<any, T[]>

key
type TKeyMulti
returns SvelteMap<any, T[]>

derived_index

Get a derived index with proper typing.

type (key: TKeyDerived): T[]

key
type TKeyDerived
returns T[]

dynamic_index

Get a dynamic (function) index with proper typing.

type <Q = any>(key: TKeyDynamic): (query: Q) => T

key
type TKeyDynamic
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
query
type TQuery
returns TResult

add

Add an item to the collection and update all indexes.

type (item: T): void

item
type T
returns void

add_many

Add multiple items to the collection at once with improved performance.

type (items: T[]): void

items
type T[]
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

Get an item by its id.

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[]

index_key
type TKeyMulti
value
type V
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[]

index_key
type TKeyMulti
value
type V
limit
type number
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[]

index_key
type TKeyMulti
value
type V
limit
type number
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
value
type V
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
value
type V
returns T | undefined

IndexedCollectionOptions
#

indexed_collection.svelte.ts view source

IndexedCollectionOptions<T, TKeySingle, TKeyMulti, TKeyDerived, TKeyDynamic>

generics

T

constraint IndexedItem

TKeySingle

constraint string
default string

TKeyMulti

constraint string
default string

TKeyDerived

constraint string
default string

TKeyDynamic

constraint string
default string

indexes

type Array<IndexDefinition<T>>

initial_items

type Array<T>

validate

type boolean

index_types

type { single?: Array<TKeySingle>; multi?: Array<TKeyMulti>; derived?: Array<TKeyDerived>; dynamic?: Array<TKeyDynamic>; }

IndexType
#

Depends on
#

Imported by
#