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

sequelize-stream

v1.0.2

Published

Create a stream of Sequelize create, update, and destroy events.

Downloads

521

Readme

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.