lazysql

repository·main·Indexed 26 days ago

https://github.com/jorgerojas26/lazysql

A terminal UI tool for interactive database exploration featuring non-blocking data loading and Foreign Key Jump for seamless navigation between related tables. It includes a CLI for managing database connections via a picker mode or direct connection URLs, and provides a Go-based application framework for managing connections, background tasks, and system clipboard interactions.

Tokens
1.9K
Snippets
4
Records
17
Agent score
88%

What's inside lazysql

  1. Understand Non-blocking Loading behavior

    main

    Lazysql uses a Non-blocking Loading pattern for database operations. When a query is in-flight, the UI remains interactive rather than showing a blocking modal or overlay.

    Key behaviors:

    • Loading Indicator: A thin text indicator appearing on the table's pagination bar (e.g., "Loading...") shows progress without stealing focus or blocking input.
    • Stale Data: Old table results remain visible during a reload to prevent a blank screen. Data is only cleared once the new results arrive successfully. (Exception: SQL editor queries clear immediately upon execution).
    • Load Cancellation: If a new load is triggered while an operation is already in-flight, the previous operation is cancelled via context cancellation before the new one begins.
  2. Use Foreign Key Jump for relation navigation

    main

    In the Lazysql terminal UI, use Foreign Key Jump to navigate from a selected cell in a source row to a referenced table view. This action automatically applies a prefilled filter to the target table based on the selected foreign key value.

    Note: Do not use terms like "FK drilldown", "relation follow", or "link jump" when referring to this feature; the canonical term is Foreign Key Jump.

  3. Manage database connections via Application

    main

    Use the Application methods to retrieve or persist database connections.

    • Connections(): Returns a slice of models.Connection currently in the configuration.
    • SaveConnections(connections []models.Connection): Persists a new slice of connections to the configuration.
  4. Set a custom callback for quit requests

    main
    Use SetOnQuitRequest(fn func()) to define a custom function that executes when the user triggers a quit (e.g., via Ctrl+C or OS signals). If a callback is provided, it will be executed instead of the default Stop() sequence.
  5. Access the application context and configuration

    main

    The Application struct provides methods to access internal state:

    • Context(): Returns the context.Context used by the application.
    • Config(): Returns the *models.AppConfig object.
    • Connections(): Returns the list of models.Connection objects.
  6. Register background tasks for graceful shutdown

    main

    To ensure the application does not exit before background tasks (like saving data or closing connections) are complete, use the Register() method.

    Register() increments an internal wait group and returns a function. You must call this returned function (which is sync.WaitGroup.Done) when your task is finished. The Stop() method will block until all registered tasks have called their respective completion functions.

  7. Use the Clipboard utility to read and write text

    main
    The lib.Clipboard type provides a wrapper around the system clipboard for reading and writing string data. You can instantiate it using NewClipboard() and then use the Write and Read methods to interact with the clipboard.
  8. List of Lazysql Commands by category

    main

    The following commands are available within Lazysql, categorized by their functional purpose:

    // Views
    SwitchToEditorView
    SwitchToConnectionsView
    HelpPopup
    ToggleQueryHistory
    ToggleTree
    
    // Movement: Basic
    MoveUp
    MoveDown
    MoveLeft
    MoveRight
    
    // Movement: Jumps
    GotoNext
    GotoPrev
    GotoStart
    GotoEnd
    GotoTop
    GotoBottom
    
    // Movement: Page
    PageNext
    PagePrev
    
    // Menu
    RecordsMenu
    ColumnsMenu
    ConstraintsMenu
    ForeignKeysMenu
    IndexesMenu
    
    // Tabs
    TabNext
    TabPrev
    TabFirst
    TabLast
    TabClose
    
    // Operations
    Refresh
    UnfocusEditor
    Copy
    Edit
    CommitEdit
    DiscardEdit
    Save
    Delete
    Search
    SearchGlobal
    Quit
    Execute
    OpenInExternalEditor
    OpenCellInExternalEditor
    AppendNewRow
    DuplicateRow
    SortAsc
    SortDesc
    UnfocusTreeFilter
    CommitTreeFilter
    NextFoundNode
    PreviousFoundNode
    TreeCollapseAll
    ExpandAll
    SetValue
    FocusSidebar
    UnfocusSidebar
    ToggleSidebar
    ShowRowJSONViewer
    ShowCellJSONViewer
    ToggleJSONViewerWrap
    
    // Connection
    NewConnection
    Connect
    TestConnection
    EditConnection
    DeleteConnection
    
    // Export
    ExportCSV
  9. Reference the Lazysql Command types

    main

    Lazysql uses a Command type (based on uint8) to represent all possible user actions and system operations. These commands are used to trigger view switches, movement, menu interactions, and database operations. When working with the internal command system, you can use these constants to dispatch actions.

    // Example of the Command type definition
    type Command uint8
    
    const (
    	Noop Command = iota
    	SwitchToEditorView
    	SwitchToConnectionsView
    	// ... other commands
    )
  10. Use the LazySQL CLI

    main

    LazySQL is a command-line tool for interacting with databases. You can start it in 'picker mode' to select a connection from your configuration, or provide a specific database URL to connect directly.

    Usage Patterns:

    • Picker Mode: Run lazysql without arguments to see a list of configured connections.
    • Direct Connection: Run lazysql [connection_url] to connect to a specific database immediately.

    Note: Only a single connection URL is allowed as a command-line argument.