hide-a-bed 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/impl/query.mjs +6 -3
- package/impl/stream.mjs +20 -0
- package/index.mjs +5 -4
- package/package.json +3 -1
package/impl/query.mjs
CHANGED
|
@@ -10,7 +10,7 @@ const { includes } = pkg
|
|
|
10
10
|
/** @type { z.infer<SimpleViewQuery> } query */
|
|
11
11
|
export const query = SimpleViewQuery.implement(async (config, view, options) => {
|
|
12
12
|
// @ts-ignore
|
|
13
|
-
const qs = queryString(options, ['key', 'startkey', 'endkey', 'reduce', 'group', 'group_level'])
|
|
13
|
+
const qs = queryString(options, ['key', 'startkey', 'endkey', 'reduce', 'group', 'group_level', 'stale', 'limit'])
|
|
14
14
|
|
|
15
15
|
const opts = {
|
|
16
16
|
json: true,
|
|
@@ -27,14 +27,17 @@ export const query = SimpleViewQuery.implement(async (config, view, options) =>
|
|
|
27
27
|
return body
|
|
28
28
|
})
|
|
29
29
|
|
|
30
|
-
function queryString (options, params) {
|
|
30
|
+
export function queryString (options, params) {
|
|
31
31
|
const parts = Object.keys(options).map(key => {
|
|
32
32
|
let value = options[key]
|
|
33
33
|
if (includes(params, key)) {
|
|
34
|
-
if (typeof value === 'string') value = `"${value}"`
|
|
34
|
+
if (typeof value === 'string' && key !== 'stale') value = `"${value}"`
|
|
35
35
|
if (Array.isArray(value)) {
|
|
36
36
|
value = '[' + value.map(i => {
|
|
37
|
+
if (i === null) return 'null'
|
|
37
38
|
if (typeof i === 'string') return `"${i}"`
|
|
39
|
+
if (typeof i === 'object' && Object.keys(i).length === 0) return '{}'
|
|
40
|
+
if (typeof i === 'object') return JSON.stringify(i)
|
|
38
41
|
return i
|
|
39
42
|
}).join(',') + ']'
|
|
40
43
|
}
|
package/impl/stream.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import needle from 'needle'
|
|
4
|
+
import { queryString } from './query.mjs'
|
|
5
|
+
import JSONStream from 'JSONStream'
|
|
6
|
+
|
|
7
|
+
export const queryStream = (config, view, options) => new Promise((resolve, reject) => {
|
|
8
|
+
if (!options) options = {}
|
|
9
|
+
|
|
10
|
+
const { onRow, ...rest } = options
|
|
11
|
+
const qs = queryString(rest, ['key', 'startkey', 'endkey', 'reduce', 'group', 'group_level', 'stale', 'limit'])
|
|
12
|
+
const url = `${config.couch}/${view}?${qs.toString()}`
|
|
13
|
+
const streamer = JSONStream.parse('rows.*')
|
|
14
|
+
streamer.on('data', onRow)
|
|
15
|
+
streamer.on('end', (err) => {
|
|
16
|
+
if (err) return reject(err)
|
|
17
|
+
resolve(null) // all work should be done in the stream
|
|
18
|
+
})
|
|
19
|
+
needle.get(url).pipe(streamer)
|
|
20
|
+
})
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { bulkGet, bulkSave, bulkRemove } from './impl/bulk'
|
|
2
|
-
import { get, put } from './impl/crud'
|
|
3
|
-
import { query } from './impl/query'
|
|
1
|
+
import { bulkGet, bulkSave, bulkRemove } from './impl/bulk.mjs'
|
|
2
|
+
import { get, put } from './impl/crud.mjs'
|
|
3
|
+
import { query } from './impl/query.mjs'
|
|
4
|
+
import { queryStream } from './impl/stream.mjs'
|
|
4
5
|
|
|
5
|
-
export { bulkGet, bulkSave, bulkRemove,
|
|
6
|
+
export { get, put, bulkGet, bulkSave, bulkRemove, query, queryStream }
|
|
6
7
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hide-a-bed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "An abstraction over couchdb calls that includes easy mock/stubs with pouchdb",
|
|
5
5
|
"main": "index.mjs",
|
|
6
6
|
"scripts": {
|
|
@@ -21,6 +21,8 @@
|
|
|
21
21
|
},
|
|
22
22
|
"homepage": "https://github.com/ryanramage/hide-a-bed#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
+
"JSONStream": "^1.3.5",
|
|
25
|
+
"lodash": "^4.17.21",
|
|
24
26
|
"needle": "^3.2.0",
|
|
25
27
|
"zod": "^3.22.4"
|
|
26
28
|
}
|