This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a follow on example from @bennadel blogpost https://www.bennadel.com/blog/3256-monkey-patching-the-mango-find-plugin-to-use-alldocs-in-pouchdb-6-2-0.htm | |
// This uses allDocs() with the keys selector to do a quick find for specific id's. | |
// It then uses pouchdb-selector-core to further filter the results. pouchdb-selector-core is the library that does the in-memory filtering | |
// for pouchdb-find and in the changes feed and with replication | |
// use pouchdb-browser here if you are running this in the browser | |
var PouchDB = require('pouchdb-node'); | |
var selectors = require('pouchdb-selector-core'); | |
const db = new PouchDB("doc-test"); | |
const selector = { | |
interests: { | |
$in: ["Cooking"] | |
} | |
}; | |
db.bulkDocs([ | |
{ | |
_id: "friend:kim", | |
name: "Kim", | |
age: 42, | |
interests: [ "Movies", "Computers", "Cooking" ], | |
collection_friend: true | |
}, | |
{ | |
_id: "friend:sarah", | |
name: "Sarah", | |
age: 35, | |
interests: [ "Museums", "Working Out", "Movies" ], | |
collection_friend: true | |
}, | |
{ | |
_id: "friend:joanna", | |
name: "Joanna", | |
age: 29, | |
interests: [ "Working Out", "Poetry", "Dancing" ], | |
collection_friend: true | |
}, | |
// Let's add some garbage documents as well in order to ensure that | |
// our subsequent queries don't try to pull back more data that we | |
// anticipate (ie, none of these documents should show up). | |
{ | |
_id: "a", | |
collection_garbage: true | |
}, | |
{ | |
_id: "z", | |
collection_garbage: true | |
} | |
]) | |
.then(resp => { | |
return db.allDocs({include_docs: true, keys:["friend:kim", "friend:sarah"]}) | |
}) | |
.then(resp => { | |
//This creates a new array of just the docs from allDocs and then filters it according to the selector | |
const docs = resp.rows.map(row => row.doc).filter(doc => selectors.matchesSelector(doc, selector)); | |
console.log('Output', docs); | |
//Have this here so we can just re-run the code without having to check if the initial docs are loaded | |
return db.destroy(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment