hide-a-bed 5.2.8 → 6.0.0-beta.0
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/.prettierrc +7 -0
- package/README.md +270 -218
- package/dist/cjs/index.cjs +1952 -0
- package/dist/esm/index.mjs +1898 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +113 -0
- package/docs/assets/icons.js +18 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +60 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1633 -0
- package/docs/classes/QueryBuilder.html +42 -0
- package/docs/functions/bindConfig.html +4 -0
- package/docs/functions/bulkGet.html +14 -0
- package/docs/functions/bulkGetDictionary.html +10 -0
- package/docs/functions/bulkRemove.html +12 -0
- package/docs/functions/bulkRemoveMap.html +11 -0
- package/docs/functions/bulkSave.html +10 -0
- package/docs/functions/bulkSaveTransaction.html +23 -0
- package/docs/functions/createLock.html +7 -0
- package/docs/functions/createQuery.html +1 -0
- package/docs/functions/get.html +1 -0
- package/docs/functions/getAtRev.html +1 -0
- package/docs/functions/getDBInfo.html +10 -0
- package/docs/functions/patch.html +8 -0
- package/docs/functions/patchDangerously.html +9 -0
- package/docs/functions/put.html +1 -0
- package/docs/functions/query.html +15 -0
- package/docs/functions/queryStream.html +6 -0
- package/docs/functions/remove.html +1 -0
- package/docs/functions/removeLock.html +6 -0
- package/docs/functions/watchDocs.html +9 -0
- package/docs/functions/withRetry.html +6 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +483 -0
- package/docs/interfaces/NetworkError.html +6 -0
- package/docs/interfaces/NotFoundError.html +10 -0
- package/docs/interfaces/RetryOptions.html +10 -0
- package/docs/interfaces/RetryableError.html +10 -0
- package/docs/interfaces/StandardSchemaV1.FailureResult.html +4 -0
- package/docs/interfaces/StandardSchemaV1.Issue.html +6 -0
- package/docs/interfaces/StandardSchemaV1.Options.html +3 -0
- package/docs/interfaces/StandardSchemaV1.PathSegment.html +4 -0
- package/docs/interfaces/StandardSchemaV1.Props.html +10 -0
- package/docs/interfaces/StandardSchemaV1.SuccessResult.html +6 -0
- package/docs/interfaces/StandardSchemaV1.Types.html +6 -0
- package/docs/interfaces/StandardSchemaV1.html +4 -0
- package/docs/modules/StandardSchemaV1.html +1 -0
- package/docs/modules.html +1 -0
- package/docs/types/BoundInstance.html +1 -0
- package/docs/types/BulkGetBound.html +2 -0
- package/docs/types/BulkGetDictionaryBound.html +1 -0
- package/docs/types/BulkGetDictionaryOptions.html +2 -0
- package/docs/types/BulkGetDictionaryResult.html +3 -0
- package/docs/types/BulkGetOptions.html +3 -0
- package/docs/types/BulkGetResponse.html +1 -0
- package/docs/types/CouchConfig-1.html +1 -0
- package/docs/types/CouchConfig.html +1 -0
- package/docs/types/CouchConfigInput.html +1 -0
- package/docs/types/CouchDoc-1.html +1 -0
- package/docs/types/CouchDoc.html +2 -0
- package/docs/types/CouchDocInput.html +2 -0
- package/docs/types/GetAtRevBound.html +1 -0
- package/docs/types/GetBound.html +1 -0
- package/docs/types/GetOptions.html +2 -0
- package/docs/types/LockDoc-1.html +1 -0
- package/docs/types/LockDoc.html +1 -0
- package/docs/types/LockOptions-1.html +1 -0
- package/docs/types/LockOptions.html +1 -0
- package/docs/types/LockOptionsInput.html +1 -0
- package/docs/types/OnInvalidDocAction.html +1 -0
- package/docs/types/OnRow.html +1 -0
- package/docs/types/QueryBound.html +1 -0
- package/docs/types/SimpleViewOptions-1.html +1 -0
- package/docs/types/SimpleViewOptions.html +1 -0
- package/docs/types/StandardSchemaV1.InferInput.html +2 -0
- package/docs/types/StandardSchemaV1.InferOutput.html +2 -0
- package/docs/types/StandardSchemaV1.Result.html +2 -0
- package/docs/types/ViewQueryResponse-1.html +1 -0
- package/docs/types/ViewQueryResponse.html +2 -0
- package/docs/types/ViewQueryResponseValidated.html +2 -0
- package/docs/types/ViewRow-1.html +1 -0
- package/docs/types/ViewRow.html +2 -0
- package/docs/types/ViewRowValidated.html +7 -0
- package/docs/types/ViewString.html +1 -0
- package/docs/types/WatchOptionsInput.html +1 -0
- package/docs/types/WatchOptionsSchema-1.html +1 -0
- package/docs/types/WatchOptionsSchema.html +1 -0
- package/eslint.config.js +15 -0
- package/impl/bindConfig.mts +140 -0
- package/impl/bulkGet.mts +256 -0
- package/impl/bulkGet.test.mts +159 -0
- package/impl/bulkRemove.mts +98 -0
- package/impl/bulkRemove.test.mts +102 -0
- package/impl/bulkSave.mts +286 -0
- package/impl/bulkSave.test.mts +319 -0
- package/impl/get.mts +137 -0
- package/impl/get.test.mts +114 -0
- package/impl/getDBInfo.mts +67 -0
- package/impl/getDBInfo.test.mts +62 -0
- package/impl/patch.mts +134 -0
- package/impl/patch.test.mts +142 -0
- package/impl/put.mts +56 -0
- package/impl/put.test.mts +114 -0
- package/impl/query.mts +224 -0
- package/impl/query.test.mts +280 -0
- package/impl/remove.mts +65 -0
- package/impl/remove.test.mts +82 -0
- package/impl/retry.mts +66 -0
- package/impl/retry.test.mts +77 -0
- package/impl/stream.mts +143 -0
- package/impl/stream.test.mts +205 -0
- package/impl/sugar/lock.mts +103 -0
- package/impl/sugar/lock.test.mts +113 -0
- package/impl/sugar/{watch.mjs → watch.mts} +56 -22
- package/impl/sugar/watch.test.mts +155 -0
- package/impl/utils/errors.mts +130 -0
- package/impl/utils/errors.test.mts +58 -0
- package/impl/utils/logger.mts +62 -0
- package/impl/utils/logger.test.mts +129 -0
- package/impl/utils/mergeNeedleOpts.mts +16 -0
- package/impl/utils/parseRows.mts +117 -0
- package/impl/utils/parseRows.test.mts +183 -0
- package/impl/utils/queryBuilder.mts +173 -0
- package/impl/utils/queryBuilder.test.mts +83 -0
- package/impl/utils/queryString.mts +44 -0
- package/impl/utils/queryString.test.mts +53 -0
- package/impl/{trackedEmitter.mjs → utils/trackedEmitter.mts} +9 -7
- package/impl/utils/transactionErrors.mts +71 -0
- package/index.mts +82 -0
- package/index.test.mts +415 -0
- package/package.json +46 -32
- package/schema/config.mts +81 -0
- package/schema/couch/couch.input.schema.ts +43 -0
- package/schema/couch/couch.output.schema.ts +169 -0
- package/schema/sugar/lock.mts +18 -0
- package/schema/sugar/watch.mts +14 -0
- package/schema/util.mts +8 -0
- package/tsconfig.json +10 -4
- package/tsdown.config.ts +16 -0
- package/typedoc.json +4 -0
- package/types/output/eslint.config.d.ts +3 -0
- package/types/output/eslint.config.d.ts.map +1 -0
- package/types/output/impl/bindConfig.d.mts +174 -0
- package/types/output/impl/bindConfig.d.mts.map +1 -0
- package/types/output/impl/bulkGet.d.mts +75 -0
- package/types/output/impl/bulkGet.d.mts.map +1 -0
- package/types/output/impl/bulkGet.test.d.mts +2 -0
- package/types/output/impl/bulkGet.test.d.mts.map +1 -0
- package/types/output/impl/bulkRemove.d.mts +63 -0
- package/types/output/impl/bulkRemove.d.mts.map +1 -0
- package/types/output/impl/bulkRemove.test.d.mts +2 -0
- package/types/output/impl/bulkRemove.test.d.mts.map +1 -0
- package/types/output/impl/bulkSave.d.mts +64 -0
- package/types/output/impl/bulkSave.d.mts.map +1 -0
- package/types/output/impl/bulkSave.test.d.mts +2 -0
- package/types/output/impl/bulkSave.test.d.mts.map +1 -0
- package/types/output/impl/get.d.mts +20 -0
- package/types/output/impl/get.d.mts.map +1 -0
- package/types/output/impl/get.test.d.mts +2 -0
- package/types/output/impl/get.test.d.mts.map +1 -0
- package/types/output/impl/getDBInfo.d.mts +52 -0
- package/types/output/impl/getDBInfo.d.mts.map +1 -0
- package/types/output/impl/getDBInfo.test.d.mts +2 -0
- package/types/output/impl/getDBInfo.test.d.mts.map +1 -0
- package/types/output/impl/patch.d.mts +45 -0
- package/types/output/impl/patch.d.mts.map +1 -0
- package/types/output/impl/patch.test.d.mts +2 -0
- package/types/output/impl/patch.test.d.mts.map +1 -0
- package/types/output/impl/put.d.mts +5 -0
- package/types/output/impl/put.d.mts.map +1 -0
- package/types/output/impl/put.test.d.mts +2 -0
- package/types/output/impl/put.test.d.mts.map +1 -0
- package/types/output/impl/query.d.mts +47 -0
- package/types/output/impl/query.d.mts.map +1 -0
- package/types/output/impl/query.test.d.mts +2 -0
- package/types/output/impl/query.test.d.mts.map +1 -0
- package/types/output/impl/remove.d.mts +9 -0
- package/types/output/impl/remove.d.mts.map +1 -0
- package/types/output/impl/remove.test.d.mts +2 -0
- package/types/output/impl/remove.test.d.mts.map +1 -0
- package/types/output/impl/retry.d.mts +32 -0
- package/types/output/impl/retry.d.mts.map +1 -0
- package/types/output/impl/retry.test.d.mts +2 -0
- package/types/output/impl/retry.test.d.mts.map +1 -0
- package/types/output/impl/stream.d.mts +13 -0
- package/types/output/impl/stream.d.mts.map +1 -0
- package/types/output/impl/stream.test.d.mts +2 -0
- package/types/output/impl/stream.test.d.mts.map +1 -0
- package/types/output/impl/sugar/lock.d.mts +24 -0
- package/types/output/impl/sugar/lock.d.mts.map +1 -0
- package/types/output/impl/sugar/lock.test.d.mts +2 -0
- package/types/output/impl/sugar/lock.test.d.mts.map +1 -0
- package/types/output/impl/sugar/watch.d.mts +21 -0
- package/types/output/impl/sugar/watch.d.mts.map +1 -0
- package/types/output/impl/sugar/watch.test.d.mts +2 -0
- package/types/output/impl/sugar/watch.test.d.mts.map +1 -0
- package/types/output/impl/utils/errors.d.mts +78 -0
- package/types/output/impl/utils/errors.d.mts.map +1 -0
- package/types/output/impl/utils/errors.test.d.mts +2 -0
- package/types/output/impl/utils/errors.test.d.mts.map +1 -0
- package/types/output/impl/utils/logger.d.mts +11 -0
- package/types/output/impl/utils/logger.d.mts.map +1 -0
- package/types/output/impl/utils/logger.test.d.mts +2 -0
- package/types/output/impl/utils/logger.test.d.mts.map +1 -0
- package/types/output/impl/utils/mergeNeedleOpts.d.mts +53 -0
- package/types/output/impl/utils/mergeNeedleOpts.d.mts.map +1 -0
- package/types/output/impl/utils/parseRows.d.mts +15 -0
- package/types/output/impl/utils/parseRows.d.mts.map +1 -0
- package/types/output/impl/utils/parseRows.test.d.mts +2 -0
- package/types/output/impl/utils/parseRows.test.d.mts.map +1 -0
- package/types/output/impl/utils/queryBuilder.d.mts +68 -0
- package/types/output/impl/utils/queryBuilder.d.mts.map +1 -0
- package/types/output/impl/utils/queryBuilder.test.d.mts +2 -0
- package/types/output/impl/utils/queryBuilder.test.d.mts.map +1 -0
- package/types/output/impl/utils/queryString.d.mts +9 -0
- package/types/output/impl/utils/queryString.d.mts.map +1 -0
- package/types/output/impl/utils/queryString.test.d.mts +2 -0
- package/types/output/impl/utils/queryString.test.d.mts.map +1 -0
- package/types/output/impl/utils/trackedEmitter.d.mts +7 -0
- package/types/output/impl/utils/trackedEmitter.d.mts.map +1 -0
- package/{impl → types/output/impl/utils}/transactionErrors.d.mts +16 -31
- package/types/output/impl/utils/transactionErrors.d.mts.map +1 -0
- package/types/output/index.d.mts +32 -0
- package/types/output/index.d.mts.map +1 -0
- package/types/output/index.test.d.mts +2 -0
- package/types/output/index.test.d.mts.map +1 -0
- package/types/output/schema/config.d.mts +90 -0
- package/types/output/schema/config.d.mts.map +1 -0
- package/types/output/schema/couch/couch.input.schema.d.ts +29 -0
- package/types/output/schema/couch/couch.input.schema.d.ts.map +1 -0
- package/types/output/schema/couch/couch.output.schema.d.ts +113 -0
- package/types/output/schema/couch/couch.output.schema.d.ts.map +1 -0
- package/types/output/schema/sugar/lock.d.mts +19 -0
- package/types/output/schema/sugar/lock.d.mts.map +1 -0
- package/types/output/schema/sugar/watch.d.mts +11 -0
- package/types/output/schema/sugar/watch.d.mts.map +1 -0
- package/types/output/schema/util.d.mts +85 -0
- package/types/output/schema/util.d.mts.map +1 -0
- package/types/output/tsdown.config.d.ts +3 -0
- package/types/output/tsdown.config.d.ts.map +1 -0
- package/types/output/types/standard-schema.d.ts +60 -0
- package/types/output/types/standard-schema.d.ts.map +1 -0
- package/types/standard-schema.ts +76 -0
- package/types/utils.d.ts +1 -0
- package/cjs/impl/bulk.cjs +0 -275
- package/cjs/impl/changes.cjs +0 -67
- package/cjs/impl/crud.cjs +0 -127
- package/cjs/impl/errors.cjs +0 -75
- package/cjs/impl/logger.cjs +0 -70
- package/cjs/impl/patch.cjs +0 -95
- package/cjs/impl/query.cjs +0 -116
- package/cjs/impl/queryBuilder.cjs +0 -163
- package/cjs/impl/retry.cjs +0 -55
- package/cjs/impl/stream.cjs +0 -121
- package/cjs/impl/sugar/lock.cjs +0 -81
- package/cjs/impl/sugar/watch.cjs +0 -159
- package/cjs/impl/trackedEmitter.cjs +0 -54
- package/cjs/impl/transactionErrors.cjs +0 -70
- package/cjs/impl/util.cjs +0 -64
- package/cjs/index.cjs +0 -132
- package/cjs/integration/changes.cjs +0 -76
- package/cjs/integration/disconnect-watch.cjs +0 -52
- package/cjs/integration/watch.cjs +0 -59
- package/cjs/schema/bind.cjs +0 -59
- package/cjs/schema/bulk.cjs +0 -92
- package/cjs/schema/changes.cjs +0 -68
- package/cjs/schema/config.cjs +0 -48
- package/cjs/schema/crud.cjs +0 -77
- package/cjs/schema/patch.cjs +0 -53
- package/cjs/schema/query.cjs +0 -62
- package/cjs/schema/stream.cjs +0 -42
- package/cjs/schema/sugar/lock.cjs +0 -59
- package/cjs/schema/sugar/watch.cjs +0 -42
- package/cjs/schema/util.cjs +0 -39
- package/config.json +0 -5
- package/docs/compiler.png +0 -0
- package/dualmode.config.json +0 -11
- package/impl/bulk.d.mts +0 -11
- package/impl/bulk.d.mts.map +0 -1
- package/impl/bulk.mjs +0 -291
- package/impl/changes.d.mts +0 -12
- package/impl/changes.d.mts.map +0 -1
- package/impl/changes.mjs +0 -53
- package/impl/crud.d.mts +0 -7
- package/impl/crud.d.mts.map +0 -1
- package/impl/crud.mjs +0 -108
- package/impl/errors.d.mts +0 -43
- package/impl/errors.d.mts.map +0 -1
- package/impl/errors.mjs +0 -65
- package/impl/logger.d.mts +0 -32
- package/impl/logger.d.mts.map +0 -1
- package/impl/logger.mjs +0 -59
- package/impl/patch.d.mts +0 -6
- package/impl/patch.d.mts.map +0 -1
- package/impl/patch.mjs +0 -88
- package/impl/query.d.mts +0 -195
- package/impl/query.d.mts.map +0 -1
- package/impl/query.mjs +0 -122
- package/impl/queryBuilder.d.mts +0 -154
- package/impl/queryBuilder.d.mts.map +0 -1
- package/impl/queryBuilder.mjs +0 -175
- package/impl/retry.d.mts +0 -2
- package/impl/retry.d.mts.map +0 -1
- package/impl/retry.mjs +0 -39
- package/impl/stream.d.mts +0 -3
- package/impl/stream.d.mts.map +0 -1
- package/impl/stream.mjs +0 -98
- package/impl/sugar/lock.d.mts +0 -5
- package/impl/sugar/lock.d.mts.map +0 -1
- package/impl/sugar/lock.mjs +0 -70
- package/impl/sugar/watch.d.mts +0 -34
- package/impl/sugar/watch.d.mts.map +0 -1
- package/impl/trackedEmitter.d.mts +0 -8
- package/impl/trackedEmitter.d.mts.map +0 -1
- package/impl/transactionErrors.d.mts.map +0 -1
- package/impl/transactionErrors.mjs +0 -47
- package/impl/util.d.mts +0 -3
- package/impl/util.d.mts.map +0 -1
- package/impl/util.mjs +0 -35
- package/index.d.mts +0 -80
- package/index.d.mts.map +0 -1
- package/index.mjs +0 -141
- package/integration/changes.mjs +0 -60
- package/integration/disconnect-watch.mjs +0 -36
- package/integration/watch.mjs +0 -40
- package/schema/bind.d.mts +0 -5461
- package/schema/bind.d.mts.map +0 -1
- package/schema/bind.mjs +0 -43
- package/schema/bulk.d.mts +0 -923
- package/schema/bulk.d.mts.map +0 -1
- package/schema/bulk.mjs +0 -83
- package/schema/changes.d.mts +0 -191
- package/schema/changes.d.mts.map +0 -1
- package/schema/changes.mjs +0 -59
- package/schema/config.d.mts +0 -79
- package/schema/config.d.mts.map +0 -1
- package/schema/config.mjs +0 -26
- package/schema/crud.d.mts +0 -491
- package/schema/crud.d.mts.map +0 -1
- package/schema/crud.mjs +0 -64
- package/schema/patch.d.mts +0 -255
- package/schema/patch.d.mts.map +0 -1
- package/schema/patch.mjs +0 -42
- package/schema/query.d.mts +0 -406
- package/schema/query.d.mts.map +0 -1
- package/schema/query.mjs +0 -45
- package/schema/stream.d.mts +0 -211
- package/schema/stream.d.mts.map +0 -1
- package/schema/stream.mjs +0 -23
- package/schema/sugar/lock.d.mts +0 -238
- package/schema/sugar/lock.d.mts.map +0 -1
- package/schema/sugar/lock.mjs +0 -50
- package/schema/sugar/watch.d.mts +0 -127
- package/schema/sugar/watch.d.mts.map +0 -1
- package/schema/sugar/watch.mjs +0 -29
- package/schema/util.d.mts +0 -160
- package/schema/util.d.mts.map +0 -1
- package/schema/util.mjs +0 -35
- package/types/changes-stream.d.ts +0 -11
package/README.md
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
1
|
### API Quick Reference
|
|
2
2
|
|
|
3
|
-
🍭 denotes a
|
|
3
|
+
🍭 denotes a _Sugar_ API - helps make some tasks sweet and easy, but may hide some complexities you might want to deal with.
|
|
4
4
|
|
|
5
|
-
| Document Operations
|
|
6
|
-
|
|
7
|
-
| [`get()`](#get)
|
|
8
|
-
| [`put()`](#put)
|
|
9
|
-
| [`patch()`](#patch) 🍭
|
|
10
|
-
| [`patchDangerously()`](#patchdangerously) 🍭 | [`
|
|
11
|
-
| [`getAtRev()`](#getatrev) 🍭
|
|
12
|
-
| [`createLock()`](#
|
|
13
|
-
| [`removeLock()`](#
|
|
5
|
+
| Document Operations | Bulk Operations | View Operations | Change Utilities |
|
|
6
|
+
| -------------------------------------------- | -------------------------------------------------- | ------------------------------- | -------------------------------------------------- |
|
|
7
|
+
| [`get()`](#get) | [`bulkGet()`](#bulkget) | [`query()`](#query) | [`watchDocs()`](#watchdocs) 🍭 |
|
|
8
|
+
| [`put()`](#put) | [`bulkSave()`](#bulksave) | [`queryStream()`](#querystream) | [Standalone changes feed](#changes-feed-companion) |
|
|
9
|
+
| [`patch()`](#patch) 🍭 | [`bulkRemove()`](#bulkremove) | [`remove()`](#remove) | |
|
|
10
|
+
| [`patchDangerously()`](#patchdangerously) 🍭 | [`bulkRemoveMap()`](#bulkremovemap) | | |
|
|
11
|
+
| [`getAtRev()`](#getatrev) 🍭 | [`bulkGetDictionary()`](#bulkgetdictionary) 🍭 | | |
|
|
12
|
+
| [`createLock()`](#createlock) 🍭 | [`bulkSaveTransaction()`](#bulksavetransaction) 🍭 | | |
|
|
13
|
+
| [`removeLock()`](#removelock) 🍭 | | | |
|
|
14
14
|
|
|
15
|
-
And some utility
|
|
15
|
+
And some utility APIs
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
- [`getDBInfo()`](#getdbinfo)
|
|
18
|
+
- [`createQuery()`](#createquery) 🍭
|
|
19
|
+
- [`withRetry()`](#withretry)
|
|
21
20
|
|
|
22
21
|
### Setup
|
|
23
22
|
|
|
24
23
|
Depending on your environment, use import or require
|
|
25
|
-
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
import { get, put, query } from 'hide-a-bed'
|
|
27
|
+
```
|
|
28
|
+
|
|
26
29
|
or
|
|
27
|
-
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
const { get, put, query } = require('hide-a-bed')
|
|
33
|
+
```
|
|
28
34
|
|
|
29
35
|
### Config
|
|
30
36
|
|
|
31
37
|
Anywhere you see a config, it is an object with the following setup
|
|
32
|
-
|
|
38
|
+
`{ couch: 'https://username:pass@the.couch.url.com:5984' }`
|
|
33
39
|
And it is passed in as the first argument of all the functions
|
|
34
|
-
|
|
40
|
+
`const doc = await get(config, 'doc-123')`
|
|
35
41
|
|
|
36
42
|
See [Advanced Config Options](#advanced-config-options) for more advanced settings.
|
|
37
43
|
|
|
38
|
-
#### bindConfig
|
|
44
|
+
#### bindConfig
|
|
45
|
+
|
|
46
|
+
bindConfig function is a convenience method to bind the config, so you don't need to pass it in every call.
|
|
47
|
+
Usually, you label the variable returned db so method calls appear to operate on the bound db.
|
|
39
48
|
|
|
40
|
-
A convience method to bind the config, so you dont need to pass it in.
|
|
41
49
|
```
|
|
42
50
|
import { bindConfig } from 'hide-a-bed'
|
|
43
51
|
const db = bindConfig(process.env)
|
|
@@ -45,33 +53,19 @@ const services = { db } // see example below
|
|
|
45
53
|
const doc = await db.get('doc-123')
|
|
46
54
|
```
|
|
47
55
|
|
|
48
|
-
If you need to force autocompletion/type-checking, you can add the following jsdoc to help your editor
|
|
49
|
-
|
|
50
|
-
```
|
|
51
|
-
function doSomething (services)
|
|
52
|
-
/** @type { import('hide-a-bed').DB} db */
|
|
53
|
-
const db = services.db;
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Here is an example of compiler warnings:
|
|
57
|
-
|
|
58
|
-

|
|
59
|
-
|
|
60
|
-
|
|
61
56
|
##### Config Overrides
|
|
62
57
|
|
|
63
|
-
You also can quickly
|
|
58
|
+
You also can quickly override (or more) config settings for a particular call using db.options(optionOverrides)
|
|
64
59
|
|
|
65
|
-
|
|
60
|
+
e.g.
|
|
66
61
|
|
|
67
62
|
```
|
|
68
63
|
const db = bindConfig({ couch: 'http://localhost:5984/db', throwOnGetNotFound: false })
|
|
69
|
-
const doc = await db.options({ throwOnGetNotFound: true }).get('doc-id')
|
|
64
|
+
const doc = await db.options({ throwOnGetNotFound: true }).get('doc-id')
|
|
70
65
|
```
|
|
71
66
|
|
|
72
67
|
You can pass any of [Config Options](#advanced-config-options) to db.options to override the original config bindings.
|
|
73
68
|
|
|
74
|
-
|
|
75
69
|
### Document Operations
|
|
76
70
|
|
|
77
71
|
#### get
|
|
@@ -79,11 +73,10 @@ You can pass any of [Config Options](#advanced-config-options) to db.options to
|
|
|
79
73
|
Get a single document by ID.
|
|
80
74
|
|
|
81
75
|
**Parameters:**
|
|
82
|
-
|
|
83
|
-
- `
|
|
84
|
-
- `
|
|
85
|
-
|
|
86
|
-
* `throwOnGetNotFound` default false. If true, 404 docs throw
|
|
76
|
+
|
|
77
|
+
- `config`: Object with
|
|
78
|
+
- `couch` URL string
|
|
79
|
+
- `throwOnGetNotFound` default false. If true, 404 docs throw
|
|
87
80
|
- `id`: Document ID string
|
|
88
81
|
- Returns: Promise resolving to document object or null if not found
|
|
89
82
|
|
|
@@ -92,16 +85,18 @@ const config = { couch: 'http://localhost:5984/mydb' }
|
|
|
92
85
|
const doc = await get(config, 'doc-123')
|
|
93
86
|
console.log(doc._id, doc._rev)
|
|
94
87
|
|
|
95
|
-
const
|
|
96
|
-
console.log(
|
|
88
|
+
const notFound = await get(config, 'notFound')
|
|
89
|
+
console.log(notFound) // null
|
|
97
90
|
|
|
98
91
|
try {
|
|
99
|
-
const config = {
|
|
100
|
-
|
|
92
|
+
const config = {
|
|
93
|
+
couch: 'http://localhost:5984/mydb',
|
|
94
|
+
throwOnGetNotFound: true
|
|
95
|
+
}
|
|
96
|
+
await get(config, 'notFound')
|
|
101
97
|
} catch (err) {
|
|
102
98
|
if (err.name === 'NotFoundError') console.log('Document not found')
|
|
103
99
|
}
|
|
104
|
-
|
|
105
100
|
```
|
|
106
101
|
|
|
107
102
|
#### put
|
|
@@ -109,13 +104,14 @@ try {
|
|
|
109
104
|
Save a document.
|
|
110
105
|
|
|
111
106
|
**Parameters:**
|
|
107
|
+
|
|
112
108
|
- `config`: Object with `couch` URL string
|
|
113
109
|
- `doc`: Document object with `_id` property
|
|
114
|
-
- Returns: Promise resolving to response with `ok`, `id`, `rev` properties
|
|
110
|
+
- Returns: Promise resolving to response with `ok`, `id`, `rev` properties, eg: { ok: boolean, id: string, rev: string }
|
|
115
111
|
|
|
116
112
|
```javascript
|
|
117
113
|
const config = { couch: 'http://localhost:5984/mydb' }
|
|
118
|
-
const doc = {
|
|
114
|
+
const doc = {
|
|
119
115
|
_id: 'doc-123',
|
|
120
116
|
type: 'user',
|
|
121
117
|
name: 'Alice'
|
|
@@ -124,28 +120,28 @@ const result = await put(config, doc)
|
|
|
124
120
|
// result: { ok: true, id: 'doc-123', rev: '1-abc123' }
|
|
125
121
|
|
|
126
122
|
// imaginary rev returns a conflict
|
|
127
|
-
const doc = { _id: 'notThereDoc', _rev: '32-does-not-compute'}
|
|
123
|
+
const doc = { _id: 'notThereDoc', _rev: '32-does-not-compute' }
|
|
128
124
|
const result2 = await db.put(doc)
|
|
129
125
|
console.log(result2) // { ok: false, error: 'conflict', statusCode: 409 }
|
|
130
126
|
```
|
|
131
127
|
|
|
132
128
|
#### patch
|
|
133
129
|
|
|
134
|
-
|
|
130
|
+
The patch function lets you update specific properties of a document. The \_rev value must be set, and passed in with properties.
|
|
135
131
|
|
|
136
132
|
**Parameters:**
|
|
133
|
+
|
|
137
134
|
- `config`: Object with couch URL string
|
|
138
135
|
- `id`: Document ID string
|
|
139
|
-
- `properties`: Object with properties to update,
|
|
136
|
+
- `properties`: Object with properties to update, must include \_rev property
|
|
140
137
|
- Returns: Promise resolving to response with `ok`, `id`, `rev` properties
|
|
141
138
|
|
|
142
139
|
```javascript
|
|
143
|
-
const config = {
|
|
140
|
+
const config = {
|
|
144
141
|
couch: 'http://localhost:5984/mydb',
|
|
145
|
-
retries: 3
|
|
146
|
-
delay: 500
|
|
142
|
+
retries: 3
|
|
147
143
|
}
|
|
148
|
-
const properties = {
|
|
144
|
+
const properties = {
|
|
149
145
|
_rev: '3-fdskjhfsdkjhfsd',
|
|
150
146
|
name: 'Alice Smith',
|
|
151
147
|
updated: true
|
|
@@ -153,28 +149,29 @@ const properties = {
|
|
|
153
149
|
const result = await patch(config, 'doc-123', properties)
|
|
154
150
|
// result: { ok: true, id: 'doc-123', rev: '2-xyz789' }
|
|
155
151
|
```
|
|
152
|
+
|
|
156
153
|
#### patchDangerously
|
|
157
154
|
|
|
158
|
-
Update specific properties of a document, no _rev is needed.
|
|
155
|
+
Update specific properties of a document, no \_rev is needed.
|
|
159
156
|
|
|
160
157
|
**Parameters:**
|
|
158
|
+
|
|
161
159
|
- `config`: Object with couch URL string
|
|
162
160
|
- `id`: Document ID string
|
|
163
161
|
- `properties`: Object with properties to update
|
|
164
162
|
|
|
165
|
-
|
|
163
|
+
_Warning_: patchDangerously can clobber data. It will retry even if a conflict happens. There are some use cases for this, but you have been warned, hence the name.
|
|
166
164
|
|
|
167
165
|
- `id`: Document ID string
|
|
168
166
|
- `properties`: Object with properties to update
|
|
169
167
|
- Returns: Promise resolving to response with `ok`, `id`, `rev` properties
|
|
170
168
|
|
|
171
169
|
```javascript
|
|
172
|
-
const config = {
|
|
170
|
+
const config = {
|
|
173
171
|
couch: 'http://localhost:5984/mydb',
|
|
174
|
-
retries: 3
|
|
175
|
-
delay: 500
|
|
172
|
+
retries: 3
|
|
176
173
|
}
|
|
177
|
-
const properties = {
|
|
174
|
+
const properties = {
|
|
178
175
|
name: 'Alice Smith',
|
|
179
176
|
updated: true
|
|
180
177
|
}
|
|
@@ -187,11 +184,12 @@ const result = await patchDangerously(config, 'doc-123', properties)
|
|
|
187
184
|
Return a document at the rev specified.
|
|
188
185
|
|
|
189
186
|
**Parameters:**
|
|
187
|
+
|
|
190
188
|
- `config`: Object with couch URL string
|
|
191
189
|
- `id`: Document ID string
|
|
192
190
|
- `rev`: Revision string to retrieve
|
|
193
191
|
|
|
194
|
-
|
|
192
|
+
_CouchDB_ is not a version control db. This is a special function for unique situations. The \_rev might not be around as couch cleans up old revs.
|
|
195
193
|
|
|
196
194
|
```javascript
|
|
197
195
|
const config = { couch: 'http://localhost:5984/mydb' }
|
|
@@ -203,11 +201,12 @@ console.log(doc._id, doc._rev)
|
|
|
203
201
|
|
|
204
202
|
Create a lock document to try and prevent concurrent modifications.
|
|
205
203
|
|
|
206
|
-
Note this does not internally lock the document that is referenced by the id. People can still mutate it with
|
|
207
|
-
all the other document mutation functions. This should just be used at an app level to coordinate access
|
|
204
|
+
Note this does not internally lock the document that is referenced by the id. People can still mutate it with
|
|
205
|
+
all the other document mutation functions. This should just be used at an app level to coordinate access
|
|
208
206
|
on long running document editing.
|
|
209
207
|
|
|
210
208
|
**Parameters:**
|
|
209
|
+
|
|
211
210
|
- `config`: Object with `couch` URL string
|
|
212
211
|
- `docId`: Document ID string to lock
|
|
213
212
|
- `options`: Lock options object:
|
|
@@ -221,6 +220,7 @@ Returns a Promise resolving to boolean indicating if lock was created successful
|
|
|
221
220
|
Remove a lock from a document.
|
|
222
221
|
|
|
223
222
|
**Parameters:**
|
|
223
|
+
|
|
224
224
|
- `config`: Object with `couch` URL string
|
|
225
225
|
- `docId`: Document ID string to unlock
|
|
226
226
|
- `options`: Lock options object:
|
|
@@ -252,6 +252,7 @@ if (locked) {
|
|
|
252
252
|
Save multiple documents in one request.
|
|
253
253
|
|
|
254
254
|
**Parameters:**
|
|
255
|
+
|
|
255
256
|
- `config`: Object with `couch` URL string
|
|
256
257
|
- `docs`: Array of document objects, each with `_id`
|
|
257
258
|
- Returns: Promise resolving to array of results with `ok`, `id`, `rev` for each doc
|
|
@@ -274,20 +275,31 @@ const results = await bulkSave(config, docs)
|
|
|
274
275
|
Get multiple documents by ID.
|
|
275
276
|
|
|
276
277
|
**Parameters:**
|
|
278
|
+
|
|
277
279
|
- `config`: Object with `couch` URL string
|
|
278
280
|
- `ids`: Array of document ID strings
|
|
279
|
-
-
|
|
281
|
+
- `options` _(optional)_:
|
|
282
|
+
- `validate.docSchema`: zod v4 schema applied to each found document before returning.
|
|
283
|
+
- Returns: Promise resolving to the optionally validated bulk response
|
|
280
284
|
|
|
281
|
-
|
|
285
|
+
Warning: documents that are not found will still have a row in the results. The doc property will be null, and the error property will be set.
|
|
282
286
|
|
|
283
287
|
```javascript
|
|
288
|
+
import z from 'zod'
|
|
289
|
+
|
|
284
290
|
const config = { couch: 'http://localhost:5984/mydb' }
|
|
285
291
|
const ids = ['doc1', 'doc2', 'doesNotExist']
|
|
286
|
-
const docs = await bulkGet(config, ids
|
|
292
|
+
const docs = await bulkGet(config, ids, {
|
|
293
|
+
docSchema: z.looseObject({
|
|
294
|
+
_id: z.string(),
|
|
295
|
+
type: z.string(),
|
|
296
|
+
name: z.string()
|
|
297
|
+
})
|
|
298
|
+
})
|
|
287
299
|
// rows: [
|
|
288
|
-
// {
|
|
289
|
-
// {
|
|
290
|
-
// { key: '
|
|
300
|
+
// { id: 'doc1', doc: { _id: 'doc1', type: 'user', name: 'Alice' } },
|
|
301
|
+
// { id: 'doc2', doc: { _id: 'doc2', type: 'user', name: 'Bob' } },
|
|
302
|
+
// { key: 'doesNotExist', error: 'not_found' }
|
|
291
303
|
// ]
|
|
292
304
|
```
|
|
293
305
|
|
|
@@ -296,6 +308,7 @@ const docs = await bulkGet(config, ids)
|
|
|
296
308
|
Delete multiple documents in one request.
|
|
297
309
|
|
|
298
310
|
**Parameters:**
|
|
311
|
+
|
|
299
312
|
- `config`: Object with `couch` URL string
|
|
300
313
|
- `ids`: Array of document ID strings to delete
|
|
301
314
|
- Returns: Promise resolving to array of results with `ok`, `id`, `rev` for each deletion
|
|
@@ -310,41 +323,95 @@ const results = await bulkRemove(config, ids)
|
|
|
310
323
|
// ]
|
|
311
324
|
```
|
|
312
325
|
|
|
313
|
-
####
|
|
326
|
+
#### remove
|
|
327
|
+
|
|
328
|
+
Delete document from DB with id and rev.
|
|
329
|
+
|
|
330
|
+
Allows more efficient deletion of document by providing only id and rev. This is useful for deleting documents that are large, or have a lot of data.
|
|
331
|
+
|
|
332
|
+
**Parameters:**
|
|
333
|
+
|
|
334
|
+
- `config`: Object with `couch` URL string
|
|
335
|
+
- `id`: document ID to delete
|
|
336
|
+
- `rev`: rev of the document to delete
|
|
337
|
+
- Returns: Promise resolving to array of results with `ok`, `id`, `rev` for the deletion
|
|
314
338
|
|
|
315
|
-
|
|
339
|
+
```javascript
|
|
340
|
+
const config = { couch: 'http://localhost:5984/mydb' }
|
|
341
|
+
const id = 'doc1'
|
|
342
|
+
const rev = '2-ghi789'
|
|
343
|
+
const results = await remove(config, id, rev)
|
|
344
|
+
// result:
|
|
345
|
+
// { ok: true, id: 'doc1', rev: '2-ghi789' }
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
#### bulkRemoveMap
|
|
349
|
+
|
|
350
|
+
Delete multiple documents in one request. Same inputs and outputs as [bulkRemove](#bulkremove), but internally the logic will handle one document at a time instead of using couch bulk operations. Useful for working with documents that have large data requirements (1MB or more).
|
|
316
351
|
|
|
317
352
|
**Parameters:**
|
|
353
|
+
|
|
318
354
|
- `config`: Object with `couch` URL string
|
|
319
355
|
- `ids`: Array of document ID strings to delete
|
|
356
|
+
- Returns: Promise resolving to array of results with `ok`, `id`, `rev` for each deletion
|
|
357
|
+
|
|
358
|
+
```javascript
|
|
359
|
+
const config = { couch: 'http://localhost:5984/mydb' }
|
|
360
|
+
const ids = ['doc1', 'doc2']
|
|
361
|
+
const results = await bulkRemoveMap(config, ids)
|
|
362
|
+
// results: [
|
|
363
|
+
// { ok: true, id: 'doc1', rev: '2-ghi789' },
|
|
364
|
+
// { ok: true, id: 'doc2', rev: '2-jkl012' }
|
|
365
|
+
// ]
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
#### bulkGetDictionary
|
|
369
|
+
|
|
370
|
+
Adds convenience to bulkGet. Organizes found and notFound documents into properties that are {id:result}. This makes it easy to deal with the results.
|
|
371
|
+
|
|
372
|
+
**Parameters:**
|
|
373
|
+
|
|
374
|
+
- `config`: Object with `couch` URL string
|
|
375
|
+
- `ids`: Array of document ID strings to get
|
|
376
|
+
- `options` _(optional)_:
|
|
377
|
+
- `validate.docSchema`: zod v4 schema applied to each found document before returning.
|
|
320
378
|
- Returns: Promise resolving to an object with found and notFound properties.
|
|
321
379
|
|
|
322
|
-
|
|
380
|
+
_found_ looks like
|
|
381
|
+
|
|
323
382
|
```
|
|
324
|
-
{
|
|
325
|
-
|
|
326
|
-
|
|
383
|
+
{
|
|
384
|
+
doc1: { _id: 'doc1', _rev: '1-221', data: {} },
|
|
385
|
+
doc2: { _id: 'doc2', _rev: '4-421', data: {} },
|
|
327
386
|
}
|
|
328
387
|
```
|
|
329
388
|
|
|
330
|
-
|
|
389
|
+
_notFound_ looks like
|
|
390
|
+
|
|
331
391
|
```
|
|
332
392
|
{
|
|
333
|
-
|
|
393
|
+
doesNotExist: { key: 'doesNotExist', error: 'not_found' }
|
|
334
394
|
}
|
|
335
395
|
```
|
|
336
396
|
|
|
337
397
|
```javascript
|
|
398
|
+
import z from 'zod'
|
|
399
|
+
|
|
338
400
|
const config = { couch: 'http://localhost:5984/mydb' }
|
|
339
|
-
const ids = ['doc1', 'doc2']
|
|
340
|
-
const results = await bulkGetDictionary(config, ids
|
|
401
|
+
const ids = ['doc1', 'doc2', 'doesNotExist']
|
|
402
|
+
const results = await bulkGetDictionary(config, ids, {
|
|
403
|
+
docSchema: z.looseObject({
|
|
404
|
+
_id: z.string(),
|
|
405
|
+
data: z.record(z.any())
|
|
406
|
+
})
|
|
407
|
+
})
|
|
341
408
|
// results: {
|
|
342
409
|
// found: {
|
|
343
|
-
//
|
|
344
|
-
//
|
|
410
|
+
// doc1: { _id: 'doc2', _rev: '1-221', data: {} },
|
|
411
|
+
// doc2: { _id: 'doc2', _rev: '4-421', data: {} },
|
|
345
412
|
// },
|
|
346
413
|
// notFound: {
|
|
347
|
-
//
|
|
414
|
+
// doesNotExist: { key: 'doesNotExist', error: 'not_found' }
|
|
348
415
|
// }
|
|
349
416
|
// }
|
|
350
417
|
```
|
|
@@ -354,6 +421,7 @@ const results = await bulkGetDictionary(config, ids)
|
|
|
354
421
|
Perform a bulk save operation with all-or-nothing semantics.
|
|
355
422
|
|
|
356
423
|
**Parameters:**
|
|
424
|
+
|
|
357
425
|
- `config`: Object with `couch` URL string
|
|
358
426
|
- `transactionId`: Unique identifier for the transaction
|
|
359
427
|
- `docs`: Array of document objects to save
|
|
@@ -364,6 +432,7 @@ This operation ensures that either all documents are saved successfully, or none
|
|
|
364
432
|
Note: The transactionId has to be unique for the lifetime of the app. It is used to prevent two processes from executing the same transaction. It is up to you to craft a transactionId that uniquely represents this transaction, and that also is the same if another process tries to generate it.
|
|
365
433
|
|
|
366
434
|
Exceptions to handle:
|
|
435
|
+
|
|
367
436
|
- `TransactionSetupError`: Thrown if the transaction document cannot be created. Usually because it already exists
|
|
368
437
|
- `TransactionVersionConflictError`: Thrown if there are version conflicts with existing documents.
|
|
369
438
|
- `TransactionBulkOperationError`: Thrown if the bulk save operation fails for some documents.
|
|
@@ -403,10 +472,9 @@ Get basic info about a db in couch
|
|
|
403
472
|
```
|
|
404
473
|
const config = { couch: 'http://localhost:5984/mydb' }
|
|
405
474
|
const result = await getDBInfo(config)
|
|
406
|
-
// result: { db_name: 'test', doc_count: 3232 }
|
|
475
|
+
// result: { db_name: 'test', doc_count: 3232 }
|
|
407
476
|
```
|
|
408
477
|
|
|
409
|
-
|
|
410
478
|
### View Queries
|
|
411
479
|
|
|
412
480
|
#### query
|
|
@@ -414,8 +482,9 @@ const result = await getDBInfo(config)
|
|
|
414
482
|
Query a view with options.
|
|
415
483
|
|
|
416
484
|
**Parameters:**
|
|
485
|
+
|
|
417
486
|
- `config`: Object with `couch` URL string
|
|
418
|
-
- `view`: View path string (e.g. '_design/doc
|
|
487
|
+
- `view`: View path string (e.g. '\_design/doc/\_view/name')
|
|
419
488
|
- `options`: Optional object with query parameters:
|
|
420
489
|
- `startkey`: Start key for range
|
|
421
490
|
- `endkey`: End key for range
|
|
@@ -441,7 +510,7 @@ const options = {
|
|
|
441
510
|
const result = await query(config, view, options)
|
|
442
511
|
// result: {
|
|
443
512
|
// rows: [
|
|
444
|
-
// {
|
|
513
|
+
// {
|
|
445
514
|
// id: 'doc1',
|
|
446
515
|
// key: 'Alice',
|
|
447
516
|
// value: 1,
|
|
@@ -452,16 +521,15 @@ const result = await query(config, view, options)
|
|
|
452
521
|
// }
|
|
453
522
|
```
|
|
454
523
|
|
|
455
|
-
Some notes on the keys. Use native js
|
|
456
|
-
|
|
457
|
-
- ```{ startkey: ['ryan'], endkey: ['ryan', {}] }```
|
|
458
|
-
- ```{ startkey: [47, null], endkey: [48, null] }```
|
|
459
|
-
- ```{ startkey: [customerIdVar], endkey: [customerIdVar, {}] }```
|
|
460
|
-
- ```{ startkey: [teamId, userId, startTimestamp], endkey: [teamId, userId, endTimestamp] }```
|
|
461
|
-
|
|
524
|
+
Some notes on the keys. Use native js types for arrays keys, rather then strings. Eg
|
|
462
525
|
|
|
526
|
+
- `{ startkey: ['ryan'], endkey: ['ryan', {}] }`
|
|
527
|
+
- `{ startkey: [47, null], endkey: [48, null] }`
|
|
528
|
+
- `{ startkey: [customerIdVar], endkey: [customerIdVar, {}] }`
|
|
529
|
+
- `{ startkey: [teamId, userId, startTimestamp], endkey: [teamId, userId, endTimestamp] }`
|
|
463
530
|
|
|
464
531
|
#### createQuery()
|
|
532
|
+
|
|
465
533
|
Create a query builder to help construct view queries with a fluent interface. Note we have stuck to couch naming conventions and not camel case.
|
|
466
534
|
|
|
467
535
|
- Returns: QueryBuilder instance with methods:
|
|
@@ -478,26 +546,22 @@ Create a query builder to help construct view queries with a fluent interface. N
|
|
|
478
546
|
- `build()`: Return the constructed query options object
|
|
479
547
|
|
|
480
548
|
```javascript
|
|
481
|
-
const options = createQuery()
|
|
482
|
-
.startkey('A')
|
|
483
|
-
.endkey('B')
|
|
484
|
-
.include_docs(true)
|
|
485
|
-
.limit(10)
|
|
486
|
-
.build()
|
|
549
|
+
const options = createQuery().startkey('A').endkey('B').include_docs(true).limit(10).build()
|
|
487
550
|
|
|
488
551
|
const result = await query(config, view, options)
|
|
489
552
|
```
|
|
490
553
|
|
|
491
554
|
Again, use js types for array keys
|
|
492
555
|
|
|
493
|
-
|
|
494
|
-
|
|
556
|
+
- `.startkey([teamId, userId]).endkey([teamId, userId, {}])`
|
|
557
|
+
- `.startkey([teamId, userId, startTimestamp]).endkey([teamId, userId, endTimestamp])`
|
|
495
558
|
|
|
496
559
|
#### queryStream
|
|
497
560
|
|
|
498
|
-
Use Cases
|
|
561
|
+
Use Cases _Streaming Data_
|
|
499
562
|
|
|
500
563
|
**Parameters:**
|
|
564
|
+
|
|
501
565
|
- `config`: Object with couch URL string
|
|
502
566
|
- `view`: View path string
|
|
503
567
|
- `options`: Query options object
|
|
@@ -505,7 +569,7 @@ Use Cases *Streaming Data*
|
|
|
505
569
|
|
|
506
570
|
Want to stream data from couch? You can with queryStream. It looks identical to query, except you add an extra 'onRow' function
|
|
507
571
|
|
|
508
|
-
Here is a small hapi example of streaming data from couch to the client as ndjson.
|
|
572
|
+
Here is a small hapi example of streaming data from couch to the client as ndjson.
|
|
509
573
|
We do a small transform by only streaming the doc. you can do a lot of things in the onrow function.
|
|
510
574
|
|
|
511
575
|
```
|
|
@@ -535,131 +599,120 @@ const init = async () => {
|
|
|
535
599
|
init()
|
|
536
600
|
```
|
|
537
601
|
|
|
538
|
-
Want to consume this in the browser? I'd
|
|
539
|
-
|
|
602
|
+
Want to consume this in the browser? I'd recommend [ndjson-readablestream](https://www.npmjs.com/package/ndjson-readablestream)
|
|
603
|
+
An [example react component](https://github.com/Azure-Samples/azure-search-openai-demo/pull/532/files#diff-506debba46b93087dc46a916384e56392808bcc02a99d9291557f3e674d4ad6c)
|
|
604
|
+
that consumes the readable stream.
|
|
605
|
+
|
|
606
|
+
#### Changes feed companion
|
|
540
607
|
|
|
541
|
-
|
|
608
|
+
The main client no longer bundles the legacy `changes-stream` dependency. Install `hide-a-bed-changes` when you need a CouchDB `_changes` feed helper:
|
|
609
|
+
|
|
610
|
+
```
|
|
611
|
+
npm install hide-a-bed-changes
|
|
612
|
+
```
|
|
542
613
|
|
|
543
|
-
|
|
614
|
+
Usage mirrors the original API:
|
|
615
|
+
|
|
616
|
+
```javascript
|
|
617
|
+
import { changes } from 'hide-a-bed-changes'
|
|
618
|
+
|
|
619
|
+
const config = { couch: 'http://localhost:5984/mydb' }
|
|
620
|
+
|
|
621
|
+
const feed = await changes(
|
|
622
|
+
config,
|
|
623
|
+
change => {
|
|
624
|
+
console.log('Document changed:', change.id)
|
|
625
|
+
},
|
|
626
|
+
{ since: 'now', include_docs: true }
|
|
627
|
+
)
|
|
628
|
+
|
|
629
|
+
feed.on('error', console.error)
|
|
630
|
+
|
|
631
|
+
// later
|
|
632
|
+
feed.stop()
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
`hide-a-bed-changes` reuses the same config structure, merges `config.needleOpts`, and resolves `since: 'now'` to the current `update_seq` before starting the feed.
|
|
636
|
+
|
|
637
|
+
#### watchDocs ()
|
|
638
|
+
|
|
639
|
+
Watch specific documents for changes in real-time.
|
|
544
640
|
|
|
545
641
|
**Parameters:**
|
|
642
|
+
|
|
546
643
|
- `config`: Object with `couch` URL string
|
|
547
|
-
-
|
|
644
|
+
- `docIds`: String or array of document IDs to watch (max 100)
|
|
645
|
+
- `onChange`: Function called for each change
|
|
548
646
|
- `options`: Optional object with parameters:
|
|
549
|
-
- `
|
|
550
|
-
- `
|
|
551
|
-
- `
|
|
552
|
-
-
|
|
647
|
+
- `include_docs`: Boolean - include full documents (default false)
|
|
648
|
+
- `maxRetries`: Number - maximum reconnection attempts (default: 10)
|
|
649
|
+
- `initialDelay`: Number - initial reconnection delay in ms (default 1000)
|
|
650
|
+
- `maxDelay`: Number - maximum reconnection delay in ms (default: 30000)
|
|
553
651
|
|
|
554
|
-
Returns an EventEmitter that emits
|
|
652
|
+
Returns an EventEmitter that emits:
|
|
653
|
+
|
|
654
|
+
- 'change' events with change objects.
|
|
655
|
+
- 'error' events when max retries reached.
|
|
656
|
+
- 'end' events with last sequence number.
|
|
555
657
|
|
|
556
658
|
```javascript
|
|
557
659
|
const config = { couch: 'http://localhost:5984/mydb' }
|
|
558
|
-
const options = {
|
|
559
|
-
since: 'now',
|
|
560
|
-
include_docs: true
|
|
561
|
-
}
|
|
562
660
|
|
|
563
|
-
|
|
661
|
+
// Watch a single document
|
|
662
|
+
const feed = await watchDocs(config, 'doc123', change => {
|
|
564
663
|
console.log('Document changed:', change.id)
|
|
565
664
|
console.log('New revision:', change.changes[0].rev)
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
665
|
+
})
|
|
666
|
+
|
|
667
|
+
// Watch multiple documents with full doc content
|
|
668
|
+
const feed = await watchDocs(
|
|
669
|
+
config,
|
|
670
|
+
['doc1', 'doc2', 'doc3'],
|
|
671
|
+
change => {
|
|
672
|
+
if (change.doc) {
|
|
673
|
+
console.log('Updated document:', change.doc)
|
|
674
|
+
}
|
|
675
|
+
},
|
|
676
|
+
{ include_docs: true }
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
// Handle errors
|
|
680
|
+
feed.on('error', error => {
|
|
681
|
+
console.error('Watch error:', error)
|
|
682
|
+
})
|
|
571
683
|
|
|
684
|
+
// Handle end of feed
|
|
685
|
+
feed.on('end', ({ lastSeq }) => {
|
|
686
|
+
console.log('Feed ended at sequence:', lastSeq)
|
|
687
|
+
})
|
|
572
688
|
|
|
573
|
-
// Stop
|
|
689
|
+
// Stop watching
|
|
574
690
|
feed.stop()
|
|
575
691
|
```
|
|
576
692
|
|
|
577
|
-
The
|
|
578
|
-
- Building real-time applications
|
|
579
|
-
- Keeping local data in sync with CouchDB
|
|
580
|
-
- Triggering actions when documents change
|
|
581
|
-
- Implementing replication
|
|
693
|
+
The watchDocs feed is useful for:
|
|
582
694
|
|
|
695
|
+
1. Building real-time applications focused on specific documents
|
|
696
|
+
2. Triggering actions when particular documents change
|
|
697
|
+
3. Maintaining cached copies of frequently-accessed documents
|
|
583
698
|
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
Watch specific documents for changes in real-time.
|
|
587
|
-
|
|
588
|
-
**Parameters:**
|
|
589
|
-
- `config`: Object with `couch` URL string
|
|
590
|
-
- `docIds`: String or array of document IDs to watch (max 100
|
|
591
|
-
- `onChange`: Function called for each change
|
|
592
|
-
- `options`: Optional object with parameters:
|
|
593
|
-
- `include_docs`: Boolean to include full documents (defaul
|
|
594
|
-
false)
|
|
595
|
-
- `maxRetries`: Maximum reconnection attempts (default: 10)
|
|
596
|
-
- `initialDelay`: Initial reconnection delay in ms (default
|
|
597
|
-
1000)
|
|
598
|
-
- `maxDelay`: Maximum reconnection delay in ms (default:
|
|
599
|
-
30000)
|
|
600
|
-
|
|
601
|
-
Returns an EventEmitter that emits:
|
|
602
|
-
- 'change' events with change objects
|
|
603
|
-
- 'error' events when max retries reached
|
|
604
|
-
- 'end' events with last sequence number
|
|
605
|
-
|
|
606
|
-
```javascript
|
|
607
|
-
const config = { couch: 'http://localhost:5984/mydb' }
|
|
608
|
-
|
|
609
|
-
// Watch a single document
|
|
610
|
-
const feed = await watchDocs(config, 'doc123', change => {
|
|
611
|
-
console.log('Document changed:', change.id)
|
|
612
|
-
console.log('New revision:', change.changes[0].rev)
|
|
613
|
-
})
|
|
614
|
-
|
|
615
|
-
// Watch multiple documents with full doc content
|
|
616
|
-
const feed = await watchDocs(
|
|
617
|
-
config,
|
|
618
|
-
['doc1', 'doc2', 'doc3'],
|
|
619
|
-
change => {
|
|
620
|
-
if (change.doc) {
|
|
621
|
-
console.log('Updated document:', change.doc)
|
|
622
|
-
}
|
|
623
|
-
},
|
|
624
|
-
{ include_docs: true }
|
|
625
|
-
)
|
|
626
|
-
|
|
627
|
-
// Handle errors
|
|
628
|
-
feed.on('error', error => {
|
|
629
|
-
console.error('Watch error:', error)
|
|
630
|
-
})
|
|
631
|
-
|
|
632
|
-
// Handle end of feed
|
|
633
|
-
feed.on('end', ({ lastSeq }) => {
|
|
634
|
-
console.log('Feed ended at sequence:', lastSeq)
|
|
635
|
-
})
|
|
636
|
-
|
|
637
|
-
// Stop watching
|
|
638
|
-
feed.stop()
|
|
639
|
-
```
|
|
640
|
-
|
|
641
|
-
The watchDocs feed is useful for:
|
|
642
|
-
- Building real-time applications focused on specific documents
|
|
643
|
-
- Triggering actions when particular documents change
|
|
644
|
-
- Maintaining cached copies of frequently accessed documents
|
|
645
|
-
|
|
646
|
-
Advanced Config Options
|
|
647
|
-
=======================
|
|
699
|
+
### Advanced Config Options
|
|
648
700
|
|
|
649
701
|
The config object supports the following properties:
|
|
650
702
|
|
|
651
|
-
| Property
|
|
652
|
-
|
|
653
|
-
| couch
|
|
654
|
-
| throwOnGetNotFound | boolean
|
|
655
|
-
| bindWithRetry
|
|
656
|
-
| maxRetries
|
|
657
|
-
| initialDelay
|
|
658
|
-
| backoffFactor
|
|
659
|
-
| useConsoleLogger
|
|
660
|
-
| logger
|
|
703
|
+
| Property | Type | Default | Description |
|
|
704
|
+
| ------------------ | --------------- | --------- | ----------------------------------------------------------------------- |
|
|
705
|
+
| couch | string | required | The URL of the CouchDB database |
|
|
706
|
+
| throwOnGetNotFound | boolean | false | If true, throws an error when get() returns 404. If false, returns null |
|
|
707
|
+
| bindWithRetry | boolean | true | When using bindConfig(), adds retry logic to bound methods |
|
|
708
|
+
| maxRetries | number | 3 | Maximum number of retry attempts for retryable operations |
|
|
709
|
+
| initialDelay | number | 1000 | Initial delay in milliseconds before first retry |
|
|
710
|
+
| backoffFactor | number | 2 | Multiplier for exponential backoff between retries |
|
|
711
|
+
| useConsoleLogger | boolean | false | If true, enables console logging when no logger is provided |
|
|
712
|
+
| logger | object/function | undefined | Custom logging interface (winston-style object or function) |
|
|
661
713
|
|
|
662
714
|
Example configuration with all options:
|
|
715
|
+
|
|
663
716
|
```javascript
|
|
664
717
|
const config = {
|
|
665
718
|
couch: 'http://localhost:5984/mydb',
|
|
@@ -673,15 +726,13 @@ const config = {
|
|
|
673
726
|
}
|
|
674
727
|
```
|
|
675
728
|
|
|
676
|
-
|
|
677
|
-
Logging Support
|
|
678
|
-
==============
|
|
729
|
+
### Logging Support
|
|
679
730
|
|
|
680
731
|
The library supports flexible logging options that can be configured through the config object:
|
|
681
732
|
|
|
682
733
|
```javascript
|
|
683
734
|
// Enable console logging (error, warn, info, debug)
|
|
684
|
-
const config = {
|
|
735
|
+
const config = {
|
|
685
736
|
couch: 'http://localhost:5984/mydb',
|
|
686
737
|
useConsoleLogger: true
|
|
687
738
|
}
|
|
@@ -690,10 +741,10 @@ const config = {
|
|
|
690
741
|
const config = {
|
|
691
742
|
couch: 'http://localhost:5984/mydb',
|
|
692
743
|
logger: {
|
|
693
|
-
error:
|
|
694
|
-
warn:
|
|
695
|
-
info:
|
|
696
|
-
debug:
|
|
744
|
+
error: msg => console.error(msg),
|
|
745
|
+
warn: msg => console.warn(msg),
|
|
746
|
+
info: msg => console.info(msg),
|
|
747
|
+
debug: msg => console.debug(msg)
|
|
697
748
|
}
|
|
698
749
|
}
|
|
699
750
|
|
|
@@ -705,6 +756,7 @@ const config = {
|
|
|
705
756
|
```
|
|
706
757
|
|
|
707
758
|
The logger will track operations including:
|
|
759
|
+
|
|
708
760
|
- Document operations (get, put, patch)
|
|
709
761
|
- Bulk operations
|
|
710
762
|
- View queries
|
|
@@ -712,8 +764,8 @@ The logger will track operations including:
|
|
|
712
764
|
- Retries and error handling
|
|
713
765
|
|
|
714
766
|
Each operation logs appropriate information at these levels:
|
|
715
|
-
- error: Fatal/unrecoverable errors
|
|
716
|
-
- warn: Retryable errors, conflicts
|
|
717
|
-
- info: Operation start/completion
|
|
718
|
-
- debug: Detailed operation information
|
|
719
767
|
|
|
768
|
+
- error: Fatal/unrecoverable errors.
|
|
769
|
+
- warn: Retryable errors, conflicts.
|
|
770
|
+
- info: Operation start/completion.
|
|
771
|
+
- debug: Detailed operation information.
|