@ibm-cloud/cloudant 0.0.11 → 0.0.15

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 CHANGED
@@ -1,26 +1,25 @@
1
1
  <!--
2
- The example codes and outputs below are generated using the `embedmd` go package.
2
+ The example codes and outputs below are generated using the `embedmd` go
3
+ package.
3
4
 
4
5
  https://github.com/campoy/embedmd
5
6
 
6
- You should regenerate the example codes after making any changes to examples in the test/examples/ folder.
7
+ You should regenerate the example codes after making any changes to
8
+ examples in the test/examples/ folder.
7
9
 
8
10
  embedmd -w README.md
9
11
  -->
10
12
 
11
13
  [![Build Status](https://travis-ci.com/IBM/cloudant-node-sdk.svg?branch=master)](https://travis-ci.com/IBM/cloudant-node-sdk)
12
- <!--
13
- [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
14
- [![npm-version](https://img.shields.io/npm/v/IBM/cloudant-node-sdk.svg)](https://www.npmjs.com/package/@ibmcloud/cloudant)
15
- [![codecov](https://codecov.io/gh/IBM/cloudant-node-sdk/branch/master/graph/badge.svg)](https://codecov.io/gh/IBM/cloudant-node-sdk)
16
- -->
14
+ [![Release](https://img.shields.io/github/v/release/IBM/cloudant-node-sdk?include_prereleases&sort=semver)](https://github.com/IBM/cloudant-node-sdk/releases/latest)
15
+ [![Docs](https://img.shields.io/static/v1?label=TypeDoc&message=latest&color=blue)](https://ibm.github.io/cloudant-node-sdk/)
17
16
 
18
- # IBM Cloudant Node.js SDK Version 0.0.11
17
+ # IBM Cloudant Node.js SDK Version 0.0.15
19
18
 
20
- Node.js client library to interact with the
19
+ IBM Cloudant Node.js SDK is a client library that interacts with the
21
20
  [IBM Cloudant APIs](https://cloud.ibm.com/apidocs/cloudant?code=node).
22
21
 
23
- Disclaimer: this SDK is being released initially as a **pre-release** version.
22
+ Disclaimer: This SDK is being released initially as a **pre-release** version.
24
23
  Changes might occur which impact applications that use this SDK.
25
24
 
26
25
  <details>
@@ -31,8 +30,6 @@ Changes might occur which impact applications that use this SDK.
31
30
 
32
31
  https://github.com/jonschlinkert/markdown-toc
33
32
 
34
- You should regenerate the TOC after making changes to this file.
35
-
36
33
  npx markdown-toc -i README.md
37
34
  -->
38
35
 
@@ -43,23 +40,24 @@ Changes might occur which impact applications that use this SDK.
43
40
  - [Prerequisites](#prerequisites)
44
41
  - [Installation](#installation)
45
42
  - [Authentication](#authentication)
46
- * [Authenticate with environment variables](#authenticate-with-environment-variables)
43
+ * [Authentication with environment variables](#authentication-with-environment-variables)
47
44
  + [IAM authentication](#iam-authentication)
48
45
  + [Session cookie authentication](#session-cookie-authentication)
49
46
  + [Basic authentication](#basic-authentication)
50
- * [Authenticate with external configuration](#authenticate-with-external-configuration)
51
- * [Authenticate programmatically](#authenticate-programmatically)
52
- - [Code examples](#code-examples)
53
- * [1. Retrieve information from an existing database](#1-retrieve-information-from-an-existing-database)
54
- * [2. Create your own database and add a document](#2-create-your-own-database-and-add-a-document)
55
- * [3. Update your previously created document](#3-update-your-previously-created-document)
56
- * [4. Delete your previously created document](#4-delete-your-previously-created-document)
57
- - [Error handling](#error-handling)
47
+ * [Authentication with external configuration](#authentication-with-external-configuration)
48
+ * [Programmatic authentication](#programmatic-authentication)
58
49
  - [Using the SDK](#using-the-sdk)
50
+ * [Code examples](#code-examples)
51
+ + [1. Retrieve information from an existing database](#1-retrieve-information-from-an-existing-database)
52
+ + [2. Create your own database and add a document](#2-create-your-own-database-and-add-a-document)
53
+ + [3. Update your previously created document](#3-update-your-previously-created-document)
54
+ + [4. Delete your previously created document](#4-delete-your-previously-created-document)
55
+ * [Error handling](#error-handling)
56
+ * [Raw IO](#raw-io)
57
+ * [Further resources](#further-resources)
59
58
  - [Questions](#questions)
60
59
  - [Issues](#issues)
61
- - [Further resources](#further-resources)
62
- - [Open source @ IBM](#open-source--ibm)
60
+ - [Open source at IBM](#open-source-at-ibm)
63
61
  - [Contributing](#contributing)
64
62
  - [License](#license)
65
63
 
@@ -70,39 +68,41 @@ Changes might occur which impact applications that use this SDK.
70
68
  ## Overview
71
69
 
72
70
  The IBM Cloudant Node.js SDK allows developers to programmatically
73
- interact with IBM [Cloudant](https://cloud.ibm.com/apidocs/cloudant)
71
+ interact with [IBM Cloudant](https://cloud.ibm.com/apidocs/cloudant)
74
72
  with the help of the `@ibm-cloud/cloudant` package.
75
73
 
76
74
  ## Features
77
75
 
78
76
  The purpose of this Node.js SDK is to wrap most of the HTTP request APIs
79
77
  provided by Cloudant and supply other functions to ease the usage of Cloudant.
80
- Moreover, it has limited support for CouchDB as well.
81
78
  This SDK should make life easier for programmers to do what’s really important
82
- for them: develop.
79
+ to them: developing software.
83
80
 
84
81
  Reasons why you should consider using Cloudant Node.js SDK in your
85
82
  project:
86
83
 
87
84
  - Supported by IBM Cloudant.
85
+ - Server compatibility with:
86
+ - IBM Cloudant "Classic".
87
+ - [Cloudant "Standard on Transaction Engine"](https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-overview-te) for APIs compatible with Cloudant "Classic". For more information, see the [Feature Parity](https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-overview-te#feature-parity-between-ibm-cloudant-on-the-transaction-engine-vs-classic-architecture) page.
88
+ - [Apache CouchDB 3.x](https://docs.couchdb.org/en/stable/) for data operations.
88
89
  - Includes all the most popular and latest supported endpoints for
89
90
  applications.
90
91
  - Handles the authentication.
91
- - Familiar user experience of IBM Cloud SDKs.
92
+ - Familiar user experience with IBM Cloud SDKs.
93
+ - Flexibility to use either built-in models or byte-based requests and responses for documents.
92
94
  - `Promise` based design with asynchronous HTTP requests.
93
95
  - Use either as native JavaScript or take advantage of TypeScript models.
94
96
  - Transparently compresses request and response bodies.
95
97
 
96
98
  ## Prerequisites
97
99
 
98
- [ibm-cloud-onboarding]: http://cloud.ibm.com/registration
99
-
100
- - You need an [IBM Cloud][ibm-cloud-onboarding] account.
101
- - An IAM API key to allow the SDK to access your account.
102
- Create one [here](https://cloud.ibm.com/iam/apikeys).
103
- - **Node.js 10, 12, 14**: This SDK is tested with Node.js versions 10, 12, and 14.
104
- It may work on other versions but those are not officially supported.
105
-
100
+ - A
101
+ [Cloudant](https://cloud.ibm.com/docs/Cloudant/getting-started.html#step-1-connect-to-your-cloudant-nosql-db-service-instance-on-ibm-cloud)
102
+ service instance or a
103
+ [CouchDB](https://docs.couchdb.org/en/latest/install/index.html)
104
+ server.
105
+ - Node.js 10, 12 or 14.
106
106
 
107
107
  ## Installation
108
108
 
@@ -127,7 +127,8 @@ account.
127
127
  1. [*IAM authentication*](#iam-authentication) is highly recommended when your
128
128
  back-end database server is [**Cloudant**][cloud-IAM-mgmt]. This
129
129
  authentication type requires a server-generated `apikey` instead of a
130
- user-given password.
130
+ user-given password. You can create one
131
+ [here](https://cloud.ibm.com/iam/apikeys).
131
132
  1. [*Session cookie (`COUCHDB_SESSION`) authentication*](#session-cookie-authentication)
132
133
  is recommended for [Apache CouchDB][couch-cookie-auth] or for
133
134
  [Cloudant][cloudant-cookie-auth] when IAM is unavailable. It exchanges username
@@ -137,23 +138,23 @@ account.
137
138
  for both [Cloudant][cloudant-basic-auth] and [Apache CouchDB][couch-basic-auth]
138
139
  back-end database servers. This authentication type requires the good old
139
140
  `username` and `password` credentials.
140
- 1. *Noauth* authentication does not need any credentials. Note that this
141
- authentication type will only work for queries against a database with read
141
+ 1. *Noauth* authentication does not require credentials. Note that this
142
+ authentication type only works with queries against a database with read
142
143
  access for everyone.
143
144
  1. The service `url`.
144
145
 
145
146
  There are several ways to **set** these properties:
146
147
 
147
- 1. As [environment variables](#authenticate-with-environment-variables)
148
- 1. The [programmatic approach](#authenticate-programmatically)
149
- 1. With an [external credentials file](#authenticate-with-external-configuration)
148
+ 1. As [environment variables](#authentication-with-environment-variables)
149
+ 1. The [programmatic approach](#programmatic-authentication)
150
+ 1. With an [external credentials file](#authentication-with-external-configuration)
150
151
 
151
- ### Authenticate with environment variables
152
+ ### Authentication with environment variables
152
153
 
153
154
  #### IAM authentication
154
155
 
155
- For Cloudant *IAM authentication* set the following environmental variables by
156
- replacing `<url>` and `<apikey>` with your proper
156
+ For Cloudant *IAM authentication*, set the following environmental variables by
157
+ replacing the `<url>` and `<apikey>` with your proper
157
158
  [service credentials][service-credentials]. There is no need to set
158
159
  `CLOUDANT_AUTH_TYPE` to `IAM` because it is the default.
159
160
 
@@ -164,8 +165,8 @@ CLOUDANT_APIKEY=<apikey>
164
165
 
165
166
  #### Session cookie authentication
166
167
 
167
- For `COUCHDB_SESSION` authentication set the following environmental variables
168
- by replacing `<url>`, `<username>` and `<password>` with your proper
168
+ For `COUCHDB_SESSION` authentication, set the following environmental variables
169
+ by replacing the `<url>`, `<username>` and `<password>` with your proper
169
170
  [service credentials][service-credentials].
170
171
 
171
172
  ```bash
@@ -177,8 +178,8 @@ CLOUDANT_PASSWORD=<password>
177
178
 
178
179
  #### Basic authentication
179
180
 
180
- For *Basic authentication* set the following environmental variables by
181
- replacing `<url>`, `<username>` and `<password>` with your proper
181
+ For *Basic authentication*, set the following environmental variables by
182
+ replacing the `<url>`, `<username>` and `<password>` with your proper
182
183
  [service credentials][service-credentials].
183
184
 
184
185
  ```bash
@@ -188,10 +189,10 @@ CLOUDANT_USERNAME=<username>
188
189
  CLOUDANT_PASSWORD=<password>
189
190
  ```
190
191
 
191
- **Note**: We recommend using [IAM](#iam-authentication) for Cloudant and
192
+ **Note**: We recommend that you use [IAM](#iam-authentication) for Cloudant and
192
193
  [Session](#session-cookie-authentication) for CouchDB authentication.
193
194
 
194
- ### Authenticate with external configuration
195
+ ### Authentication with external configuration
195
196
 
196
197
  To use an external configuration file, the
197
198
  [Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node#authentication-with-external-configuration),
@@ -199,28 +200,43 @@ or the
199
200
  [general SDK usage information](https://github.com/IBM/ibm-cloud-sdk-common#using-external-configuration)
200
201
  will guide you.
201
202
 
202
- ### Authenticate programmatically
203
+ ### Programmatic authentication
203
204
 
204
205
  To learn more about how to use programmatic authentication, see the related
205
206
  documentation in the
206
- [Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=python#programmatic-authentication)
207
+ [Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node#programmatic-authentication)
207
208
  or in the
208
- [Node.js SDK Core document about authentication](https://github.com/IBM/node-sdk-core/blob/master/Authentication.md).
209
+ [Node.js SDK Core document](https://github.com/IBM/node-sdk-core/blob/master/Authentication.md) about authentication.
209
210
 
210
- ## Code examples
211
+ ## Using the SDK
211
212
 
212
- The code examples below will follow the
213
- [authentication with environment variables](#authenticate-with-environment-variables).
213
+ For general IBM Cloud SDK usage information, please see
214
+ [IBM Cloud SDK Common](https://github.com/IBM/ibm-cloud-sdk-common/blob/master/README.md).
214
215
 
215
- ### 1. Retrieve information from an existing database
216
+ ### Code examples
216
217
 
217
- This example code gathers some information about an existing database hosted on
218
- the https://examples.cloudant.com/ service `url`. To do this, you need to
218
+ The following code examples
219
+ [authenticate with the environment variables](#authenticate-with-environment-variables).
220
+
221
+ #### 1. Retrieve information from an existing database
222
+
223
+ **Note:** This example code assumes that `animaldb` database does not exist in your account.
224
+
225
+ This example code gathers information about an existing database hosted on
226
+ the https://examples.cloudant.com/ service `url`. To connect, you must
219
227
  extend your environment variables with the *service url* and *authentication
220
- type* to use `NOAUTH` authentication while reaching the `animaldb` database.
228
+ type* to use `NOAUTH` authentication while you connect to the `animaldb` database.
221
229
  This step is necessary for the SDK to distinguish the `EXAMPLES` custom service
222
230
  name from the default service name which is `CLOUDANT`.
223
231
 
232
+ Cloudant environment variable naming starts with a *service name* prefix that identifies your service.
233
+ By default this is `CLOUDANT`, see the settings in the
234
+ [authentication with environment variables section](#authentication-with-environment-variables).
235
+
236
+ If you would like to rename your Cloudant service from `CLOUDANT`,
237
+ you must use your defined service name as the prefix for all Cloudant related environment variables.
238
+ The code block below provides an example of instantiating a user-defined `EXAMPLES` service name.
239
+
224
240
  ```bash
225
241
  EXAMPLES_URL=https://examples.cloudant.com
226
242
  EXAMPLES_AUTH_TYPE=NOAUTH
@@ -231,52 +247,58 @@ Once the environment variables are set, you can try out the code examples.
231
247
  <details open>
232
248
  <summary>TypeScript:</summary>
233
249
 
250
+ ```ts
251
+ import {CloudantV1} from "@ibm-cloud/cloudant";
252
+ ```
234
253
  [embedmd]:# (test/examples/src/ts/GetInfoFromExistingDatabase.ts /\/\/ 1./ $)
235
254
  ```ts
236
255
  // 1. Create a Cloudant client with "EXAMPLES" service name ===================
237
- const client =
238
- CloudantV1.newInstance({serviceName:"EXAMPLES"});
256
+ const client = CloudantV1.newInstance({ serviceName: 'EXAMPLES' });
239
257
 
240
258
  // 2. Get server information ==================================================
241
259
  // call service without parameters:
242
- client.getServerInformation()
243
- .then(serverInformation => {
244
- const version = serverInformation.result.version;
245
- console.log(`Server version ${version}`);
246
- });
260
+ client.getServerInformation().then((serverInformation) => {
261
+ const version = serverInformation.result.version;
262
+ console.log(`Server version ${version}`);
263
+ });
247
264
 
248
265
  // 3. Get database information for "animaldb" =================================
249
- const dbName = "animaldb";
266
+ const dbName = 'animaldb';
250
267
 
251
268
  // call service with embedded parameters:
252
- client.getDatabaseInformation({db: dbName})
253
- .then(dbInfo => {
254
- const documentCount = dbInfo.result.doc_count;
255
- const dbNameResult = dbInfo.result.db_name;
256
-
257
- // 4. Show document count in database =================================
258
- console.log(`Document count in "${dbNameResult}" database is ` +
259
- documentCount + ".");
260
- });
269
+ client.getDatabaseInformation({ db: dbName }).then((dbInfo) => {
270
+ const documentCount = dbInfo.result.doc_count;
271
+ const dbNameResult = dbInfo.result.db_name;
272
+
273
+ // 4. Show document count in database =================================
274
+ console.log(
275
+ `Document count in "${dbNameResult}" database is ` + documentCount + '.'
276
+ );
277
+ });
261
278
 
262
279
  // 5. Get zebra document out of the database by document id ===================
263
- const getDocParams:
264
- CloudantV1.GetDocumentParams = {db: dbName, docId: "zebra"};
280
+ const getDocParams: CloudantV1.GetDocumentParams = {
281
+ db: dbName,
282
+ docId: 'zebra',
283
+ };
265
284
 
266
285
  // call service with predefined parameters:
267
- client.getDocument(getDocParams)
268
- .then(documentAboutZebra => {
269
- // result object is defined as a Document here:
270
- const result: CloudantV1.Document = documentAboutZebra.result;
271
- console.log("Document retrieved from database:\n" +
272
- JSON.stringify(result, null, 2));
273
- });
286
+ client.getDocument(getDocParams).then((documentAboutZebra) => {
287
+ // result object is defined as a Document here:
288
+ const result: CloudantV1.Document = documentAboutZebra.result;
289
+ console.log(
290
+ 'Document retrieved from database:\n' + JSON.stringify(result, null, 2)
291
+ );
292
+ });
274
293
  ```
275
294
  </details>
276
295
 
277
296
  <details>
278
297
  <summary>JavaScript:</summary>
279
298
 
299
+ ```js
300
+ const { CloudantV1 } = require('@ibm-cloud/cloudant');
301
+ ```
280
302
  [embedmd]:# (test/examples/src/js/GetInfoFromExistingDatabase.js /const getInfoFromExistingDatabase/ /getInfoFromExistingDatabase\(\);\n\}/)
281
303
  ```js
282
304
  const getInfoFromExistingDatabase = async () => {
@@ -297,7 +319,9 @@ const getInfoFromExistingDatabase = async () => {
297
319
  const dbNameResult = dbInfo.result.db_name;
298
320
 
299
321
  // 4. Show document count in database =================================
300
- console.log(`Document count in "${dbNameResult}" database is ` + documentCount + '.');
322
+ console.log(
323
+ `Document count in "${dbNameResult}" database is ` + documentCount + '.'
324
+ );
301
325
 
302
326
  // 5. Get zebra document out of the database by document id ===================
303
327
  const getDocParams = { db: dbName, docId: 'zebra' };
@@ -308,7 +332,9 @@ const getInfoFromExistingDatabase = async () => {
308
332
  // result object is defined as a Document here:
309
333
  const result = documentAboutZebra.result;
310
334
 
311
- console.log('Document retrieved from database:\n' + JSON.stringify(result, null, 2));
335
+ console.log(
336
+ 'Document retrieved from database:\n' + JSON.stringify(result, null, 2)
337
+ );
312
338
  };
313
339
 
314
340
  if (require.main === module) {
@@ -318,8 +344,7 @@ if (require.main === module) {
318
344
 
319
345
  </details>
320
346
 
321
- The result of the code is similar to the following output.
322
- The order of the output lines may change due to the asynchronicity.
347
+ When you run the code, you see a result similar to the following output.
323
348
 
324
349
  [embedmd]:# (test/examples/output/GetInfoFromExistingDatabase.txt)
325
350
  ```txt
@@ -339,90 +364,89 @@ Document retrieved from database:
339
364
  }
340
365
  ```
341
366
 
342
- ### 2. Create your own database and add a document
367
+ #### 2. Create your own database and add a document
343
368
 
344
- Now comes the exciting part of creating your own `orders` database and adding
345
- a document about *Bob Smith* with your own [IAM](#iam-authentication) or
369
+ **Note:** This example code assumes that `orders` database does not exist in your account.
370
+
371
+ Now comes the exciting part, you create your own `orders` database and add a document about *Bob Smith* with your own [IAM](#iam-authentication) or
346
372
  [Basic](#basic-authentication) service credentials.
347
373
 
374
+ <details>
375
+ <summary>Create code example</summary>
376
+
348
377
  <details open>
349
378
  <summary>TypeScript:</summary>
350
379
 
380
+ ```ts
381
+ import {CloudantV1} from "@ibm-cloud/cloudant";
382
+ ```
351
383
  [embedmd]:# (test/examples/src/ts/CreateDbAndDoc.ts /interface/ $)
352
384
  ```ts
353
385
  interface OrderDocument extends CloudantV1.Document {
354
- name?: string;
355
- joined?: string;
356
- _id: string;
357
- _rev?: string;
386
+ name?: string;
387
+ joined?: string;
388
+ _id: string;
389
+ _rev?: string;
358
390
  }
359
391
 
360
392
  // 1. Create a client with `CLOUDANT` default service name ================
361
393
  const client = CloudantV1.newInstance({});
362
394
 
363
395
  // 2. Create a database =======================================================
364
- const exampleDbName = "orders";
396
+ const exampleDbName = 'orders';
365
397
 
366
398
  // Try to create database if it doesn't exist
367
- const createDb = client.putDatabase({db: exampleDbName})
368
- .then(putDatabaseResult => {
369
- if (putDatabaseResult.result.ok) {
370
- console.log(`"${exampleDbName}" database created."`);
371
- }
372
- })
373
- .catch(err => {
374
- if (err.code === 412){
375
- console.log("Cannot create \"" + exampleDbName +
376
- "\" database, it already exists.");
377
- }
378
- });
399
+ const createDb = client
400
+ .putDatabase({ db: exampleDbName })
401
+ .then((putDatabaseResult) => {
402
+ if (putDatabaseResult.result.ok) {
403
+ console.log(`"${exampleDbName}" database created."`);
404
+ }
405
+ })
406
+ .catch((err) => {
407
+ if (err.code === 412) {
408
+ console.log(
409
+ 'Cannot create "' + exampleDbName + '" database, it already exists.'
410
+ );
411
+ }
412
+ });
379
413
 
380
414
  // 3. Create a document =======================================================
381
415
  // Create a document object with "example" id
382
- const exampleDocId = "example";
416
+ const exampleDocId = 'example';
383
417
 
384
418
  // set required _id property on exampleDocument:
385
- const exampleDocument: OrderDocument = {_id: exampleDocId};
419
+ const exampleDocument: OrderDocument = { _id: exampleDocId };
386
420
 
387
421
  // Add "name" and "joined" fields to the document
388
- exampleDocument.name = "Bob Smith";
389
- exampleDocument.joined = "2019-01-24T10:42:99.000Z";
422
+ exampleDocument.name = 'Bob Smith';
423
+ exampleDocument.joined = '2019-01-24T10:42:99.000Z';
390
424
 
391
- /* Try to get the document and set revision of exampleDocument to the
392
- latest one if it previously existed in the database */
425
+ // Save the document in the database
393
426
  createDb.then(() => {
394
- client.getDocument({db: exampleDbName, docId: exampleDocId})
395
- .then(documentInfo => {
396
- exampleDocument._rev = documentInfo.result._rev;
397
- console.log("The document revision for" + exampleDocId +
398
- "is set to " + exampleDocument._rev);
399
- })
400
- .catch(err => {
401
- if (err.code === 404) {
402
- // Document does not exist in database
403
- }
404
- })
405
- .finally(() => {
406
- // Save the document in the database
407
- client.postDocument({
408
- db: exampleDbName,
409
- document: exampleDocument
410
- }
411
- ).then(createDocumentResponse => {
412
- // Keep track with the revision number of the document object
413
- exampleDocument._rev = createDocumentResponse.result.rev;
414
- console.log("You have created the document:\n" +
415
- JSON.stringify(exampleDocument, null, 2));
416
- });
417
- });
427
+ client
428
+ .postDocument({
429
+ db: exampleDbName,
430
+ document: exampleDocument,
431
+ })
432
+ .then((createDocumentResponse) => {
433
+ // Keep track with the revision number of the document object
434
+ exampleDocument._rev = createDocumentResponse.result.rev;
435
+ console.log(
436
+ 'You have created the document:\n' +
437
+ JSON.stringify(exampleDocument, null, 2)
438
+ );
439
+ });
418
440
  });
419
441
  ```
420
-
421
442
  </details>
422
443
 
423
444
  <details>
424
445
  <summary>JavaScript:</summary>
425
446
 
447
+ ```js
448
+ const { CloudantV1 } = require('@ibm-cloud/cloudant');
449
+ ```
426
450
  [embedmd]:# (test/examples/src/js/CreateDbAndDoc.js /const createDbAndDoc/ /createDbAndDoc\(\);\n\}/)
427
451
  ```js
428
452
  const createDbAndDoc = async () => {
@@ -444,7 +468,9 @@ const createDbAndDoc = async () => {
444
468
  }
445
469
  } catch (err) {
446
470
  if (err.code === 412) {
447
- console.log('Cannot create "' + exampleDbName + '" database, it already exists.');
471
+ console.log(
472
+ 'Cannot create "' + exampleDbName + '" database, it already exists.'
473
+ );
448
474
  }
449
475
  }
450
476
 
@@ -458,44 +484,29 @@ const createDbAndDoc = async () => {
458
484
  exampleDocument['name'] = 'Bob Smith';
459
485
  exampleDocument.joined = '2019-01-24T10:42:99.000Z';
460
486
 
461
- /* Try to get the document and set revision of exampleDocument to the
462
- latest one if it previously existed in the database */
463
- try {
464
- exampleDocument._rev = (
465
- await client.getDocument({
466
- db: exampleDbName,
467
- docId: exampleDocId,
468
- })
469
- ).result._rev;
470
- console.log(
471
- 'The document revision for "' + exampleDocId + '" is set to ' + exampleDocument._rev
472
- );
473
- } catch (err) {
474
- if (err.code === 404) {
475
- // Document does not exist in database
476
- }
477
- } finally {
478
- // Save the document in the database
479
- const createDocumentResponse = await client.postDocument({
480
- db: exampleDbName,
481
- document: exampleDocument,
482
- });
483
-
484
- // Keep track with the revision number of the document object
485
- exampleDocument._rev = createDocumentResponse.result.rev;
486
- console.log('You have created the document:\n' + JSON.stringify(exampleDocument, null, 2));
487
- }
487
+ // Save the document in the database
488
+ const createDocumentResponse = await client.postDocument({
489
+ db: exampleDbName,
490
+ document: exampleDocument,
491
+ });
492
+
493
+ // Keep track with the revision number of the document object
494
+ exampleDocument._rev = createDocumentResponse.result.rev;
495
+ console.log(
496
+ 'You have created the document:\n' +
497
+ JSON.stringify(exampleDocument, null, 2)
498
+ );
488
499
  };
489
500
 
490
501
  if (require.main === module) {
491
502
  createDbAndDoc();
492
503
  }
493
504
  ```
494
-
495
505
  </details>
496
506
 
497
- The result of the code is similar to the following output.
498
- The order of the output lines may change due to the asynchronicity.
507
+
508
+ </details>
509
+ When you run the code, you see a result similar to the following output.
499
510
 
500
511
  [embedmd]:# (test/examples/output/CreateDbAndDoc.txt)
501
512
  ```txt
@@ -509,72 +520,84 @@ You have created the document:
509
520
  }
510
521
  ```
511
522
 
512
- ### 3. Update your previously created document
523
+ #### 3. Update your previously created document
513
524
 
514
- **Note**: this example code assumes that you have created both the `orders`
525
+ **Note**: This example code assumes that you have created both the `orders`
515
526
  database and the `example` document by
516
- [running this previous example code](#2-create-your-own-database-and-add-a-document)
517
- successfully, otherwise you get the `Cannot update document because either "orders"
518
- database or "example" document was not found.` message.
527
+ [running the previous example code](#2-create-your-own-database-and-add-a-document)
528
+ successfully. Otherwise, the following error message occurs, "Cannot update document because either 'orders'
529
+ database or 'example' document was not found."
530
+
531
+ <details>
532
+ <summary>Update code example</summary>
519
533
 
520
534
  <details open>
521
535
  <summary>TypeScript:</summary>
522
536
 
537
+ ```ts
538
+ import {CloudantV1} from "@ibm-cloud/cloudant";
539
+ ```
523
540
  [embedmd]:# (test/examples/src/ts/UpdateDoc.ts /interface/ $)
524
541
  ```ts
525
542
  interface OrderDocument extends CloudantV1.Document {
526
- address?: string;
527
- joined?: string;
528
- _id?: string;
529
- _rev?: string;
543
+ address?: string;
544
+ joined?: string;
545
+ _id?: string;
546
+ _rev?: string;
530
547
  }
531
548
 
532
549
  // 1. Create a client with `CLOUDANT` default service name ================
533
550
  const client = CloudantV1.newInstance({});
534
551
  // 2. Update the document =====================================================
535
552
  // Set the options to get the document out of the database if it exists
536
- const exampleDbName = "orders";
553
+ const exampleDbName = 'orders';
537
554
 
538
555
  // Try to get the document if it previously existed in the database
539
- const getDocParams: CloudantV1.GetDocumentParams =
540
- {docId: "example", db: exampleDbName};
541
-
542
- client.getDocument(getDocParams)
543
- .then(docResult => {
544
- // using OrderDocument on getDocument result:
545
- const document: OrderDocument = docResult.result;
546
-
547
- // Add Bob Smith's address to the document
548
- document.address = "19 Front Street, Darlington, DL5 1TY";
549
-
550
- // Remove the joined property from document object
551
- delete document.joined;
552
-
553
- // Update the document in the database
554
- client.postDocument({db: exampleDbName, document})
555
- .then(res => {
556
- // Keeping track with the revision number of the document object:
557
- document._rev = res.result.rev;
558
- console.log("You have updated the document:\n" +
559
- JSON.stringify(document, null, 2));
560
- });
561
- })
562
- .catch(err => {
563
- if (err.code === 404) {
564
- console.log(
565
- "Cannot update document because either \"" +
566
- exampleDbName + "\" database or the \"example\" " +
567
- "document was not found."
568
- );
569
- }
556
+ const getDocParams: CloudantV1.GetDocumentParams = {
557
+ docId: 'example',
558
+ db: exampleDbName,
559
+ };
560
+
561
+ client
562
+ .getDocument(getDocParams)
563
+ .then((docResult) => {
564
+ // using OrderDocument on getDocument result:
565
+ const document: OrderDocument = docResult.result;
566
+
567
+ // Add Bob Smith's address to the document
568
+ document.address = '19 Front Street, Darlington, DL5 1TY';
569
+
570
+ // Remove the joined property from document object
571
+ delete document.joined;
572
+
573
+ // Update the document in the database
574
+ client.postDocument({ db: exampleDbName, document }).then((res) => {
575
+ // Keeping track with the revision number of the document object:
576
+ document._rev = res.result.rev;
577
+ console.log(
578
+ 'You have updated the document:\n' + JSON.stringify(document, null, 2)
579
+ );
570
580
  });
581
+ })
582
+ .catch((err) => {
583
+ if (err.code === 404) {
584
+ console.log(
585
+ 'Cannot update document because either "' +
586
+ exampleDbName +
587
+ '" database or the "example" ' +
588
+ 'document was not found.'
589
+ );
590
+ }
591
+ });
571
592
  ```
572
-
573
593
  </details>
574
594
 
575
595
  <details>
576
596
  <summary>JavaScript:</summary>
577
597
 
598
+ ```js
599
+ const { CloudantV1 } = require('@ibm-cloud/cloudant');
600
+ ```
578
601
  [embedmd]:# (test/examples/src/js/UpdateDoc.js /const updateDoc/ /updateDoc\(\);\n\}/)
579
602
  ```js
580
603
  const updateDoc = async () => {
@@ -607,7 +630,9 @@ const updateDoc = async () => {
607
630
  })
608
631
  ).result.rev;
609
632
 
610
- console.log('You have updated the document:\n' + JSON.stringify(document, null, 2));
633
+ console.log(
634
+ 'You have updated the document:\n' + JSON.stringify(document, null, 2)
635
+ );
611
636
  } catch (err) {
612
637
  if (err.code === 404) {
613
638
  console.log(
@@ -624,41 +649,48 @@ if (require.main === module) {
624
649
  updateDoc();
625
650
  }
626
651
  ```
627
-
628
652
  </details>
629
653
 
630
- The result of the code is similar to the following output.
654
+
655
+ </details>
656
+ When you run the code, you see a result similar to the following output.
631
657
 
632
658
  [embedmd]:# (test/examples/output/UpdateDoc.txt)
633
659
  ```txt
634
660
  You have updated the document:
635
661
  {
636
662
  "_id": "example",
637
- "_rev": "2-70476cf75eb02f55c6c4061aa6941ec8",
663
+ "_rev": "2-4e2178e85cffb32d38ba4e451f6ca376",
638
664
  "name": "Bob Smith",
639
665
  "address": "19 Front Street, Darlington, DL5 1TY"
640
666
  }
641
667
  ```
642
668
 
643
- ### 4. Delete your previously created document
669
+ #### 4. Delete your previously created document
644
670
 
645
- **Note**: this example code assumes that you have created both the `orders`
671
+ **Note**: This example code assumes that you have created both the `orders`
646
672
  database and the `example` document by
647
- [running this previous example code](#2-create-your-own-database-and-add-a-document)
648
- successfully, otherwise you get the `Cannot delete document because either "orders"
649
- database or "example" document was not found.` message.
673
+ [running the previous example code](#2-create-your-own-database-and-add-a-document)
674
+ successfully. Otherwise, the following error message occurs, "Cannot delete document because either 'orders'
675
+ database or 'example' document was not found."
676
+
677
+ <details>
678
+ <summary>Delete code example</summary>
650
679
 
651
680
  <details open>
652
681
  <summary>TypeScript:</summary>
653
682
 
683
+ ```ts
684
+ import {CloudantV1} from "@ibm-cloud/cloudant";
685
+ ```
654
686
  [embedmd]:# (test/examples/src/ts/DeleteDoc.ts /interface/ $)
655
687
  ```ts
656
688
  interface OrderDocument extends CloudantV1.Document {
657
- name?: string;
658
- address?: string;
659
- joined?: string;
660
- _id?: string;
661
- _rev?: string;
689
+ name?: string;
690
+ address?: string;
691
+ joined?: string;
692
+ _id?: string;
693
+ _rev?: string;
662
694
  }
663
695
 
664
696
  // 1. Create a client with `CLOUDANT` default service name ================
@@ -666,40 +698,49 @@ const client = CloudantV1.newInstance({});
666
698
 
667
699
  // 2. Delete the document =============================================
668
700
  // Set the options to get the document out of the database if it exists
669
- const exampleDbName = "orders";
670
- const exampleDocId = "example";
701
+ const exampleDbName = 'orders';
702
+ const exampleDocId = 'example';
671
703
 
672
704
  // Try to get the document if it previously existed in the database
673
- const getDocParams: CloudantV1.GetDocumentParams =
674
- {docId: exampleDocId, db: exampleDbName};
675
-
676
- client.getDocument(getDocParams)
677
- .then(docResult => {
678
- const document: OrderDocument = docResult.result;
679
-
680
- client.deleteDocument({
681
- db: exampleDbName,
682
- docId: document._id,
683
- rev: document._rev}).then(() => {
684
- console.log("You have deleted the document.");
685
- });
686
- })
687
- .catch(err => {
688
- if (err.code === 404) {
689
- console.log(
690
- "Cannot delete document because either \"" +
691
- exampleDbName + "\" database or the \"example\" " +
692
- "document was not found."
693
- );
694
- }
695
- });
696
- ```
705
+ const getDocParams: CloudantV1.GetDocumentParams = {
706
+ docId: exampleDocId,
707
+ db: exampleDbName,
708
+ };
697
709
 
710
+ client
711
+ .getDocument(getDocParams)
712
+ .then((docResult) => {
713
+ const document: OrderDocument = docResult.result;
714
+
715
+ client
716
+ .deleteDocument({
717
+ db: exampleDbName,
718
+ docId: document._id,
719
+ rev: document._rev,
720
+ })
721
+ .then(() => {
722
+ console.log('You have deleted the document.');
723
+ });
724
+ })
725
+ .catch((err) => {
726
+ if (err.code === 404) {
727
+ console.log(
728
+ 'Cannot delete document because either "' +
729
+ exampleDbName +
730
+ '" database or the "example" ' +
731
+ 'document was not found.'
732
+ );
733
+ }
734
+ });
735
+ ```
698
736
  </details>
699
737
 
700
738
  <details>
701
739
  <summary>JavaScript:</summary>
702
740
 
741
+ ```js
742
+ const { CloudantV1 } = require('@ibm-cloud/cloudant');
743
+ ```
703
744
  [embedmd]:# (test/examples/src/js/DeleteDoc.js /const deleteDoc/ /deleteDoc\(\);\n\}/)
704
745
  ```js
705
746
  const deleteDoc = async () => {
@@ -742,60 +783,69 @@ if (require.main === module) {
742
783
  deleteDoc();
743
784
  }
744
785
  ```
745
-
746
786
  </details>
747
787
 
748
- The result of the code is the following output.
788
+
789
+ </details>
790
+ When you run the code, you see the following output.
749
791
 
750
792
  [embedmd]:# (test/examples/output/DeleteDoc.txt)
751
793
  ```txt
752
794
  You have deleted the document.
753
795
  ```
754
796
 
755
- ## Error handling
797
+ ### Error handling
756
798
 
757
- For sample code on handling errors, please see
799
+ For sample code on handling errors, see
758
800
  [Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node#error-handling).
759
801
 
760
- ## Using the SDK
802
+ ### Raw IO
761
803
 
762
- For general SDK usage information, please see
763
- [this link](https://github.com/IBM/ibm-cloud-sdk-common/blob/master/README.md).
804
+ For endpoints that read or write document content it is possible to bypass
805
+ usage of the built-in models and send or receive a bytes response.
806
+ For examples of using byte streams, see the API reference documentation
807
+ ("Example request as a stream" section).
764
808
 
765
- ## Questions
766
-
767
- If you are having difficulties using this SDK or have a question about
768
- the IBM Cloud services, please ask a question on
769
- [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-cloud).
770
-
771
- ## Issues
809
+ - [Bulk modify multiple documents in a database](https://cloud.ibm.com/apidocs/cloudant?code=node#postbulkdocs)
810
+ - [Query a list of all documents in a database](https://cloud.ibm.com/apidocs/cloudant?code=node#postalldocs)
811
+ - [Query the database document changes feed](https://cloud.ibm.com/apidocs/cloudant?code=node#postchanges)
772
812
 
773
- If you encounter an issue with the SDK, you are welcome to submit
774
- a [bug report](https://github.com/IBM/cloudant-node-sdk/issues).
775
- Before that, please search for similar issues. It's possible someone has
776
- already encountered this issue.
777
-
778
- ## Further resources
813
+ ### Further resources
779
814
 
780
815
  - [Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node):
781
- API examples for Cloudant Node.js SDK.
816
+ API reference including usage examples for Cloudant Node.js SDK API.
817
+ - [TypeDoc](https://ibm.github.io/cloudant-node-sdk/):
818
+ Cloudant Node.js SDK API Documentation.
782
819
  - [Cloudant docs](https://cloud.ibm.com/docs/services/Cloudant?topic=cloudant-overview#overview):
783
820
  The official documentation page for Cloudant.
784
821
  - [Cloudant Learning Center](https://developer.ibm.com/clouddataservices/docs/compose/cloudant/):
785
- The official learning center with several useful videos which help you to use
822
+ The official learning center with several useful videos which help you use
786
823
  Cloudant successfully.
787
824
  - [Cloudant blog](https://blog.cloudant.com/):
788
- Many useful articles how to optimize Cloudant for common problems.
825
+ Many useful articles about how to optimize Cloudant for common problems.
826
+
827
+ ## Questions
828
+
829
+ If you are having difficulties using this SDK or have a question about the
830
+ IBM Cloud services, ask a question on
831
+ [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-cloud).
832
+
833
+ ## Issues
834
+
835
+ If you encounter an issue with the project, you are welcome to submit a
836
+ [bug report](https://github.com/IBM/cloudant-node-sdk/issues).
837
+ Before you submit a bug report, search for
838
+ [similar issues](https://github.com/IBM/cloudant-node-sdk/issues?q=is%3Aissue) and review the
839
+ [KNOWN_ISSUES file](KNOWN_ISSUES.md) to verify that your issue hasn't been reported yet.
789
840
 
790
- ## Open source @ IBM
841
+ ## Open source at IBM
791
842
 
792
- Find more open source projects on the [IBM Github Page](http://ibm.github.io/).
843
+ Find more open source projects on the [IBM Github](http://ibm.github.io/) page.
793
844
 
794
845
  ## Contributing
795
846
 
796
- See [CONTRIBUTING](CONTRIBUTING.md).
847
+ For more information, see [CONTRIBUTING](CONTRIBUTING.md).
797
848
 
798
849
  ## License
799
850
 
800
- This SDK is released under the Apache 2.0 license.
801
- The license's full text can be found in [LICENSE](LICENSE).
851
+ This SDK is released under the Apache 2.0 license. To read the full text of the license, see [LICENSE](LICENSE).