← Back to team overview

maria-discuss team mailing list archive

mariasql client question (zappajs, coffeescript, nodejs)

 

Hi,

I have switched from mysql to mariadb using it with the node-mysql
client and this has been runing in prod. for some time.

I now want to switch to the mariasql client...

inspect = require('util').inspect
mdbClient = require('mariasql')

...

To do the many queries and simplify the source I wanted to write
a principal function for queries that simply returns an object (or
array) with the query result and then I could just re-use the existing
functionality for the render. So I copied this from the examples

doSql = (sqlstring) ->
  c = new mdbClient()
  c.connect
    host: 'localhost'
    db: 'xyz'     # NOT database
    user: 'abc'
    password: 'defghi'

  c.on("connect", ->
    console.log "Client connected"
  ).on("error", (err) ->
    console.log "Client error: " + err
  ).on "close", (hadError) ->
    console.log "Client closed"

  c.query(sqlstring).on("result", (res) ->
    res.on("row", (row) ->
      console.log "Result row: " + inspect(row)
    ).on("error", (err) ->
      console.log "Result error: " + inspect(err)
    ).on "end", (info) ->
console.log "Result finished successfully, number of rows: " + info.numRows

  ).on "end", ->
    console.log "Done with all results"

  c.end()

....

and then

@get '/': (req,res) ->     # LANDING PAGE)
   result = doSql("select pakz from pkt where pktnr=10001")
   console.log "919, result = ",result
....
@view....

Console log in the doSql functions shows the results correctly but I have been playing around for a while and I can't get the function to return the result for
further processing to the @get routine. Seems like I have not understood
the scope issues here and probably a few more things, I am still green with this.

My questions

- what is the correct approach to do pass the query object from the function
  to the calling program if this is possible at all?

- how do I access the individual rows the way I do it with node-mysql like so
  row[1].myfield ...?

- should I create the instance of the mdbClient in the function or rather once at the very beginning of the program, my principal worry being stability even if the db
  disconnects here and there for some reason?

Thanks, hope this is the right list for this, Karl

--
Karl-L. Rumpf
klrumpf@xxxxxxxxx
Málaga, Spain



Follow ups