couchinator
Advanced tools
Comparing version 0.30.2 to 0.40.1
@@ -21,4 +21,4 @@ const Couchinator = require('../lib'); | ||
c.create().then(r => c.destroy()); | ||
c.create() //.then(r => c.destroy()); | ||
// c.rereate({designDocsOnly: true}) |
@@ -24,3 +24,3 @@ const path = require('path'); | ||
logger | ||
).updateDocs(allDocs) | ||
).updateDocs(allDocs, true) | ||
) | ||
@@ -27,0 +27,0 @@ ) |
const CREATED = '(created)'; | ||
const UPDATED = '(updated)'; | ||
const UPSERTED = '(upserted)'; | ||
const FOUND = '(found)'; | ||
const ERROR = '(error)'; | ||
module.exports = function Report(logger, designDocsStateMap, dbStateMap) { | ||
module.exports = function Report(logger, dbStateMap) { | ||
return { | ||
@@ -21,3 +22,3 @@ generate(dbName, designDocs, docs) { | ||
if (r.ok) { | ||
logger.info(` ${docUpdated(r.id) ? UPDATED : CREATED} ${r.id}`, r) | ||
logger.info(` ${UPSERTED} ${r.id}`, r) | ||
} else { | ||
@@ -54,3 +55,3 @@ if (r.error === 'conflict') { | ||
if (docs.length > 0) { | ||
logger.info(` updated: ${stats.updated}, skipped: ${stats.skipped}`); | ||
logger.info(` upserted: ${stats.updated}, skipped: ${stats.skipped}`); | ||
} | ||
@@ -62,7 +63,2 @@ } | ||
} | ||
function docUpdated(docId) { | ||
const state = designDocsStateMap.get(docId); | ||
return state && !!state.found | ||
} | ||
}; |
@@ -15,11 +15,9 @@ const fs = require('fs'); | ||
const self = this; // TODO logger and report can be refed directly | ||
const designDocsStateMap = new Map(); | ||
const report = Report(logger, designDocsStateMap, databaseStateMap); | ||
const report = Report(logger, databaseStateMap); | ||
return { | ||
updateDocs(allDocs) { | ||
// TODO change all docs to object | ||
const ps = [updateDesignDocsBulk()]; | ||
updateDocs(allDocs, upsert) { | ||
const ps = [updateDesignDocsBulk(upsert)]; | ||
if (allDocs) { | ||
ps.push(updateDocsBulk()); | ||
ps.push(updateDocsBulk(upsert)); | ||
} | ||
@@ -36,16 +34,40 @@ | ||
function updateDocsBulk() { | ||
function updateDocsBulk(upsert) { | ||
const docsPath = path.join(resourcePath, dbDirName); | ||
if (!fs.existsSync(docsPath)) return Promise.resolve(); // TODO remove sync | ||
return updateDocsBulkFromDir(docsPath); | ||
return updateDocsBulkFromDir(docsPath, false, upsert); | ||
} | ||
function updateDesignDocsBulk() { | ||
function updateDesignDocsBulk(upsert) { | ||
const docsPath = path.join(resourcePath, dbDirName, '_design'); | ||
if (!fs.existsSync(docsPath)) return Promise.resolve(); | ||
return updateDocsBulkFromDir(docsPath, true); | ||
return updateDocsBulkFromDir(docsPath, true, upsert); | ||
} | ||
function updateDocsBulkFromDir(docsDir, design) { | ||
const bulkInsert = payload => cloudant.db.use(dbName).bulk(payload); | ||
function bulkGetRecordsIdToRevMap(dbName, payload) { | ||
const keys = payload.docs.map(d => d._id); | ||
return cloudant.db | ||
.use(dbName) | ||
.fetchRevs({ keys }) | ||
.then(res => { | ||
return res.rows.reduce((acc, r) => { | ||
if (r.value) { | ||
acc[r.id] = r.value.rev; | ||
} | ||
return acc; | ||
}, {}); | ||
}); | ||
} | ||
function mergeRevIntoRecords(idToRevMap, payload) { | ||
payload.docs.forEach(p => { | ||
const rev = idToRevMap[p._id]; | ||
p._rev = rev; | ||
return p; | ||
}); | ||
return payload; | ||
} | ||
function updateDocsBulkFromDir(docsDir, design, upsert) { | ||
const createBulkPayload = files => { | ||
@@ -66,8 +88,2 @@ const docs = files.reduce((acc, f) => { | ||
if (design) { | ||
if (doc._rev) { | ||
logger.error( | ||
`Couchinator removed '_rev' property from ${docPath}. Will still update design doc if necessary.` | ||
); | ||
delete doc._rev; | ||
} | ||
acc.push(doc); | ||
@@ -89,31 +105,11 @@ } else { | ||
const upsert = payload => { | ||
const map = new Map(); | ||
payload.docs.forEach(d => map.set(d._id, d)); | ||
const keys = Array.from(map.keys()); | ||
return cloudant.db | ||
.use(dbName) | ||
.fetch({ keys }) | ||
.then(r => { | ||
const p = r.rows.map((e, i) => { | ||
if (e.error === 'not_found') { | ||
return payload.docs[i]; | ||
} else { | ||
// TODO: Fix e.doc is missing if _design json doens't match | ||
// the _design's name e.g. _design/name | ||
const newDoc = map.get(e.doc._id); | ||
const foundDoc = Object.assign(e.doc); | ||
delete foundDoc._rev; | ||
if (JSON.stringify(newDoc) === JSON.stringify(foundDoc)) { | ||
designDocsStateMap.set(newDoc._id, false); | ||
return newDoc; // Will result in conflict | ||
} else { | ||
designDocsStateMap.set(e.doc._id, true); | ||
const o = Object.assign(newDoc, { _rev: e.value.rev }); // Will update | ||
return o; | ||
} | ||
} | ||
}); | ||
return { docs: p }; | ||
const bulkInsert = payload => cloudant.db.use(dbName).bulk(payload); | ||
const mergePayloadWithRevs = payload => { | ||
if (upsert) { | ||
return bulkGetRecordsIdToRevMap(dbName, payload).then(r => { | ||
return mergeRevIntoRecords(r, payload); | ||
}); | ||
} else { | ||
return payload; | ||
} | ||
}; | ||
@@ -124,4 +120,4 @@ | ||
.then(createBulkPayload) | ||
.then(payload => (design ? upsert(payload) : payload)) | ||
.then(r => bulkInsert(r)) | ||
.then(mergePayloadWithRevs) | ||
.then(bulkInsert) | ||
.catch(e => { | ||
@@ -128,0 +124,0 @@ if (e.reason) { |
{ | ||
"name": "couchinator", | ||
"version": "0.30.2", | ||
"version": "0.40.1", | ||
"description": "Fixtures for Cloudant: Create and destroy cloudant databases with ease", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1967058
584