Skip to content

๐Ÿ”Œ Run a function when RethinkDB detects a change

License

Notifications You must be signed in to change notification settings

gsandf/rethinkdb-on-change

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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

About

๐Ÿ”Œ Run a function when RethinkDB detects a change

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •