@ibm-cloud/cloudant 0.0.12 → 0.0.16
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 +277 -206
- package/auth/couchdbSessionAuthenticator.d.ts +2 -2
- package/auth/couchdbSessionAuthenticator.js +4 -2
- package/auth/couchdbSessionAuthenticator.js.map +1 -1
- package/auth/index.d.ts +1 -1
- package/auth/index.js +2 -1
- package/auth/index.js.map +1 -1
- package/auth/sessionTokenManager.d.ts +2 -2
- package/auth/sessionTokenManager.js +16 -8
- package/auth/sessionTokenManager.js.map +1 -1
- package/cloudant/v1.d.ts +298 -460
- package/cloudant/v1.js +507 -608
- package/cloudant/v1.js.map +1 -1
- package/index.d.ts +3 -3
- package/index.js +3 -1
- package/index.js.map +1 -1
- package/lib/cloudantBaseService.d.ts +10 -2
- package/lib/cloudantBaseService.js +17 -2
- package/lib/cloudantBaseService.js.map +1 -1
- package/lib/common.d.ts +1 -1
- package/lib/common.js +1 -1
- package/lib/common.js.map +1 -1
- package/lib/getAuthenticatorFromEnvCloudantExtension.d.ts +2 -2
- package/lib/getAuthenticatorFromEnvCloudantExtension.js +1 -1
- package/lib/getAuthenticatorFromEnvCloudantExtension.js.map +1 -1
- package/package.json +9 -13
package/README.md
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
The example codes and outputs below are generated using the `embedmd` go
|
|
3
|
-
package.
|
|
4
|
-
|
|
5
|
-
https://github.com/campoy/embedmd
|
|
6
|
-
|
|
7
|
-
You should regenerate the example codes after making any changes to
|
|
8
|
-
examples in the test/examples/ folder.
|
|
9
|
-
|
|
10
|
-
embedmd -w README.md
|
|
11
|
-
-->
|
|
12
|
-
|
|
13
1
|
[](https://travis-ci.com/IBM/cloudant-node-sdk)
|
|
14
2
|
[](https://github.com/IBM/cloudant-node-sdk/releases/latest)
|
|
3
|
+
[](https://ibm.github.io/cloudant-node-sdk/)
|
|
15
4
|
|
|
16
|
-
# IBM Cloudant Node.js SDK Version 0.0.
|
|
5
|
+
# IBM Cloudant Node.js SDK Version 0.0.16
|
|
17
6
|
|
|
18
|
-
Node.js client library
|
|
7
|
+
IBM Cloudant Node.js SDK is a client library that interacts with the
|
|
19
8
|
[IBM Cloudant APIs](https://cloud.ibm.com/apidocs/cloudant?code=node).
|
|
20
9
|
|
|
21
|
-
Disclaimer:
|
|
10
|
+
Disclaimer: This SDK is being released initially as a **pre-release** version.
|
|
22
11
|
Changes might occur which impact applications that use this SDK.
|
|
23
12
|
|
|
24
13
|
<details>
|
|
@@ -29,8 +18,6 @@ Changes might occur which impact applications that use this SDK.
|
|
|
29
18
|
|
|
30
19
|
https://github.com/jonschlinkert/markdown-toc
|
|
31
20
|
|
|
32
|
-
You should regenerate the TOC after making changes to this file.
|
|
33
|
-
|
|
34
21
|
npx markdown-toc -i README.md
|
|
35
22
|
-->
|
|
36
23
|
|
|
@@ -58,7 +45,7 @@ Changes might occur which impact applications that use this SDK.
|
|
|
58
45
|
* [Further resources](#further-resources)
|
|
59
46
|
- [Questions](#questions)
|
|
60
47
|
- [Issues](#issues)
|
|
61
|
-
- [Open source
|
|
48
|
+
- [Open source at IBM](#open-source-at-ibm)
|
|
62
49
|
- [Contributing](#contributing)
|
|
63
50
|
- [License](#license)
|
|
64
51
|
|
|
@@ -69,7 +56,7 @@ Changes might occur which impact applications that use this SDK.
|
|
|
69
56
|
## Overview
|
|
70
57
|
|
|
71
58
|
The IBM Cloudant Node.js SDK allows developers to programmatically
|
|
72
|
-
interact with IBM
|
|
59
|
+
interact with [IBM Cloudant](https://cloud.ibm.com/apidocs/cloudant)
|
|
73
60
|
with the help of the `@ibm-cloud/cloudant` package.
|
|
74
61
|
|
|
75
62
|
## Features
|
|
@@ -77,20 +64,20 @@ with the help of the `@ibm-cloud/cloudant` package.
|
|
|
77
64
|
The purpose of this Node.js SDK is to wrap most of the HTTP request APIs
|
|
78
65
|
provided by Cloudant and supply other functions to ease the usage of Cloudant.
|
|
79
66
|
This SDK should make life easier for programmers to do what’s really important
|
|
80
|
-
|
|
67
|
+
to them: developing software.
|
|
81
68
|
|
|
82
69
|
Reasons why you should consider using Cloudant Node.js SDK in your
|
|
83
70
|
project:
|
|
84
71
|
|
|
85
72
|
- Supported by IBM Cloudant.
|
|
86
73
|
- Server compatibility with:
|
|
87
|
-
- IBM Cloudant "Classic"
|
|
88
|
-
- [Cloudant "Standard on Transaction Engine"](https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-overview-te) for APIs compatible with Cloudant "Classic"
|
|
89
|
-
- [Apache CouchDB 3.x](https://docs.couchdb.org/en/stable/) for data operations
|
|
74
|
+
- IBM Cloudant "Classic".
|
|
75
|
+
- [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.
|
|
76
|
+
- [Apache CouchDB 3.x](https://docs.couchdb.org/en/stable/) for data operations.
|
|
90
77
|
- Includes all the most popular and latest supported endpoints for
|
|
91
78
|
applications.
|
|
92
79
|
- Handles the authentication.
|
|
93
|
-
- Familiar user experience
|
|
80
|
+
- Familiar user experience with IBM Cloud SDKs.
|
|
94
81
|
- Flexibility to use either built-in models or byte-based requests and responses for documents.
|
|
95
82
|
- `Promise` based design with asynchronous HTTP requests.
|
|
96
83
|
- Use either as native JavaScript or take advantage of TypeScript models.
|
|
@@ -103,8 +90,7 @@ project:
|
|
|
103
90
|
service instance or a
|
|
104
91
|
[CouchDB](https://docs.couchdb.org/en/latest/install/index.html)
|
|
105
92
|
server.
|
|
106
|
-
-
|
|
107
|
-
It may work on other versions but those are not officially supported.
|
|
93
|
+
- Node.js 12, 14 or 16.
|
|
108
94
|
|
|
109
95
|
## Installation
|
|
110
96
|
|
|
@@ -140,8 +126,8 @@ account.
|
|
|
140
126
|
for both [Cloudant][cloudant-basic-auth] and [Apache CouchDB][couch-basic-auth]
|
|
141
127
|
back-end database servers. This authentication type requires the good old
|
|
142
128
|
`username` and `password` credentials.
|
|
143
|
-
1. *Noauth* authentication does not
|
|
144
|
-
authentication type
|
|
129
|
+
1. *Noauth* authentication does not require credentials. Note that this
|
|
130
|
+
authentication type only works with queries against a database with read
|
|
145
131
|
access for everyone.
|
|
146
132
|
1. The service `url`.
|
|
147
133
|
|
|
@@ -155,8 +141,8 @@ There are several ways to **set** these properties:
|
|
|
155
141
|
|
|
156
142
|
#### IAM authentication
|
|
157
143
|
|
|
158
|
-
For Cloudant *IAM authentication
|
|
159
|
-
replacing `<url>` and `<apikey>` with your proper
|
|
144
|
+
For Cloudant *IAM authentication*, set the following environmental variables by
|
|
145
|
+
replacing the `<url>` and `<apikey>` with your proper
|
|
160
146
|
[service credentials][service-credentials]. There is no need to set
|
|
161
147
|
`CLOUDANT_AUTH_TYPE` to `IAM` because it is the default.
|
|
162
148
|
|
|
@@ -167,8 +153,8 @@ CLOUDANT_APIKEY=<apikey>
|
|
|
167
153
|
|
|
168
154
|
#### Session cookie authentication
|
|
169
155
|
|
|
170
|
-
For `COUCHDB_SESSION` authentication set the following environmental variables
|
|
171
|
-
by replacing `<url>`, `<username>` and `<password>` with your proper
|
|
156
|
+
For `COUCHDB_SESSION` authentication, set the following environmental variables
|
|
157
|
+
by replacing the `<url>`, `<username>` and `<password>` with your proper
|
|
172
158
|
[service credentials][service-credentials].
|
|
173
159
|
|
|
174
160
|
```bash
|
|
@@ -180,8 +166,8 @@ CLOUDANT_PASSWORD=<password>
|
|
|
180
166
|
|
|
181
167
|
#### Basic authentication
|
|
182
168
|
|
|
183
|
-
For *Basic authentication
|
|
184
|
-
replacing `<url>`, `<username>` and `<password>` with your proper
|
|
169
|
+
For *Basic authentication*, set the following environmental variables by
|
|
170
|
+
replacing the `<url>`, `<username>` and `<password>` with your proper
|
|
185
171
|
[service credentials][service-credentials].
|
|
186
172
|
|
|
187
173
|
```bash
|
|
@@ -191,7 +177,7 @@ CLOUDANT_USERNAME=<username>
|
|
|
191
177
|
CLOUDANT_PASSWORD=<password>
|
|
192
178
|
```
|
|
193
179
|
|
|
194
|
-
**Note**: We recommend
|
|
180
|
+
**Note**: We recommend that you use [IAM](#iam-authentication) for Cloudant and
|
|
195
181
|
[Session](#session-cookie-authentication) for CouchDB authentication.
|
|
196
182
|
|
|
197
183
|
### Authentication with external configuration
|
|
@@ -206,9 +192,9 @@ will guide you.
|
|
|
206
192
|
|
|
207
193
|
To learn more about how to use programmatic authentication, see the related
|
|
208
194
|
documentation in the
|
|
209
|
-
[Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=
|
|
195
|
+
[Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node#programmatic-authentication)
|
|
210
196
|
or in the
|
|
211
|
-
[Node.js SDK Core document
|
|
197
|
+
[Node.js SDK Core document](https://github.com/IBM/node-sdk-core/blob/master/Authentication.md) about authentication.
|
|
212
198
|
|
|
213
199
|
## Using the SDK
|
|
214
200
|
|
|
@@ -217,20 +203,28 @@ For general IBM Cloud SDK usage information, please see
|
|
|
217
203
|
|
|
218
204
|
### Code examples
|
|
219
205
|
|
|
220
|
-
The code examples
|
|
221
|
-
[
|
|
206
|
+
The following code examples
|
|
207
|
+
[authenticate with the environment variables](#authenticate-with-environment-variables).
|
|
222
208
|
|
|
223
209
|
#### 1. Retrieve information from an existing database
|
|
224
210
|
|
|
225
|
-
**Note:**
|
|
211
|
+
**Note:** This example code assumes that `animaldb` database does not exist in your account.
|
|
226
212
|
|
|
227
|
-
This example code gathers
|
|
228
|
-
the https://examples.cloudant.com/ service `url`. To
|
|
213
|
+
This example code gathers information about an existing database hosted on
|
|
214
|
+
the https://examples.cloudant.com/ service `url`. To connect, you must
|
|
229
215
|
extend your environment variables with the *service url* and *authentication
|
|
230
|
-
type* to use `NOAUTH` authentication while
|
|
216
|
+
type* to use `NOAUTH` authentication while you connect to the `animaldb` database.
|
|
231
217
|
This step is necessary for the SDK to distinguish the `EXAMPLES` custom service
|
|
232
218
|
name from the default service name which is `CLOUDANT`.
|
|
233
219
|
|
|
220
|
+
Cloudant environment variable naming starts with a *service name* prefix that identifies your service.
|
|
221
|
+
By default this is `CLOUDANT`, see the settings in the
|
|
222
|
+
[authentication with environment variables section](#authentication-with-environment-variables).
|
|
223
|
+
|
|
224
|
+
If you would like to rename your Cloudant service from `CLOUDANT`,
|
|
225
|
+
you must use your defined service name as the prefix for all Cloudant related environment variables.
|
|
226
|
+
The code block below provides an example of instantiating a user-defined `EXAMPLES` service name.
|
|
227
|
+
|
|
234
228
|
```bash
|
|
235
229
|
EXAMPLES_URL=https://examples.cloudant.com
|
|
236
230
|
EXAMPLES_AUTH_TYPE=NOAUTH
|
|
@@ -247,43 +241,43 @@ import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
|
247
241
|
[embedmd]:# (test/examples/src/ts/GetInfoFromExistingDatabase.ts /\/\/ 1./ $)
|
|
248
242
|
```ts
|
|
249
243
|
// 1. Create a Cloudant client with "EXAMPLES" service name ===================
|
|
250
|
-
const client =
|
|
251
|
-
CloudantV1.newInstance({serviceName:"EXAMPLES"});
|
|
244
|
+
const client = CloudantV1.newInstance({ serviceName: 'EXAMPLES' });
|
|
252
245
|
|
|
253
246
|
// 2. Get server information ==================================================
|
|
254
247
|
// call service without parameters:
|
|
255
|
-
client.getServerInformation()
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
});
|
|
248
|
+
client.getServerInformation().then((serverInformation) => {
|
|
249
|
+
const version = serverInformation.result.version;
|
|
250
|
+
console.log(`Server version ${version}`);
|
|
251
|
+
});
|
|
260
252
|
|
|
261
253
|
// 3. Get database information for "animaldb" =================================
|
|
262
|
-
const dbName =
|
|
254
|
+
const dbName = 'animaldb';
|
|
263
255
|
|
|
264
256
|
// call service with embedded parameters:
|
|
265
|
-
client.getDatabaseInformation({db: dbName})
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
257
|
+
client.getDatabaseInformation({ db: dbName }).then((dbInfo) => {
|
|
258
|
+
const documentCount = dbInfo.result.doc_count;
|
|
259
|
+
const dbNameResult = dbInfo.result.db_name;
|
|
260
|
+
|
|
261
|
+
// 4. Show document count in database =================================
|
|
262
|
+
console.log(
|
|
263
|
+
`Document count in "${dbNameResult}" database is ` + documentCount + '.'
|
|
264
|
+
);
|
|
265
|
+
});
|
|
274
266
|
|
|
275
267
|
// 5. Get zebra document out of the database by document id ===================
|
|
276
|
-
const getDocParams:
|
|
277
|
-
|
|
268
|
+
const getDocParams: CloudantV1.GetDocumentParams = {
|
|
269
|
+
db: dbName,
|
|
270
|
+
docId: 'zebra',
|
|
271
|
+
};
|
|
278
272
|
|
|
279
273
|
// call service with predefined parameters:
|
|
280
|
-
client.getDocument(getDocParams)
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
274
|
+
client.getDocument(getDocParams).then((documentAboutZebra) => {
|
|
275
|
+
// result object is defined as a Document here:
|
|
276
|
+
const result: CloudantV1.Document = documentAboutZebra.result;
|
|
277
|
+
console.log(
|
|
278
|
+
'Document retrieved from database:\n' + JSON.stringify(result, null, 2)
|
|
279
|
+
);
|
|
280
|
+
});
|
|
287
281
|
```
|
|
288
282
|
</details>
|
|
289
283
|
|
|
@@ -313,7 +307,9 @@ const getInfoFromExistingDatabase = async () => {
|
|
|
313
307
|
const dbNameResult = dbInfo.result.db_name;
|
|
314
308
|
|
|
315
309
|
// 4. Show document count in database =================================
|
|
316
|
-
console.log(
|
|
310
|
+
console.log(
|
|
311
|
+
`Document count in "${dbNameResult}" database is ` + documentCount + '.'
|
|
312
|
+
);
|
|
317
313
|
|
|
318
314
|
// 5. Get zebra document out of the database by document id ===================
|
|
319
315
|
const getDocParams = { db: dbName, docId: 'zebra' };
|
|
@@ -324,7 +320,9 @@ const getInfoFromExistingDatabase = async () => {
|
|
|
324
320
|
// result object is defined as a Document here:
|
|
325
321
|
const result = documentAboutZebra.result;
|
|
326
322
|
|
|
327
|
-
console.log(
|
|
323
|
+
console.log(
|
|
324
|
+
'Document retrieved from database:\n' + JSON.stringify(result, null, 2)
|
|
325
|
+
);
|
|
328
326
|
};
|
|
329
327
|
|
|
330
328
|
if (require.main === module) {
|
|
@@ -334,7 +332,7 @@ if (require.main === module) {
|
|
|
334
332
|
|
|
335
333
|
</details>
|
|
336
334
|
|
|
337
|
-
|
|
335
|
+
When you run the code, you see a result similar to the following output.
|
|
338
336
|
|
|
339
337
|
[embedmd]:# (test/examples/output/GetInfoFromExistingDatabase.txt)
|
|
340
338
|
```txt
|
|
@@ -356,10 +354,9 @@ Document retrieved from database:
|
|
|
356
354
|
|
|
357
355
|
#### 2. Create your own database and add a document
|
|
358
356
|
|
|
359
|
-
**Note:**
|
|
357
|
+
**Note:** This example code assumes that `orders` database does not exist in your account.
|
|
360
358
|
|
|
361
|
-
Now comes the exciting part
|
|
362
|
-
a document about *Bob Smith* with your own [IAM](#iam-authentication) or
|
|
359
|
+
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
|
|
363
360
|
[Basic](#basic-authentication) service credentials.
|
|
364
361
|
|
|
365
362
|
<details>
|
|
@@ -374,53 +371,59 @@ import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
|
374
371
|
[embedmd]:# (test/examples/src/ts/CreateDbAndDoc.ts /interface/ $)
|
|
375
372
|
```ts
|
|
376
373
|
interface OrderDocument extends CloudantV1.Document {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
374
|
+
name?: string;
|
|
375
|
+
joined?: string;
|
|
376
|
+
_id: string;
|
|
377
|
+
_rev?: string;
|
|
381
378
|
}
|
|
382
379
|
|
|
383
380
|
// 1. Create a client with `CLOUDANT` default service name ================
|
|
384
381
|
const client = CloudantV1.newInstance({});
|
|
385
382
|
|
|
386
383
|
// 2. Create a database =======================================================
|
|
387
|
-
const exampleDbName =
|
|
384
|
+
const exampleDbName = 'orders';
|
|
388
385
|
|
|
389
386
|
// Try to create database if it doesn't exist
|
|
390
|
-
const createDb = client
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
387
|
+
const createDb = client
|
|
388
|
+
.putDatabase({ db: exampleDbName })
|
|
389
|
+
.then((putDatabaseResult) => {
|
|
390
|
+
if (putDatabaseResult.result.ok) {
|
|
391
|
+
console.log(`"${exampleDbName}" database created."`);
|
|
392
|
+
}
|
|
393
|
+
})
|
|
394
|
+
.catch((err) => {
|
|
395
|
+
if (err.code === 412) {
|
|
396
|
+
console.log(
|
|
397
|
+
'Cannot create "' + exampleDbName + '" database, it already exists.'
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
402
401
|
|
|
403
402
|
// 3. Create a document =======================================================
|
|
404
403
|
// Create a document object with "example" id
|
|
405
|
-
const exampleDocId =
|
|
404
|
+
const exampleDocId = 'example';
|
|
406
405
|
|
|
407
406
|
// set required _id property on exampleDocument:
|
|
408
|
-
const exampleDocument: OrderDocument = {_id: exampleDocId};
|
|
407
|
+
const exampleDocument: OrderDocument = { _id: exampleDocId };
|
|
409
408
|
|
|
410
409
|
// Add "name" and "joined" fields to the document
|
|
411
|
-
exampleDocument.name =
|
|
412
|
-
exampleDocument.joined =
|
|
410
|
+
exampleDocument.name = 'Bob Smith';
|
|
411
|
+
exampleDocument.joined = '2019-01-24T10:42:99.000Z';
|
|
413
412
|
|
|
414
413
|
// Save the document in the database
|
|
415
414
|
createDb.then(() => {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
415
|
+
client
|
|
416
|
+
.postDocument({
|
|
417
|
+
db: exampleDbName,
|
|
418
|
+
document: exampleDocument,
|
|
419
|
+
})
|
|
420
|
+
.then((createDocumentResponse) => {
|
|
421
|
+
// Keep track with the revision number of the document object
|
|
422
|
+
exampleDocument._rev = createDocumentResponse.result.rev;
|
|
423
|
+
console.log(
|
|
424
|
+
'You have created the document:\n' +
|
|
425
|
+
JSON.stringify(exampleDocument, null, 2)
|
|
426
|
+
);
|
|
424
427
|
});
|
|
425
428
|
});
|
|
426
429
|
```
|
|
@@ -453,7 +456,9 @@ const createDbAndDoc = async () => {
|
|
|
453
456
|
}
|
|
454
457
|
} catch (err) {
|
|
455
458
|
if (err.code === 412) {
|
|
456
|
-
console.log(
|
|
459
|
+
console.log(
|
|
460
|
+
'Cannot create "' + exampleDbName + '" database, it already exists.'
|
|
461
|
+
);
|
|
457
462
|
}
|
|
458
463
|
}
|
|
459
464
|
|
|
@@ -475,7 +480,10 @@ const createDbAndDoc = async () => {
|
|
|
475
480
|
|
|
476
481
|
// Keep track with the revision number of the document object
|
|
477
482
|
exampleDocument._rev = createDocumentResponse.result.rev;
|
|
478
|
-
console.log(
|
|
483
|
+
console.log(
|
|
484
|
+
'You have created the document:\n' +
|
|
485
|
+
JSON.stringify(exampleDocument, null, 2)
|
|
486
|
+
);
|
|
479
487
|
};
|
|
480
488
|
|
|
481
489
|
if (require.main === module) {
|
|
@@ -486,7 +494,7 @@ if (require.main === module) {
|
|
|
486
494
|
|
|
487
495
|
|
|
488
496
|
</details>
|
|
489
|
-
|
|
497
|
+
When you run the code, you see a result similar to the following output.
|
|
490
498
|
|
|
491
499
|
[embedmd]:# (test/examples/output/CreateDbAndDoc.txt)
|
|
492
500
|
```txt
|
|
@@ -502,11 +510,11 @@ You have created the document:
|
|
|
502
510
|
|
|
503
511
|
#### 3. Update your previously created document
|
|
504
512
|
|
|
505
|
-
**Note**:
|
|
513
|
+
**Note**: This example code assumes that you have created both the `orders`
|
|
506
514
|
database and the `example` document by
|
|
507
|
-
[running
|
|
508
|
-
successfully,
|
|
509
|
-
database or
|
|
515
|
+
[running the previous example code](#2-create-your-own-database-and-add-a-document)
|
|
516
|
+
successfully. Otherwise, the following error message occurs, "Cannot update document because either 'orders'
|
|
517
|
+
database or 'example' document was not found."
|
|
510
518
|
|
|
511
519
|
<details>
|
|
512
520
|
<summary>Update code example</summary>
|
|
@@ -520,51 +528,70 @@ import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
|
520
528
|
[embedmd]:# (test/examples/src/ts/UpdateDoc.ts /interface/ $)
|
|
521
529
|
```ts
|
|
522
530
|
interface OrderDocument extends CloudantV1.Document {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
531
|
+
address?: string;
|
|
532
|
+
joined?: string;
|
|
533
|
+
_id?: string;
|
|
534
|
+
_rev?: string;
|
|
527
535
|
}
|
|
528
536
|
|
|
529
537
|
// 1. Create a client with `CLOUDANT` default service name ================
|
|
530
538
|
const client = CloudantV1.newInstance({});
|
|
531
539
|
// 2. Update the document =====================================================
|
|
532
540
|
// Set the options to get the document out of the database if it exists
|
|
533
|
-
const exampleDbName =
|
|
541
|
+
const exampleDbName = 'orders';
|
|
534
542
|
|
|
535
543
|
// Try to get the document if it previously existed in the database
|
|
536
|
-
const getDocParams: CloudantV1.GetDocumentParams =
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
544
|
+
const getDocParams: CloudantV1.GetDocumentParams = {
|
|
545
|
+
docId: 'example',
|
|
546
|
+
db: exampleDbName,
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
// Note: for response byte stream use:
|
|
550
|
+
// const getdocAsStreamParam: CloudantV1.GetDocumentAsStreamParams = {
|
|
551
|
+
// docId: 'example',
|
|
552
|
+
// db: exampleDbName,
|
|
553
|
+
// };
|
|
554
|
+
// client
|
|
555
|
+
// .getDocumentAsStream(getdocAsStreamParam)
|
|
556
|
+
// .then((documentAsByteStream) => {});
|
|
557
|
+
|
|
558
|
+
client
|
|
559
|
+
.getDocument(getDocParams)
|
|
560
|
+
.then((docResult) => {
|
|
561
|
+
// using OrderDocument on getDocument result:
|
|
562
|
+
const document: OrderDocument = docResult.result;
|
|
563
|
+
|
|
564
|
+
// Add Bob Smith's address to the document
|
|
565
|
+
document.address = '19 Front Street, Darlington, DL5 1TY';
|
|
566
|
+
|
|
567
|
+
// Remove the joined property from document object
|
|
568
|
+
delete document.joined;
|
|
569
|
+
|
|
570
|
+
// Update the document in the database
|
|
571
|
+
client
|
|
572
|
+
.postDocument({ db: exampleDbName, document: document })
|
|
573
|
+
// Note: for request byte stream use:
|
|
574
|
+
// .postDocument(
|
|
575
|
+
// {db: exampleDbName, document: documentAsByteStream}
|
|
576
|
+
// )
|
|
577
|
+
.then((res) => {
|
|
578
|
+
// Keeping track with the revision number of the document object:
|
|
579
|
+
document._rev = res.result.rev;
|
|
580
|
+
console.log(
|
|
581
|
+
'You have updated the document:\n' + JSON.stringify(document, null, 2)
|
|
582
|
+
);
|
|
583
|
+
});
|
|
584
|
+
})
|
|
585
|
+
.catch((err) => {
|
|
586
|
+
if (err.code === 404) {
|
|
587
|
+
console.log(
|
|
588
|
+
'Cannot update document because either "' +
|
|
589
|
+
exampleDbName +
|
|
590
|
+
'" database or the "example" ' +
|
|
591
|
+
'document was not found.'
|
|
592
|
+
);
|
|
593
|
+
}
|
|
594
|
+
});
|
|
568
595
|
```
|
|
569
596
|
</details>
|
|
570
597
|
|
|
@@ -592,6 +619,14 @@ const updateDoc = async () => {
|
|
|
592
619
|
})
|
|
593
620
|
).result;
|
|
594
621
|
|
|
622
|
+
// Note: for response byte stream use:
|
|
623
|
+
// const documentAsByteStream = (
|
|
624
|
+
// await client.getDocumentAsStream({
|
|
625
|
+
// docId: 'example',
|
|
626
|
+
// db: exampleDbName,
|
|
627
|
+
// })
|
|
628
|
+
// ).result;
|
|
629
|
+
|
|
595
630
|
// Add Bob Smith's address to the document
|
|
596
631
|
document.address = '19 Front Street, Darlington, DL5 1TY';
|
|
597
632
|
|
|
@@ -606,7 +641,17 @@ const updateDoc = async () => {
|
|
|
606
641
|
})
|
|
607
642
|
).result.rev;
|
|
608
643
|
|
|
609
|
-
|
|
644
|
+
// Note: for request byte stream use:
|
|
645
|
+
// document._rev = (
|
|
646
|
+
// await client.postDocument({
|
|
647
|
+
// db: exampleDbName,
|
|
648
|
+
// document: documentAsByteStream,
|
|
649
|
+
// })
|
|
650
|
+
// ).result.rev;
|
|
651
|
+
|
|
652
|
+
console.log(
|
|
653
|
+
'You have updated the document:\n' + JSON.stringify(document, null, 2)
|
|
654
|
+
);
|
|
610
655
|
} catch (err) {
|
|
611
656
|
if (err.code === 404) {
|
|
612
657
|
console.log(
|
|
@@ -627,7 +672,7 @@ if (require.main === module) {
|
|
|
627
672
|
|
|
628
673
|
|
|
629
674
|
</details>
|
|
630
|
-
|
|
675
|
+
When you run the code, you see a result similar to the following output.
|
|
631
676
|
|
|
632
677
|
[embedmd]:# (test/examples/output/UpdateDoc.txt)
|
|
633
678
|
```txt
|
|
@@ -642,11 +687,11 @@ You have updated the document:
|
|
|
642
687
|
|
|
643
688
|
#### 4. Delete your previously created document
|
|
644
689
|
|
|
645
|
-
**Note**:
|
|
690
|
+
**Note**: This example code assumes that you have created both the `orders`
|
|
646
691
|
database and the `example` document by
|
|
647
|
-
[running
|
|
648
|
-
successfully,
|
|
649
|
-
database or
|
|
692
|
+
[running the previous example code](#2-create-your-own-database-and-add-a-document)
|
|
693
|
+
successfully. Otherwise, the following error message occurs, "Cannot delete document because either 'orders'
|
|
694
|
+
database or 'example' document was not found."
|
|
650
695
|
|
|
651
696
|
<details>
|
|
652
697
|
<summary>Delete code example</summary>
|
|
@@ -660,11 +705,11 @@ import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
|
660
705
|
[embedmd]:# (test/examples/src/ts/DeleteDoc.ts /interface/ $)
|
|
661
706
|
```ts
|
|
662
707
|
interface OrderDocument extends CloudantV1.Document {
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
708
|
+
name?: string;
|
|
709
|
+
address?: string;
|
|
710
|
+
joined?: string;
|
|
711
|
+
_id?: string;
|
|
712
|
+
_rev?: string;
|
|
668
713
|
}
|
|
669
714
|
|
|
670
715
|
// 1. Create a client with `CLOUDANT` default service name ================
|
|
@@ -672,33 +717,40 @@ const client = CloudantV1.newInstance({});
|
|
|
672
717
|
|
|
673
718
|
// 2. Delete the document =============================================
|
|
674
719
|
// Set the options to get the document out of the database if it exists
|
|
675
|
-
const exampleDbName =
|
|
676
|
-
const exampleDocId =
|
|
720
|
+
const exampleDbName = 'orders';
|
|
721
|
+
const exampleDocId = 'example';
|
|
677
722
|
|
|
678
723
|
// Try to get the document if it previously existed in the database
|
|
679
|
-
const getDocParams: CloudantV1.GetDocumentParams =
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
724
|
+
const getDocParams: CloudantV1.GetDocumentParams = {
|
|
725
|
+
docId: exampleDocId,
|
|
726
|
+
db: exampleDbName,
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
client
|
|
730
|
+
.getDocument(getDocParams)
|
|
731
|
+
.then((docResult) => {
|
|
732
|
+
const document: OrderDocument = docResult.result;
|
|
733
|
+
|
|
734
|
+
client
|
|
735
|
+
.deleteDocument({
|
|
736
|
+
db: exampleDbName,
|
|
737
|
+
docId: document._id,
|
|
738
|
+
rev: document._rev,
|
|
739
|
+
})
|
|
740
|
+
.then(() => {
|
|
741
|
+
console.log('You have deleted the document.');
|
|
742
|
+
});
|
|
743
|
+
})
|
|
744
|
+
.catch((err) => {
|
|
745
|
+
if (err.code === 404) {
|
|
746
|
+
console.log(
|
|
747
|
+
'Cannot delete document because either "' +
|
|
748
|
+
exampleDbName +
|
|
749
|
+
'" database or the "example" ' +
|
|
750
|
+
'document was not found.'
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
});
|
|
702
754
|
```
|
|
703
755
|
</details>
|
|
704
756
|
|
|
@@ -754,7 +806,7 @@ if (require.main === module) {
|
|
|
754
806
|
|
|
755
807
|
|
|
756
808
|
</details>
|
|
757
|
-
|
|
809
|
+
When you run the code, you see the following output.
|
|
758
810
|
|
|
759
811
|
[embedmd]:# (test/examples/output/DeleteDoc.txt)
|
|
760
812
|
```txt
|
|
@@ -763,54 +815,73 @@ You have deleted the document.
|
|
|
763
815
|
|
|
764
816
|
### Error handling
|
|
765
817
|
|
|
766
|
-
For sample code on handling errors,
|
|
818
|
+
For sample code on handling errors, see
|
|
767
819
|
[Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node#error-handling).
|
|
768
820
|
|
|
769
821
|
### Raw IO
|
|
770
822
|
|
|
771
823
|
For endpoints that read or write document content it is possible to bypass
|
|
772
|
-
usage of the built-in
|
|
773
|
-
|
|
774
|
-
|
|
824
|
+
usage of the built-in interface with byte streams.
|
|
825
|
+
|
|
826
|
+
Depending on the specific SDK operation it may be possible to:
|
|
827
|
+
* accept a user-provided byte stream to send to the server as a request body
|
|
828
|
+
* return a byte stream of the server response body to the user
|
|
829
|
+
|
|
830
|
+
Request byte stream can be supplied for `NodeJS.ReadableStream` or `Buffer` type parameters
|
|
831
|
+
.
|
|
832
|
+
For these cases you can pass this byte stream directly to the HTTP request body.
|
|
833
|
+
|
|
834
|
+
Response byte stream is supported in functions with the suffix of `AsStream`.
|
|
835
|
+
The returned byte stream allows the response body to be consumed
|
|
836
|
+
without triggering JSON unmarshalling that is typically performed by the SDK.
|
|
837
|
+
|
|
838
|
+
The [update document](#3-update-your-previously-created-document) section
|
|
839
|
+
contains examples for both request and response byte stream cases.
|
|
840
|
+
|
|
841
|
+
The API reference contains further examples of using byte streams.
|
|
842
|
+
They are titled "Example request as stream" and are initially collapsed.
|
|
843
|
+
Expand them to see examples of:
|
|
844
|
+
|
|
845
|
+
- Byte requests:
|
|
846
|
+
- [Bulk modify multiple documents in a database](https://cloud.ibm.com/apidocs/cloudant?code=node#postbulkdocs)
|
|
775
847
|
|
|
776
|
-
-
|
|
777
|
-
- [Query a list of all documents in a database](https://cloud.ibm.com/apidocs/cloudant?code=node#postalldocs)
|
|
778
|
-
- [Query the database document changes feed](https://cloud.ibm.com/apidocs/cloudant?code=node#postchanges)
|
|
848
|
+
- Byte responses:
|
|
849
|
+
- [Query a list of all documents in a database](https://cloud.ibm.com/apidocs/cloudant?code=node#postalldocs)
|
|
850
|
+
- [Query the database document changes feed](https://cloud.ibm.com/apidocs/cloudant?code=node#postchanges)
|
|
779
851
|
|
|
780
852
|
### Further resources
|
|
781
853
|
|
|
782
854
|
- [Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node):
|
|
783
|
-
API examples for Cloudant Node.js SDK.
|
|
784
|
-
- [
|
|
855
|
+
API reference including usage examples for Cloudant Node.js SDK API.
|
|
856
|
+
- [TypeDoc](https://ibm.github.io/cloudant-node-sdk/):
|
|
857
|
+
Cloudant Node.js SDK API Documentation.
|
|
858
|
+
- [Cloudant docs](https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-getting-started-with-cloudant):
|
|
785
859
|
The official documentation page for Cloudant.
|
|
786
|
-
- [Cloudant Learning Center](https://developer.ibm.com/clouddataservices/docs/compose/cloudant/):
|
|
787
|
-
The official learning center with several useful videos which help you to use
|
|
788
|
-
Cloudant successfully.
|
|
789
860
|
- [Cloudant blog](https://blog.cloudant.com/):
|
|
790
|
-
Many useful articles how to optimize Cloudant for common problems.
|
|
861
|
+
Many useful articles about how to optimize Cloudant for common problems.
|
|
791
862
|
|
|
792
863
|
## Questions
|
|
793
864
|
|
|
794
865
|
If you are having difficulties using this SDK or have a question about the
|
|
795
|
-
IBM Cloud services,
|
|
866
|
+
IBM Cloud services, ask a question on
|
|
796
867
|
[Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-cloud).
|
|
797
868
|
|
|
798
869
|
## Issues
|
|
799
870
|
|
|
800
871
|
If you encounter an issue with the project, you are welcome to submit a
|
|
801
872
|
[bug report](https://github.com/IBM/cloudant-node-sdk/issues).
|
|
802
|
-
Before
|
|
803
|
-
|
|
873
|
+
Before you submit a bug report, search for
|
|
874
|
+
[similar issues](https://github.com/IBM/cloudant-node-sdk/issues?q=is%3Aissue) and review the
|
|
875
|
+
[KNOWN_ISSUES file](KNOWN_ISSUES.md) to verify that your issue hasn't been reported yet.
|
|
804
876
|
|
|
805
|
-
## Open source
|
|
877
|
+
## Open source at IBM
|
|
806
878
|
|
|
807
|
-
Find more open source projects on the [IBM Github
|
|
879
|
+
Find more open source projects on the [IBM Github](http://ibm.github.io/) page.
|
|
808
880
|
|
|
809
881
|
## Contributing
|
|
810
882
|
|
|
811
|
-
|
|
883
|
+
For more information, see [CONTRIBUTING](CONTRIBUTING.md).
|
|
812
884
|
|
|
813
885
|
## License
|
|
814
886
|
|
|
815
|
-
This SDK is released under the Apache 2.0 license.
|
|
816
|
-
The license's full text can be found in [LICENSE](LICENSE).
|
|
887
|
+
This SDK is released under the Apache 2.0 license. To read the full text of the license, see [LICENSE](LICENSE).
|