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

Function createView

lib/index.es.js:8052–8120  ·  view source on GitHub ↗
(sourceDB, viewName, mapFun, reduceFun, temporary, localDocName)

Source from the content-addressed store, hash-verified

8050}
8051
8052async function createView(sourceDB, viewName, mapFun, reduceFun, temporary, localDocName) {
8053 const viewSignature = createViewSignature(mapFun, reduceFun);
8054
8055 let cachedViews;
8056 if (!temporary) {
8057 // cache this to ensure we don't try to update the same view twice
8058 cachedViews = sourceDB._cachedViews = sourceDB._cachedViews || {};
8059 if (cachedViews[viewSignature]) {
8060 return cachedViews[viewSignature];
8061 }
8062 }
8063
8064 const promiseForView = sourceDB.info().then(async function (info) {
8065 const depDbName = info.db_name + '-mrview-' +
8066 (temporary ? 'temp' : stringMd5(viewSignature));
8067
8068 // save the view name in the source db so it can be cleaned up if necessary
8069 // (e.g. when the _design doc is deleted, remove all associated view data)
8070 function diffFunction(doc) {
8071 doc.views = doc.views || {};
8072 let fullViewName = viewName;
8073 if (fullViewName.indexOf('/') === -1) {
8074 fullViewName = viewName + '/' + viewName;
8075 }
8076 const depDbs = doc.views[fullViewName] = doc.views[fullViewName] || {};
8077 /* istanbul ignore if */
8078 if (depDbs[depDbName]) {
8079 return; // no update necessary
8080 }
8081 depDbs[depDbName] = true;
8082 return doc;
8083 }
8084 await upsert(sourceDB, '_local/' + localDocName, diffFunction);
8085 const res$$1 = await sourceDB.registerDependentDatabase(depDbName);
8086 const db = res$$1.db;
8087 db.auto_compaction = true;
8088 const view = {
8089 name: depDbName,
8090 db,
8091 sourceDB,
8092 adapter: sourceDB.adapter,
8093 mapFun,
8094 reduceFun
8095 };
8096
8097 let lastSeqDoc;
8098 try {
8099 lastSeqDoc = await view.db.get('_local/lastSeq');
8100 } catch (err) {
8101 /* istanbul ignore if */
8102 if (err.status !== 404) {
8103 throw err;
8104 }
8105 }
8106
8107 view.seq = lastSeqDoc ? lastSeqDoc.seq : 0;
8108 if (cachedViews) {
8109 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…