@ibm-cloud/cloudant 0.5.5 → 0.7.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/README.md +729 -72
- package/auth/couchdbSessionAuthenticator.js +2 -1
- package/auth/couchdbSessionAuthenticator.js.map +1 -1
- package/cloudant/features/changesFollower.d.ts +148 -0
- package/cloudant/features/changesFollower.js +178 -0
- package/cloudant/features/changesFollower.js.map +1 -0
- package/cloudant/features/changesParamsHelper.d.ts +32 -0
- package/cloudant/features/changesParamsHelper.js +99 -0
- package/cloudant/features/changesParamsHelper.js.map +1 -0
- package/cloudant/features/changesResultItemStream.d.ts +24 -0
- package/cloudant/features/changesResultItemStream.js +61 -0
- package/cloudant/features/changesResultItemStream.js.map +1 -0
- package/cloudant/features/changesResultIterator.d.ts +46 -0
- package/cloudant/features/changesResultIterator.js +296 -0
- package/cloudant/features/changesResultIterator.js.map +1 -0
- package/cloudant/features/stream.d.ts +23 -0
- package/cloudant/features/stream.js +60 -0
- package/cloudant/features/stream.js.map +1 -0
- package/cloudant/v1.d.ts +442 -168
- package/cloudant/v1.js +208 -65
- package/cloudant/v1.js.map +1 -1
- package/index.d.ts +2 -0
- package/index.js +5 -1
- package/index.js.map +1 -1
- package/lib/cloudantBaseService.d.ts +3 -2
- package/lib/cloudantBaseService.js +7 -6
- package/lib/cloudantBaseService.js.map +1 -1
- package/package.json +24 -23
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
[](https://github.com/IBM/cloudant-node-sdk/releases/latest)
|
|
3
3
|
[](https://ibm.github.io/cloudant-node-sdk/)
|
|
4
4
|
|
|
5
|
-
# IBM Cloudant Node.js SDK Version 0.
|
|
5
|
+
# IBM Cloudant Node.js SDK Version 0.7.0
|
|
6
6
|
|
|
7
7
|
IBM Cloudant Node.js SDK is a client library that interacts with the
|
|
8
8
|
[IBM Cloudant APIs](https://cloud.ibm.com/apidocs/cloudant?code=node).
|
|
@@ -32,14 +32,15 @@ to avoid surprises.
|
|
|
32
32
|
- [Features](#features)
|
|
33
33
|
- [Prerequisites](#prerequisites)
|
|
34
34
|
- [Installation](#installation)
|
|
35
|
-
- [Authentication](#authentication)
|
|
36
|
-
* [Authentication with environment variables](#authentication-with-environment-variables)
|
|
37
|
-
+ [IAM authentication](#iam-authentication)
|
|
38
|
-
+ [Session cookie authentication](#session-cookie-authentication)
|
|
39
|
-
+ [Basic authentication](#basic-authentication)
|
|
40
|
-
* [Authentication with external configuration](#authentication-with-external-configuration)
|
|
41
|
-
* [Programmatic authentication](#programmatic-authentication)
|
|
42
35
|
- [Using the SDK](#using-the-sdk)
|
|
36
|
+
* [Authentication](#authentication)
|
|
37
|
+
+ [Authentication with environment variables](#authentication-with-environment-variables)
|
|
38
|
+
- [IAM API key authentication](#iam-api-key-authentication)
|
|
39
|
+
- [IAM Trusted profile (container) authentication](#iam-trusted-profile-container-authentication)
|
|
40
|
+
- [IAM Trusted profile (VPC) authentication](#iam-trusted-profile-vpc-authentication)
|
|
41
|
+
- [Session cookie authentication](#session-cookie-authentication)
|
|
42
|
+
+ [Authentication with external configuration](#authentication-with-external-configuration)
|
|
43
|
+
+ [Programmatic authentication](#programmatic-authentication)
|
|
43
44
|
* [Request timeout configuration](#request-timeout-configuration)
|
|
44
45
|
* [Code examples](#code-examples)
|
|
45
46
|
+ [1. Create a database and add a document](#1-create-a-database-and-add-a-document)
|
|
@@ -50,8 +51,25 @@ to avoid surprises.
|
|
|
50
51
|
* [Error handling](#error-handling)
|
|
51
52
|
* [Raw IO](#raw-io)
|
|
52
53
|
* [Further resources](#further-resources)
|
|
54
|
+
* [Changes feed follower (beta)](#changes-feed-follower-beta)
|
|
55
|
+
+ [Introduction](#introduction)
|
|
56
|
+
+ [Modes of operation](#modes-of-operation)
|
|
57
|
+
+ [Configuring the changes follower](#configuring-the-changes-follower)
|
|
58
|
+
+ [Error suppression](#error-suppression)
|
|
59
|
+
+ [Follower operation](#follower-operation)
|
|
60
|
+
+ [Checkpointing](#checkpointing)
|
|
61
|
+
+ [Code examples](#code-examples-1)
|
|
62
|
+
- [Initializing a changes follower](#initializing-a-changes-follower)
|
|
63
|
+
- [Starting the changes follower](#starting-the-changes-follower)
|
|
64
|
+
* [Start mode for continuous listening](#start-mode-for-continuous-listening)
|
|
65
|
+
* [Start mode for one-off fetching](#start-mode-for-one-off-fetching)
|
|
66
|
+
- [Processing changes](#processing-changes)
|
|
67
|
+
* [Process continuous changes](#process-continuous-changes)
|
|
68
|
+
* [Process one-off changes](#process-one-off-changes)
|
|
69
|
+
- [Stopping the changes follower](#stopping-the-changes-follower)
|
|
53
70
|
- [Questions](#questions)
|
|
54
71
|
- [Issues](#issues)
|
|
72
|
+
- [Versioning and LTS support](#versioning-and-lts-support)
|
|
55
73
|
- [Open source at IBM](#open-source-at-ibm)
|
|
56
74
|
- [Contributing](#contributing)
|
|
57
75
|
- [License](#license)
|
|
@@ -85,6 +103,7 @@ project:
|
|
|
85
103
|
- Handles the authentication.
|
|
86
104
|
- Familiar user experience with IBM Cloud SDKs.
|
|
87
105
|
- Flexibility to use either built-in models or byte-based requests and responses for documents.
|
|
106
|
+
- Built-in [Changes feed follower](#changes-feed-follower-beta) (beta)
|
|
88
107
|
- `Promise` based design with asynchronous HTTP requests.
|
|
89
108
|
- Use either as native JavaScript or take advantage of TypeScript models.
|
|
90
109
|
- Transparently compresses request and response bodies.
|
|
@@ -92,11 +111,12 @@ project:
|
|
|
92
111
|
## Prerequisites
|
|
93
112
|
|
|
94
113
|
- A
|
|
95
|
-
[Cloudant](https://cloud.ibm.com/docs/Cloudant
|
|
114
|
+
[Cloudant](https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant)
|
|
96
115
|
service instance or a
|
|
97
116
|
[CouchDB](https://docs.couchdb.org/en/latest/install/index.html)
|
|
98
117
|
server.
|
|
99
|
-
- Node.js
|
|
118
|
+
- Node.js 18.
|
|
119
|
+
|
|
100
120
|
|
|
101
121
|
## Installation
|
|
102
122
|
|
|
@@ -104,90 +124,129 @@ project:
|
|
|
104
124
|
npm install @ibm-cloud/cloudant
|
|
105
125
|
```
|
|
106
126
|
|
|
107
|
-
## Authentication
|
|
108
127
|
|
|
109
|
-
|
|
110
|
-
[cloud-IAM-mgmt]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-managing-access-for-cloudant#introduction-iam-ai
|
|
111
|
-
[couch-cookie-auth]: https://docs.couchdb.org/en/stable/api/server/authn.html#cookie-authentication
|
|
112
|
-
[cloudant-cookie-auth]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-work-with-your-account#cookie-authentication
|
|
113
|
-
[couch-basic-auth]: https://docs.couchdb.org/en/stable/api/server/authn.html#basic-authentication
|
|
114
|
-
[cloudant-basic-auth]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-work-with-your-account#basic-authentication
|
|
128
|
+
## Using the SDK
|
|
115
129
|
|
|
116
|
-
|
|
130
|
+
For fundamental SDK usage information and config options, please see the common [IBM Cloud SDK](https://github.com/IBM/ibm-cloud-sdk-common/blob/main/README.md) documentation.
|
|
131
|
+
|
|
132
|
+
This library requires configuration with a service URL and
|
|
117
133
|
[Cloudant service credentials][service-credentials] to authenticate with your
|
|
118
134
|
account.
|
|
119
135
|
|
|
120
|
-
|
|
121
|
-
1. [*IAM authentication*](#iam-authentication) is highly recommended when your
|
|
122
|
-
back-end database server is [**Cloudant**][cloud-IAM-mgmt]. This
|
|
123
|
-
authentication type requires a server-generated `apikey` instead of a
|
|
124
|
-
user-given password. You can create one
|
|
125
|
-
[here](https://cloud.ibm.com/iam/apikeys).
|
|
126
|
-
1. [*Session cookie (`COUCHDB_SESSION`) authentication*](#session-cookie-authentication)
|
|
127
|
-
is recommended for [Apache CouchDB][couch-cookie-auth] or for
|
|
128
|
-
[Cloudant][cloudant-cookie-auth] when IAM is unavailable. It exchanges username
|
|
129
|
-
and password credentials for an `AuthSession` cookie from the `/_session`
|
|
130
|
-
endpoint.
|
|
131
|
-
1. [*Basic* (or legacy) *authentication*](#basic-authentication) is a fallback
|
|
132
|
-
for both [Cloudant][cloudant-basic-auth] and [Apache CouchDB][couch-basic-auth]
|
|
133
|
-
back-end database servers. This authentication type requires the good old
|
|
134
|
-
`username` and `password` credentials.
|
|
135
|
-
1. *Noauth* authentication does not require credentials. Note that this
|
|
136
|
-
authentication type only works with queries against a database with read
|
|
137
|
-
access for everyone.
|
|
138
|
-
1. The service `url`.
|
|
139
|
-
|
|
140
|
-
There are several ways to **set** these properties:
|
|
136
|
+
There are several ways to **set** these authentication properties:
|
|
141
137
|
|
|
142
138
|
1. As [environment variables](#authentication-with-environment-variables)
|
|
143
139
|
1. The [programmatic approach](#programmatic-authentication)
|
|
144
140
|
1. With an [external credentials file](#authentication-with-external-configuration)
|
|
145
141
|
|
|
146
|
-
|
|
142
|
+
The following section describes the different authentication types and provides environment variable examples.
|
|
143
|
+
Examples for other configuration methods are available by following the provided links.
|
|
147
144
|
|
|
148
|
-
|
|
145
|
+
### Authentication
|
|
149
146
|
|
|
150
|
-
|
|
147
|
+
[service-credentials]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-locating-your-service-credentials
|
|
148
|
+
[cloud-IAM-mgmt]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-managing-access-for-cloudant#introduction-iam-ai
|
|
149
|
+
[couch-cookie-auth]: https://docs.couchdb.org/en/stable/api/server/authn.html#cookie-authentication
|
|
150
|
+
[cloudant-cookie-auth]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-work-with-your-account#cookie-authentication
|
|
151
|
+
[couch-basic-auth]: https://docs.couchdb.org/en/stable/api/server/authn.html#basic-authentication
|
|
152
|
+
[couch-jwt-auth]: https://docs.couchdb.org/en/stable/api/server/authn.html#jwt-authentication
|
|
153
|
+
[cloudant-basic-auth]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-work-with-your-account#basic-authentication
|
|
154
|
+
[cloudant-legacy-api-keys]: https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-work-with-your-account#api-keys
|
|
155
|
+
[core-container-auth]: https://github.com/IBM/node-sdk-core/blob/main/Authentication.md#container-authentication
|
|
156
|
+
[core-vpc-auth]: https://github.com/IBM/node-sdk-core/blob/main/Authentication.md#vpc-instance-authentication
|
|
157
|
+
[core-iam-auth]: https://github.com/IBM/node-sdk-core/blob/main/Authentication.md#identity-and-access-management-iam-authentication
|
|
158
|
+
[core-bearer-auth]: https://github.com/IBM/node-sdk-core/blob/main/Authentication.md#bearer-token-authentication
|
|
159
|
+
[core-basic-auth]: https://github.com/IBM/node-sdk-core/blob/main/Authentication.md#basic-authentication
|
|
160
|
+
[core-no-auth]: https://github.com/IBM/node-sdk-core/blob/main/Authentication.md#no-auth-authentication
|
|
161
|
+
|
|
162
|
+
This library requires credentials to authenticate with IBM Cloudant. These credentials may be:
|
|
163
|
+
* IBM Cloud IAM credentials (can be used with authentication types `CONTAINER`, `VPC` and `IAM`)
|
|
164
|
+
* [IBM Cloud account][cloud-IAM-mgmt] user, service ID or trusted profile credentials
|
|
165
|
+
that have been granted access to the IBM Cloud Cloudant resource instance.
|
|
166
|
+
* [IBM Cloudant service credentials][service-credentials] generated by the IBM Cloud Cloudant resource instance.
|
|
167
|
+
* Username and password credentials (can be used with authentication types `COUCHDB_SESSION` and `BASIC`)
|
|
168
|
+
* [IBM Cloudant service credentials][service-credentials] generated for an IBM Cloud Cloudant resource instance not configured as `IAM only`.
|
|
169
|
+
* IBM Cloudant [legacy credentials][cloudant-basic-auth] (i.e. username and password) for instances not in IBM Cloud.
|
|
170
|
+
* IBM Cloudant [legacy API keys][cloudant-legacy-api-keys].
|
|
171
|
+
|
|
172
|
+
For other compatible APIs that are not Cloudant accounts (e.g. Apache CouchDB) non-IAM based authentication types
|
|
173
|
+
must be used.
|
|
174
|
+
|
|
175
|
+
This table summarizes the available authentication types.
|
|
176
|
+
The authentication types are listed in order of recommendation, preferably use the authentication type
|
|
177
|
+
from the first row in the table that is compatible with your environment.
|
|
178
|
+
|
|
179
|
+
| Authentication type | Recommended for | `AUTH_TYPE` | Description |
|
|
180
|
+
| --- | --- | --- | --- |
|
|
181
|
+
| IAM Trusted Profiles compute resource ([container][core-container-auth]) | Cloudant<BR>(SDK running in IBM Cloud IKS) | `CONTAINER` | Obtains a compute resource (CR) token from the container.<BR>Exchanges the CR token for an IAM `access_token`.<BR>Adds an `Authorization: Bearer <access_token>` header to each HTTP request.<BR>Automatically renews the access token when needed. |
|
|
182
|
+
| IAM Trusted Profiles compute resource ([VPC][core-vpc-auth]) | Cloudant<BR>(SDK running in IBM Cloud VPC) | `VPC` | Obtains an identity token from the VPC instance metadata.<BR>Exchanges the identity token for an IAM `access_token`.<BR>Adds an `Authorization: Bearer <access_token>` header to each HTTP request.<BR>Automatically renews the access token when needed. |
|
|
183
|
+
| [IAM API key][core-iam-auth] | Cloudant | `IAM` | Exchanges an IAM API key for an IAM `access_token`.<BR>Adds an `Authorization: Bearer <access_token>` header to each HTTP request.<BR>Automatically renews the access token when needed. |
|
|
184
|
+
| [Session cookie](#session-cookie-authentication) | [Cloudant][cloudant-cookie-auth]<BR>(legacy credentials & instances without IAM)<BR><BR>[Apache CouchDB][couch-cookie-auth] | `COUCHDB_SESSION` | Exchanges credentials with `/_session` endpoint to retrieve a cookie.<BR>Adds `Cookie` header and content to each HTTP request.<BR>Automatically renews session when needed. |
|
|
185
|
+
| [Bearer token][core-bearer-auth] | [Apache CouchDB][couch-jwt-auth]<BR>(using JWT authentication) | `BEARERTOKEN` | Adds an `Authorization: Bearer <token>` to each HTTP request.<BR>No token management or renewal.<BR>Also compatible with IAM access tokens managed independently of the SDK. |
|
|
186
|
+
| [Basic][core-basic-auth] | [Apache CouchDB][couch-basic-auth]<BR>(if cookies are not enabled) | `BASIC` | Adds an `Authorization: Basic <encoded username and password>` header to each HTTP request. |
|
|
187
|
+
| [None][core-no-auth] | - | `NOAUTH` | Note that this authentication type only works for operations against a database allowing access for unauthenticated users. |
|
|
188
|
+
|
|
189
|
+
The default authentication type for the SDK is `CONTAINER` unless `APIKEY` configuration is supplied, which changes the default authentication type to `IAM`.
|
|
190
|
+
|
|
191
|
+
#### Authentication with environment variables
|
|
192
|
+
|
|
193
|
+
The default service name is `CLOUDANT` so `CLOUDANT_` prefixed names are used in these examples.
|
|
194
|
+
|
|
195
|
+
Any custom service name prefix can be used as long as the matching name is used to instantiate the SDK client
|
|
196
|
+
and the same prefix is used for all configuration options.
|
|
197
|
+
|
|
198
|
+
##### IAM API key authentication
|
|
199
|
+
|
|
200
|
+
For Cloudant *IAM API key authentication*, set the following environmental variables by
|
|
151
201
|
replacing the `<url>` and `<apikey>` with your proper
|
|
152
202
|
[service credentials][service-credentials]. There is no need to set
|
|
153
|
-
`CLOUDANT_AUTH_TYPE` to `IAM` because it is the default.
|
|
203
|
+
`CLOUDANT_AUTH_TYPE` to `IAM` because it is the default when an `APIKEY` is set.
|
|
154
204
|
|
|
155
205
|
```bash
|
|
156
206
|
CLOUDANT_URL=<url>
|
|
157
207
|
CLOUDANT_APIKEY=<apikey>
|
|
158
208
|
```
|
|
159
209
|
|
|
160
|
-
|
|
210
|
+
##### IAM Trusted profile (container) authentication
|
|
161
211
|
|
|
162
|
-
For
|
|
163
|
-
|
|
164
|
-
|
|
212
|
+
For Cloudant *IAM Trusted profile compute resource container authentication*, set the following environmental variables by
|
|
213
|
+
replacing the `<url>` and `<id>` with your values. There is no need to set
|
|
214
|
+
`CLOUDANT_AUTH_TYPE` to `CONTAINER` because it is the default.
|
|
165
215
|
|
|
166
216
|
```bash
|
|
167
|
-
CLOUDANT_AUTH_TYPE=COUCHDB_SESSION
|
|
168
217
|
CLOUDANT_URL=<url>
|
|
169
|
-
|
|
170
|
-
|
|
218
|
+
CLOUDANT_IAM_PROFILE_ID=<id>
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Alternatively a profile name may be used instead of an ID by replacing `CLOUDANT_IAM_PROFILE_ID` with `CLOUDANT_IAM_PROFILE_NAME`.
|
|
222
|
+
|
|
223
|
+
##### IAM Trusted profile (VPC) authentication
|
|
224
|
+
|
|
225
|
+
For Cloudant *IAM Trusted profile compute resource vpc authentication*, set the following environmental variables by
|
|
226
|
+
replacing the `<url>` and `<id>` with your values.
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
CLOUDANT_AUTH_TYPE=VPC
|
|
230
|
+
CLOUDANT_URL=<url>
|
|
231
|
+
CLOUDANT_IAM_PROFILE_ID=<id>
|
|
171
232
|
```
|
|
172
233
|
|
|
173
|
-
|
|
234
|
+
Alternatively a profile CRN may be used instead of an ID by replacing `CLOUDANT_IAM_PROFILE_ID` with `CLOUDANT_IAM_PROFILE_CRN`.
|
|
235
|
+
|
|
236
|
+
##### Session cookie authentication
|
|
174
237
|
|
|
175
|
-
For
|
|
176
|
-
replacing the `<url>`, `<username>` and `<password>` with your proper
|
|
238
|
+
For `COUCHDB_SESSION` authentication, set the following environmental variables
|
|
239
|
+
by replacing the `<url>`, `<username>` and `<password>` with your proper
|
|
177
240
|
[service credentials][service-credentials].
|
|
178
241
|
|
|
179
242
|
```bash
|
|
180
|
-
CLOUDANT_AUTH_TYPE=
|
|
243
|
+
CLOUDANT_AUTH_TYPE=COUCHDB_SESSION
|
|
181
244
|
CLOUDANT_URL=<url>
|
|
182
245
|
CLOUDANT_USERNAME=<username>
|
|
183
246
|
CLOUDANT_PASSWORD=<password>
|
|
184
247
|
```
|
|
185
248
|
|
|
186
|
-
|
|
187
|
-
We recommend that you use [IAM](#iam-authentication) for Cloudant and
|
|
188
|
-
[Session](#session-cookie-authentication) for CouchDB authentication.
|
|
189
|
-
|
|
190
|
-
### Authentication with external configuration
|
|
249
|
+
#### Authentication with external configuration
|
|
191
250
|
|
|
192
251
|
To use an external configuration file, the
|
|
193
252
|
[Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node#authentication-with-external-configuration),
|
|
@@ -195,7 +254,7 @@ or the
|
|
|
195
254
|
[general SDK usage information](https://github.com/IBM/ibm-cloud-sdk-common#using-external-configuration)
|
|
196
255
|
will guide you.
|
|
197
256
|
|
|
198
|
-
|
|
257
|
+
#### Programmatic authentication
|
|
199
258
|
|
|
200
259
|
To learn more about how to use programmatic authentication, see the related
|
|
201
260
|
documentation in the
|
|
@@ -203,10 +262,6 @@ documentation in the
|
|
|
203
262
|
or in the
|
|
204
263
|
[Node.js SDK Core document](https://github.com/IBM/node-sdk-core/blob/main/Authentication.md) about authentication.
|
|
205
264
|
|
|
206
|
-
## Using the SDK
|
|
207
|
-
|
|
208
|
-
For fundamental SDK usage information and config options, please see the common [IBM Cloud SDK](https://github.com/IBM/ibm-cloud-sdk-common/blob/main/README.md) documentation.
|
|
209
|
-
|
|
210
265
|
### Request timeout configuration
|
|
211
266
|
|
|
212
267
|
No request timeout is defined, but a 2.5m connect and 2.5m read timeout is set by default. Be sure to set a request timeout appropriate to your application usage and environment.
|
|
@@ -241,7 +296,7 @@ Once the environment variables are set, you can try out the code examples.
|
|
|
241
296
|
<summary>TypeScript:</summary>
|
|
242
297
|
|
|
243
298
|
```ts
|
|
244
|
-
import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
299
|
+
import { CloudantV1 } from "@ibm-cloud/cloudant";
|
|
245
300
|
```
|
|
246
301
|
[embedmd]:# (test/examples/src/ts/CreateDbAndDoc.ts /interface/ $)
|
|
247
302
|
```ts
|
|
@@ -422,7 +477,7 @@ database or 'example' document was not found."
|
|
|
422
477
|
<summary>TypeScript:</summary>
|
|
423
478
|
|
|
424
479
|
```ts
|
|
425
|
-
import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
480
|
+
import { CloudantV1 } from "@ibm-cloud/cloudant";
|
|
426
481
|
```
|
|
427
482
|
[embedmd]:# (test/examples/src/ts/GetInfoFromExistingDatabase.ts /\/\/ 1./ $)
|
|
428
483
|
```ts
|
|
@@ -550,7 +605,7 @@ database or 'example' document was not found."
|
|
|
550
605
|
<summary>TypeScript:</summary>
|
|
551
606
|
|
|
552
607
|
```ts
|
|
553
|
-
import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
608
|
+
import { CloudantV1 } from "@ibm-cloud/cloudant";
|
|
554
609
|
```
|
|
555
610
|
[embedmd]:# (test/examples/src/ts/UpdateDoc.ts /interface/ $)
|
|
556
611
|
```ts
|
|
@@ -769,7 +824,7 @@ database or 'example' document was not found."
|
|
|
769
824
|
<summary>TypeScript:</summary>
|
|
770
825
|
|
|
771
826
|
```ts
|
|
772
|
-
import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
827
|
+
import { CloudantV1 } from "@ibm-cloud/cloudant";
|
|
773
828
|
```
|
|
774
829
|
[embedmd]:# (test/examples/src/ts/DeleteDoc.ts /interface/ $)
|
|
775
830
|
```ts
|
|
@@ -880,7 +935,7 @@ You have deleted the document.
|
|
|
880
935
|
|
|
881
936
|
#### Further code examples
|
|
882
937
|
|
|
883
|
-
For a complete list of code examples, see the [examples directory](examples#examples-for-node).
|
|
938
|
+
For a complete list of code examples, see the [examples directory](https://github.com/IBM/cloudant-node-sdk/tree/v0.7.0/examples#examples-for-node).
|
|
884
939
|
|
|
885
940
|
### Error handling
|
|
886
941
|
|
|
@@ -918,6 +973,7 @@ Expand them to see examples of:
|
|
|
918
973
|
- [Query a list of all documents in a database](https://cloud.ibm.com/apidocs/cloudant?code=node#postalldocs)
|
|
919
974
|
- [Query the database document changes feed](https://cloud.ibm.com/apidocs/cloudant?code=node#postchanges)
|
|
920
975
|
|
|
976
|
+
|
|
921
977
|
### Further resources
|
|
922
978
|
|
|
923
979
|
- [Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node):
|
|
@@ -929,6 +985,587 @@ Expand them to see examples of:
|
|
|
929
985
|
- [Cloudant blog](https://blog.cloudant.com/):
|
|
930
986
|
Many useful articles about how to optimize Cloudant for common problems.
|
|
931
987
|
|
|
988
|
+
### Changes feed follower (beta)
|
|
989
|
+
|
|
990
|
+
#### Introduction
|
|
991
|
+
|
|
992
|
+
The SDK provides a changes feed follower utility (currently beta).
|
|
993
|
+
This helper utility connects to the `_changes` endpoint and returns the individual change items.
|
|
994
|
+
It removes some of the complexity of using the `_changes` endpoint by setting some options automatically
|
|
995
|
+
and providing error suppression and retries.
|
|
996
|
+
|
|
997
|
+
*Tip: the changes feed often does not meet user expectations or assumptions.*
|
|
998
|
+
|
|
999
|
+
Consult the [Cloudant changes feed FAQ](https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-faq-using-changes-feed)
|
|
1000
|
+
to get a better understanding of the limitations and suitable use-cases before using the changes feed in your application.
|
|
1001
|
+
|
|
1002
|
+
#### Modes of operation
|
|
1003
|
+
|
|
1004
|
+
There are two modes of operation:
|
|
1005
|
+
* Start mode
|
|
1006
|
+
* Fetches the changes from the supplied `since` sequence (by default the feed will start from `now`).
|
|
1007
|
+
* Fetches all available changes and then continues listening for new changes indefinitely unless encountering an end condition.
|
|
1008
|
+
* An example use case for this mode is event driven workloads.
|
|
1009
|
+
* Start one-off mode
|
|
1010
|
+
* Fetches the changes from the supplied `since` sequence (by default the feed will start from the beginning).
|
|
1011
|
+
* Fetches all available changes and then stops when either there are no further changes pending or encountering an end condition.
|
|
1012
|
+
* An example use case for this mode is ETL style workloads.
|
|
1013
|
+
|
|
1014
|
+
#### Configuring the changes follower
|
|
1015
|
+
|
|
1016
|
+
The SDK's model of changes feed options is also used to configure the follower.
|
|
1017
|
+
However, a subset of the options are invalid as they are configured internally by the implementation.
|
|
1018
|
+
Supplying these options when instantiating the follower causes an error.
|
|
1019
|
+
The invalid options are:
|
|
1020
|
+
* `descending`
|
|
1021
|
+
* `feed`
|
|
1022
|
+
* `heartbeat`
|
|
1023
|
+
* `lastEventId` - use `since` instead
|
|
1024
|
+
* `timeout`
|
|
1025
|
+
* Only the value of `_selector` is permitted for the `filter` option. This restriction is because selector
|
|
1026
|
+
based filters perform better than JavaScript backed filters. Configuring a non-selector based filter will
|
|
1027
|
+
cause the follower to error.
|
|
1028
|
+
|
|
1029
|
+
Note that the `limit` parameter will terminate the follower at the given number of changes in either
|
|
1030
|
+
operating mode.
|
|
1031
|
+
|
|
1032
|
+
The changes follower requires the client to have HTTP timeouts of at least 1 minute and will error during
|
|
1033
|
+
instantiation if it is insufficient. The default client configuration has sufficiently long timeouts.
|
|
1034
|
+
|
|
1035
|
+
For use-cases where these configuration limitations are deemed too restrictive then it is recommended to
|
|
1036
|
+
write code to use the SDK's [POST `_changes` API](https://github.com/IBM/cloudant-node-sdk/tree/v0.7.0/examples#postchanges) instead of the follower.
|
|
1037
|
+
|
|
1038
|
+
#### Error suppression
|
|
1039
|
+
|
|
1040
|
+
By default, the changes follower will suppress transient errors indefinitely and attempt to run to completion or listen forever as
|
|
1041
|
+
dictated by the operating mode.
|
|
1042
|
+
For applications where that is not desirable an optional error tolerance duration may be specified to control the time since
|
|
1043
|
+
the last successful response that transient errors will be suppressed. This can be used, for example, by applications as a grace period
|
|
1044
|
+
before reporting an error and requiring intervention.
|
|
1045
|
+
|
|
1046
|
+
There are some additional points to consider for error suppression:
|
|
1047
|
+
* Errors considered terminal, for example, the database not existing or invalid credentials are never suppressed and will error immediately.
|
|
1048
|
+
* The error suppression duration is not guaranteed to fire immediately after lapsing and should be considered a minimum suppression time.
|
|
1049
|
+
* The changes follower will back-off between retries and as such may remain paused for a short while after the transient errors have resolved.
|
|
1050
|
+
* If the underlying SDK client used to initialize the follower also has retries configured then errors could be suppressed for significantly
|
|
1051
|
+
longer than the follower's configured error tolerance duration depending on the configuration options.
|
|
1052
|
+
|
|
1053
|
+
#### Follower operation
|
|
1054
|
+
|
|
1055
|
+
For both modes:
|
|
1056
|
+
* The end conditions are:
|
|
1057
|
+
* A terminal error (HTTP codes `400`, `401`, `403` `404`).
|
|
1058
|
+
* Transient errors occur for longer than the error tolerance duration. Transient errors are all other HTTP status codes and connection errors.
|
|
1059
|
+
* The number of changes received reaches the configured `limit`.
|
|
1060
|
+
* The feed is terminated early by calling stop.
|
|
1061
|
+
|
|
1062
|
+
As is true for the `_changes` endpoint change items have _at least once_ delivery and an individual item
|
|
1063
|
+
may be received multiple times. When using the follower change items may be repeated even within a limited
|
|
1064
|
+
number of changes (i.e. using the `limit` option) this is a minor difference from using `limit` on the HTTP native API.
|
|
1065
|
+
|
|
1066
|
+
The follower is not optimized for some use cases and it is not recommended to use it in cases where:
|
|
1067
|
+
* Setting `include_docs` and larger document sizes (for example > 10 kiB).
|
|
1068
|
+
* The volume of changes is very high (if the rate of changes in the database exceeds the follower's rate of pulling them it will never catch-up).
|
|
1069
|
+
|
|
1070
|
+
In these cases use-case specific control over the number of change requests made and the content size of the responses
|
|
1071
|
+
may be achieved by using the SDK's [POST `_changes` API](https://github.com/IBM/cloudant-node-sdk/tree/v0.7.0/examples#postchanges).
|
|
1072
|
+
|
|
1073
|
+
#### Checkpointing
|
|
1074
|
+
|
|
1075
|
+
The changes follower does not checkpoint since it has no information about whether a change item has been
|
|
1076
|
+
processed by the consuming application after being received. It is the application developer's responsibility
|
|
1077
|
+
to store the sequence IDs to have appropriate checkpoints and to re-initialize the follower with the required
|
|
1078
|
+
`since` value after, for example, the application restarts.
|
|
1079
|
+
|
|
1080
|
+
The frequency and conditions for checkpointing are application specific and some applications may be tolerant
|
|
1081
|
+
of dropped changes. This section is intended only to provide general guidance on how to avoid missing changes.
|
|
1082
|
+
|
|
1083
|
+
To guarantee processing of all changes the sequence ID from a change item must not be persisted until _after_
|
|
1084
|
+
the processing of the change item by the application has completed. As indicated previously change items are
|
|
1085
|
+
delivered _at least once_ so application code must be able to handle repeated changes already and it is
|
|
1086
|
+
preferable to restart from an older `since` value and receive changes again than risk missing them.
|
|
1087
|
+
|
|
1088
|
+
The sequence IDs are available on each change item by default, but may be omitted from some change items when
|
|
1089
|
+
using the `seq_interval` configuration option. Infrequent sequence IDs may improve performance by reducing
|
|
1090
|
+
the amount of data that needs to be transferred, but the trade-off is that more changes will be repeated if
|
|
1091
|
+
it is necessary to resume the changes follower.
|
|
1092
|
+
|
|
1093
|
+
Extreme care should be taken with persisting sequences if choosing to process change items in parallel as there
|
|
1094
|
+
is a considerable risk of missing changes on a restart if the sequence is recorded out of order.
|
|
1095
|
+
|
|
1096
|
+
#### Code examples
|
|
1097
|
+
|
|
1098
|
+
##### Initializing a changes follower
|
|
1099
|
+
<details open>
|
|
1100
|
+
<summary>TypeScript:</summary>
|
|
1101
|
+
|
|
1102
|
+
```ts
|
|
1103
|
+
import { ChangesFollower, CloudantV1 } from '@ibm-cloud/cloudant';
|
|
1104
|
+
import { PostChangesParams } from '@ibm-cloud/cloudant/cloudant/v1';
|
|
1105
|
+
```
|
|
1106
|
+
[embedmd]:# (test/examples/src/features/ts/initialize.ts /const client/ $)
|
|
1107
|
+
```ts
|
|
1108
|
+
const client = CloudantV1.newInstance({});
|
|
1109
|
+
const changesParams: PostChangesParams = {
|
|
1110
|
+
db: 'example', // Required: the database name.
|
|
1111
|
+
limit: 100, // Optional: return only 100 changes (including duplicates).
|
|
1112
|
+
since: '3-g1AG3...' // Optional: start from this sequence ID (e.g. with a value read from persistent storage).
|
|
1113
|
+
};
|
|
1114
|
+
const errorTolerance: number = 10000; // 10 second duration to suppress transient errors
|
|
1115
|
+
const changesFollower: ChangesFollower = new ChangesFollower(
|
|
1116
|
+
client, // Required: the Cloudant service client instance.
|
|
1117
|
+
changesParams, // Required: changes feed configuration options dict.
|
|
1118
|
+
10000 // Optional: suppress transient errors for at least 10 seconds before terminating.
|
|
1119
|
+
);
|
|
1120
|
+
```
|
|
1121
|
+
</details>
|
|
1122
|
+
|
|
1123
|
+
<details>
|
|
1124
|
+
<summary>JavaScript:</summary>
|
|
1125
|
+
|
|
1126
|
+
```js
|
|
1127
|
+
const { ChangesFollower, CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
1128
|
+
```
|
|
1129
|
+
[embedmd]:# (test/examples/src/features/js/initialize.js /const client/ $)
|
|
1130
|
+
```js
|
|
1131
|
+
const client = CloudantV1.newInstance();
|
|
1132
|
+
const changesParams = {
|
|
1133
|
+
db: 'example', // Required: the database name.
|
|
1134
|
+
limit: 100, // Optional: return only 100 changes (including duplicates).
|
|
1135
|
+
since: '3-g1AG3...' // Optional: start from this sequence ID (e.g. with a value read from persistent storage).
|
|
1136
|
+
};
|
|
1137
|
+
const changesFollower = new ChangesFollower(
|
|
1138
|
+
client, // Required: the Cloudant service client instance.
|
|
1139
|
+
changesParams, // Required: changes feed configuration options dict.
|
|
1140
|
+
10000 // Optional: suppress transient errors for at least 10 seconds before terminating.
|
|
1141
|
+
);
|
|
1142
|
+
```
|
|
1143
|
+
</details>
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
##### Starting the changes follower
|
|
1147
|
+
|
|
1148
|
+
###### Start mode for continuous listening
|
|
1149
|
+
<details open>
|
|
1150
|
+
<summary>TypeScript:</summary>
|
|
1151
|
+
|
|
1152
|
+
```ts
|
|
1153
|
+
import { ChangesFollower, CloudantV1, Stream } from '@ibm-cloud/cloudant';
|
|
1154
|
+
import { ChangesResultItem, PostChangesParams } from '@ibm-cloud/cloudant/cloudant/v1';
|
|
1155
|
+
```
|
|
1156
|
+
[embedmd]:# (test/examples/src/features/ts/start.ts /const client/ $)
|
|
1157
|
+
```ts
|
|
1158
|
+
const client = CloudantV1.newInstance({});
|
|
1159
|
+
const changesParams: PostChangesParams = {
|
|
1160
|
+
db: 'example'
|
|
1161
|
+
};
|
|
1162
|
+
const changesFollower: ChangesFollower = new ChangesFollower(client, changesParams);
|
|
1163
|
+
const changesItemsStream: Stream<ChangesResultItem> = changesFollower.start();
|
|
1164
|
+
// Create for-async-loop or pipeline to begin the flow of changes
|
|
1165
|
+
// e.g. pipeline(changesItemsStream, destinationStream).then(() => { ... }).catch((err) => { ... });
|
|
1166
|
+
```
|
|
1167
|
+
</details>
|
|
1168
|
+
|
|
1169
|
+
<details>
|
|
1170
|
+
<summary>JavaScript:</summary>
|
|
1171
|
+
|
|
1172
|
+
```js
|
|
1173
|
+
const { ChangesFollower, CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
1174
|
+
```
|
|
1175
|
+
[embedmd]:# (test/examples/src/features/js/start.js /const client/ $)
|
|
1176
|
+
```js
|
|
1177
|
+
const client = CloudantV1.newInstance();
|
|
1178
|
+
const changesParams = {
|
|
1179
|
+
db: 'example'
|
|
1180
|
+
};
|
|
1181
|
+
const changesFollower = new ChangesFollower(client, changesParams);
|
|
1182
|
+
const changesItemsStream = changesFollower.start();
|
|
1183
|
+
// Create for-async-loop or pipeline to begin the flow of changes
|
|
1184
|
+
// e.g. pipeline(changesItemsStream, destinationStream).then(() => { ... }).catch((err) => { ... });
|
|
1185
|
+
```
|
|
1186
|
+
</details>
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
###### Start mode for one-off fetching
|
|
1190
|
+
<details open>
|
|
1191
|
+
<summary>TypeScript:</summary>
|
|
1192
|
+
|
|
1193
|
+
```ts
|
|
1194
|
+
import { ChangesFollower, CloudantV1, Stream } from '@ibm-cloud/cloudant';
|
|
1195
|
+
import { ChangesResultItem, PostChangesParams } from '@ibm-cloud/cloudant/cloudant/v1';
|
|
1196
|
+
```
|
|
1197
|
+
[embedmd]:# (test/examples/src/features/ts/startOneOff.ts /const client/ $)
|
|
1198
|
+
```ts
|
|
1199
|
+
const client = CloudantV1.newInstance({});
|
|
1200
|
+
const changesParams: PostChangesParams = {
|
|
1201
|
+
db: 'example'
|
|
1202
|
+
};
|
|
1203
|
+
const changesFollower: ChangesFollower = new ChangesFollower(client, changesParams);
|
|
1204
|
+
const changesItemsStream: Stream<ChangesResultItem> = changesFollower.startOneOff();
|
|
1205
|
+
// Create for-async-loop or pipeline to begin the flow of changes
|
|
1206
|
+
// e.g. pipeline(changesItemsStream, destinationStream).then(() => { ... }).catch((err) => { ... });
|
|
1207
|
+
```
|
|
1208
|
+
</details>
|
|
1209
|
+
|
|
1210
|
+
<details>
|
|
1211
|
+
<summary>JavaScript:</summary>
|
|
1212
|
+
|
|
1213
|
+
```js
|
|
1214
|
+
const { ChangesFollower, CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
1215
|
+
```
|
|
1216
|
+
[embedmd]:# (test/examples/src/features/js/startOneOff.js /const client/ $)
|
|
1217
|
+
```js
|
|
1218
|
+
const client = CloudantV1.newInstance();
|
|
1219
|
+
const changesParams = {
|
|
1220
|
+
db: 'example'
|
|
1221
|
+
};
|
|
1222
|
+
const changesFollower = new ChangesFollower(client, changesParams);
|
|
1223
|
+
const changesItemsStream = changesFollower.startOneOff();
|
|
1224
|
+
// Create for-async-loop or pipeline to begin the flow of changes
|
|
1225
|
+
// e.g. pipeline(changesItemsStream, destinationStream).then(() => { ... }).catch((err) => { ... });
|
|
1226
|
+
```
|
|
1227
|
+
</details>
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
##### Processing changes
|
|
1231
|
+
|
|
1232
|
+
###### Process continuous changes
|
|
1233
|
+
<details open>
|
|
1234
|
+
<summary>TypeScript:</summary>
|
|
1235
|
+
|
|
1236
|
+
```ts
|
|
1237
|
+
import { ChangesFollower, CloudantV1, Stream } from '@ibm-cloud/cloudant';
|
|
1238
|
+
import { ChangesResultItem, PostChangesParams } from '@ibm-cloud/cloudant/cloudant/v1';
|
|
1239
|
+
import { Writable } from 'node:stream';
|
|
1240
|
+
import { pipeline } from 'node:stream/promises'
|
|
1241
|
+
```
|
|
1242
|
+
[embedmd]:# (test/examples/src/features/ts/startAndProcess.ts /const client/ $)
|
|
1243
|
+
```ts
|
|
1244
|
+
const client = CloudantV1.newInstance({});
|
|
1245
|
+
// Start from a previously persisted seq
|
|
1246
|
+
// Normally this would be read by the app from persistent storage
|
|
1247
|
+
// e.g. previouslyPersistedSeq = yourAppPersistenceReadFunc()
|
|
1248
|
+
const previouslyPersistedSeq = '3-g1AG3...'
|
|
1249
|
+
const changesParams: PostChangesParams = {
|
|
1250
|
+
db: 'example',
|
|
1251
|
+
since: previouslyPersistedSeq
|
|
1252
|
+
};
|
|
1253
|
+
const changesFollower = new ChangesFollower(client, changesParams);
|
|
1254
|
+
const changesItemsStream: Stream<ChangesResultItem> = changesFollower.start();
|
|
1255
|
+
|
|
1256
|
+
const destinationStream = new Writable({
|
|
1257
|
+
objectMode: true,
|
|
1258
|
+
write(changesItem: CloudantV1.ChangesResultItem, _, callback) {
|
|
1259
|
+
// do something with change item
|
|
1260
|
+
console.log(changesItem.id);
|
|
1261
|
+
for (const change of changesItem.changes) {
|
|
1262
|
+
console.log(change.rev)
|
|
1263
|
+
}
|
|
1264
|
+
// when change item processing is complete app can store seq
|
|
1265
|
+
const seq = changesItem.seq
|
|
1266
|
+
// write seq to persistent storage for use as since if required to resume later
|
|
1267
|
+
// e.g. yourAppPersistenceWriteFunc()
|
|
1268
|
+
callback();
|
|
1269
|
+
}
|
|
1270
|
+
});
|
|
1271
|
+
|
|
1272
|
+
// A pipeline to keep processing changes until the follower is stopped or some other stop condition is reached
|
|
1273
|
+
pipeline(changesItemsStream, destinationStream)
|
|
1274
|
+
.then(() => {
|
|
1275
|
+
console.log('Stopped');
|
|
1276
|
+
})
|
|
1277
|
+
.catch((err) => {
|
|
1278
|
+
console.log(err);
|
|
1279
|
+
});
|
|
1280
|
+
```
|
|
1281
|
+
</details>
|
|
1282
|
+
|
|
1283
|
+
<details>
|
|
1284
|
+
<summary>JavaScript:</summary>
|
|
1285
|
+
|
|
1286
|
+
```js
|
|
1287
|
+
const { ChangesFollower, CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
1288
|
+
const { Writable } = require('node:stream');
|
|
1289
|
+
const { pipeline } = require('node:stream/promises');
|
|
1290
|
+
```
|
|
1291
|
+
[embedmd]:# (test/examples/src/features/js/startAndProcess.js /const client/ $)
|
|
1292
|
+
```js
|
|
1293
|
+
const client = CloudantV1.newInstance();
|
|
1294
|
+
// Start from a previously persisted seq
|
|
1295
|
+
// Normally this would be read by the app from persistent storage
|
|
1296
|
+
// e.g. previouslyPersistedSeq = yourAppPersistenceReadFunc()
|
|
1297
|
+
const previouslyPersistedSeq = '3-g1AG3...'
|
|
1298
|
+
const changesParams = {
|
|
1299
|
+
db: 'example',
|
|
1300
|
+
since: previouslyPersistedSeq
|
|
1301
|
+
};
|
|
1302
|
+
const changesFollower = new ChangesFollower(client, changesParams);
|
|
1303
|
+
const changesItemsStream = changesFollower.start();
|
|
1304
|
+
|
|
1305
|
+
const destinationStream = new Writable({
|
|
1306
|
+
objectMode: true,
|
|
1307
|
+
write(changesItem, _, callback) {
|
|
1308
|
+
// do something with change item
|
|
1309
|
+
console.log(changesItem.id);
|
|
1310
|
+
for (const change of changesItem.changes) {
|
|
1311
|
+
console.log(change.rev)
|
|
1312
|
+
}
|
|
1313
|
+
// when change item processing is complete app can store seq
|
|
1314
|
+
const seq = changesItem.seq
|
|
1315
|
+
// write seq to persistent storage for use as since if required to resume later
|
|
1316
|
+
// e.g. yourAppPersistenceWriteFunc()
|
|
1317
|
+
callback();
|
|
1318
|
+
}
|
|
1319
|
+
});
|
|
1320
|
+
|
|
1321
|
+
// A pipeline to keep processing changes until the follower is stopped or some other stop condition is reached
|
|
1322
|
+
pipeline(changesItemsStream, destinationStream)
|
|
1323
|
+
.then(() => {
|
|
1324
|
+
console.log('Stopped');
|
|
1325
|
+
})
|
|
1326
|
+
.catch((err) => {
|
|
1327
|
+
console.log(err);
|
|
1328
|
+
});
|
|
1329
|
+
```
|
|
1330
|
+
</details>
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
###### Process one-off changes
|
|
1334
|
+
<details open>
|
|
1335
|
+
<summary>TypeScript:</summary>
|
|
1336
|
+
|
|
1337
|
+
```ts
|
|
1338
|
+
import { ChangesFollower, CloudantV1, Stream } from '@ibm-cloud/cloudant';
|
|
1339
|
+
import { ChangesResultItem, PostChangesParams } from '@ibm-cloud/cloudant/cloudant/v1';
|
|
1340
|
+
import { Writable } from 'node:stream';
|
|
1341
|
+
import { pipeline } from 'node:stream/promises'
|
|
1342
|
+
```
|
|
1343
|
+
[embedmd]:# (test/examples/src/features/ts/startOneOffAndProcess.ts /const client/ $)
|
|
1344
|
+
```ts
|
|
1345
|
+
const client = CloudantV1.newInstance({});
|
|
1346
|
+
// Start from a previously persisted seq
|
|
1347
|
+
// Normally this would be read by the app from persistent storage
|
|
1348
|
+
// e.g. previouslyPersistedSeq = yourAppPersistenceReadFunc()
|
|
1349
|
+
const previouslyPersistedSeq = '3-g1AG3...'
|
|
1350
|
+
const changesParams: PostChangesParams = {
|
|
1351
|
+
db: 'example',
|
|
1352
|
+
since: previouslyPersistedSeq
|
|
1353
|
+
};
|
|
1354
|
+
const changesFollower: ChangesFollower = new ChangesFollower(client, changesParams);
|
|
1355
|
+
const changesItemsStream: Stream<ChangesResultItem> = changesFollower.startOneOff();
|
|
1356
|
+
|
|
1357
|
+
const destinationStream = new Writable({
|
|
1358
|
+
objectMode: true,
|
|
1359
|
+
write(changesItem: CloudantV1.ChangesResultItem, _, callback) {
|
|
1360
|
+
// do something with change item
|
|
1361
|
+
console.log(changesItem.id);
|
|
1362
|
+
for (const change of changesItem.changes) {
|
|
1363
|
+
console.log(change.rev)
|
|
1364
|
+
}
|
|
1365
|
+
// when change item processing is complete app can store seq
|
|
1366
|
+
const seq = changesItem.seq
|
|
1367
|
+
// write seq to persistent storage for use as since if required to resume later
|
|
1368
|
+
// e.g. yourAppPersistenceWriteFunc()
|
|
1369
|
+
callback();
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
|
|
1373
|
+
pipeline(changesItemsStream, destinationStream)
|
|
1374
|
+
.then(() => {
|
|
1375
|
+
console.log('All changes done');
|
|
1376
|
+
})
|
|
1377
|
+
.catch((err) => {
|
|
1378
|
+
console.log(err);
|
|
1379
|
+
});
|
|
1380
|
+
|
|
1381
|
+
// use for-async-loop feature for stream
|
|
1382
|
+
/*
|
|
1383
|
+
getChangesFromFollower(changesItemsStream);
|
|
1384
|
+
async function getChangesFromFollower(changesItemsStream: Stream<CloudantV1.ChangesResultItem>) {
|
|
1385
|
+
for await (const changesItem of changesItemsStream) {
|
|
1386
|
+
// do something with change item
|
|
1387
|
+
// write seq to persistent storage for use as since
|
|
1388
|
+
console.log(changesItem.id);
|
|
1389
|
+
for (const change of changesItem.changes) {
|
|
1390
|
+
console.log(change.rev)
|
|
1391
|
+
}
|
|
1392
|
+
// when change item processing is complete app can store seq
|
|
1393
|
+
seq = changesItem.seq
|
|
1394
|
+
// write seq to persistent storage for use as since if required to resume later
|
|
1395
|
+
// e.g. yourAppPersistenceWriteFunc()
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
*/
|
|
1399
|
+
```
|
|
1400
|
+
</details>
|
|
1401
|
+
|
|
1402
|
+
<details>
|
|
1403
|
+
<summary>JavaScript:</summary>
|
|
1404
|
+
|
|
1405
|
+
```js
|
|
1406
|
+
const { ChangesFollower, CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
1407
|
+
const { Writable } = require('node:stream');
|
|
1408
|
+
const { pipeline } = require('node:stream/promises');
|
|
1409
|
+
```
|
|
1410
|
+
[embedmd]:# (test/examples/src/features/js/startOneOffAndProcess.js /const client/ $)
|
|
1411
|
+
```js
|
|
1412
|
+
const client = CloudantV1.newInstance();
|
|
1413
|
+
// Start from a previously persisted seq
|
|
1414
|
+
// Normally this would be read by the app from persistent storage
|
|
1415
|
+
// e.g. previouslyPersistedSeq = yourAppPersistenceReadFunc()
|
|
1416
|
+
const previouslyPersistedSeq = '3-g1AG3...'
|
|
1417
|
+
const changesParams = {
|
|
1418
|
+
db: 'example',
|
|
1419
|
+
since: previouslyPersistedSeq
|
|
1420
|
+
};
|
|
1421
|
+
const changesFollower = new ChangesFollower(client, changesParams);
|
|
1422
|
+
const changesItemsStream = changesFollower.startOneOff();
|
|
1423
|
+
|
|
1424
|
+
const destinationStream = new Writable({
|
|
1425
|
+
objectMode: true,
|
|
1426
|
+
write(changesItem, _, callback) {
|
|
1427
|
+
// do something with change item
|
|
1428
|
+
console.log(changesItem.id);
|
|
1429
|
+
for (const change of changesItem.changes) {
|
|
1430
|
+
console.log(change.rev)
|
|
1431
|
+
}
|
|
1432
|
+
// when change item processing is complete app can store seq
|
|
1433
|
+
const seq = changesItem.seq
|
|
1434
|
+
// write seq to persistent storage for use as since if required to resume later
|
|
1435
|
+
// e.g. yourAppPersistenceWriteFunc()
|
|
1436
|
+
callback();
|
|
1437
|
+
}
|
|
1438
|
+
});
|
|
1439
|
+
|
|
1440
|
+
pipeline(changesItemsStream, destinationStream)
|
|
1441
|
+
.then(() => {
|
|
1442
|
+
console.log('All changes done');
|
|
1443
|
+
})
|
|
1444
|
+
.catch((err) => {
|
|
1445
|
+
console.log(err);
|
|
1446
|
+
});
|
|
1447
|
+
|
|
1448
|
+
// use for-async-loop feature for stream
|
|
1449
|
+
/*
|
|
1450
|
+
getChangesFromFollower(changesItemsStream);
|
|
1451
|
+
async function getChangesFromFollower(changesItemsStream) {
|
|
1452
|
+
for await (const changesItem of changesItemsStream) {
|
|
1453
|
+
// do something with change item
|
|
1454
|
+
// write seq to persistent storage for use as since
|
|
1455
|
+
console.log(changesItem.id);
|
|
1456
|
+
for (const change of changesItem.changes) {
|
|
1457
|
+
console.log(change.rev)
|
|
1458
|
+
}
|
|
1459
|
+
// when change item processing is complete app can store seq
|
|
1460
|
+
seq = changesItem.seq
|
|
1461
|
+
// write seq to persistent storage for use as since if required to resume later
|
|
1462
|
+
// e.g. yourAppPersistenceWriteFunc()
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
*/
|
|
1466
|
+
```
|
|
1467
|
+
</details>
|
|
1468
|
+
|
|
1469
|
+
|
|
1470
|
+
##### Stopping the changes follower
|
|
1471
|
+
<details open>
|
|
1472
|
+
<summary>TypeScript:</summary>
|
|
1473
|
+
|
|
1474
|
+
```ts
|
|
1475
|
+
import { ChangesFollower, CloudantV1, Stream } from '@ibm-cloud/cloudant';
|
|
1476
|
+
import { ChangesResultItem, PostChangesParams } from '@ibm-cloud/cloudant/cloudant/v1';
|
|
1477
|
+
import { Writable } from 'node:stream';
|
|
1478
|
+
import { pipeline } from 'node:stream/promises'
|
|
1479
|
+
```
|
|
1480
|
+
[embedmd]:# (test/examples/src/features/ts/stop.ts /const client/ $)
|
|
1481
|
+
```ts
|
|
1482
|
+
const client = CloudantV1.newInstance({});
|
|
1483
|
+
const changesParams: PostChangesParams = {
|
|
1484
|
+
db: 'example'
|
|
1485
|
+
};
|
|
1486
|
+
const changesFollower: ChangesFollower = new ChangesFollower(client, changesParams);
|
|
1487
|
+
const changesItemsStream: Stream<ChangesResultItem> = changesFollower.start();
|
|
1488
|
+
|
|
1489
|
+
const destinationStream = new Writable({
|
|
1490
|
+
objectMode: true,
|
|
1491
|
+
write(changesItem: CloudantV1.ChangesResultItem, _, callback) {
|
|
1492
|
+
// Option 1: call stop after some condition
|
|
1493
|
+
// Note that at least one item
|
|
1494
|
+
// must be returned to reach to this point.
|
|
1495
|
+
// Additional changes may be processed before the iterator stops.
|
|
1496
|
+
changesFollower.stop();
|
|
1497
|
+
callback();
|
|
1498
|
+
}
|
|
1499
|
+
});
|
|
1500
|
+
|
|
1501
|
+
pipeline(changesItemsStream, destinationStream)
|
|
1502
|
+
.then(() => {
|
|
1503
|
+
console.log('Stopped');
|
|
1504
|
+
})
|
|
1505
|
+
.catch((err) => {
|
|
1506
|
+
console.log(err);
|
|
1507
|
+
});
|
|
1508
|
+
|
|
1509
|
+
// Option 2: call stop method when you want to end the continuous loop from
|
|
1510
|
+
// outside the pipeline.
|
|
1511
|
+
// Normally the call would be made from some other application function
|
|
1512
|
+
// executing later.
|
|
1513
|
+
// For example, stop the changesFollower after 1 minute of listening for changes
|
|
1514
|
+
setTimeout(() => {
|
|
1515
|
+
changesFollower.stop();
|
|
1516
|
+
}, 60000);
|
|
1517
|
+
```
|
|
1518
|
+
</details>
|
|
1519
|
+
|
|
1520
|
+
<details>
|
|
1521
|
+
<summary>JavaScript:</summary>
|
|
1522
|
+
|
|
1523
|
+
```js
|
|
1524
|
+
const { ChangesFollower, CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
1525
|
+
const { Writable } = require('node:stream');
|
|
1526
|
+
const { pipeline } = require('node:stream/promises');
|
|
1527
|
+
```
|
|
1528
|
+
[embedmd]:# (test/examples/src/features/js/stop.js /const client/ $)
|
|
1529
|
+
```js
|
|
1530
|
+
const client = CloudantV1.newInstance();
|
|
1531
|
+
const changesParams = {
|
|
1532
|
+
db: 'example'
|
|
1533
|
+
};
|
|
1534
|
+
const changesFollower = new ChangesFollower(client, changesParams);
|
|
1535
|
+
const changesItemsStream = changesFollower.start();
|
|
1536
|
+
|
|
1537
|
+
const destinationStream = new Writable({
|
|
1538
|
+
objectMode: true,
|
|
1539
|
+
write(changesItem, _, callback) {
|
|
1540
|
+
// Option 1: call stop after some condition
|
|
1541
|
+
// Note that at least one item
|
|
1542
|
+
// must be returned to reach to this point.
|
|
1543
|
+
// Additional changes may be processed before the iterator stops.
|
|
1544
|
+
changesFollower.stop();
|
|
1545
|
+
callback();
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
1548
|
+
|
|
1549
|
+
pipeline(changesItemsStream, destinationStream)
|
|
1550
|
+
.then(() =>{
|
|
1551
|
+
console.log('Stopped');
|
|
1552
|
+
})
|
|
1553
|
+
.catch((err) => {
|
|
1554
|
+
console.log(err);
|
|
1555
|
+
});
|
|
1556
|
+
|
|
1557
|
+
// Option 2: call stop method when you want to end the continuous loop from
|
|
1558
|
+
// outside the pipeline.
|
|
1559
|
+
// Normally the call would be made from some other application function
|
|
1560
|
+
// executing later.
|
|
1561
|
+
// For example, stop the changesFollower after 1 minute of listening for changes
|
|
1562
|
+
setTimeout(() => {
|
|
1563
|
+
changesFollower.stop();
|
|
1564
|
+
}, 60000);
|
|
1565
|
+
```
|
|
1566
|
+
</details>
|
|
1567
|
+
|
|
1568
|
+
|
|
932
1569
|
## Questions
|
|
933
1570
|
|
|
934
1571
|
If you are having difficulties using this SDK or have a question about the
|
|
@@ -942,18 +1579,38 @@ If you encounter an issue with the project, you are welcome to submit a
|
|
|
942
1579
|
|
|
943
1580
|
Before you submit a bug report, search for
|
|
944
1581
|
[similar issues](https://github.com/IBM/cloudant-node-sdk/issues?q=is%3Aissue) and review the
|
|
945
|
-
[KNOWN_ISSUES file](KNOWN_ISSUES.md) to verify that your issue hasn't been reported yet.
|
|
1582
|
+
[KNOWN_ISSUES file](https://github.com/IBM/cloudant-node-sdk/tree/v0.7.0/KNOWN_ISSUES.md) to verify that your issue hasn't been reported yet.
|
|
946
1583
|
|
|
947
1584
|
Please consult the [security policy](https://github.com/IBM/cloudant-node-sdk/security/policy) before opening security related issues.
|
|
948
1585
|
|
|
1586
|
+
## Versioning and LTS support
|
|
1587
|
+
|
|
1588
|
+
This SDK follows semantic versioning with respect to the definition of user facing APIs.
|
|
1589
|
+
This means under some circumstances breaking changes may occur within a major or minor version
|
|
1590
|
+
of the SDK related to changes in supported language platforms.
|
|
1591
|
+
|
|
1592
|
+
The SDK is supported on the available LTS releases of the language platform.
|
|
1593
|
+
The LTS language versions are listed in the prerequisites:
|
|
1594
|
+
* [LTS versions currently supported by the SDK](https://github.com/IBM/cloudant-node-sdk/#prerequisites)
|
|
1595
|
+
* [LTS versions for this release of the SDK](#prerequisites)
|
|
1596
|
+
|
|
1597
|
+
Incompatible changes from new language versions are not added to the SDK
|
|
1598
|
+
until they are available in the minimum supported language version.
|
|
1599
|
+
|
|
1600
|
+
When language LTS versions move out of support the following will happen:
|
|
1601
|
+
* Existing SDK releases will continue to run on obsolete language versions, but will no longer be supported.
|
|
1602
|
+
* The minimum language version supported by the SDK will be updated to the next available LTS.
|
|
1603
|
+
* New language features may be added in subsequent SDK releases that will cause breaking changes
|
|
1604
|
+
if the new releases of the SDK are used with older, now unsupported, language levels.
|
|
1605
|
+
|
|
949
1606
|
## Open source at IBM
|
|
950
1607
|
|
|
951
1608
|
Find more open source projects on the [IBM GitHub](http://ibm.github.io/) page.
|
|
952
1609
|
|
|
953
1610
|
## Contributing
|
|
954
1611
|
|
|
955
|
-
For more information, see [CONTRIBUTING](CONTRIBUTING.md).
|
|
1612
|
+
For more information, see [CONTRIBUTING](https://github.com/IBM/cloudant-node-sdk/tree/v0.7.0/CONTRIBUTING.md).
|
|
956
1613
|
|
|
957
1614
|
## License
|
|
958
1615
|
|
|
959
|
-
This SDK is released under the Apache 2.0 license. To read the full text of the license, see [LICENSE](LICENSE).
|
|
1616
|
+
This SDK is released under the Apache 2.0 license. To read the full text of the license, see [LICENSE](https://github.com/IBM/cloudant-node-sdk/tree/v0.7.0/LICENSE).
|