Comparing version 0.6.17 to 0.6.18
@@ -12,2 +12,3 @@ | ||
var savePoint = require('./lib/savepoint'); | ||
var TxMonad = require('./lib/tx-monad'); | ||
@@ -147,2 +148,13 @@ var queryMethods = [ | ||
extQuery.execTx = function() { | ||
var q = self.toQuery(); | ||
return new TxMonad(q.text, q.values); | ||
} | ||
extQuery.allTx = extQuery.execTx; | ||
extQuery.getTx = function() { | ||
return self.execTx().chain(function(rows) { | ||
return rows && rows.length ? rows[0] : null; | ||
}); | ||
} | ||
queryMethods.forEach(function (key) { | ||
@@ -149,0 +161,0 @@ extQuery[key] = function extFn() { |
@@ -10,4 +10,5 @@ var Promise = require('bluebird'); | ||
this.parent = null; | ||
this.hasQuery = true; | ||
var self = this; | ||
this.tx.promise.then(function(tx) { | ||
this.tx.promise.done(function(tx) { | ||
if (self.query != null) { | ||
@@ -20,13 +21,44 @@ var result = tx.query(self.query, self.params); | ||
function isTxMonad(m) { | ||
return m != null && | ||
typeof(m.propagateToRoot) === 'function' && | ||
typeof(m.hasQuery) === 'boolean'; | ||
} | ||
function fromParent(parent) { | ||
var txm = new TxMonad(); | ||
txm.parent = parent; | ||
txm.hasQuery = false; | ||
txm.tx.resolve(parent.tx.promise); | ||
return txm; | ||
} | ||
var currentError; | ||
function tryExec(f, arg) { | ||
try { | ||
return f(arg); | ||
} catch(e) { | ||
return (currentError = e); | ||
} | ||
} | ||
TxMonad.prototype.follow = function(other) { | ||
if (isTxMonad(other)) { | ||
other.propagateToRoot(this.tx.promise); | ||
this.result.resolve(other.result.promise); | ||
} | ||
else | ||
this.result.resolve(other); | ||
} | ||
TxMonad.prototype.chain = function(f) { | ||
var self = this; | ||
var newTxm = new TxMonad(); | ||
newTxm.parent = this; | ||
newTxm.hasQuery = false; | ||
this.result.promise.then(function(val) { | ||
var res = f(val); | ||
res.propagateToRoot(self.tx.promise.value()); | ||
newTxm.tx.resolve(self.tx.promise); | ||
newTxm.result.resolve(res.result.promise); | ||
return res.result.promise; | ||
var newTxm = fromParent(this); | ||
this.result.promise.done(function(val) { | ||
var res = tryExec(f, val); | ||
if (res === currentError) | ||
newTxm.result.reject(currentError); | ||
else | ||
newTxm.follow(res); | ||
}, function(e) { | ||
newTxm.result.reject(e); | ||
}); | ||
@@ -36,2 +68,16 @@ return newTxm; | ||
TxMonad.prototype.catch = function(f) { | ||
var newTxm = fromParent(this); | ||
this.result.promise.done(function(val) { | ||
newTxm.result.resolve(val); | ||
}, function(err) { | ||
var res = tryExec(f, err); | ||
if (res === currentError) | ||
newTxm.result.reject(currentError); | ||
else | ||
newTxm.follow(res); | ||
}); | ||
return newTxm; | ||
} | ||
TxMonad.prototype.propagateToRoot = function(tx) { | ||
@@ -38,0 +84,0 @@ if (this.parent) |
{ | ||
"name": "anydb-sql", | ||
"version": "0.6.17", | ||
"version": "0.6.18", | ||
"description": "Minimal ORM for mysql, postgresql and sqlite with complete arbitrary SQL query support (based on brianc's query builder sql)", | ||
@@ -38,3 +38,3 @@ "main": "anydb-sql.js", | ||
"any-db-transaction": "^2.2.1", | ||
"bluebird": "^2.3.5", | ||
"bluebird": "^2.3.6", | ||
"sql": "^0.40.0" | ||
@@ -41,0 +41,0 @@ }, |
@@ -35,9 +35,9 @@ var TxMonad = require('../lib/tx-monad'); | ||
assert.equal(result[0], "Test 4") | ||
return query("Test 5", []); | ||
return "Test 5" | ||
}).chain(function(result) { | ||
assert.equal(result, "Test 5", "should pass-through"); | ||
}); | ||
return chainOfQueries.runWithin(makeTx("LolTx")).then(function(res) { | ||
console.log(res); | ||
}); | ||
return chainOfQueries.runWithin(makeTx("LolTx")); | ||
}); | ||
@@ -68,7 +68,11 @@ | ||
t.ok(e, "should catch error"); | ||
console.log(e); | ||
return "ok"; | ||
return query("handle first error"); | ||
}).chain(function() { | ||
throw new Error("Also handles thrown errors"); | ||
}).catch(function(e) { | ||
t.ok(e, "should catch thrown error"); | ||
return query("ok"); | ||
}).runWithin(makeTx("Tx3")).then(function(res) { | ||
console.log(res[0]); | ||
t.ok(res[0] === 'ok', "We are done"); | ||
}); | ||
}); |
41473
1039
Updatedbluebird@^2.3.6