Skip to content

Latest commit

 

History

History
120 lines (88 loc) · 4.27 KB

README.md

File metadata and controls

120 lines (88 loc) · 4.27 KB

sequelize-stream NPM version Dependency Status Build Status

Create a stream of Sequelize create, update, and destroy events. This is useful if you want to build a real-time stream of events in your database.

Contents

Install

npm i -S sequelize-stream

Usage

// setup sequelize
import Sequelize from 'sequelize'
const sequelize = new Sequelize({ dialect: 'sqlite' })
const Cat = sequelize.define('cat', {
  name: Sequelize.STRING
  , spots: Sequelize.INTEGER
})
sequelize.sync({force: true})

// install sequelizeStream
import sequelizeStream from 'sequelize-stream'
const stream = sequelizeStream(sequelize)

// when the stream receives data, log
stream.on('data', ({instance, event}) => console.log(event, instance.toJSON()))


// examples
Cat.bulkCreate([{name: 'fluffy'}, {name: 'spot'}])
// => 'create', {name: 'fluffy', id: 1}
// => 'create', {name: 'spot', id: 2}

Cat.create({name: 'sparky'})
// => 'create', {name: 'sparky', id: 3}
.then((sparky) => {
  return sparky.update({spots: 2})
})
// => 'update', {name: 'sparky', spots: 2, id: 3}
.tap((sparky) => {
  return Cat.update({spots: 1}, {where: {name: 'sparky'}})
})
// => 'update', {name: 'sparky', spots: 1, id: 3}
.then((sparky) => {
  sparky.destroy()
})
// => 'destroy', {name: 'sparky', spots: 1, id: 3}

// NOTE: bulk destroy doesn't work due to Sequelize limitations.

Methods

sequelizeStream (<Sequelize instance> sequelize) => <stream.Readable instance>

Pass a sequelize instance (new Sequelize()), and get back a standard node.js object readable stream. Subscribe to get events on all models as they go through your sequelize instance.

Events

data (<Object> {<String> event, <Sequelize instance> instance})

The stream will emit objects with keys of event and instance.

const onData = ({event, instance} => {
  console.log(`${instance._modelOptions.name.singular} had a ${event} event`)
  // might log something like 'cat had a create event'
})

Caveats

Bulk Destroy

Model.destroy({where}) doesn't work because there's no good way to get affected instances and be sure they were actually deleted. Regular destroy does work though (instance.destroy()). You should use Model.destroy({where, individualHooks: true}) if you want stream events on the bulk method.

Bulk Update

Model.update({where}) works, but instance.previous() and instance.changed() will note return anything because there's no good way to get affected instances from Sequelize. Instead, you receive new instances which are ignorant of changes. Regular update does work though (instance.update()). You should use Model.update({where, individualHooks: true}) if you want stream events on the bulk method.

Tests

Tests are in AVA.

  • npm test will run the tests
  • npm run tdd will run the tests on every file change.

Developing

To publish, run npm run release -- [{patch,minor,major}]

NOTE: you might need to sudo ln -s /usr/local/bin/node /usr/bin/node to ensure node is in your path for the git hooks to work

Requirements

  • npm > 2.0.0 So that passing args to a npm script will work. npm i -g npm
  • git > 1.8.3 So that git push --follow-tags will work. brew install git

License

Artistic 2.0 © Joey Baker and contributors. A copy of the license can be found in the file LICENSE.