data-api-builder icon indicating copy to clipboard operation
data-api-builder copied to clipboard

🥕[Enhancement]: Enable caching through CLI

Open JerryNixon opened this issue 1 year ago • 1 comments

What happened?

There is no way to add caching through the CLI.

If we compare how we add telemetry:

dab add-telemetry
    --app-insights-enabled true
    --app-insights-conn-string "@env('app-insights-connection-string')"
{
  "runtime": {
    "telemetry": {
      "application-insights": {
        "enabled": true,
        "connection-string": "@env('app-insights-connection-string')"
      }
    }
  }
}

add-telemetry also works to update telemetry

There is no comparable for caching.

We need two commands, one for runtime the other for entity.

  1. dab add-caching --enabled true --ttl-seconds 10
  2. dab update Actor --caching.enabled true --caching.ttl-seconds 10 assuming my entity name is "Actor"

The only way to edit from the command line today is, for example, using PowerShell.

function Add-Caching-Runtime {
    param (
        [PSCustomObject]$jsonContent,
        [bool]$enabled,
        [int]$ttl
    )

    if (-not $jsonContent.runtime) {
        throw "Runtime section is missing in the configuration file."
    }

    if (-not $jsonContent.runtime.cache) {
        $jsonContent.runtime | Add-Member -MemberType NoteProperty -Name cache -Value @{ enabled = $enabled; "ttl-seconds" = $ttl }
    } else {
        $jsonContent.runtime.cache.enabled = $enabled
        $jsonContent.runtime.cache."ttl-seconds" = $ttl
    }

    return $jsonContent
}

function Add-Caching-Entity {
    param (
        [PSCustomObject]$jsonContent,
        [string]$entityName,
        [bool]$enabled,
        [int]$ttl
    )

    if (-not $jsonContent.entities.$entityName) {
        throw "Entity '$entityName' is missing in the configuration file."
    }

    if (-not $jsonContent.entities.$entityName.cache) {
        $jsonContent.entities.$entityName | Add-Member -MemberType NoteProperty -Name cache -Value @{ enabled = $enabled; "ttl-seconds" = $ttl }
    } else {
        $jsonContent.entities.$entityName.cache.enabled = $enabled
        $jsonContent.entities.$entityName.cache."ttl-seconds" = $ttl
    }

    return $jsonContent
}

# Example usage inline
$configFilePath = "dab-config.json"
$jsonContent = Get-Content $configFilePath -Raw | ConvertFrom-Json
$jsonContent = Add-Caching-Runtime -jsonContent $jsonContent -enabled $true -ttl 60
$jsonContent = Add-Caching-Entity -jsonContent $jsonContent -entityName "Actor" -enabled $true -ttl 60
$jsonContent | ConvertTo-Json -Depth 10 | Set-Content $configFilePath
Write-Output "Complete"

I recognize that this "works" but this is far from ideal.

Version

GA

What database are you using?

Azure SQL

What hosting model are you using?

Local (including CLI)

Which API approach are you accessing DAB through?

REST, GraphQL

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

JerryNixon avatar May 23 '24 20:05 JerryNixon

Similar to #1919

seantleonard avatar May 23 '24 23:05 seantleonard