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 sequelizeconst sequelize = dialect: 'sqlite' const Cat = sequelizesequelize // install sequelizeStreamconst stream = // when the stream receives data, logstream // examplesCat// => 'create', {name: 'fluffy', id: 1}// => 'create', {name: 'spot', id: 2} Cat// => 'create', {name: 'sparky', id: 3}// => 'update', {name: 'sparky', spots: 2, id: 3}// => 'update', {name: 'sparky', spots: 1, id: 3}// => 'destroy', {name: 'sparky', spots: 1, id: 3} // NOTE: bulk destroy doesn't work due to Sequelize limitations.
Methods
(<Sequelize instance> sequelize)
=> <stream.Readable instance>
sequelizeStream 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
(<Object> {<String> event, <Sequelize instance> instance})
data The stream will emit objects with keys of event
and instance
.
const onData = event instance console // 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 testsnpm 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
.