1.1.0 • Published 5 years ago

rethinkdb-on-change v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

rethinkdb-on-change

🔌 Run a function when RethinkDB detects a change

Build Status Greenkeeper badge

Don't changefeeds already cover this?

Yes, but we wanted a way of more easily looking for changes on multiple databases and tables with a whitelist/blacklist without writing the same thing multiple times.

Also, this comes with the feature of debouncing changes so you can run your logic only when needed.

✨ Features:

  • Simple: specifying a connection and a function to run is all that's needed
  • Tested

Usage

Simple example:

import dbOnChange from 'rethinkdb-on-change';

function changeFn(newData) {
  console.log('Received new data:', newData);
}

const options = {
  rethinkdb: { host: '127.0.0.1', port: 28015 },
  tables: [{ db: 'megacorp', table: 'users' }]
};

dbOnChange(changeFn, options);

Everything:

import dbOnChange from 'rethinkdb-on-change';

function changeFunction({
  db, /* The database where the change occurred */
  previousData, /* The old document data (if `previousData` is true).  Otherwise `undefined`. */
  nextData, /* The new document data (if `nextData` is true).  Otherwise `undefined`. */
  table /* The table where the change occurred */
}) {

}

await dbOnChange(changeFunction, {
  // Connection details for the RethinkDB instance to be copied
  // See `rethinkdbdash` (https://github.com/neumino/rethinkdbdash) for all possible options.
  rethinkdb: {
    // (optional) The host name. Defaults to '127.0.0.1'.
    host: '127.0.0.1',
    // (optional) The port to connect to the host to. Default is 28015.
    port: 28015,
    // (optional) protocol for connection (`http` or `https`).  Defaults to `http`.
    protocol: 'http'
  },

  // (optional) If the document's new data should be a property of the argument
  // sent to the change function.
  nextData: false,

  // (optional) If the document's previous data should be a property of the argument
  // sent to the change function.
  previousData: false,

  // Tables to duplicate and watch for changes
  tables: [
    {
      // Database containing table
      db: 'megacorp',
      // Table to copy
      table: 'users'
    }
  ]
});

Install

With Yarn or npm installed, run:

yarn add rethinkdb-on-change

# ...or, if using `npm`
npm install rethinkdb-on-change

See Also

License

MIT