Comparing version 0.9.1 to 0.9.2
const R = require('ramda') | ||
const Bluebird = require('bluebird') | ||
const CouchWrapper = require('./wrapper.js') | ||
const ID = require('../identifier.js') | ||
const Err = require('../error.js') | ||
const R = require('ramda') | ||
const Bluebird = require('bluebird') | ||
const CouchWrapper = require('./wrapper.js') | ||
const ID = require('../identifier.js') | ||
const Err = require('../error.js') | ||
const Util = require('../util') | ||
@@ -87,2 +88,35 @@ const STORE = 'couchdb' | ||
const _findOneByIdWhereEq = R.curry((couch, bucket, id, predicates) => | ||
getById(couch, bucket, id) | ||
.then(R.unless( | ||
R.allPass([Util.notNil, R.whereEq(ID.couchify(predicates))]) | ||
, R.always(null) | ||
)) | ||
) | ||
const _findOneWhereEq = R.curry((couch, bucket, predicates) => | ||
findWhereEq( | ||
couch | ||
, bucket | ||
, R.objOf('predicates', ID.couchify(predicates)) | ||
) | ||
.then(R.ifElse( | ||
hasOnlyOne | ||
, R.head | ||
, R.compose(Err.TooManyRecords.throw(STORE, bucket, predicates), R.length) | ||
)) | ||
) | ||
const findOneWhereEq = R.curry((couch, bucket, predicates) => | ||
Bluebird.resolve(predicates) | ||
.then(R.ifElse( | ||
R.has('id') | ||
, _findOneByIdWhereEq(couch, bucket, predicates.id) | ||
, _findOneWhereEq(couch, bucket) | ||
)) | ||
) | ||
const bulk_upsert = R.curry((couch, bucket, keys, docs) => | ||
@@ -95,12 +129,8 @@ Bluebird.map(docs, (doc) => | ||
, R.always(doc) | ||
, (predicates) => findWhereEq( | ||
couch | ||
, bucket | ||
, R.objOf('predicates', ID.couchify(predicates)) | ||
) | ||
, findOneWhereEq(couch, bucket) | ||
)) | ||
.then(R.ifElse( | ||
R.isEmpty | ||
Util.isEmptyOrNil | ||
, R.always(ID.couchify(doc)) | ||
, R.compose(ID.couchify, R.merge(R.__, doc), R.head) | ||
, R.merge(R.__, ID.couchify(doc)) | ||
)) | ||
@@ -175,1 +205,2 @@ ) | ||
})) | ||
@@ -47,2 +47,8 @@ /* eslint no-magic-numbers: 0 */ | ||
const notNil = R.compose(R.not, R.isNil) | ||
const isEmptyOrNil = R.either(R.isEmpty, R.isNil) | ||
module.exports = { | ||
@@ -54,2 +60,4 @@ ascend | ||
, replaceElement | ||
, notNil | ||
, isEmptyOrNil | ||
} |
{ | ||
"name": "kuss", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"description": "Combined Universal Storage Service", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -198,3 +198,3 @@ /*eslint-env node, mocha*/ | ||
it(`should update a list of documents in the given bucket based on the | ||
_id and _rev keys. if these two keys don't exist, create the document | ||
_id and _rev keys. If these two keys don't exist, create the document | ||
in the bucket`, function() { | ||
@@ -222,2 +222,27 @@ | ||
it(`should update a list of documents in the given bucket based on the | ||
_id and _rev keys. If these two keys don't exist, create the document | ||
in the bucket`, function() { | ||
return couchdb.insert(DB_NAME, { id: '1', first: 'mat', last: 'chuang' }) | ||
.then(() => couchdb.bulk_upsert(DB_NAME, ['last'], [ | ||
{ first: 'matt', last: 'chuang' } | ||
, { first: 'jon', last: 'lee' } | ||
])) | ||
.then(() => couchdb.getAll(DB_NAME)) | ||
.then((res) => { | ||
const map = R.compose( | ||
R.mergeAll | ||
, R.map((row) => ({ [row.doc.last] : row.doc })) | ||
)(res.rows) | ||
demand(res.rows).have.length(2) | ||
demand(map['chuang'].first).equal('matt') | ||
demand(map['lee'].first).equal('jon') | ||
}) | ||
}) | ||
}) | ||
@@ -224,0 +249,0 @@ |
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
160519
2778