Re: [nodejs] Waiting for two Mysql Functions?

104 views
Skip to first unread message

Martin Wawrusch

unread,
Dec 25, 2012, 3:49:13 PM12/25/12
to nod...@googlegroups.com
check out the async module in npm. It is exactly what you need.

On Tue, Dec 25, 2012 at 9:14 AM, Joman Sierra <joman...@gmail.com> wrote:
I have this scenario:

function one
{
Makes a query to a big mysql table

}

Function two
{
Makes another query to a big myslq table
}

res.render(showing results of the queries)


The problem is that when render is executed the mysql queries are still in progress.  What's the best way to be sure that the functions has finished before sending the render?

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en




klrumpf

unread,
Dec 26, 2012, 5:12:25 AM12/26/12
to nod...@googlegroups.com
an example in coffeescript, don't know if it is the most elegant solution.

You nest the two (three...) functions (q1...q2...qx) with your mysql statements having the
first call the second (and the second the third...) and at the end you call the first (q1).
 
That way they will get executed in the order determined by you (sync). You have a local
"myRender" inherit the render method and call that in your function. That should be all.

Alternatively use a module like async. (I try keep use of modules low to avoid extra overhead)

...
    myRender = @render   # make render method available for funcs below

    q1 = ->
      MysqlConnection = mysql.createConnection(mysql_options)
      MysqlConnection.connect()
      # is there a matching user (pktnr) with this password (dance)?
      sql = "select count(*) as pkt_count from pkt where pktnr = " + MysqlConnection.escape(suchpktnr) + " and dance = " + MysqlConnection.escape(suchpwd)
      MysqlConnection.query sql, (err, result) ->
        throw err if err
        MysqlConnection.end
        q2(result[0].pkt_count)

    #nested callback, write sessionid to mysql if pktnr+pwd exists in db
    q2 = (pkt_count) ->
      if pkt_count > 0
        pr_pktnr = suchpktnr
        #if correct then write sessionid to the customers record in mysql
        MysqlConnection = mysql.createConnection(mysql_options)
        MysqlConnection.connect()
        sql = "update pkt set sessionid = '" + pr_sid + "' where pktnr = " + MysqlConnection.escape(suchpktnr) + " and dance = " + MysqlConnection.escape(suchpwd)
        MysqlConnection.query sql, (err, result) ->
          throw err if err
          MysqlConnection.end
                myRender showstock1: {
            pktnr: pr_pktnr
            currentBrowser: currentBrowser
            }
      else 
        pr_pktnr = '00000'   
        myRender showstock1: {
          pktnr: pr_pktnr
          currentBrowser: currentBrowser
          }

    q1()

Hope this is useful, best wishes for the New Year
Karl-L. Rumpf
klr...@gmail.com
Málaga, Spain

Jake Verbaten

unread,
Dec 26, 2012, 11:54:10 AM12/26/12
to nod...@googlegroups.com
npm docs execute

Execute, executes multiple functions in parallel and aggregates the results in a hash
that has the same keys as your functions did.

Don't use async, it's a kitchensink of junk patterns.

```js
var execute = require("execute")

function handler(req, res) {
  execute({
    one: function (cb) { queryOne("query", cb) }
    , two: function (cb) { queryTwo("query" cb) }
  }, function (err, results) {
    if (err) { 
      return res.error(err)
    }

    var one = results.one
      , two = results.two
     
     res.render("tmpl", { one: one, two: two })
  })
}
```

Tim Smart

unread,
Dec 26, 2012, 4:18:49 PM12/26/12
to nod...@googlegroups.com
Hello.

If you spend enough time in node.js you will eventually run into the need of a
control flow library. Creating your own control flow library is almost the hello
world of node.js.

My library is called async-array.

var async = require('async-array').async
var mysql = require('mysql').createConnection()
mysql.connect()

function mysqlmap (query, i, next) {
mysql.query(query, next)
}

function mysqldone (err, results) {
// OMG There was an error somewhere. Handle it houston.
if (err) throw err

// Change results from an AsyncArray back to a normal array.
results.array()

// Results are mapped to the same place as the query.
results[0]
results[1]
}

async(
[ 'SELECT * FROM my_ultimate_table'
, 'SELECT bacon FROM the_fridge'
]
)
.map(mysqlmap)
.exec(mysqldone)

It tries to keep the basic semantics of a normal array, adding some asyncronous
magic. https://github.com/tim-smart/async-array

Tim.

Andrey

unread,
Dec 26, 2012, 11:31:00 PM12/26/12
to nod...@googlegroups.com
In addition to all previous posts:

Note that mysql protocol is synchronous (you can't send next query before receiving result from previous) and all mysql clients queue commands in some way. Unless you have two separate connections from pool you can safely do

mysql.query("one big query");
mysql.query("second big query").on('end', render);

- this way render is called only after both queries are finished.

Sapardee

unread,
Dec 26, 2012, 11:48:20 PM12/26/12
to nod...@googlegroups.com

Have you try node-pool module.?

--
Reply all
Reply to author
Forward
0 new messages