 
   db.get "SELECT value FROM metadata WHERE key = $key", {$key: key}, (err, row) ->
     if err then return cb "Couldn't get metadata for team #{team}: #{err}"
-    return cb null, row[0]
+    return cb null, row.value
 
 setMetaData = (team, key, value, cb) ->
   db = getDatabase team


  db.get "SELECT COUNT(*) FROM metadata", (err, row) ->
    if err
      db.run "CREATE TABLE metadata (key TEXT, value TEXT)", (err) ->
        if err then return cb "Couldn't create metadata table for team #{team}"
        db.run "INSERT INTO metadata VALUES($key, $value)", {$key: key, $value: value}, (err) ->
          if err then return cb "Couldn't insert metadata #{key} into table for team #{team}: #{err}"
          log.info "Set metadata key #{key} for team #{team} with new table"
          return cb null
    else
      db.run "UPDATE metadata SET value = $value WHERE key = $key", {$key: key, $value: value}, (err) ->
        if err then return cb "Couldn't update metadata for team #{team}: #{err}"
        log.info "Set metadata key #{key} for team #{team} with existing table"
        return cb null

app.listen process.env.PORT, ->
  log.info "Listening on #{process.env.PORT}..."

# FUNCTIONS ---------------------------------------------------------------------

getDatabase = (team) ->
  if not process.env.DATA_DIR? then throw new Error('DATA_DIR missing')
