Comparing version 6.0.0 to 6.1.0
# CHANGELOG | ||
*versions follow [SemVer](http://semver.org)* | ||
## 6.1.0 - 2020-09-28 | ||
[`db.update`](https://github.com/maxlath/blue-cot#update): allow to set `createIfMissing=true`, allowing to recover the behavior from `< 6.0.0` | ||
## 6.0.0 - 2020-08-10 | ||
@@ -5,0 +8,0 @@ **BREAKING CHANGES**: [`db.update`](https://github.com/maxlath/blue-cot#update) stops to create empty (`{ _id }`) docs when the updated doc can't be found, rejecting with a 404 instead. |
module.exports = { | ||
cot: { | ||
port: 5984, | ||
port: 7984, | ||
// debug: true, | ||
@@ -5,0 +5,0 @@ username: 'max', |
@@ -24,3 +24,3 @@ const querystring = require('querystring') | ||
get: async (docId, revId) => { | ||
var url = API.docUrl(docId) | ||
let url = API.docUrl(docId) | ||
if (typeof revId === 'string') url += `?rev=${revId}` | ||
@@ -56,3 +56,3 @@ const res = await jsonRequest('GET', url) | ||
} else { | ||
throw errors_.buildFromRes(res, `error posting new doc`) | ||
throw errors_.buildFromRes(res, 'error posting new doc') | ||
} | ||
@@ -69,14 +69,20 @@ }, | ||
} else { | ||
throw errors_.buildFromRes(res, `error batch posting new doc`) | ||
throw errors_.buildFromRes(res, 'error batch posting new doc') | ||
} | ||
}, | ||
update: async (docId, fn) => { | ||
update: async (docId, fn, options = {}) => { | ||
const db = API | ||
let attempt = 0 | ||
const { createIfMissing } = options | ||
const tryIt = async () => { | ||
if (++attempt > 10) throw errors_.new('too many attempts', 400, { docId, fn }) | ||
// Throw if the doc doesn't exist | ||
const doc = await db.get(docId) | ||
let doc | ||
try { | ||
doc = await db.get(docId) | ||
} catch (err) { | ||
if (err.statusCode === 404 && createIfMissing) doc = { _id: docId } | ||
else throw err | ||
} | ||
try { | ||
const res = await db.put(fn(doc)) | ||
@@ -109,6 +115,5 @@ if (res.ok) return res | ||
var url = API.docUrl(docId) + '?revs=true&open_revs=all' | ||
const url = API.docUrl(docId) + '?revs=true&open_revs=all' | ||
const res = await jsonRequest('GET', url) | ||
const data = res.data[0].ok | ||
const currentRev = data._rev | ||
const preDeleteRevNum = data._revisions.start - 1 | ||
@@ -118,3 +123,4 @@ const preDeleteRevId = data._revisions.ids[1] | ||
const preDeleteDoc = await API.get(docId, preDeleteRev) | ||
preDeleteDoc._rev = currentRev | ||
// Re-created documents shouldn't have a rev, see https://github.com/apache/couchdb/issues/3177 | ||
delete preDeleteDoc._rev | ||
return API.put(preDeleteDoc) | ||
@@ -137,3 +143,3 @@ } | ||
const res = await jsonRequest('POST', url, { docs }) | ||
if (res.statusCode !== 201) throw errors_.buildFromRes(res, `error posting to _bulk_docs`) | ||
if (res.statusCode !== 201) throw errors_.buildFromRes(res, 'error posting to _bulk_docs') | ||
@@ -154,3 +160,3 @@ for (let i = 0; i < res.data.length; i++) { | ||
// Avoid any possible conflict with object inherited attributes and methods | ||
if (query.hasOwnProperty(key)) { | ||
if (Object.prototype.hasOwnProperty.call(query, key)) { | ||
if (key === 'startkey_docid' || key === 'endkey_docid') { | ||
@@ -246,3 +252,3 @@ q[key] = query[key] | ||
if (res.statusCode === 200) return res.data | ||
else throw errors_.buildFromRes(res, `error reading _changes`) | ||
else throw errors_.buildFromRes(res, 'error reading _changes') | ||
} | ||
@@ -249,0 +255,0 @@ } |
@@ -12,3 +12,3 @@ const formatError = function (message, statusCode, context) { | ||
var bodyStr | ||
let bodyStr | ||
try { | ||
@@ -15,0 +15,0 @@ bodyStr = JSON.stringify(body) |
@@ -16,3 +16,3 @@ const request = require('./request') | ||
// Required by old CouchDB (ex: v1.6.1) | ||
'Authorization': `Basic ${getBasicCredentials(username, password)}` | ||
Authorization: `Basic ${getBasicCredentials(username, password)}` | ||
}, | ||
@@ -19,0 +19,0 @@ agent, |
@@ -15,3 +15,3 @@ { | ||
"test-watch": "mocha --watch", | ||
"lint": "standard", | ||
"lint": "eslint lib test", | ||
"prepublishOnly": "npm run lint && npm test", | ||
@@ -25,3 +25,3 @@ "postpublish": "git push --tags", | ||
], | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"main": "lib/cot.js", | ||
@@ -45,6 +45,10 @@ "dependencies": { | ||
"config": "^3.2.4", | ||
"eslint": "^7.16.0", | ||
"eslint-config-standard": "^16.0.2", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"git-hooks": "^1.1.10", | ||
"mocha": "^7.0.0", | ||
"should": "^13.2.1", | ||
"standard": "^10.0.3" | ||
"should": "^13.2.1" | ||
}, | ||
@@ -51,0 +55,0 @@ "standard": { |
@@ -207,2 +207,7 @@ [CouchDB](http://couchdb.org/) library with a simple, functional-programing-friendly API. | ||
By default, `db.update` only accepts to update existing docs, but this can be changed by setting `createIfMissing=true`: | ||
```js | ||
const res = await db.update(docId, updateFunction, { createIfMissing: true }) | ||
``` | ||
#### bulk | ||
@@ -209,0 +214,0 @@ `POST /<dbName>/_bulk_docs` |
32953
514
355
9