How do I build a database connection string using KOTS Config templates?

I have a use case where based on the type of database selected, the connection string needs to be built differently. For example:

items:
        - name: db_type
          title: Database type
          type: select_one
          required: yes
          items:
            - name: postgresql
              title: POSTGRESQL
            - name: oracle
              title: ORACLE

Then based on the database selected, I need to build a connection string similar to this:

- name: db_connection_string
          type: text
          readonly: yes
          value: '{{ if repl{{ConfigOption "db_type" "postgresql"}}}} jdbc:postgresql:server:5432{{ else }} jdbc:oracle:thin@server:1521{{ end }}'

something similar to this… Is this possible today?

Best regards,
Ravi

@ravi_devarakonda here’s a better example than my original post that one of my colleagues worked up, showing 2 different ways of building the connection string. First, we could use 2 different Config elements, and select the right one with with a when statement, OR, in the second case, using one template to build up the string:

- name: db_type
  title: Database type
  type: select_one
  required: yes
  default: postgresql
  items:
    - name: postgresql
      title: POSTGRESQL
    - name: oracle
      title: ORACLE
- name: db_connection_string
  title: Database Connection String Example
  type: text
  value: jdbc:postgresql:server:5432
  when: repl{{ConfigOptionEquals "db_type" "postgresql"}}
- name: db_connection_string
  title: Database Connection String Example
  type: text
  when: repl{{ConfigOptionEquals "db_type" "oracle"}}
- name: db_connection_string2
  title: Database Connection String Example2
  type: text
  value: '{{repl if ConfigOptionEquals "db_type" "postgresql"}}jdbc:postgresql:server:5432{{repl else}}jdbc:oracle:thin@server:1521{{repl end }}'
1 Like

Hello @adamancini,

Thank you very much for this! I was looking for the:

in the second case, using one template to build up the string:

This is very helpful.
Much appreciated!

Best Regards,
Ravi

1 Like