[Maria-discuss] 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@gmail.com Málaga, Spain
klrumpf <klrumpf@gmail.com> writes:
I now want to switch to the mariasql client...
Cool, that is an interesting project ...
res.on("row", (row) -> console.log "Result row: " + inspect(row)
@get '/': (req,res) -> # LANDING PAGE) result = doSql("select pakz from pkt where pktnr=10001") console.log "919, result = ",result
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
I assume you mean that - You get output linis with "Result row: ..." - You get no line "919, result = ", or it is empty. Unfortunately, there may not be many on this list who are familiar with node.js. I have only general knowledge about event-driven programming. This leads me to think that you need to pass a handler to your doSql() function and invoke this handler in your res.on() handler. But I frankly admit I do not know if something else could work with node.js.
Thanks, hope this is the right list for this, Karl
You are welcome to try :-). Maybe someone with knowledge of node.js will answer. But it might not be the best list, your problem looks more related to general node.js than to MariaDB. Hope this helps (a little), - Kristian.
Karl-L. Rumpf klrumpf@gmail.com Málaga, Spain Thanks, I got a conclusive answer from the author Brian White On 05/02/13 14:28, Kristian Nielsen wrote:
klrumpf<klrumpf@gmail.com> writes:
I now want to switch to the mariasql client... Cool, that is an interesting project ...
res.on("row", (row) -> console.log "Result row: " + inspect(row) @get '/': (req,res) -> # LANDING PAGE) result = doSql("select pakz from pkt where pktnr=10001") console.log "919, result = ",result 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
I assume you mean that
- You get output linis with "Result row: ..."
- You get no line "919, result = ", or it is empty.
Unfortunately, there may not be many on this list who are familiar with node.js.
I have only general knowledge about event-driven programming. This leads me to think that you need to pass a handler to your doSql() function and invoke this handler in your res.on() handler. But I frankly admit I do not know if something else could work with node.js.
Thanks, hope this is the right list for this, Karl You are welcome to try :-). Maybe someone with knowledge of node.js will answer. But it might not be the best list, your problem looks more related to general node.js than to MariaDB.
Hope this helps (a little),
- Kristian.
participants (2)
-
klrumpf
-
Kristian Nielsen