Saturday, 31 August 2013

node.js / sqlite - Serializing callbacks as well as the main functions

node.js / sqlite - Serializing callbacks as well as the main functions

Example:
Database table with 2 rows:
rowid name qty date
1 milk 3 8/25
2 milk 40 8/30
An inventory transaction to subtract 5 milk items needs to be executed
using the oldest milk first.
e.g.
Select rowid 1 wait for its callback and in it Delete rowid 1 as its qty
goes to 0 and is therefore spent
New deletion qty = 5 - 3 = 2. Since its not 0, another Select needs to be
made.
Select rowid 2
wait for its callback and in it Update rowid 2 to set qty = 38.
The serialize capability in the sqlite binding will only serialize the two
Selects. There's no way to know the second Select is even needed until the
first Select and its callback are finished. It's only then that its
realized that the first Select offered an insufficient qty.
How does one serialize the first Select and its callback using pure
JavaScript - no JQuery or other libraries.

No comments:

Post a Comment