pouchdb / upsert

PouchDB plugin for upsert() and putIfNotExists() functions
Apache License 2.0
149 stars 25 forks source link

Use of `this` referencing global scope #65

Open shellscape opened 4 years ago

shellscape commented 4 years ago

Use of and assumption of this being the database reference is resulting in inconsistent behavior. Specifically this line https://github.com/pouchdb/upsert/blob/5020aa5aa264ecc6cefac0a060819504fe88f8b7/index.js#L55

If declaring a database namespace as such:

  const db = {
    config: new PouchDB('config', { adapter })
  }

And running the upset method as such:

    await db.users.upsert(`${data.userName}-cursor`, (doc) => {
      (doc as CursorDoc).cursor = data.nextCursor;
      return doc;
    });

Will result in the db variable being assigned to the global scope and .get being undefined.