couchdb-update-views
Advanced tools
Comparing version 1.0.9 to 1.0.11
31
cli.js
@@ -16,5 +16,5 @@ #! /usr/bin/env node | ||
var config = nconf.argv().env().file({file: configFilePath}) | ||
var getDB = require('./getDB') | ||
var update = require('./update') | ||
var path = require('path') | ||
var cradleNconf = require('cradle-nconf') | ||
function sync(callback) { | ||
@@ -29,18 +29,17 @@ var couch = config.get('couch') | ||
if (err) { return callback(err) } | ||
getDB(couch, function (err, db) { | ||
getDocs(docsDir, function (err, files) { | ||
async.forEach( | ||
files, | ||
function(file, cb) { | ||
var db = cradleNconf(config) | ||
getDocs(docsDir, function (err, files) { | ||
async.forEach( | ||
files, | ||
function(file, cb) { | ||
var filePath = file.filePath | ||
inspect(filePath, 'updating document at path') | ||
var docPath = '_design/' + file.fileName.replace(/\.js/,'') | ||
var doc = require(filePath) | ||
inspect(docPath, 'docPath') | ||
update(db, docPath, doc, cb) | ||
}, | ||
callback | ||
) | ||
}) | ||
var filePath = file.filePath | ||
inspect(filePath, 'updating document at path') | ||
var docPath = '_design/' + file.fileName.replace(/\.js/,'') | ||
var doc = require(filePath) | ||
inspect(docPath, 'docPath') | ||
update(db, docPath, doc, cb) | ||
}, | ||
callback | ||
) | ||
}) | ||
@@ -47,0 +46,0 @@ }) |
@@ -7,3 +7,6 @@ module.exports = { | ||
if (doc.resource === 'Customer') { | ||
emit(doc._id, doc) } | ||
emit(doc._id, null) } | ||
}, | ||
reduce: function(key, values) { | ||
return sum(values) | ||
} | ||
@@ -14,3 +17,3 @@ }, | ||
if (doc.resource === 'Customer' && doc.customerName) { | ||
emit(doc.customerName, doc) | ||
emit(doc.customerName, null) | ||
} | ||
@@ -17,0 +20,0 @@ } |
{ | ||
"name": "couchdb-update-views", | ||
"version": "1.0.9", | ||
"version": "1.0.11", | ||
"description": "Keep couchdb views up to date", | ||
@@ -28,3 +28,2 @@ "main": "sync.js", | ||
"optimist": "~0.3.5", | ||
"cradle": "~0.6.4", | ||
"nconf": "~0.6.7", | ||
@@ -31,0 +30,0 @@ "should": "~1.2.2", |
@@ -9,3 +9,3 @@ Keep couchdb views up to date | ||
```bash | ||
npm install -g update-couchdb-views | ||
npm install -g couchdb-update-views | ||
``` | ||
@@ -17,3 +17,3 @@ | ||
```bash | ||
update-couchdb-views --config ./sampeConfig.json --docsDir ./docs | ||
couchdb-update-views --config ./sampleConfig.json --docsDir ./docs | ||
``` | ||
@@ -20,0 +20,0 @@ Look at <project_root>/sampleConfig.json for an example of what your config json file should look like |
@@ -11,3 +11,3 @@ var inspect = require('eyespect').inspector() | ||
protocol: 'http', | ||
database: 'fooDB', | ||
database: 'views_test', | ||
port: 5984 | ||
@@ -14,0 +14,0 @@ } |
@@ -17,2 +17,3 @@ var ce = require('cloneextend') | ||
describe('Customer', function () { | ||
this.slow(300) | ||
var db | ||
@@ -45,13 +46,50 @@ before(function (done) { | ||
var newCustomer = ce.clone(customer) | ||
newCustomer.views.fooView = { | ||
newCustomer.views.test = { | ||
map: function(doc) { | ||
emit(doc._id, doc) | ||
}, | ||
reduce: function (keys, values) { | ||
return values.length | ||
} | ||
} | ||
update(db, '_design/customer', newCustomer, done) | ||
update(db, '_design/customer', newCustomer, function (err, reply) { | ||
should.not.exist(err) | ||
// confirm the design document was updated correctly | ||
db.get('_design/customer', function (err, reply) { | ||
should.not.exist(err) | ||
should.exist(reply) | ||
var inputTestView = newCustomer.views.test | ||
var inputTestViewMap = inputTestView.map.toString().replace(/\s/g,'') | ||
var inputTestViewReduce = inputTestView.reduce.toString().replace(/\s/g,'') | ||
var newViews = reply.views | ||
var newViewMap = newViews.test.map.toString().replace(/\s/g,'') | ||
var newViewReduce = newViews.test.reduce.toString().replace(/\s/g,'') | ||
newViewReduce.should.eql(inputTestViewReduce) | ||
newViewMap.should.eql(inputTestViewMap) | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should reset customer design document back to original', function (done) { | ||
update(db, '_design/customer', customer, done) | ||
update(db, '_design/customer', customer, function (err) { | ||
should.not.exist(err) | ||
db.get('_design/customer', function (err, reply) { | ||
should.not.exist(err) | ||
should.exist(reply) | ||
var inputView = customer.views.all | ||
var inputViewMap = inputView.map.toString().replace(/\s/g,'') | ||
var inputViewReduce = inputView.reduce.toString().replace(/\s/g,'') | ||
var newViews = reply.views | ||
var newViewMap = newViews.all.map.toString().replace(/\s/g,'') | ||
var newViewReduce = newViews.all.reduce.toString().replace(/\s/g,'') | ||
newViewReduce.should.eql(inputViewReduce) | ||
newViewMap.should.eql(inputViewMap) | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should create new customer', function (done) { | ||
@@ -94,3 +132,7 @@ removeAll(db, function (err) { | ||
function removeAll(db, callback) { | ||
db.view('customer/all', {}, function (err, docs) { | ||
var opts = { | ||
include_docs: true, | ||
reduce: false | ||
} | ||
db.view('customer/all', opts, function (err, docs) { | ||
if (err) { return callback(err) } | ||
@@ -102,5 +144,6 @@ if (docs.length === 0) { | ||
docs, | ||
function (doc, cb) { | ||
var rev = doc.value._rev | ||
var id = doc.value._id | ||
function (element, cb) { | ||
var doc = element.doc | ||
var rev = doc._rev | ||
var id = doc._id | ||
db.remove(id, rev, cb) | ||
@@ -107,0 +150,0 @@ }, callback) |
@@ -0,1 +1,6 @@ | ||
/** | ||
* See if the "doc" in the database matches the code in the source file | ||
*/ | ||
var inspect = require('eyespect').inspector(); | ||
var shouldUpdate = require('./lib//shouldUpdate') | ||
module.exports = function updateViews(db, docPath, code, cb) { | ||
@@ -7,11 +12,7 @@ // compare function definitions in document and in code | ||
} | ||
var rev = doc._rev | ||
var docUpdates = doc.updates | ||
var codeUpdates = code.updates | ||
var docViews = doc.views | ||
var codeViews = code.views | ||
var viewsPending = compareDef(doc.views, code.views, docPath) | ||
var updatesPending = compareDef(doc.updates, code.updates, docPath) | ||
if (updatesPending || viewsPending) { | ||
var viewsPending = shouldUpdate(doc.views, code.views) | ||
inspect(viewsPending, 'is update needed?') | ||
if (viewsPending) { | ||
var rev = doc._rev | ||
return updateDoc(db, docPath, code, rev, cb) | ||
@@ -40,24 +41,1 @@ } | ||
} | ||
function compareDef(docDef, codeDef, docPath) { | ||
var u | ||
var i = 0 | ||
if (!codeDef && !docDef) { | ||
return false | ||
} | ||
for (u in docDef) { | ||
i++ | ||
if (!codeDef[u] || docDef[u] != codeDef[u].toString()) { | ||
return true | ||
} | ||
} | ||
// check that both doc and code have same number of functions | ||
for (u in codeDef) { | ||
i-- | ||
if (i < 0) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Sorry, the diff of this file is not supported yet
22565
6
17
672
6
- Removedcradle@~0.6.4
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedbrowser-request@0.3.3(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removedcradle@0.6.9(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddebug@0.7.4(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfollow@0.11.4(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedvargs@0.1.0(transitive)
- Removedverror@1.10.0(transitive)