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

Cannot override bulkDocs anymore in custom Plugin with version 8.0.0 #8594

Open
gensenio opened this issue Feb 2, 2023 · 2 comments · May be fixed by #8648
Open

Cannot override bulkDocs anymore in custom Plugin with version 8.0.0 #8594

gensenio opened this issue Feb 2, 2023 · 2 comments · May be fixed by #8648

Comments

@gensenio
Copy link

gensenio commented Feb 2, 2023

Issue

According to the documentation:

https://pouchdb.com/api.html#plugins

Example Plugin: Intercept Updates
A useful feature of plugins is to intercept updates before they are stored in PouchDB. In this way, a plugin might validate that the data is correct for the application, or even alter documents before they are committed to the database.

The best way to intercept all updates to a PouchDB database is to override the bulkDocs() method. All changes to PouchDB documents ultimately pass through the bulkDocs() method. For example, a call to put() will become a bulkDocs() call with a “batch” of one document.

Because PouchDB guarantees to plugin authors that all data changes ultimately happen via bulkDocs(), it is the ideal place for an application or plugin to intercept updates.

// Keep a reference to the "upstream" function.
var pouchBulkDocs = PouchDB.prototype.bulkDocs;
PouchDB.plugin({bulkDocs: validBulkDocs});

function validBulkDocs(body, options, callback) {
  if (typeof options == 'function') {
    callback = options
    options = {}
  }

  if (Array.isArray(body)) {
    var docs = body;
  } else {
    var docs = body.docs;
  }

  // All documents must have a .name field.
  for (var i = 0; i < docs.length; i++) {
    if (!docs[i].name) {
      var id = doc._id || '(no _id given)';
      return callback(new Error('Document is missing .name field: ' + id));
    }
  }

  // All documents check out. Pass them to PouchDB.
  return pouchBulkDocs.call(this, docs, options, callback);
}

The problems are that now with version 8.0.0:

var pouchBulkDocs = PouchDB.prototype.bulkDocs is undefined.

and

PouchDB.plugin({bulkDocs: validBulkDocs});

it's not overriding the bulkDocs with validBulkDocs anymore (the original one will be called).

Info

  • Environment: Browser
  • Platform: Chrome
  • Adapter: idb
  • Server: CouchDB
@gensenio
Copy link
Author

gensenio commented Feb 26, 2023

Any news on this issue? Custom plugins are not working anymore.

@funblaster22
Copy link

funblaster22 commented May 1, 2023

I had the same issue and independently came to the same conclusion until I found this Github issue 🤦. I suspect that it could do with the fact that the prototypes are being defined in plugin and then overridden by the constructor? However, the plugin method hasn't changed in 6 years, so it must be somewhere else...

Update: I am petty sure the issue arises from the fact that in the constructor, all the core methods are redefined, which would override the prototype defined by the plugin. I think this could be solved by either passing the plugins to the constructor, saving the plugins and applying them after the constructor, or saving and re-applying the prototype at the end of the constructor

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