bigpresh / Dancer-Plugin-Database

Dancer::Plugin::Database - easy database support for Dancer applications
http://search.cpan.org/dist/Dancer-Plugin-Database
37 stars 36 forks source link

Wishlist: default config params #25

Open knutov opened 12 years ago

knutov commented 12 years ago

part of the real config:

plugins:
  Database:
    handle_class: 'Dancer::Plugin::Database::KISS'
    driver:   'mysql'
    database: '...'
    host:     '...'
    port:      3306
    username: '...'
    password: '..'
    connection_check_threshold: 300
    dbi_params:
      RaiseError: 1
      AutoCommit: 1
      mysql_auto_reconnect: 1
      mysql_enable_utf8: 1
    on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
    log_queries: 1
    connections:
      one:
        handle_class: 'Dancer::Plugin::Database::KISS'
        driver:   'mysql'
        database: '...'
        host:     '...'
        port:      3306
        username: '...'
        password: '...'
        connection_check_threshold: 300
        dbi_params:
          RaiseError: 1
          AutoCommit: 1
          mysql_auto_reconnect: 1
          mysql_enable_utf8: 1
        on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
        log_queries: 1
      two:
        handle_class: 'Dancer::Plugin::Database::KISS'
        driver:   'mysql'
        database: '...'
        host:     '...'
        port:      3306
        username: '...'
        password: '...'
        connection_check_threshold: 300
        dbi_params:
          RaiseError: 1
          AutoCommit: 1
          mysql_auto_reconnect: 1
          mysql_enable_utf8: 1
        on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
        log_queries: 1
      three:
        handle_class: 'Dancer::Plugin::Database::KISS'
        driver:   'mysql'
        database: '...'
        host:     '...'
        port:      3306
        username: '...'
        password: '...'
        connection_check_threshold: 300
        dbi_params:
          RaiseError: 1
          AutoCommit: 1
          mysql_auto_reconnect: 1
          mysql_enable_utf8: 1
        on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
        log_queries: 1

Not good. Not readable. A lot of duplicated characters.

May be it's better to make default: section in config like this?

plugins:
  Database:
    database: '...'
    host:     '...'
    username: '...'
    password: '..'
    default:
      handle_class: 'Dancer::Plugin::Database::KISS'
      driver:   'mysql'
      port:      3306
      connection_check_threshold: 300
      dbi_params:
        RaiseError: 1
        AutoCommit: 1
        mysql_auto_reconnect: 1
        mysql_enable_utf8: 1
      on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
      log_queries: 1
    connections:
      one:
        database: '...'
        host:     '...'
        username: '...'
        password: '...'
      two:
        database: '...'
        host:     '...'
        username: '...'
        password: '...'
      three:
        database: '...'
        host:     '...'
        username: '...'
        password: '...'
bigpresh commented 11 years ago

Sorry for the huge delay responding to this.

I agree this would be quite useful.

I could add a default section, or have the code that fetches settings for a connection by name first take the settings from the "default" connection, then override them with the specific details specified in the named connection's settings - that might perhaps lead to even more readable config, e.g.:

plugins:
  Database:
    handle_class: 'Dancer::Plugin::Database::KISS'
    driver:   'mysql'
    database: '...'
    host:     '...'
    port:      3306
    username: '...'
    password: '..'
    connection_check_threshold: 300
    dbi_params:
      RaiseError: 1
      AutoCommit: 1
      mysql_auto_reconnect: 1
      mysql_enable_utf8: 1
    on_connect_do: ["SET NAMES 'utf8'", "SET CHARACTER SET 'utf8'" ]
    log_queries: 1
    connections:
      one:
        host: '...'
        username: '...'
        password: '...'

In the example above, database() would give you a handle using all the default settings; what you'd get from database('one') would be a handle using exactly the same settings, except with the hostname, username & password overriden by the ones in that named connection's config.

The problem with this approach, though, is that it's a potentially surprising change of behaviour which could bite people already using the module, who don't expect settings declared for the default connection to be inherited.

So, given more thought, your suggestion of a defaults key in the config may be safer.

ambs commented 10 years ago

Like both. Probably we can use the second and add some code to warn the users...