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

Need hoodie.database.on('change', change) and db.on('change', change) methods #24

Open
inator opened this issue Nov 20, 2013 · 6 comments

Comments

@inator
Copy link

inator commented Nov 20, 2013

My dream additions allow for _db_updates (couchDB-wide) and _changes (for individual dbs) to be exposed through the plugin api.

Could pass an object with type (updated, created, deleted) and other pertenent details much like: npm follow-db-updates:

https://npmjs.org/package/follow-db-updates

@nintra
Copy link

nintra commented Jun 7, 2015

i suggest the following additions to the api. as far as i know we can't track 'created' events for documents without huge additional effort because it might be that a document got created, and updated client-side before sync happened. we can say that the document got deleted because of a _deleted flag.

if there are no objections ( @inator @janl ) i would start implementing it.
it would affect:

  • plugins-manager
  • plugins-api
  • plugin-users (minor changes)

i have a little trouble executing the tests on my windows laptop. i could use some help for the final testing. (i need a linux setup soon :) )

/**
 * event emitter that automatically listens on all user databases
 * type   document-type
 * action 'update', 'remove', '*'
 */
hoodie.userDatabases.on('type:action', function(dbName, userId, doc) { /* ... */ });


/**
 * get user database, sugar for hoodie.database('user/'+userId)
 */
hoodie.userDatabases.get(userId);


/**
 * scoped events
 */
hoodie.userDatabases.get(userId).on('type:action', function(doc) { /* ... */ });


/**
 * listen for events on specific non-user databases
 * returns an db event emitter
 * if it is a user database, return special userDb event emitter
 */
var reportEvents = hoodie.database('reports').events();
reportEvents.on('report:update', function(doc) { /* ... */ });
reportEvents.on('report:remove', function(doc) { /* ... */ });

@nintra
Copy link

nintra commented Jun 8, 2015

i would also add a replicate method. this way you can work with data across all user accounts. (read-only) write operations have to be executed in a specific user database.

/**
 * start replicating user documents of `type` into a single database `dbName`
 * - if no dbName is provided: dbName = type + 's';
 * - create database
 * - listen for changes and copy/remove docs in one direction only
 */
hoodie.userDatabases.replicate(type, [dbName], function(error, dbName) { /* ... */ });

@gr2m
Copy link
Contributor

gr2m commented Jun 8, 2015

instead of

var reportEvents = hoodie.database('reports').events();
reportEvents.on('report:update', function(doc) { /* ... */ });
reportEvents.on('report:remove', function(doc) { /* ... */ });

I'd do

var reportsDb = hoodie.database('reports')
reportsDb.on('report:update', function(doc) { /* ... */ });
reportsDb.on('report:remove', function(doc) { /* ... */ });

@nintra
Copy link

nintra commented Jun 8, 2015

@gr2m
yes, you are right. we could do that and just wait for the first .on() to setup listening to that database. I just want to avoid to always listen to all databases. im afraid it might be some kind of overkill?

@gr2m
Copy link
Contributor

gr2m commented Jun 8, 2015

yeah you can do something like this:
https://github.com/hoodiehq/pouchdb-hoodie-api/blob/events/on.js#L23-L50

start listening on the first .on call

@janl
Copy link
Contributor

janl commented Jun 16, 2015

@nintra let’s make the replicate() a separate discussion/PR.

The rest looks good, happy to help testing on *nix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants