hide-a-bed 3.0.1 → 4.0.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/README.md +104 -0
- package/cjs/impl/bulk.cjs +55 -6
- package/cjs/impl/crud.cjs +57 -7
- package/cjs/impl/errors.cjs +63 -0
- package/cjs/impl/logger.cjs +61 -0
- package/cjs/impl/patch.cjs +10 -5
- package/cjs/impl/query.cjs +11 -1
- package/cjs/impl/retry.cjs +54 -0
- package/cjs/impl/stream.cjs +48 -7
- package/cjs/index.cjs +26 -2
- package/cjs/schema/bind.cjs +40 -0
- package/cjs/schema/bulk.cjs +14 -0
- package/cjs/schema/config.cjs +20 -1
- package/cjs/schema/crud.cjs +9 -1
- package/cjs/schema/patch.cjs +6 -6
- package/cjs/schema/query.cjs +28 -19
- package/cjs/schema/stream.cjs +42 -0
- package/impl/bulk.mjs +58 -6
- package/impl/crud.mjs +67 -6
- package/impl/errors.mjs +53 -0
- package/impl/logger.mjs +59 -0
- package/impl/patch.mjs +36 -4
- package/impl/query.mjs +38 -2
- package/impl/retry.mjs +39 -0
- package/impl/stream.mjs +72 -17
- package/index.mjs +34 -4
- package/package.json +1 -1
- package/schema/bind.mjs +21 -0
- package/schema/bulk.mjs +17 -0
- package/schema/config.mjs +22 -1
- package/schema/crud.mjs +12 -0
- package/schema/patch.mjs +10 -6
- package/schema/query.mjs +30 -18
- package/schema/stream.mjs +23 -0
- package/build/build.mjs +0 -16
- package/build/build.rewrite-imports.mjs +0 -14
- package/impl/bulk.d.mts +0 -7
- package/impl/bulk.d.mts.map +0 -1
- package/impl/crud.d.mts +0 -10
- package/impl/crud.d.mts.map +0 -1
- package/impl/patch.d.mts +0 -2
- package/impl/patch.d.mts.map +0 -1
- package/impl/query.d.mts +0 -104
- package/impl/query.d.mts.map +0 -1
- package/impl/stream.d.mts +0 -2
- package/impl/stream.d.mts.map +0 -1
- package/index.d.mts +0 -34
- package/index.d.mts.map +0 -1
- package/schema/bulk.d.mts +0 -100
- package/schema/bulk.d.mts.map +0 -1
- package/schema/config.d.mts +0 -9
- package/schema/config.d.mts.map +0 -1
- package/schema/crud.d.mts +0 -83
- package/schema/crud.d.mts.map +0 -1
- package/schema/patch.d.mts +0 -47
- package/schema/patch.d.mts.map +0 -1
- package/schema/query.d.mts +0 -151
- package/schema/query.d.mts.map +0 -1
package/impl/query.mjs
CHANGED
|
@@ -3,14 +3,21 @@
|
|
|
3
3
|
import { z } from 'zod' // eslint-disable-line
|
|
4
4
|
import needle from 'needle'
|
|
5
5
|
import { SimpleViewQuery, SimpleViewQueryResponse } from '../schema/query.mjs' // eslint-disable-line
|
|
6
|
+
import { RetryableError } from './errors.mjs'
|
|
7
|
+
import { createLogger } from './logger.mjs'
|
|
6
8
|
|
|
7
9
|
import pkg from 'lodash'
|
|
8
10
|
const { includes } = pkg
|
|
9
11
|
|
|
10
12
|
/** @type { z.infer<SimpleViewQuery> } query */
|
|
11
13
|
export const query = SimpleViewQuery.implement(async (config, view, options) => {
|
|
14
|
+
const logger = createLogger(config)
|
|
15
|
+
logger.info(`Starting view query: ${view}`)
|
|
16
|
+
logger.debug('Query options:', options)
|
|
17
|
+
|
|
12
18
|
// @ts-ignore
|
|
13
19
|
const qs = queryString(options, ['key', 'startkey', 'endkey', 'reduce', 'group', 'group_level', 'stale', 'limit'])
|
|
20
|
+
logger.debug('Generated query string:', qs)
|
|
14
21
|
|
|
15
22
|
const opts = {
|
|
16
23
|
json: true,
|
|
@@ -20,10 +27,35 @@ export const query = SimpleViewQuery.implement(async (config, view, options) =>
|
|
|
20
27
|
}
|
|
21
28
|
const url = `${config.couch}/${view}?${qs.toString()}`
|
|
22
29
|
// @ts-ignore
|
|
23
|
-
|
|
30
|
+
let results
|
|
31
|
+
try {
|
|
32
|
+
logger.debug(`Sending GET request to: ${url}`)
|
|
33
|
+
results = await needle('get', url, opts)
|
|
34
|
+
} catch (err) {
|
|
35
|
+
logger.error('Network error during query:', err)
|
|
36
|
+
RetryableError.handleNetworkError(err)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!results) {
|
|
40
|
+
logger.error('No response received from query request')
|
|
41
|
+
throw new RetryableError('no response', 503)
|
|
42
|
+
}
|
|
43
|
+
|
|
24
44
|
/** @type { z.infer<SimpleViewQueryResponse> } body */
|
|
25
45
|
const body = results.body
|
|
26
|
-
|
|
46
|
+
|
|
47
|
+
if (RetryableError.isRetryableStatusCode(results.statusCode)) {
|
|
48
|
+
logger.warn(`Retryable status code received: ${results.statusCode}`)
|
|
49
|
+
throw new RetryableError(body.error || 'retryable error during query', results.statusCode)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (body.error) {
|
|
53
|
+
logger.error(`Query error: ${body.error}`)
|
|
54
|
+
throw new Error(body.error)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
logger.info(`Successfully executed view query: ${view}`)
|
|
58
|
+
logger.debug('Query response:', body)
|
|
27
59
|
return body
|
|
28
60
|
})
|
|
29
61
|
|
|
@@ -32,6 +64,10 @@ export const query = SimpleViewQuery.implement(async (config, view, options) =>
|
|
|
32
64
|
* @param {string[]} params - The list of parameter names to include in the query string.
|
|
33
65
|
*/
|
|
34
66
|
export function queryString (options, params) {
|
|
67
|
+
const logger = createLogger({ useConsoleLogger: false })
|
|
68
|
+
logger.debug('Building query string with options:', options)
|
|
69
|
+
logger.debug('Allowed params:', params)
|
|
70
|
+
|
|
35
71
|
const parts = Object.keys(options).map(key => {
|
|
36
72
|
let value = options[key]
|
|
37
73
|
if (includes(params, key)) {
|
package/impl/retry.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { RetryableError } from './errors.mjs'
|
|
2
|
+
import { sleep } from './patch.mjs'
|
|
3
|
+
|
|
4
|
+
export function withRetry(fn, options = {}) {
|
|
5
|
+
const {
|
|
6
|
+
maxRetries = 3,
|
|
7
|
+
initialDelay = 1000, // 1 second
|
|
8
|
+
backoffFactor = 2 // exponential backoff multiplier
|
|
9
|
+
} = options
|
|
10
|
+
|
|
11
|
+
return async (...args) => {
|
|
12
|
+
let lastError
|
|
13
|
+
let delay = initialDelay
|
|
14
|
+
|
|
15
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
16
|
+
try {
|
|
17
|
+
return await fn(...args)
|
|
18
|
+
} catch (error) {
|
|
19
|
+
lastError = error
|
|
20
|
+
|
|
21
|
+
// Only retry if it's a RetryableError
|
|
22
|
+
if (!(error instanceof RetryableError)) {
|
|
23
|
+
throw error
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// If we've used all retries, throw the last error
|
|
27
|
+
if (attempt === maxRetries) {
|
|
28
|
+
throw error
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Wait with exponential backoff
|
|
32
|
+
await sleep(delay)
|
|
33
|
+
delay *= backoffFactor
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
throw lastError
|
|
38
|
+
}
|
|
39
|
+
}
|
package/impl/stream.mjs
CHANGED
|
@@ -1,29 +1,84 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
|
|
3
2
|
import needle from 'needle'
|
|
4
3
|
import { queryString } from './query.mjs'
|
|
4
|
+
import { RetryableError } from './errors.mjs'
|
|
5
|
+
import { createLogger } from './logger.mjs'
|
|
5
6
|
// @ts-ignore
|
|
6
7
|
import JSONStream from 'JSONStream'
|
|
7
8
|
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export const queryStream = (config, view, options) => new Promise((resolve, reject) => {
|
|
9
|
+
/** @type { import('../schema/stream.mjs').SimpleViewQueryStreamSchema } */
|
|
10
|
+
export const queryStream = (config, view, options, onRow) => new Promise((resolve, reject) => {
|
|
11
|
+
const logger = createLogger(config)
|
|
12
|
+
logger.info(`Starting stream query for view: ${view}`)
|
|
13
|
+
|
|
14
14
|
if (!options) options = {}
|
|
15
|
+
logger.debug('Stream query options:', options)
|
|
15
16
|
|
|
16
|
-
const
|
|
17
|
-
|
|
17
|
+
const qs = queryString(options, ['key', 'startkey', 'endkey', 'reduce', 'group', 'group_level', 'stale', 'limit'])
|
|
18
|
+
logger.debug('Generated query string:', qs)
|
|
18
19
|
const url = `${config.couch}/${view}?${qs.toString()}`
|
|
20
|
+
const opts = {
|
|
21
|
+
json: true,
|
|
22
|
+
headers: {
|
|
23
|
+
'Content-Type': 'application/json'
|
|
24
|
+
},
|
|
25
|
+
parse_response: false // Keep as stream
|
|
26
|
+
}
|
|
27
|
+
|
|
19
28
|
const streamer = JSONStream.parse('rows.*')
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
|
|
30
|
+
let rowCount = 0
|
|
31
|
+
streamer.on('data', row => {
|
|
32
|
+
rowCount++
|
|
33
|
+
onRow(row)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
streamer.on('error', /** @param {Error} err */ err => {
|
|
37
|
+
logger.error('Stream parsing error:', err)
|
|
38
|
+
reject(new Error(`Stream parsing error: ${err.message}`))
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
streamer.on('done', /** @param {Error|null} err */ err => {
|
|
42
|
+
if (err) {
|
|
43
|
+
logger.error('Stream done with error:', err)
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
RetryableError.handleNetworkError(err)
|
|
47
|
+
} catch (e) {
|
|
48
|
+
logger.error('Retryable error in stream:', e)
|
|
49
|
+
reject(e)
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
streamer.on('end', () => {
|
|
54
|
+
logger.info(`Stream completed successfully, processed ${rowCount} rows`)
|
|
55
|
+
resolve(undefined) // all work should be done in the stream
|
|
27
56
|
})
|
|
28
|
-
|
|
57
|
+
|
|
58
|
+
const req = needle.get(url, opts)
|
|
59
|
+
|
|
60
|
+
req.on('response', response => {
|
|
61
|
+
logger.debug(`Received response with status code: ${response.statusCode}`)
|
|
62
|
+
if (RetryableError.isRetryableStatusCode(response.statusCode)) {
|
|
63
|
+
logger.warn(`Retryable status code received: ${response.statusCode}`)
|
|
64
|
+
reject(new RetryableError('retryable error during stream query', response.statusCode))
|
|
65
|
+
// req.abort()
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
req.on('error', err => {
|
|
71
|
+
logger.error('Request error:', err)
|
|
72
|
+
try {
|
|
73
|
+
RetryableError.handleNetworkError(err)
|
|
74
|
+
} catch (retryErr) {
|
|
75
|
+
logger.error('Retryable error in request:', retryErr)
|
|
76
|
+
reject(retryErr)
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
reject(err)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
req.pipe(streamer)
|
|
83
|
+
|
|
29
84
|
})
|
package/index.mjs
CHANGED
|
@@ -1,26 +1,56 @@
|
|
|
1
|
+
// @ts-check */
|
|
1
2
|
import { bulkGet, bulkSave, bulkRemove } from './impl/bulk.mjs'
|
|
2
3
|
import { get, put } from './impl/crud.mjs'
|
|
3
4
|
import { patch } from './impl/patch.mjs'
|
|
4
5
|
import { query } from './impl/query.mjs'
|
|
5
6
|
import { queryStream } from './impl/stream.mjs'
|
|
6
|
-
import {
|
|
7
|
+
import { withRetry } from './impl/retry.mjs'
|
|
8
|
+
import { BulkSave, BulkGet, BulkRemove } from './schema/bulk.mjs'
|
|
7
9
|
import { CouchConfig } from './schema/config.mjs'
|
|
8
10
|
import { SimpleViewQuery, SimpleViewQueryResponse } from './schema/query.mjs'
|
|
9
|
-
import {
|
|
11
|
+
import { SimpleViewQueryStream, OnRow } from './schema/stream.mjs'
|
|
12
|
+
import { Patch } from './schema/patch.mjs'
|
|
10
13
|
import { CouchDoc, CouchDocResponse, CouchPut, CouchGet } from './schema/crud.mjs'
|
|
14
|
+
import { Bind } from './schema/bind.mjs'
|
|
11
15
|
|
|
12
16
|
const schema = {
|
|
13
17
|
CouchConfig,
|
|
14
18
|
SimpleViewQuery,
|
|
15
19
|
SimpleViewQueryResponse,
|
|
20
|
+
SimpleViewQueryStream,
|
|
21
|
+
OnRow,
|
|
16
22
|
BulkSave,
|
|
17
23
|
BulkGet,
|
|
24
|
+
BulkRemove,
|
|
18
25
|
CouchGet,
|
|
19
26
|
CouchPut,
|
|
20
27
|
CouchDoc,
|
|
21
28
|
CouchDocResponse,
|
|
22
|
-
PatchConfig,
|
|
23
29
|
Patch
|
|
24
30
|
}
|
|
25
31
|
|
|
26
|
-
|
|
32
|
+
/** @type { import('./schema/bind.mjs').BindSchema } */
|
|
33
|
+
const bindConfig = Bind.implement((
|
|
34
|
+
/** @type { import('./schema/config.mjs').CouchConfigSchema } */
|
|
35
|
+
config
|
|
36
|
+
) => {
|
|
37
|
+
// Default retry options
|
|
38
|
+
const retryOptions = {
|
|
39
|
+
maxRetries: config.maxRetries ?? 10,
|
|
40
|
+
initialDelay: config.initialDelay ?? 1000,
|
|
41
|
+
backoffFactor: config.backoffFactor ?? 2
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
get: config.bindWithRetry ? withRetry(get.bind(null, config), retryOptions) : get.bind(null, config),
|
|
46
|
+
put: config.bindWithRetry ? withRetry(put.bind(null, config), retryOptions) : put.bind(null, config),
|
|
47
|
+
patch: patch.bind(null, config), // patch not included in retry
|
|
48
|
+
bulkGet: config.bindWithRetry ? withRetry(bulkGet.bind(null, config), retryOptions) : bulkGet.bind(null, config),
|
|
49
|
+
bulkSave: config.bindWithRetry ? withRetry(bulkSave.bind(null, config), retryOptions) : bulkSave.bind(null, config),
|
|
50
|
+
bulkRemove: config.bindWithRetry ? withRetry(bulkRemove.bind(null, config), retryOptions) : bulkRemove.bind(null, config),
|
|
51
|
+
query: config.bindWithRetry ? withRetry(query.bind(null, config), retryOptions) : query.bind(null, config),
|
|
52
|
+
queryStream: config.bindWithRetry? withRetry(queryStream.bind(null, config), retryOptions) : queryStream.bind(null, config)
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
export { get, put, patch, bulkGet, bulkSave, bulkRemove, query, queryStream, schema, bindConfig, withRetry }
|
package/package.json
CHANGED
package/schema/bind.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
import { CouchConfig } from './config.mjs'
|
|
4
|
+
import { BulkSaveBound, BulkGetBound } from './bulk.mjs'
|
|
5
|
+
import { CouchGetBound, CouchPutBound } from './crud.mjs'
|
|
6
|
+
import { PatchBound } from './patch.mjs'
|
|
7
|
+
import { SimpleViewQueryBound } from './query.mjs'
|
|
8
|
+
import { SimpleViewQueryStreamBound } from './stream.mjs'
|
|
9
|
+
|
|
10
|
+
const BindReturns = z.object({
|
|
11
|
+
bulkGet: BulkGetBound,
|
|
12
|
+
bulkSave: BulkSaveBound,
|
|
13
|
+
get: CouchGetBound,
|
|
14
|
+
put: CouchPutBound,
|
|
15
|
+
patch: PatchBound,
|
|
16
|
+
query: SimpleViewQueryBound,
|
|
17
|
+
queryStream: SimpleViewQueryStreamBound
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
export const Bind = z.function().args(CouchConfig).returns(BindReturns)
|
|
21
|
+
/** @typedef { z.infer<typeof Bind> } BindSchema */
|
package/schema/bulk.mjs
CHANGED
|
@@ -19,14 +19,31 @@ export const BulkSave = z.function().args(
|
|
|
19
19
|
).returns(z.promise(BulkSaveResponseSchema))
|
|
20
20
|
/** @typedef { z.infer<typeof BulkSave> } BulkSaveSchema */
|
|
21
21
|
|
|
22
|
+
export const BulkSaveBound = z.function().args(
|
|
23
|
+
z.array(z.object({
|
|
24
|
+
_id: z.string()
|
|
25
|
+
}).passthrough())
|
|
26
|
+
).returns(z.promise(BulkSaveResponseSchema))
|
|
27
|
+
/** @typedef { z.infer<typeof BulkSaveBound> } BulkSaveBoundSchema */
|
|
28
|
+
|
|
22
29
|
export const BulkGet = z.function().args(
|
|
23
30
|
CouchConfig,
|
|
24
31
|
z.array(z.string().describe('the ids to get'))
|
|
25
32
|
).returns(z.promise(z.array(CouchDoc)))
|
|
26
33
|
/** @typedef { z.infer<typeof BulkGet> } BulkGetSchema */
|
|
27
34
|
|
|
35
|
+
export const BulkGetBound = z.function().args(
|
|
36
|
+
z.array(z.string().describe('the ids to get'))
|
|
37
|
+
).returns(z.promise(z.array(CouchDoc)))
|
|
38
|
+
/** @typedef { z.infer<typeof BulkGetBound> } BulkGetBoundSchema */
|
|
39
|
+
|
|
28
40
|
export const BulkRemove = z.function().args(
|
|
29
41
|
CouchConfig,
|
|
30
42
|
z.array(z.string().describe('the ids to delete'))
|
|
31
43
|
).returns(z.promise(BulkSaveResponseSchema))
|
|
32
44
|
/** @typedef { z.infer<typeof BulkRemove> } BulkRemoveSchema */
|
|
45
|
+
|
|
46
|
+
export const BulkRemoveBound = z.function().args(
|
|
47
|
+
z.array(z.string().describe('the ids to delete'))
|
|
48
|
+
).returns(z.promise(BulkSaveResponseSchema))
|
|
49
|
+
/** @typedef { z.infer<typeof BulkRemoveBound> } BulkRemoveBoundSchema */
|
package/schema/config.mjs
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
const LoggerSchema = z.object({
|
|
4
|
+
error: z.function().args(z.any()).returns(z.void()).optional(),
|
|
5
|
+
warn: z.function().args(z.any()).returns(z.void()).optional(),
|
|
6
|
+
info: z.function().args(z.any()).returns(z.void()).optional(),
|
|
7
|
+
debug: z.function().args(z.any()).returns(z.void()).optional()
|
|
8
|
+
}).or(z.function().args(
|
|
9
|
+
z.string(), // level
|
|
10
|
+
z.any() // message/args
|
|
11
|
+
).returns(z.void()))
|
|
12
|
+
|
|
2
13
|
export const CouchConfig = z.object({
|
|
3
|
-
|
|
14
|
+
throwOnGetNotFound: z.boolean().optional().default(false).describe('if a get is 404 should we throw or return undefined'),
|
|
15
|
+
couch: z.string().describe('the url of the couch db'),
|
|
16
|
+
bindWithRetry: z.boolean().optional().default(true).describe('should we bind with retry'),
|
|
17
|
+
maxRetries: z.number().optional().default(3).describe('maximum number of retry attempts'),
|
|
18
|
+
initialDelay: z.number().optional().default(1000).describe('initial retry delay in milliseconds'),
|
|
19
|
+
backoffFactor: z.number().optional().default(2).describe('multiplier for exponential backoff'),
|
|
20
|
+
useConsoleLogger: z.boolean().optional().default(false).describe('turn on console as a fallback logger'),
|
|
21
|
+
logger: LoggerSchema.optional().describe('logging interface supporting winston-like or simple function interface'),
|
|
22
|
+
_normalizedLogger: z.any().optional() // Internal property for caching normalized logger
|
|
4
23
|
}).passthrough().describe('The std config object')
|
|
24
|
+
|
|
25
|
+
/** @typedef { z.infer<typeof CouchConfig> } CouchConfigSchema*/
|
package/schema/crud.mjs
CHANGED
|
@@ -19,8 +19,20 @@ export const CouchPut = z.function().args(
|
|
|
19
19
|
CouchConfig,
|
|
20
20
|
CouchDoc
|
|
21
21
|
).returns(z.promise(CouchDocResponse))
|
|
22
|
+
/** @typedef { z.infer<typeof CouchPut> } CouchPutSchema */
|
|
23
|
+
|
|
24
|
+
export const CouchPutBound = z.function().args(
|
|
25
|
+
CouchDoc
|
|
26
|
+
).returns(z.promise(CouchDocResponse))
|
|
27
|
+
/** @typedef { z.infer<typeof CouchPutBound> } CouchPutBoundSchema */
|
|
22
28
|
|
|
23
29
|
export const CouchGet = z.function().args(
|
|
24
30
|
CouchConfig,
|
|
25
31
|
z.string().describe('the couch doc id')
|
|
26
32
|
).returns(z.promise(CouchDoc.nullable()))
|
|
33
|
+
/** @typedef { z.infer<typeof CouchGet> } CouchGetSchema */
|
|
34
|
+
|
|
35
|
+
export const CouchGetBound = z.function().args(
|
|
36
|
+
z.string().describe('the couch doc id')
|
|
37
|
+
).returns(z.promise(CouchDoc.nullable()))
|
|
38
|
+
/** @typedef { z.infer<typeof CouchGetBound> } CouchBoundSchema */
|
package/schema/patch.mjs
CHANGED
|
@@ -2,17 +2,21 @@ import { z } from 'zod'
|
|
|
2
2
|
import { CouchConfig } from './config.mjs'
|
|
3
3
|
import { CouchDocResponse } from './crud.mjs'
|
|
4
4
|
|
|
5
|
-
export const PatchConfig = CouchConfig.extend({
|
|
6
|
-
retries: z.number().min(0).max(100).optional(),
|
|
7
|
-
delay: z.number().min(0).optional()
|
|
8
|
-
})
|
|
9
|
-
|
|
10
5
|
export const PatchProperties = z.record(z.string(), z.any())
|
|
11
6
|
|
|
12
7
|
export const Patch = z.function()
|
|
13
8
|
.args(
|
|
14
|
-
|
|
9
|
+
CouchConfig,
|
|
10
|
+
z.string().describe('the couch doc id'),
|
|
11
|
+
PatchProperties
|
|
12
|
+
)
|
|
13
|
+
.returns(z.promise(CouchDocResponse))
|
|
14
|
+
/** @typedef { z.infer<typeof Patch> } PatchSchema */
|
|
15
|
+
|
|
16
|
+
export const PatchBound = z.function()
|
|
17
|
+
.args(
|
|
15
18
|
z.string().describe('the couch doc id'),
|
|
16
19
|
PatchProperties
|
|
17
20
|
)
|
|
18
21
|
.returns(z.promise(CouchDocResponse))
|
|
22
|
+
/** @typedef { z.infer<typeof PatchBound> } PatchBoundSchema */
|
package/schema/query.mjs
CHANGED
|
@@ -1,29 +1,41 @@
|
|
|
1
1
|
import { z } from 'zod'
|
|
2
2
|
import { CouchConfig } from './config.mjs'
|
|
3
3
|
|
|
4
|
+
export const ViewRow = z.object({
|
|
5
|
+
id: z.string().optional(),
|
|
6
|
+
key: z.any().nullable(),
|
|
7
|
+
value: z.any().nullable(),
|
|
8
|
+
doc: z.object({}).passthrough().optional()
|
|
9
|
+
})
|
|
4
10
|
export const SimpleViewQueryResponse = z.object({
|
|
5
11
|
error: z.string().optional().describe('if something is wrong'),
|
|
6
|
-
rows: z.array(
|
|
7
|
-
id: z.string().optional(),
|
|
8
|
-
key: z.any().nullable(),
|
|
9
|
-
value: z.any().nullable(),
|
|
10
|
-
doc: z.object({}).passthrough().optional()
|
|
11
|
-
}))
|
|
12
|
+
rows: z.array(ViewRow),
|
|
12
13
|
}).passthrough()
|
|
14
|
+
/** @typedef { z.infer<typeof SimpleViewQueryResponse> } SimpleViewQueryResponseSchema */
|
|
15
|
+
|
|
16
|
+
export const SimpleViewOptions = z.object({
|
|
17
|
+
startkey: z.any().optional(),
|
|
18
|
+
endkey: z.any().optional(),
|
|
19
|
+
descending: z.boolean().optional().describe('sort results descending'),
|
|
20
|
+
skip: z.number().positive().optional().describe('skip this many rows'),
|
|
21
|
+
limit: z.number().positive().optional().describe('limit the results to this many rows'),
|
|
22
|
+
key: z.any().optional(),
|
|
23
|
+
include_docs: z.boolean().optional().describe('join the id to the doc and return it'),
|
|
24
|
+
reduce: z.boolean().optional().describe('reduce the results'),
|
|
25
|
+
group: z.boolean().optional().describe('group the results'),
|
|
26
|
+
group_level: z.number().positive().optional().describe('group the results at this level')
|
|
27
|
+
}).optional().describe('query options')
|
|
28
|
+
/** @typedef { z.infer<typeof SimpleViewOptions> } SimpleViewOptionsSchema */
|
|
13
29
|
|
|
14
30
|
export const SimpleViewQuery = z.function().args(
|
|
15
31
|
CouchConfig,
|
|
16
32
|
z.string().describe('the view name'),
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
include_docs: z.boolean().optional().describe('join the id to the doc and return it'),
|
|
25
|
-
reduce: z.boolean().optional().describe('reduce the results'),
|
|
26
|
-
group: z.boolean().optional().describe('group the results'),
|
|
27
|
-
group_level: z.number().positive().optional().describe('group the results at this level')
|
|
28
|
-
}).optional().describe('query options')
|
|
33
|
+
SimpleViewOptions
|
|
34
|
+
).returns(z.promise(SimpleViewQueryResponse))
|
|
35
|
+
/** @typedef { z.infer<typeof SimpleViewQuery> } SimpleViewQuerySchema */
|
|
36
|
+
|
|
37
|
+
export const SimpleViewQueryBound = z.function().args(
|
|
38
|
+
z.string().describe('the view name'),
|
|
39
|
+
SimpleViewOptions
|
|
29
40
|
).returns(z.promise(SimpleViewQueryResponse))
|
|
41
|
+
/** @typedef { z.infer<typeof SimpleViewQueryBound> } SimpleViewQueryBoundSchema */
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import { CouchConfig } from './config.mjs'
|
|
3
|
+
import { SimpleViewOptions, ViewRow } from './query.mjs'
|
|
4
|
+
|
|
5
|
+
export const OnRow = z.function().args(
|
|
6
|
+
ViewRow
|
|
7
|
+
)
|
|
8
|
+
/** @typedef { z.infer<typeof OnRow> } OnRowSchema */
|
|
9
|
+
|
|
10
|
+
export const SimpleViewQueryStream = z.function().args(
|
|
11
|
+
CouchConfig,
|
|
12
|
+
z.string().describe('the view name'),
|
|
13
|
+
SimpleViewOptions,
|
|
14
|
+
OnRow
|
|
15
|
+
).returns(z.promise(z.undefined()))
|
|
16
|
+
/** @typedef { z.infer<typeof SimpleViewQueryStream> } SimpleViewQueryStreamSchema */
|
|
17
|
+
|
|
18
|
+
export const SimpleViewQueryStreamBound = z.function().args(
|
|
19
|
+
z.string().describe('the view name'),
|
|
20
|
+
SimpleViewOptions,
|
|
21
|
+
OnRow
|
|
22
|
+
).returns(z.promise(z.undefined()))
|
|
23
|
+
/** @typedef { z.infer<typeof SimpleViewQueryStreamBound> } SimpleViewQueryStreamBoundSchema */
|
package/build/build.mjs
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import esbuild from 'esbuild'
|
|
2
|
-
import { globSync } from 'glob'
|
|
3
|
-
import { RewriteImportsPlugin } from './build.rewrite-imports.mjs'
|
|
4
|
-
|
|
5
|
-
esbuild
|
|
6
|
-
.build({
|
|
7
|
-
entryPoints: globSync('./**/*.mjs', {
|
|
8
|
-
ignore: ['./node_modules/**/*', './tests/**/*', './build/**/*']
|
|
9
|
-
}),
|
|
10
|
-
outdir: 'cjs',
|
|
11
|
-
format: 'cjs',
|
|
12
|
-
outExtension: { '.js': '.cjs' },
|
|
13
|
-
bundle: false,
|
|
14
|
-
plugins: [new RewriteImportsPlugin()]
|
|
15
|
-
})
|
|
16
|
-
.catch(() => process.exit(1))
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
|
|
3
|
-
export class RewriteImportsPlugin {
|
|
4
|
-
name = 'rewrite-imports'
|
|
5
|
-
|
|
6
|
-
setup (build) {
|
|
7
|
-
build.onLoad({ filter: /\.mjs$/ }, async (args) => {
|
|
8
|
-
const contents = fs
|
|
9
|
-
.readFileSync(args.path, 'utf8')
|
|
10
|
-
.replace(/(from\s+['"].*?)\.mjs(['"])/g, '$1.cjs$2')
|
|
11
|
-
return { contents, loader: 'js' }
|
|
12
|
-
})
|
|
13
|
-
}
|
|
14
|
-
}
|
package/impl/bulk.d.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/** @type { import('../schema/bulk.mjs').BulkSaveSchema } */
|
|
2
|
-
export const bulkSave: import("../schema/bulk.mjs").BulkSaveSchema;
|
|
3
|
-
/** @type { import('../schema/bulk.mjs').BulkGetSchema } */
|
|
4
|
-
export const bulkGet: import("../schema/bulk.mjs").BulkGetSchema;
|
|
5
|
-
/** @type { import('../schema/bulk.mjs').BulkRemoveSchema } */
|
|
6
|
-
export const bulkRemove: import("../schema/bulk.mjs").BulkRemoveSchema;
|
|
7
|
-
//# sourceMappingURL=bulk.d.mts.map
|
package/impl/bulk.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bulk.d.mts","sourceRoot":"","sources":["bulk.mjs"],"names":[],"mappings":"AAWA,4DAA4D;AAC5D,uBADY,OAAO,oBAAoB,EAAE,cAAc,CAWrD;AAEF,2DAA2D;AAC3D,sBADY,OAAO,oBAAoB,EAAE,aAAa,CAqBpD;AAEF,8DAA8D;AAC9D,yBADY,OAAO,oBAAoB,EAAE,gBAAgB,CAKvD"}
|
package/impl/crud.d.mts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export const get: (args_0: import("zod").objectInputType<{
|
|
2
|
-
couch: import("zod").ZodString;
|
|
3
|
-
}, import("zod").ZodTypeAny, "passthrough">, args_1: string, ...args: unknown[]) => Promise<any>;
|
|
4
|
-
export const put: (args_0: import("zod").objectInputType<{
|
|
5
|
-
couch: import("zod").ZodString;
|
|
6
|
-
}, import("zod").ZodTypeAny, "passthrough">, args_1: import("zod").objectInputType<{
|
|
7
|
-
_id: import("zod").ZodString;
|
|
8
|
-
_rev: import("zod").ZodOptional<import("zod").ZodString>;
|
|
9
|
-
}, import("zod").ZodTypeAny, "passthrough">, ...args: unknown[]) => Promise<any>;
|
|
10
|
-
//# sourceMappingURL=crud.d.mts.map
|
package/impl/crud.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"crud.d.mts","sourceRoot":"","sources":["crud.mjs"],"names":[],"mappings":"AAWA;;iGAOE;AAEF;;;;;iFAWE"}
|
package/impl/patch.d.mts
DELETED
package/impl/patch.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"patch.d.mts","sourceRoot":"","sources":["patch.mjs"],"names":[],"mappings":"AAKA,wBA8BE"}
|