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
Conversation
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){ 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 Commit Summary inserted, updated and removed events M lib/datastore.js (24) https://github.com/louischatriot/nedb/pull/156.patch |
👍 This implements exactly what I need for a node-webkit app. Please consider this PR! |
👍 |
@louischatriot any comments on this? |
It's still on my radar, some more high priority stuff needed to be done first. Definitely a very good idea. |
Cool! Thanks for your time. |
👍 this would be really nice to have. |
+1 |
+1, that would be nice for my Node Webkit app |
👍 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); |
There was a problem hiding this comment.
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);
// ...
There was a problem hiding this comment.
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.
FWIW, here's a Gist of a derivative class I use ("EventedDatastore.js") in order to add eventing: https://gist.github.com/JamesMGreene/0e0b2506c7bd2a2557f7 |
+1 for this feature. |
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. |
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. |
Is worth mentioning in the official DOC the possibility to add eventing using @JamesMGreene gist? It's an important functionality. |
I've updated my gist accordingly since the |
FWIW, I've also implemented these in my active fork of NeDB: NestDB |
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:
For consistency
docs
is always an array, even when only a single document has been inserted/updated/removed.