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