MCPcopy Index your code
hub / github.com/apache/pouchdb / queryViewInQueue

Function queryViewInQueue

lib/index-browser.js:8809–8969  ·  view source on GitHub ↗
(view, opts)

Source from the content-addressed store, hash-verified

8807 }
8808
8809 async function queryViewInQueue(view, opts) {
8810 let totalRows;
8811 const shouldReduce = view.reduceFun && opts.reduce !== false;
8812 const skip = opts.skip || 0;
8813 if (typeof opts.keys !== 'undefined' && !opts.keys.length) {
8814 // equivalent query
8815 opts.limit = 0;
8816 delete opts.keys;
8817 }
8818
8819 async function fetchFromView(viewOpts) {
8820 viewOpts.include_docs = true;
8821 const res = await view.db.allDocs(viewOpts);
8822 totalRows = res.total_rows;
8823
8824 return res.rows.map(function (result) {
8825 // implicit migration - in older versions of PouchDB,
8826 // we explicitly stored the doc as {id: ..., key: ..., value: ...}
8827 // this is tested in a migration test
8828 /* istanbul ignore next */
8829 if ('value' in result.doc && typeof result.doc.value === 'object' &&
8830 result.doc.value !== null) {
8831 const keys = Object.keys(result.doc.value).sort();
8832 // this detection method is not perfect, but it's unlikely the user
8833 // emitted a value which was an object with these 3 exact keys
8834 const expectedKeys = ['id', 'key', 'value'];
8835 if (!(keys < expectedKeys || keys > expectedKeys)) {
8836 return result.doc.value;
8837 }
8838 }
8839
8840 const parsedKeyAndDocId = parseIndexableString(result.doc._id);
8841 return {
8842 key: parsedKeyAndDocId[0],
8843 id: parsedKeyAndDocId[1],
8844 value: ('value' in result.doc ? result.doc.value : null)
8845 };
8846 });
8847 }
8848
8849 async function onMapResultsReady(rows) {
8850 let finalResults;
8851 if (shouldReduce) {
8852 finalResults = reduceView(view, rows, opts);
8853 } else if (typeof opts.keys === 'undefined') {
8854 finalResults = {
8855 total_rows: totalRows,
8856 offset: skip,
8857 rows
8858 };
8859 } else {
8860 // support limit, skip for keys query
8861 finalResults = {
8862 total_rows: totalRows,
8863 offset: skip,
8864 rows: sliceResults(rows,opts.limit,opts.skip)
8865 };
8866 }

Callers 1

queryViewFunction · 0.70

Calls 3

toIndexableStringFunction · 0.70
fetchFromViewFunction · 0.70
onMapResultsReadyFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…