0

I am doing queries like below in existings apps and would like some advice/example on how to refactor this for KOA + co-mysql with generators avoiding or simplifying the chained functions with the sql calls. JS is fine, of course, I use a nodejs 0.11 and a CS version that supports generators. Still trying to get the hang of the 'new way'.

Thanks, K

client = mysql.createPool(mysql_options)
getSql = (sql, callback) ->
  client.query sql, (err, rows, fields) ->
    if err  #mysql error handling
      if err.code isnt 'PROTOCOL_CONNECTION_LOST'
        console.log 'Mysql Error <> Conn.Lost: ',err.stack
        process.on "uncaughtException", (err) ->
          console.log "getSql Mysql Error Caught Exception: ", err.stack
          return
      else
        setTimeout ->
          console.log 'Connection Lost; Re-connecting lost client (restart delay): ',err.stack
          client = mysql.createPool(mysql_options)
          return
        , 700
    client.release
    callback rows

    q10 = ->         # and in the routes...
      sql = "select count(*) as custCount from cust01 where sessionid = '" +  prSid + "' and custNum = " + "'" + sessionLoginUser + "'"
      getSql sql, (results) ->
        q20(results[0].custCount)

    q20 = (custCount) ->
     ....etc etc

    q10()

1 Answers1

0

With co-mysql their example script would be in CoffeeScript (with xixixao fork):

co = require('co')
mysql = require('co-mysql')

co( -->
  connection = mysql.createConnection options
  connection.connect()
  result = yield connection.query('SELECT 10086 + 10000 AS q')
  connection.end()
)()
tpisto
  • 61
  • 1