# Repository Reactive Svelte stores for querying events from a Repository with automatic updates. ## Event Stores ```typescript // Derive map of events by ID deriveEventsById(options: { repository: Repository filters: Filter[] includeDeleted?: boolean }): Readable> // Convert events by ID to array deriveEvents(eventsByIdStore: Readable>): Readable // Sort events ascending by created_at deriveEventsAsc(eventsStore: Readable): Readable // Sort events descending by created_at deriveEventsDesc(eventsStore: Readable): Readable // Derive single event by ID or address deriveEvent(repository: Repository, idOrAddress: string): Readable // Track if event is deleted deriveIsDeleted(repository: Repository, event: TrustedEvent): Readable ``` ## Indexed Collections ```typescript // Create indexed map of items from repository events deriveItemsByKey(options: { repository: Repository filters: Filter[] eventToItem: (event: TrustedEvent) => MaybeAsync> getKey: (item: T) => string includeDeleted?: boolean }): Readable> // Convert itemsByKey map to array deriveItems(itemsByKeyStore: Readable>): Readable // Create function to derive single item by key makeDeriveItem( itemsByKeyStore: Readable>, onDerive?: (key: string, ...args: any[]) => void ): (key: string, ...args: any[]) => Readable // Create cached loader with staleness checking and exponential backoff makeLoadItem( loadItem: (key: string, ...args: any[]) => Promise, getItem: (key: string) => T | undefined, options?: {getFetched?, setFetched?, timeout?} ): (key: string, ...args: any[]) => Promise // Create loader that always fetches fresh data makeForceLoadItem( loadItem: (key: string, ...args: any[]) => Promise, getItem: (key: string) => T | undefined ): (key: string, ...args: any[]) => Promise // Optimized getter that switches to subscription when hot getter(store: Readable, options?: {threshold?: number}): () => T ``` ## Example ```typescript import {Repository} from "@welshman/net" import {deriveEventsById, deriveEvents, deriveItemsByKey, deriveItems} from "@welshman/store" import {readProfile, PROFILE} from "@welshman/util" const repository = new Repository() // Reactive store of text notes const noteEventsById = deriveEventsById({ repository, filters: [{kinds: [1], limit: 100}] }) const notes = deriveEvents(noteEventsById) // Reactive store of profiles indexed by pubkey const profilesByPubkey = deriveItemsByKey({ repository, filters: [{kinds: [PROFILE]}], eventToItem: event => readProfile(event), getKey: profile => profile.event.pubkey }) const profiles = deriveItems(profilesByPubkey) // Subscribe to updates notes.subscribe($notes => { console.log(`Found ${$notes.length} text notes`) }) profiles.subscribe($profiles => { console.log(`Found ${$profiles.length} profiles`) }) ```