# 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(options: { repository: Repository, filters: Filter[], includeDeleted?: boolean }): Readable // Sort events ascending by created_at deriveEventsAsc(eventsByIdStore: Readable>): Readable // Sort events descending by created_at deriveEventsDesc(eventsByIdStore: Readable>): Readable // Derive single event by ID or address makeDeriveEvent(options: { repository: Repository, includeDeleted?: boolean, onDerive?: (filters: Filter[], ...args: any[]) => void }): (idOrAddress: string, ...args: any[]) => 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, ...args: any[]) => 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, ...args: any[]) => 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 {deriveEvents, deriveItemsByKey, deriveItems} from "@welshman/store" import {readProfile, PROFILE} from "@welshman/util" const repository = new Repository() // Reactive store of text notes const notes = deriveEvents({ repository, filters: [{kinds: [1], limit: 100}] }) // 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`) }) ```