npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rethink-plus

v0.4.0

Published

The RethinkDB interface you know with connection pooling and slightly easier usage.

Downloads

4

Readme

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.

import RethinkPlus from 'rethink-plus';

let db = new RethinkPlus({
  // 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
  .table('test')
  .get('07290fa5-1691-430c-a397-111575370881')
  .run(/*..optional options..*/)
  .then(/*...*/);

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.table('test');
let doc = await table.get('07290fa5-1691-430c-a397-111575370881');

If you wanted to use the callback interface like it was 2014, you could do that too:

db
  .table('test')
  .get('07290fa5-1691-430c-a397-111575370881')
  .run(/*..optional options..*/, function (err, result) {

  });

The library exports r in case you need it, for filters for example:

import RethinkPlus, {r} from 'rethink-plus';
// ...
let people = db.table('people');
let adults = await people.filter(r.row('age').gt(16));

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.toArray();

However, I've monkey-patched it:

let docs = await table.toArray();

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 = new RethinkPlus({/** connection options **/, autoToArray: true});
let docs = await db.table('test');

There are also the following helper methods:

  • first() - return the first element or throw an exception
  • first(defaultValue) - return the first element or a default value
  • single() - return the only document or throw an exception
  • singleOrDefault(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:

function filterPlugin(receiver) {
  if (receiver.filter) {
    var _filter = receiver.filter;

    receiver.filter = function () {
      // do something cool...

      // and then:
      return _filter.apply(receiver, arguments);
    };
  }
}

This can be used like so:

let db = new RethinkPlus({/** connection options **/, plugins: [filterPlugin]});

Or, if you want it to be used for all instances:

RethinkPlus.plugins.push(filterPlugin);

let db = new RethinkPlus({/** 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.