Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inserted, updated and removed events #156

Closed
wants to merge 1 commit into from

Conversation

duncanbrown
Copy link

I think it would be useful for the datastore to expose data change events. This is an implementation with events that get emitted whenever documents are inserted, updated or removed:

db.on('inserted', function(docs){
//docs is an array of documents that have just been inserted
});
db.on('updated', function(docs){
//docs is an array of documents that have just been updated
});
db.on('removed', function(docs){
//docs is an array of documents that have just been removed
});

For consistency docs is always an array, even when only a single document has been inserted/updated/removed.

@marshallswain
Copy link

Wow.  What a great idea.

On March 16, 2014 at 3:44:31 PM, duncanbrown (notifications@github.com) wrote:

I think it would be useful for the datastore to expose data change events. This is an implementation with events that get emitted whenever documents are inserted, updated or removed:

db.on('inserted', function(docs){
//docs is an array of documents that have just been inserted
});
db.on('updated', function(docs){
//docs is an array of documents that have just been updated
});
db.on('removed', function(docs){
//docs is an array of documents that have just been removed
});

For consistency docs is always an array, even when only a single document has been inserted/updated/removed.

You can merge this Pull Request by running

git pull https://github.com/duncanbrown/nedb master
Or view, comment on, or merge it at:

  #156

Commit Summary

inserted, updated and removed events
File Changes

M lib/datastore.js (24)
M test/db.test.js (203)
Patch Links:

https://github.com/louischatriot/nedb/pull/156.patch
https://github.com/louischatriot/nedb/pull/156.diff

Reply to this email directly or view it on GitHub.

@brumm
Copy link

brumm commented Apr 25, 2014

👍 This implements exactly what I need for a node-webkit app. Please consider this PR!

@timoweiss
Copy link

👍

@brumm
Copy link

brumm commented Aug 27, 2014

@louischatriot any comments on this?

@louischatriot
Copy link
Owner

It's still on my radar, some more high priority stuff needed to be done first. Definitely a very good idea.

@brumm
Copy link

brumm commented Aug 28, 2014

Cool! Thanks for your time.

@lvivier
Copy link

lvivier commented Nov 26, 2014

👍 this would be really nice to have.

@psi-4ward
Copy link

+1

@inluxc
Copy link

inluxc commented Jan 21, 2015

+1, that would be nice for my Node Webkit app

@JamesMGreene
Copy link
Contributor

👍 This is actually rather important functionality for me as of now, though I can temporarily workaround it by overriding Datastore prototype methods. 😕

if (err) { return callback(err); }
if (newDocs.length > 0)
self.emit('updated', newDocs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though not critical (IMHO, anyway), it would be lovely if the 'updated' event would actually include 2 arguments: the newDocs array AND an oldDocs array, e.g.

var newDocs = _.pluck(modifications, 'newDoc');
var oldDocs = _.pluck(modifications, 'oldDoc');
// ...
self.emit('updated', newDocs, oldDocs);
// ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, this point does make me wonder if perhaps there should be an individual event fired for each document rather than per document array, e.g.

modifications.forEach(function(mod) {
  self.emit('updated', mod.newDoc, mod.oldDoc);
});

The same array enumerating would apply for the 'inserted' and 'removed' events as well.

@JamesMGreene
Copy link
Contributor

FWIW, here's a Gist of a derivative class I use ("EventedDatastore.js") in order to add eventing: https://gist.github.com/JamesMGreene/0e0b2506c7bd2a2557f7

@roccomuso
Copy link

+1 for this feature.

@louischatriot
Copy link
Owner

Took me a long time to answer, sorry. I actually think this is not a good idea to implement this event emitter at database level. Most if not all usecases will be very application specific and there is no good common way to expose an API around this, even though the pubsub pattern is of course very often useful.

@marshallswain
Copy link

For those who need this kind of functionality, I created an NeDB adapter for FeathersJS. You can find it here: feathers-nedb. The current implementation works on the server, but since we've just made Feathers isomorphic, we'll be making a browser version.

Thanks @louischatriot for sharing your great code.

@roccomuso
Copy link

Is worth mentioning in the official DOC the possibility to add eventing using @JamesMGreene gist? It's an important functionality.

@JamesMGreene
Copy link
Contributor

I've updated my gist accordingly since the Datastore class in nedb@^1.7.0 now inherits from the EventEmitter class.

@JamesMGreene
Copy link
Contributor

FWIW, I've also implemented these in my active fork of NeDB: NestDB

JamesMGreene added a commit to JamesMGreene/nestdb that referenced this pull request Aug 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants