RethinkPlus
This is a simple wrapper (< 200 lines of code) over the standard RethinkDB driver. It mostly just adds connection pooling.
Installation
$ npm install --save rethink-plus
Usage
First, import the library and instantiate the class.
; let db = // standard RethinkDB connection options, e.g. database: 'test';
This will create a connection pool for you. The instance has all the same methods as r
would in the normal driver docs. The only difference is that the run
method doesn't take a connection anymore: it takes one from the pool.
For example, to get a document with a specific ID:
db ;
If you don't have any options to pass, you can just skip the call to run
entirely, and it'll be invoked on then
. If you're using async
/await
like it's 2016, this leads to quite a natural-feeling usage:
let table = db;let doc = await table;
If you wanted to use the callback interface like it was 2014, you could do that too:
db ;
The library exports r
in case you need it, for filters for example:
;// ...let people = db;let adults = await people;
Cursors
For convenience, there's an extra toArray()
method. Without it, if you wanted to get an array of all the documents, you'd have to do something like this:
let cursor = await table;let docs = await cursor;
However, I've monkey-patched it:
let docs = await table;
If you have no need for cursors in your project, you can instruct the driver to always convert cursors to arrays for you:
let db = /** connection options **/ autoToArray: true;let docs = await db;
There are also the following helper methods:
first()
- return the first element or throw an exceptionfirst(defaultValue)
- return the first element or a default valuesingle()
- return the only document or throw an exceptionsingleOrDefault(defaultValue)
- return the only document or a default value
These are inspired by the IEnumerable<T>
methods in the CLR.
Plugins
You can pass an array of plugins to the constructor. A plugin should be a function which possibly modifies its parameter, which is an object. The plugin functions are called for every function call.
Example
Let's say you wanted to modify the filter
function. You could define a plugin like this:
{ if receiverfilter var _filter = receiverfilter; receiver { // do something cool... // and then: return _filter; }; }
This can be used like so:
let db = /** connection options **/ plugins: filterPlugin;
Or, if you want it to be used for all instances:
RethinkPlusplugins; let db = /** connection options **/;// db will use the filterPlugin
The experimental rethink-filter-parser is the only such external plugin at the moment, but the extra cursor functions described above are implemented with a default plugin.
Licence etc
This project is ISC licensed - do whatever you like. Comments, pull requests, and bug reports are all welcome! Thanks for your time.