couch-init2
Advanced tools
Comparing version 2.1.6 to 2.1.7
const _ = require('inv-loggers') | ||
const bluebird = require('bluebird') | ||
const nanoBlue = require('nano-blue2') | ||
@@ -12,3 +11,3 @@ // example: | ||
// name: 'dbname', | ||
// designDocs: ['designdoc1', 'designdoc2'] | ||
// designDocs: [ 'designdoc1', 'designdoc2' ] | ||
// } | ||
@@ -35,8 +34,7 @@ // ] | ||
const nano = nanoBlue(dbBaseUrl) | ||
const initDb = require('./lib/init_db')(dbBaseUrl, nano, designDocFolder) | ||
const initDb = require('./lib/init_db')(dbBaseUrl, designDocFolder) | ||
return bluebird.all(dbsList.map(initDb)) | ||
.then((res) => { return { ok: true } }) | ||
.then(res => ({ ok: true })) | ||
.catch(_.ErrorRethrow('db init err')) | ||
} |
@@ -5,6 +5,4 @@ const bluebird = require('bluebird') | ||
module.exports = { | ||
readFile: function (path) { | ||
return fs.readFileAsync(path, {encoding: 'utf-8'}) | ||
}, | ||
readFile: path => fs.readFileAsync(path, { encoding: 'utf-8' }), | ||
writeFile: fs.writeFileAsync.bind(fs) | ||
} |
const _ = require('inv-loggers') | ||
const bluebird = require('bluebird') | ||
const putSecurityDoc = require('./put_security_doc') | ||
const nanoBlue = require('nano-blue2') | ||
module.exports = function (dbBaseUrl, nano, designDocFolder) { | ||
const initDb = function (dbData) { | ||
// _.log(dbData, 'initDb') | ||
module.exports = function (dbBaseUrl, designDocFolder) { | ||
const nano = nanoBlue(dbBaseUrl) | ||
return function (dbData) { | ||
const dbName = dbData.name | ||
@@ -13,3 +14,3 @@ const designDocs = dbData.designDocs | ||
return ensureDbExistance(dbName, db) | ||
return ensureDbExistance(nano, db, dbName) | ||
.then(function () { | ||
@@ -22,22 +23,15 @@ return bluebird.all([ | ||
} | ||
} | ||
const ensureDbExistance = function (dbName, db) { | ||
return db.info() | ||
.then((res) => _.success(`${dbName} database: exist`)) | ||
.catch(Create(dbName)) | ||
.catch(_.ErrorRethrow('ensureDbExistance')) | ||
} | ||
const ensureDbExistance = function (nano, db, dbName) { | ||
return db.info() | ||
.then(res => _.success(`${dbName} database: exist`)) | ||
.catch(create(nano, dbName)) | ||
.catch(_.ErrorRethrow('ensureDbExistance')) | ||
} | ||
const Create = function (dbName) { | ||
return function (err) { | ||
if (err.statusCode === 404) { | ||
return nano.db.create(dbName) | ||
.then(_.Log(`${dbName} database: created`)) | ||
} else { | ||
throw err | ||
} | ||
} | ||
} | ||
return initDb | ||
const create = (nano, dbName) => err => { | ||
if (err.statusCode !== 404) throw err | ||
return nano.db.create(dbName) | ||
.then(_.Log(`${dbName} database: created`)) | ||
} |
@@ -22,5 +22,3 @@ const _ = require('inv-loggers') | ||
// to 'username' | ||
const parseUsername = function (dbBaseUrl) { | ||
return dbBaseUrl.split('://')[1].split(':')[0] | ||
} | ||
const parseUsername = dbBaseUrl => dbBaseUrl.split('://')[1].split(':')[0] | ||
@@ -27,0 +25,0 @@ const securityDoc = function (username) { |
@@ -5,19 +5,15 @@ const _ = require('inv-loggers') | ||
module.exports = function (designDocFolder) { | ||
// this verifies that the database design documents are up-to-date | ||
// with the design docs files | ||
const syncDesignDocs = function (db, designDocs) { | ||
return bluebird.all(designDocs.map(syncDesignDoc.bind(null, db))) | ||
} | ||
// This verifies that the database design documents are up-to-date | ||
// with the design docs files | ||
module.exports = designDocFolder => (db, designDocs) => { | ||
return bluebird.all(designDocs.map(syncDesignDoc(designDocFolder, db))) | ||
} | ||
const syncDesignDoc = function (db, designDocName) { | ||
const designDocId = `_design/${designDocName}` | ||
return getDesignDocFile(designDocFolder, designDocName) | ||
.then(function (designDocFile) { | ||
return getCurrentDesignDoc(db, designDocId) | ||
.then(updateDesignDoc.bind(null, db, designDocId, designDocFile)) | ||
}) | ||
} | ||
return syncDesignDocs | ||
const syncDesignDoc = (designDocFolder, db) => designDocName => { | ||
const designDocId = `_design/${designDocName}` | ||
return getDesignDocFile(designDocFolder, designDocName) | ||
.then(designDocFile => { | ||
return getCurrentDesignDoc(db, designDocId) | ||
.then(updateDesignDoc(db, designDocId, designDocFile)) | ||
}) | ||
} | ||
@@ -28,19 +24,19 @@ | ||
return fs.readFile(designDocPath) | ||
.catch(function (err) { | ||
if (err.code === 'ENOENT') { | ||
// initialize the design doc if none is found | ||
// return a stringify version to keep consistency | ||
// with what would the normal readFile | ||
const initDoc = JSON.stringify(emtpyDesignDoc(designDocName), null, 4) | ||
// creating the design doc file but not waiting for its creation | ||
fs.writeFile(designDocPath, initDoc) | ||
.then(function () { | ||
_.log(designDocPath, 'design doc file created') | ||
}) | ||
return initDoc | ||
} else { | ||
.catch(err => { | ||
if (err.code !== 'ENOENT') { | ||
_.error(err, 'reloadDesignDoc readFile err') | ||
throw err | ||
} | ||
// Initialize the design doc if none is found | ||
// Return a stringify version to keep consistency | ||
// with what would the normal readFile | ||
const initDoc = JSON.stringify(emtpyDesignDoc(designDocName), null, 4) | ||
// Creating the design doc file but not waiting for its creation | ||
fs.writeFile(designDocPath, initDoc) | ||
.then(function () { | ||
_.log(designDocPath, 'design doc file created') | ||
}) | ||
return initDoc | ||
}) | ||
@@ -51,38 +47,34 @@ } | ||
return db.get(designDocId) | ||
.spread(function (body, header) { | ||
return body | ||
.spread((body, header) => body) | ||
.catch(err => { | ||
if (err.statusCode !== 404) throw err | ||
_.info(designDocId, 'design doc not found: creating') | ||
// pass an empty document to trigger a document update | ||
return {} | ||
}) | ||
.catch(function (err) { | ||
if (err.statusCode === 404) { | ||
_.info(designDocId, 'design doc not found: creating') | ||
// pass an empty document to trigger a document update | ||
return {} | ||
} else { | ||
throw err | ||
} | ||
}) | ||
} | ||
const updateDesignDoc = function (db, designDocId, designDocFile, currentDesignDoc) { | ||
const updateDesignDoc = (db, designDocId, designDocFile) => currentDesignDoc => { | ||
const rev = currentDesignDoc && currentDesignDoc._rev | ||
// delete the rev to be able to compare object | ||
// Delete the rev to be able to compare object | ||
delete currentDesignDoc._rev | ||
// designDocFile should be a stringified object | ||
const currentDesignDocStr = JSON.stringify(currentDesignDoc) | ||
// comparison is made without spaces to avoid false negative | ||
if (removeSpaces(designDocFile) === removeSpaces(currentDesignDocStr)) { | ||
// _.info(designDocId, 'design doc up-to-date') | ||
} else { | ||
_.info(designDocId, 'updating design doc') | ||
const update = JSON.parse(designDocFile) | ||
update._rev = rev | ||
return db.insert(update) | ||
.spread(function (body) { | ||
_.success(designDocId, 'design doc updated') | ||
}) | ||
.catch(function (err) { | ||
_.error(err.request, err.message) | ||
}) | ||
} | ||
// Comparison is made without spaces to avoid false negative | ||
if (removeSpaces(designDocFile) === removeSpaces(currentDesignDocStr)) return | ||
_.info(designDocId, 'updating design doc') | ||
const update = JSON.parse(designDocFile) | ||
update._rev = rev | ||
return db.insert(update) | ||
.spread(body => { | ||
_.success(designDocId, 'design doc updated') | ||
}) | ||
.catch(err => { | ||
_.error(err.request, err.message) | ||
}) | ||
} | ||
@@ -97,4 +89,2 @@ | ||
const removeSpaces = function (string) { | ||
return string.replace(/\s/g, '') | ||
} | ||
const removeSpaces = string => string.replace(/\s/g, '') |
{ | ||
"name": "couch-init2", | ||
"version": "2.1.6", | ||
"version": "2.1.7", | ||
"description": "Opiniated CouchDB databases initializer", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "mocha", | ||
"lint": "./node_modules/standard/bin/cmd.js index.js lib/*js", | ||
@@ -14,8 +14,11 @@ "prepublish": "npm run lint" | ||
"dependencies": { | ||
"bluereq": "^2.1.1", | ||
"inv-loggers": "^3.2.7", | ||
"nano-blue2": "^0.3.4" | ||
"bluereq": "^2.1.4", | ||
"inv-loggers": "^3.3.1", | ||
"nano-blue2": "^0.3.6" | ||
}, | ||
"devDependencies": { | ||
"standard": "^6.0.7" | ||
"config": "^1.26.2", | ||
"mocha": "^3.5.3", | ||
"should": "^13.1.0", | ||
"standard": "^10.0.3" | ||
}, | ||
@@ -33,2 +36,9 @@ "repository": { | ||
], | ||
"standard": { | ||
"globals": [ | ||
"it", | ||
"describe", | ||
"beforeEach" | ||
] | ||
}, | ||
"bugs": { | ||
@@ -35,0 +45,0 @@ "url": "https://github.com/maxlath/couch-init2/issues" |
Sorry, the diff of this file is not supported yet
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
11
0
53340
4
178
Updatedbluereq@^2.1.4
Updatedinv-loggers@^3.3.1
Updatednano-blue2@^0.3.6