ndjson-to-couchdb
Advanced tools
Comparing version 2.0.1 to 4.0.0
@@ -9,3 +9,4 @@ #!/usr/bin/env node | ||
force: false, | ||
prev_rev_field: undefined | ||
prev_rev_field: undefined, | ||
copy_fields_from_prev_rev: undefined | ||
}) | ||
@@ -12,0 +13,0 @@ |
@@ -30,3 +30,3 @@ var couchr = require('couchr'); | ||
} | ||
if(config.force) get_prev_rev(obj, config, update) | ||
if(config.force || config.copy_fields_from_prev_rev) prev(obj, config, update) | ||
else update() | ||
@@ -38,3 +38,8 @@ | ||
function get_prev_rev(obj, config, cb) { | ||
function prev(obj, config, cb) { | ||
if (config.copy_fields_from_prev_rev) prev_get(obj, config, cb); | ||
else prev_head(obj, config, cb) | ||
} | ||
function prev_head(obj, config, cb) { | ||
var req_opts = { | ||
@@ -47,7 +52,28 @@ method: 'HEAD', | ||
if (resp.headers.etag) obj._rev = JSON.parse(resp.headers.etag); | ||
} catch(e){} | ||
cb() | ||
}) | ||
} | ||
function prev_get(obj, config, cb) { | ||
var req_opts = { | ||
method: 'GET', | ||
json: true, | ||
url: config.url + '/' + obj._id | ||
} | ||
var fields = config.copy_fields_from_prev_rev; | ||
if (! Array.isArray(fields)) { | ||
fields = config.copy_fields_from_prev_rev.split(','); | ||
} | ||
couchr.get(config.url + '/' + obj._id, function(err, doc) { | ||
if (err) return cb(err); | ||
obj._rev = doc._rev; | ||
fields.forEach(function(field){ | ||
obj[field] = doc[field]; | ||
}) | ||
cb() | ||
}) | ||
} | ||
{ | ||
"name": "ndjson-to-couchdb", | ||
"version": "2.0.1", | ||
"version": "4.0.0", | ||
"description": "Stream ndjson to couchdb", | ||
@@ -5,0 +5,0 @@ "bin": "bin/cli.js", |
@@ -22,1 +22,2 @@ ndjson-to-couchdb | ||
- --prev_rev_fiel=prev_rev add a field to the emitted doc with name 'prev_rev' that will hold the last _rev before the update | ||
- --copy_fields_from_prev_rev=a,b preserve the fields a and b values by copying the val from the old rev to the new rev |
4075
85
23