Comparing version 6.2.4 to 7.0.0
@@ -149,19 +149,4 @@ const querystring = require('querystring') | ||
buildQueryString: (query = {}) => { | ||
validatePlainObject(query, 'query') | ||
buildQueryString: query => buildSanitizedQueryString(query, viewQueryKeys), | ||
const q = {} | ||
viewQueryKeys.forEach(key => { | ||
// Avoid any possible conflict with object inherited attributes and methods | ||
if (Object.prototype.hasOwnProperty.call(query, key)) { | ||
if (key === 'startkey_docid' || key === 'endkey_docid') { | ||
q[key] = query[key] | ||
} else { | ||
q[key] = JSON.stringify(query[key]) | ||
} | ||
} | ||
}) | ||
return querystring.stringify(q) | ||
}, | ||
viewQuery: async (path, query) => { | ||
@@ -244,8 +229,3 @@ const qs = API.buildQueryString(query) | ||
changes: async (query = {}) => { | ||
const q = {} | ||
changesQueryKeys.forEach(key => { | ||
if (query[key] != null) q[key] = query[key] | ||
}) | ||
if (query.longpoll) q.feed = 'longpoll' | ||
const qs = querystring.stringify(q) | ||
const qs = buildSanitizedQueryString(query, changesQueryKeys) | ||
const path = `/${dbName}/_changes?${qs}` | ||
@@ -285,32 +265,76 @@ | ||
const viewQueryKeys = [ | ||
'descending', | ||
'endkey', | ||
'endkey_docid', | ||
'group', | ||
'group_level', | ||
'include_docs', | ||
'inclusive_end', | ||
'key', | ||
'limit', | ||
'reduce', | ||
'skip', | ||
'stale', | ||
'startkey', | ||
'startkey_docid', | ||
'update_seq' | ||
] | ||
const buildSanitizedQueryString = (query = {}, queryKeys) => { | ||
validatePlainObject(query, 'query') | ||
const q = {} | ||
for (const key of Object.keys(query)) { | ||
// Avoid any possible conflict with object inherited attributes and methods | ||
if (Object.prototype.hasOwnProperty.call(query, key)) { | ||
validateQueryKey(queryKeys, key, query) | ||
if (query[key] != null) { | ||
if (queryKeys[key] === 'json') { | ||
q[key] = JSON.stringify(query[key]) | ||
} else { | ||
q[key] = query[key] | ||
} | ||
} | ||
} | ||
} | ||
return querystring.stringify(q) | ||
} | ||
const changesQueryKeys = [ | ||
'filter', | ||
'include_docs', | ||
'limit', | ||
'since', | ||
'timeout', | ||
'descending', | ||
'heartbeat', | ||
'style' | ||
// Not including feed as a possible option | ||
// as it doesn't play well with promises | ||
// 'feed' | ||
] | ||
const validateQueryKey = (queryKeys, key, query) => { | ||
if (queryKeys[key] == null) { | ||
throw errors_.new('invalid query key', 400, { key, query, validKeys: Object.keys(queryKeys) }) | ||
} | ||
} | ||
// Source: https://docs.couchdb.org/en/latest/api/ddoc/views.html | ||
const viewQueryKeys = { | ||
conflicts: 'boolean', | ||
descending: 'boolean', | ||
endkey: 'json', | ||
end_key: 'json', | ||
endkey_docid: 'string', | ||
end_key_doc_id: 'string', | ||
group: 'boolean', | ||
group_level: 'number', | ||
include_docs: 'boolean', | ||
attachments: 'boolean', | ||
att_encoding_info: 'boolean', | ||
inclusive_end: 'boolean', | ||
key: 'json', | ||
keys: 'json', | ||
limit: 'number', | ||
reduce: 'boolean', | ||
skip: 'number', | ||
sorted: 'boolean', | ||
stable: 'boolean', | ||
stale: 'string', | ||
startkey: 'json', | ||
start_key: 'json', | ||
startkey_docid: 'string', | ||
start_key_doc_id: 'string', | ||
update: 'string', | ||
update_seq: 'boolean', | ||
} | ||
// Source: https://docs.couchdb.org/en/latest/api/database/changes.html | ||
const changesQueryKeys = { | ||
doc_ids: 'json', | ||
conflicts: 'boolean', | ||
descending: 'boolean', | ||
// Not including feed as a possible option as it doesn't play well with promises | ||
// feed: 'string', | ||
filter: 'string', | ||
heartbeat: 'number', | ||
include_docs: 'boolean', | ||
attachments: 'boolean', | ||
att_encoding_info: 'boolean', | ||
'last-event-id': 'number', | ||
limit: 'number', | ||
style: 'string', | ||
since: 'string', | ||
timeout: 'number', | ||
view: 'string', | ||
seq_interval: 'number', | ||
} |
@@ -24,3 +24,3 @@ { | ||
], | ||
"version": "6.2.4", | ||
"version": "7.0.0", | ||
"main": "lib/cot.js", | ||
@@ -27,0 +27,0 @@ "dependencies": { |
@@ -300,12 +300,10 @@ [CouchDB](http://couchdb.org/) library with a simple, functional-programing-friendly API. | ||
#### changes | ||
Queries the changes feed given the specified query. `query` may contain the following keys: | ||
* `filter`: filter function to use | ||
* `include_docs`: if true, results will contain entire document | ||
* `limit`: the maximum number of change rows this query should return | ||
* `since`: results will start immediately after the sequence number provided here | ||
* `longpoll`: if true, query will send feed=longpoll | ||
* `timeout`: timeout in milliseconds for logpoll queries | ||
Queries the changes given the specified [query parameters](https://docs.couchdb.org/en/latest/api/database/changes.html). | ||
See [CouchDB changes feed documentation](http://wiki.apache.org/couchdb/HTTP_database_API#Changes) | ||
```js | ||
const latestChanges = await db.changes({ descending: true, limit: 10 }) | ||
``` | ||
:warning: the `feed` mode is not supported as a feed can not be returned as a stream. To follow a change feed, see [`cloudant-follow`](https://github.com/cloudant-labs/cloudant-follow) | ||
#### listRevs | ||
@@ -363,21 +361,12 @@ | ||
``` | ||
Queries a view with the given name in the given design doc. `query` should be an object with any of the following keys: | ||
* descending | ||
* endkey | ||
* endkey_docid | ||
* group | ||
* group_level | ||
* include_docs | ||
* inclusive_end | ||
* key | ||
* limit | ||
* reduce | ||
* skip | ||
* stale | ||
* startkey | ||
* startkey_docid | ||
* update_seq | ||
Queries a view with the given name in the given design doc. `query` should be an object with any of the [query parameters](https://docs.couchdb.org/en/latest/api/ddoc/views.html) | ||
```js | ||
const { rows } = await db.view('someDesignDocName', 'someViewName', { | ||
keys: [ 'a', 'b', 'c' ], | ||
include_docs: true, | ||
limit: 5, | ||
skip: 1 | ||
}) | ||
``` | ||
For more information, refer to [Couchdb documentation](http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options) | ||
#### viewQuery | ||
@@ -384,0 +373,0 @@ #### viewKeysQuery |
34365
583
397