text: const _validateFails = option => {
    throw new Error(`Invalid database option: ${option}`)
  }
  
  const _validateDatabase = database => {
    if (!database) {
      _validateFails('no "options.credentials.database" provided')
    }
  }
  
  const _validatePool = options => {
    if (!isNaN(options.pool.min) && !isNaN(options.pool.max) && options.pool.min > options.pool.max) {
      _validateFails('"options.pool.min" is bigger than "options.pool.max"')
    }
  
    if (options.credentials.database === ':memory:') {
      if (options.pool.max !== 1) {
        _validateFails('"options.pool.max" in :memory: should equal 1')
      }
  
      if (options.pool.evictionRunIntervalMillis !== 0) {
        _validateFails('"options.pool.evictionRunIntervalMillis" in :memory: should equal 0')
      }
  
      if (options.pool.evictionRunIntervalMillisForPools !== 0) {
        _validateFails('"options.pool.evictionRunIntervalMillisForPools" in :memory: should equal 0')
      }
    }
  }
  
  const _validateJournalMode = mode => {
    // https://www.sqlite.org/draft/pragma.html#pragma_journal_mode
    const allowed = ['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF']
  
    if (mode && !allowed.includes(mode.toUpperCase())) {
      _validateFails(`journalMode "${mode}" is not one of "${allowed.join(', ')}"`)
    }
  }
  
  /**
   * Validates the connect and pool options and adds defaults if not given.
   * @param {Object} [options] - The db connection options.
   * @param {Object} [options.credentials] - The db connection options.
   * @param {Object} [options.credentials.database] - Alias for url.
   * @param {string} [options.credentials.journalMode] - SQLite PRAGMA journal mode as a string.
   * @param {Object} [options.pool] - The min and max pool options.
   * @param {number} [options.pool.min] - The minimum number of db connection clients.
   * @param {number} [options.pool.max] - The maximum number of db connection clients.
   * @param {number} [options.pool.evictionRunIntervalMillis] - How often to run eviction checks.
   * @param {number} [options.pool.evictionRunIntervalMillisForPools] - The time interval in ms until an idle pool is evicted.
   * @throws Error if one of the options is invalid or missing
   */
  const options = options => {
    options.credentials = options.credentials || {}
    options.credentials.database =
      options.credentials.database || options.database || options.host || options.url || options.credentials.url
  
    options.pool.min = options.pool.min || 1
    options.pool.max = options.pool.max || 1
    options.pool.evictionRunIntervalMillis = options.pool.evictionRunIntervalMillis || 0
    options.pool.evictionRunIntervalMillisForPools = options.pool.evictionRunIntervalMillisForPools || 0
  
    if (!('acquireTimeoutMillis' in options.pool)) {
      options.pool.acquireTimeoutMillis = 5000
    }
  
    _validateDatabase(options.credentials.database)
    _validateJournalMode(options.credentials.journalMode)
    _validatePool(options)
  }
  
  module.exports = options
  