DuckDB Terminal - v0.2.0
    Preparing search index...

    Class HistoryStore

    A persistent command history store backed by IndexedDB.

    Features:

    • Persists command history to IndexedDB
    • Falls back to in-memory storage if IndexedDB is unavailable
    • Supports navigation through history with previous/next
    • Automatically limits history size to prevent unbounded growth
    • Deduplicates consecutive identical commands
    const history = new HistoryStore();
    await history.init();

    // Add commands to history
    await history.add('SELECT * FROM users;');
    await history.add('SELECT count(*) FROM orders;');

    // Navigate history
    const prev = history.previous(''); // Returns 'SELECT count(*) FROM orders;'
    const prev2 = history.previous(''); // Returns 'SELECT * FROM users;'
    const next = history.next(); // Returns 'SELECT count(*) FROM orders;'
    const history = new HistoryStore();
    await history.init();

    // Current input is preserved when navigating
    const prev = history.previous('SELECT '); // Saves 'SELECT ' as current input
    // ... user navigates through history ...
    const current = history.next(); // Eventually returns 'SELECT '
    Index

    Constructors

    Properties

    currentInput: string = ''
    cursor: number = -1
    db: IDBDatabase | null = null
    history: string[] = []
    initialized: boolean = false

    Methods

    • Adds a command to the history.

      The command is stored both in memory and persisted to IndexedDB. Empty commands and consecutive duplicates are ignored.

      Parameters

      • command: string

        The command to add to history

      Returns Promise<void>

      A promise that resolves when the command has been persisted

      await history.add('SELECT * FROM users;');
      await history.add('SELECT * FROM users;'); // Ignored (duplicate)
      await history.add(''); // Ignored (empty)
    • Clears all history from both memory and IndexedDB.

      Returns Promise<void>

      A promise that resolves when the history has been cleared

    • Returns a copy of all commands in history.

      Returns string[]

      An array of all stored commands, oldest first

    • Initializes the history store by opening IndexedDB and loading existing history.

      This method must be called before using other methods. It loads any previously stored commands from IndexedDB. If IndexedDB is unavailable (e.g., in private browsing), the store falls back to in-memory storage.

      Returns Promise<void>

      A promise that resolves when initialization is complete

      const history = new HistoryStore();
      await history.init();
      // Now ready to use
    • Load history from IndexedDB

      Returns Promise<string[]>

    • Navigates to the next command in history.

      When reaching the end of history, returns the previously saved current input.

      Returns string | null

      The next command, the saved current input if at the end, or null if beyond

      // After navigating backward with previous()
      const cmd1 = history.next(); // Returns next newer command
      const cmd2 = history.next(); // Returns original input when at end
    • Open IndexedDB database

      Returns Promise<IDBDatabase>

    • Navigates to the previous command in history.

      When called from the end of history (most recent position), this method saves the current input so it can be restored when navigating forward.

      Parameters

      • currentInput: string

        The current input buffer content to preserve

      Returns string | null

      The previous command, or null if at the beginning of history

      // Assuming history has ['SELECT 1;', 'SELECT 2;']
      const cmd1 = history.previous('SELECT 3'); // Returns 'SELECT 2;'
      const cmd2 = history.previous(''); // Returns 'SELECT 1;'
      const cmd3 = history.previous(''); // Returns null (at beginning)
    • Resets the history cursor to the end and clears the saved current input.

      This should be called after a command is executed to prepare for new navigation.

      Returns void

    • Save command to IndexedDB

      Parameters

      • command: string

      Returns Promise<void>

    • Trim history in IndexedDB to max size.

      Uses a single transaction for all deletions for better performance.

      Returns Promise<void>