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

Function createView

lib/index-browser.js:8019–8087  ·  view source on GitHub ↗
(sourceDB, viewName, mapFun, reduceFun, temporary, localDocName)

Source from the content-addressed store, hash-verified

8017}
8018
8019async function createView(sourceDB, viewName, mapFun, reduceFun, temporary, localDocName) {
8020 const viewSignature = createViewSignature(mapFun, reduceFun);
8021
8022 let cachedViews;
8023 if (!temporary) {
8024 // cache this to ensure we don't try to update the same view twice
8025 cachedViews = sourceDB._cachedViews = sourceDB._cachedViews || {};
8026 if (cachedViews[viewSignature]) {
8027 return cachedViews[viewSignature];
8028 }
8029 }
8030
8031 const promiseForView = sourceDB.info().then(async function (info) {
8032 const depDbName = info.db_name + '-mrview-' +
8033 (temporary ? 'temp' : stringMd5(viewSignature));
8034
8035 // save the view name in the source db so it can be cleaned up if necessary
8036 // (e.g. when the _design doc is deleted, remove all associated view data)
8037 function diffFunction(doc) {
8038 doc.views = doc.views || {};
8039 let fullViewName = viewName;
8040 if (fullViewName.indexOf('/') === -1) {
8041 fullViewName = viewName + '/' + viewName;
8042 }
8043 const depDbs = doc.views[fullViewName] = doc.views[fullViewName] || {};
8044 /* istanbul ignore if */
8045 if (depDbs[depDbName]) {
8046 return; // no update necessary
8047 }
8048 depDbs[depDbName] = true;
8049 return doc;
8050 }
8051 await upsert(sourceDB, '_local/' + localDocName, diffFunction);
8052 const res = await sourceDB.registerDependentDatabase(depDbName);
8053 const db = res.db;
8054 db.auto_compaction = true;
8055 const view = {
8056 name: depDbName,
8057 db,
8058 sourceDB,
8059 adapter: sourceDB.adapter,
8060 mapFun,
8061 reduceFun
8062 };
8063
8064 let lastSeqDoc;
8065 try {
8066 lastSeqDoc = await view.db.get('_local/lastSeq');
8067 } catch (err) {
8068 /* istanbul ignore if */
8069 if (err.status !== 404) {
8070 throw err;
8071 }
8072 }
8073
8074 view.seq = lastSeqDoc ? lastSeqDoc.seq : 0;
8075 if (cachedViews) {
8076 view.db.once('destroyed', function () {

Callers 1

queryPromisedFunction · 0.70

Calls 4

createViewSignatureFunction · 0.70
stringMd5Function · 0.70
upsertFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…