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

Function queryViewInQueue

lib/index.js:8847–9007  ·  view source on GitHub ↗
(view, opts)

Source from the content-addressed store, hash-verified

8845 }
8846
8847 async function queryViewInQueue(view, opts) {
8848 let totalRows;
8849 const shouldReduce = view.reduceFun && opts.reduce !== false;
8850 const skip = opts.skip || 0;
8851 if (typeof opts.keys !== 'undefined' && !opts.keys.length) {
8852 // equivalent query
8853 opts.limit = 0;
8854 delete opts.keys;
8855 }
8856
8857 async function fetchFromView(viewOpts) {
8858 viewOpts.include_docs = true;
8859 const res$$1 = await view.db.allDocs(viewOpts);
8860 totalRows = res$$1.total_rows;
8861
8862 return res$$1.rows.map(function (result) {
8863 // implicit migration - in older versions of PouchDB,
8864 // we explicitly stored the doc as {id: ..., key: ..., value: ...}
8865 // this is tested in a migration test
8866 /* istanbul ignore next */
8867 if ('value' in result.doc && typeof result.doc.value === 'object' &&
8868 result.doc.value !== null) {
8869 const keys = Object.keys(result.doc.value).sort();
8870 // this detection method is not perfect, but it's unlikely the user
8871 // emitted a value which was an object with these 3 exact keys
8872 const expectedKeys = ['id', 'key', 'value'];
8873 if (!(keys < expectedKeys || keys > expectedKeys)) {
8874 return result.doc.value;
8875 }
8876 }
8877
8878 const parsedKeyAndDocId = parseIndexableString(result.doc._id);
8879 return {
8880 key: parsedKeyAndDocId[0],
8881 id: parsedKeyAndDocId[1],
8882 value: ('value' in result.doc ? result.doc.value : null)
8883 };
8884 });
8885 }
8886
8887 async function onMapResultsReady(rows) {
8888 let finalResults;
8889 if (shouldReduce) {
8890 finalResults = reduceView(view, rows, opts);
8891 } else if (typeof opts.keys === 'undefined') {
8892 finalResults = {
8893 total_rows: totalRows,
8894 offset: skip,
8895 rows
8896 };
8897 } else {
8898 // support limit, skip for keys query
8899 finalResults = {
8900 total_rows: totalRows,
8901 offset: skip,
8902 rows: sliceResults(rows,opts.limit,opts.skip)
8903 };
8904 }

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…