co-rethinkdb
Advanced tools
Comparing version 1.1.0 to 1.2.0
32
index.js
@@ -38,13 +38,23 @@ // When installing co-rethinkdb for an application that uses the same version | ||
// Since every rethinkdb method call, e.g., r.table('...') retuns a | ||
// function, `co` is going to call their `.call()` method making it | ||
// the perfect place to execute the `.run()` method instead. | ||
RDBOp.prototype.call = function(_, done) { | ||
var query = this | ||
co(function*() { | ||
var conn = yield r.getConnection | ||
var res = yield run.call(query, conn) | ||
yield r.releaseConnection(conn) | ||
done(null, res) | ||
})() | ||
// Adding a `.then()` method to the object returned by, .e.g., `r.table('...')` | ||
// makes `co` to recognize these objects as promises, making the `.then()` | ||
// method the perfect place to execute the `.run()` method instead. | ||
var Promise = require('bluebird') | ||
RDBOp.prototype.then = function() { | ||
if (!this._promise) { | ||
var query = this | ||
this._promise = new Promise(function(resolve, reject) { | ||
co(function*() { | ||
var conn = yield r.getConnection | ||
var res = yield run.call(query, conn) | ||
yield r.releaseConnection(conn) | ||
resolve(res) | ||
})(function(err) { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
this._promise.then.apply(this._promise, arguments) | ||
} | ||
@@ -51,0 +61,0 @@ |
{ | ||
"name": "co-rethinkdb", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "Markus Ast <npm.m@rkusa.st>", | ||
"description": "Generator based querying goodness for RethinkDB", | ||
"description": "Generator/Promise based querying goodness for RethinkDB", | ||
"keywords": [ | ||
@@ -19,2 +19,3 @@ "rethinkdb", | ||
"dependencies": { | ||
"bluebird": "^2.2.2", | ||
"co": "^3.0.5", | ||
@@ -21,0 +22,0 @@ "rethinkdb": "~1.13.0-0" |
@@ -42,2 +42,18 @@ # co-rethinkdb | ||
### In non-generator context | ||
With `co-rethinkdb` each RethinkDB query is directly a Promise. If called, it uses `r.getConnection` to retrieve a connection. | ||
```js | ||
var conn | ||
r.getConnection = function*() { | ||
return conn || (conn = yield r.connect({ db: db })) | ||
} | ||
r.table('users').get(1).then(function(user) { | ||
... | ||
}) | ||
``` | ||
### Koa Middleware | ||
@@ -44,0 +60,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7704
61
121
3
+ Addedbluebird@^2.2.2
+ Addedbluebird@2.11.0(transitive)
- Removedbluebird@3.7.2(transitive)