Functions/GenXdev.Data.KeyValueStore/Get-KeyValueStoreNames.ps1

###############################################################################
<#
.SYNOPSIS
Retrieves a list of all available key-value store names from the database.
 
.DESCRIPTION
Queries the SQLite database to get unique store names based on the provided
synchronization key. The function handles database initialization if needed and
performs synchronization for non-local scopes. Returns store names that are
not marked as deleted and match the specified synchronization scope.
 
.PARAMETER SynchronizationKey
Key to identify synchronization scope. Use '%' for all stores, 'Local' for
local stores only. Synchronization occurs for non-local scopes. Supports SQL
LIKE pattern matching for flexible store filtering.
 
.PARAMETER DatabasePath
Directory path for preferences database files. When specified, overrides the
default database location for storing key-value store configurations.
 
.PARAMETER SessionOnly
Use alternative settings stored in session for Data preferences like Language,
Database paths, etc. When enabled, retrieves settings from session variables
instead of persistent storage.
 
.PARAMETER ClearSession
Clear the session setting (Global variable) before retrieving. Forces a fresh
retrieval of settings by clearing any cached session data before processing.
 
.PARAMETER SkipSession
Dont use alternative settings stored in session for Data preferences like
Language, Database paths, etc. Forces retrieval from persistent storage,
bypassing any session-cached settings.
 
.EXAMPLE
Get-KeyValueStoreNames -SynchronizationKey "Local" -DatabasePath "C:\Data"
 
.EXAMPLE
getstorenames "%"
#>

###############################################################################
function Get-KeyValueStoreNames {

    [CmdletBinding()]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")]
    [Alias("getstorenames")]

    param(
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            Position = 0,
            HelpMessage = "Key to identify synchronization scope, defaults to all"
        )]
        [string] $SynchronizationKey = "%",
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = "Database path for key-value store data files"
        )]
        [string] $DatabasePath,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = "Use alternative settings stored in session for Data preferences like Language, Database paths, etc"
        )]
        [switch] $SessionOnly,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = "Clear the session setting (Global variable) before retrieving"
        )]
        [switch] $ClearSession,
        ###############################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = "Dont use alternative settings stored in session for Data preferences like Language, Database paths, etc"
        )]
        [Alias("FromPreferences")]
        [switch] $SkipSession
        ###############################################################################
    )

    begin {

        # use provided database path or default to local app data
        if ([string]::IsNullOrWhiteSpace($DatabasePath)) {

            $databaseFilePath = GenXdev.FileSystem\Expand-Path `
                ("$($ENV:LOCALAPPDATA)\GenXdev.PowerShell\" +
                "KeyValueStores.sqllite.db") `
                -CreateDirectory
        }
        else {

            $databaseFilePath = GenXdev.FileSystem\Expand-Path $DatabasePath `
                -CreateDirectory
        }

        # output verbose message for database file path
        Microsoft.PowerShell.Utility\Write-Verbose (
            "Using database at: $databaseFilePath"
        )
    }

    process {

        # check if database file exists and create if needed
        if (-not (Microsoft.PowerShell.Management\Test-Path $databaseFilePath)) {

            # output verbose information about database initialization
            Microsoft.PowerShell.Utility\Write-Verbose (
                "Database not found, initializing..."
            )

            # copy identical parameter values for Initialize-KeyValueStores
            $initParams = GenXdev.Helpers\Copy-IdenticalParamValues `
                -BoundParameters $PSBoundParameters `
                -FunctionName "GenXdev.Data\Initialize-KeyValueStores" `
                -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable `
                    -Scope Local `
                    -ErrorAction SilentlyContinue)

            # initialize the key value stores database
            GenXdev.Data\Initialize-KeyValueStores @initParams
        }

        # perform synchronization for non-local stores
        if ($SynchronizationKey -ne "Local") {

            # output verbose information about synchronization
            Microsoft.PowerShell.Utility\Write-Verbose (
                "Synchronizing non-local store: $SynchronizationKey"
            )

            # copy identical parameter values for Sync-KeyValueStore
            $syncParams = GenXdev.Helpers\Copy-IdenticalParamValues `
                -BoundParameters $PSBoundParameters `
                -FunctionName "GenXdev.Data\Sync-KeyValueStore" `
                -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable `
                    -Scope Local `
                    -ErrorAction SilentlyContinue)

            # assign specific parameters for synchronization
            $syncParams.SynchronizationKey = $SynchronizationKey

            # synchronize the key value store with remote sources
            GenXdev.Data\Sync-KeyValueStore @syncParams
        }

        # construct sql query to get unique store names
        $sqlQuery = @"
SELECT DISTINCT storeName
FROM KeyValueStore
WHERE synchronizationKey LIKE @syncKey
AND deletedDate IS NULL;
"@


        # create parameters hashtable for the sql query
        $params = @{
            'syncKey' = $SynchronizationKey
        }

        # output verbose information about the query parameters
        Microsoft.PowerShell.Utility\Write-Verbose (
            "Querying stores with sync key: $SynchronizationKey"
        )

        # execute the sql query and extract store names from results
        GenXdev.Data\Invoke-SQLiteQuery `
            -DatabaseFilePath $databaseFilePath `
            -Queries $sqlQuery `
            -SqlParameters $params |
        Microsoft.PowerShell.Core\ForEach-Object storeName
    }

    end {
    }
}
###############################################################################