MCPcopy
hub / github.com/apache/pouchdb / localViewCleanup

Function localViewCleanup

lib/index.es.js:9012–9074  ·  view source on GitHub ↗
(db)

Source from the content-addressed store, hash-verified

9010 }
9011
9012 async function localViewCleanup(db) {
9013 try {
9014 const metaDoc = await db.get('_local/' + localDocName);
9015 const docsToViews = new Map();
9016
9017 for (const fullViewName of Object.keys(metaDoc.views)) {
9018 const parts = parseViewName(fullViewName);
9019 const designDocName = '_design/' + parts[0];
9020 const viewName = parts[1];
9021 let views = docsToViews.get(designDocName);
9022 if (!views) {
9023 views = new Set();
9024 docsToViews.set(designDocName, views);
9025 }
9026 views.add(viewName);
9027 }
9028 const opts = {
9029 keys : mapToKeysArray(docsToViews),
9030 include_docs : true
9031 };
9032
9033 const res$$1 = await db.allDocs(opts);
9034 const viewsToStatus = {};
9035 for (const row of res$$1.rows) {
9036 const ddocName = row.key.substring(8); // cuts off '_design/'
9037 for (const viewName of docsToViews.get(row.key)) {
9038 let fullViewName = ddocName + '/' + viewName;
9039 /* istanbul ignore if */
9040 if (!metaDoc.views[fullViewName]) {
9041 // new format, without slashes, to support PouchDB 2.2.0
9042 // migration test in pouchdb's browser.migration.js verifies this
9043 fullViewName = viewName;
9044 }
9045 const viewDBNames = Object.keys(metaDoc.views[fullViewName]);
9046 // design doc deleted, or view function nonexistent
9047 const statusIsGood = row.doc && row.doc.views &&
9048 row.doc.views[viewName];
9049 for (const viewDBName of viewDBNames) {
9050 viewsToStatus[viewDBName] = viewsToStatus[viewDBName] || statusIsGood;
9051 }
9052 }
9053 }
9054
9055 const dbsToDelete = Object.keys(viewsToStatus)
9056 .filter(function (viewDBName) { return !viewsToStatus[viewDBName]; });
9057
9058 const destroyPromises = dbsToDelete.map(function (viewDBName) {
9059 return sequentialize(getQueue(viewDBName), function () {
9060 return new db.constructor(viewDBName, db.__opts).destroy();
9061 })();
9062 });
9063
9064 return Promise.all(destroyPromises).then(function () {
9065 return {ok: true};
9066 });
9067 } catch (err) {
9068 if (err.status === 404) {
9069 return {ok: true};

Callers 1

createAbstractMapReduceFunction · 0.70

Calls 7

parseViewNameFunction · 0.70
mapToKeysArrayFunction · 0.70
sequentializeFunction · 0.70
getQueueFunction · 0.70
getMethod · 0.45
addMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…