@ibm-cloud/cloudant 0.1.5 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +143 -155
- package/cloudant/v1.d.ts +26 -426
- package/cloudant/v1.js +10 -340
- package/cloudant/v1.js.map +1 -1
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
[](https://github.com/IBM/cloudant-node-sdk/actions/workflows/test.yml)
|
|
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.1
|
|
5
|
+
# IBM Cloudant Node.js SDK Version 0.2.1
|
|
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).
|
|
@@ -42,8 +42,8 @@ to avoid surprises.
|
|
|
42
42
|
- [Using the SDK](#using-the-sdk)
|
|
43
43
|
* [Request timeout configuration](#request-timeout-configuration)
|
|
44
44
|
* [Code examples](#code-examples)
|
|
45
|
-
+ [1.
|
|
46
|
-
+ [2.
|
|
45
|
+
+ [1. Create a database and add a document](#1-create-a-database-and-add-a-document)
|
|
46
|
+
+ [2. Retrieve information from an existing database](#2-retrieve-information-from-an-existing-database)
|
|
47
47
|
+ [3. Update your previously created document](#3-update-your-previously-created-document)
|
|
48
48
|
+ [4. Delete your previously created document](#4-delete-your-previously-created-document)
|
|
49
49
|
+ [Further code examples](#further-code-examples)
|
|
@@ -202,16 +202,16 @@ To learn more about how to use programmatic authentication, see the related
|
|
|
202
202
|
documentation in the
|
|
203
203
|
[Cloudant API docs](https://cloud.ibm.com/apidocs/cloudant?code=node#programmatic-authentication)
|
|
204
204
|
or in the
|
|
205
|
-
[Node.js SDK Core document](https://github.com/IBM/node-sdk-core/blob/
|
|
205
|
+
[Node.js SDK Core document](https://github.com/IBM/node-sdk-core/blob/main/Authentication.md) about authentication.
|
|
206
206
|
|
|
207
207
|
## Using the SDK
|
|
208
208
|
|
|
209
|
-
For fundamental SDK usage information and config options, please see the common [IBM Cloud SDK](https://github.com/IBM/ibm-cloud-sdk-common/blob/
|
|
209
|
+
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.
|
|
210
210
|
|
|
211
211
|
### Request timeout configuration
|
|
212
212
|
|
|
213
213
|
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.
|
|
214
|
-
The [request timeout](https://github.com/IBM/ibm-cloud-sdk-common/blob/
|
|
214
|
+
The [request timeout](https://github.com/IBM/ibm-cloud-sdk-common/blob/main/README.md) section contains details on how to change the value.
|
|
215
215
|
|
|
216
216
|
**Note:** System settings may take precedence over configured timeout values.
|
|
217
217
|
|
|
@@ -220,16 +220,14 @@ The [request timeout](https://github.com/IBM/ibm-cloud-sdk-common/blob/master/RE
|
|
|
220
220
|
The following code examples
|
|
221
221
|
[authenticate with the environment variables](#authenticate-with-environment-variables).
|
|
222
222
|
|
|
223
|
-
#### 1.
|
|
223
|
+
#### 1. Create a database and add a document
|
|
224
224
|
|
|
225
|
-
**Note:** This example code assumes that `
|
|
225
|
+
**Note:** This example code assumes that `orders` database does not exist in your account.
|
|
226
226
|
|
|
227
|
-
This example code
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
This step is necessary for the SDK to distinguish the `EXAMPLES` custom service
|
|
232
|
-
name from the default service name which is `CLOUDANT`.
|
|
227
|
+
This example code creates `orders` database and adds a new document "example"
|
|
228
|
+
into it. To connect, you must set your environment variables with
|
|
229
|
+
the *service url*, *authentication type* and *authentication credentials*
|
|
230
|
+
of your Cloudant service.
|
|
233
231
|
|
|
234
232
|
Cloudant environment variable naming starts with a *service name* prefix that identifies your service.
|
|
235
233
|
By default this is `CLOUDANT`, see the settings in the
|
|
@@ -237,148 +235,12 @@ By default this is `CLOUDANT`, see the settings in the
|
|
|
237
235
|
|
|
238
236
|
If you would like to rename your Cloudant service from `CLOUDANT`,
|
|
239
237
|
you must use your defined service name as the prefix for all Cloudant related environment variables.
|
|
240
|
-
The code block below provides an example of instantiating a user-defined `EXAMPLES` service name.
|
|
241
|
-
|
|
242
|
-
```bash
|
|
243
|
-
EXAMPLES_URL=https://examples.cloudant.com
|
|
244
|
-
EXAMPLES_AUTH_TYPE=NOAUTH
|
|
245
|
-
```
|
|
246
238
|
|
|
247
239
|
Once the environment variables are set, you can try out the code examples.
|
|
248
240
|
|
|
249
241
|
<details open>
|
|
250
242
|
<summary>TypeScript:</summary>
|
|
251
243
|
|
|
252
|
-
```ts
|
|
253
|
-
import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
254
|
-
```
|
|
255
|
-
[embedmd]:# (test/examples/src/ts/GetInfoFromExistingDatabase.ts /\/\/ 1./ $)
|
|
256
|
-
```ts
|
|
257
|
-
// 1. Create a Cloudant client with "EXAMPLES" service name =====================
|
|
258
|
-
const client = CloudantV1.newInstance({ serviceName: 'EXAMPLES' });
|
|
259
|
-
|
|
260
|
-
// 2. Get server information ====================================================
|
|
261
|
-
// call service without parameters:
|
|
262
|
-
client.getServerInformation().then((serverInformation) => {
|
|
263
|
-
const { version } = serverInformation.result;
|
|
264
|
-
console.log(`Server version ${version}`);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
// 3. Get database information for "animaldb" ===================================
|
|
268
|
-
const dbName = 'animaldb';
|
|
269
|
-
|
|
270
|
-
// call service with embedded parameters:
|
|
271
|
-
client.getDatabaseInformation({ db: dbName }).then((dbInfo) => {
|
|
272
|
-
const documentCount = dbInfo.result.doc_count;
|
|
273
|
-
const dbNameResult = dbInfo.result.db_name;
|
|
274
|
-
|
|
275
|
-
// 4. Show document count in database =========================================
|
|
276
|
-
console.log(
|
|
277
|
-
`Document count in "${dbNameResult}" database is ${documentCount}.`
|
|
278
|
-
);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
// 5. Get zebra document out of the database by document id =====================
|
|
282
|
-
const getDocParams: CloudantV1.GetDocumentParams = {
|
|
283
|
-
db: dbName,
|
|
284
|
-
docId: 'zebra',
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
// call service with predefined parameters:
|
|
288
|
-
client.getDocument(getDocParams).then((documentAboutZebra) => {
|
|
289
|
-
// result object is defined as a Document here:
|
|
290
|
-
const { result } = documentAboutZebra;
|
|
291
|
-
console.log(
|
|
292
|
-
`Document retrieved from database:\n${JSON.stringify(result, null, 2)}`
|
|
293
|
-
);
|
|
294
|
-
});
|
|
295
|
-
```
|
|
296
|
-
</details>
|
|
297
|
-
|
|
298
|
-
<details>
|
|
299
|
-
<summary>JavaScript:</summary>
|
|
300
|
-
|
|
301
|
-
```js
|
|
302
|
-
const { CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
303
|
-
```
|
|
304
|
-
[embedmd]:# (test/examples/src/js/GetInfoFromExistingDatabase.js /const getInfoFromExistingDatabase/ /getInfoFromExistingDatabase\(\);\n\}/)
|
|
305
|
-
```js
|
|
306
|
-
const getInfoFromExistingDatabase = async () => {
|
|
307
|
-
// 1. Create a Cloudant client with "EXAMPLES" service name ===================
|
|
308
|
-
const client = CloudantV1.newInstance({ serviceName: 'EXAMPLES' });
|
|
309
|
-
|
|
310
|
-
// 2. Get server information ==================================================
|
|
311
|
-
// call service without parameters:
|
|
312
|
-
const { version } = (await client.getServerInformation()).result;
|
|
313
|
-
console.log(`Server version ${version}`);
|
|
314
|
-
|
|
315
|
-
// 3. Get database information for "animaldb" =================================
|
|
316
|
-
const dbName = 'animaldb';
|
|
317
|
-
|
|
318
|
-
// call service with embedded parameters:
|
|
319
|
-
const dbInfo = await client.getDatabaseInformation({ db: dbName });
|
|
320
|
-
const documentCount = dbInfo.result.doc_count;
|
|
321
|
-
const dbNameResult = dbInfo.result.db_name;
|
|
322
|
-
|
|
323
|
-
// 4. Show document count in database =========================================
|
|
324
|
-
console.log(
|
|
325
|
-
`Document count in "${dbNameResult}" database is ${documentCount}.`
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
// 5. Get zebra document out of the database by document id ===================
|
|
329
|
-
const getDocParams = { db: dbName, docId: 'zebra' };
|
|
330
|
-
|
|
331
|
-
// call service with predefined parameters:
|
|
332
|
-
const documentAboutZebra = await client.getDocument(getDocParams);
|
|
333
|
-
|
|
334
|
-
// result object is defined as a Document here:
|
|
335
|
-
const { result } = documentAboutZebra;
|
|
336
|
-
|
|
337
|
-
console.log(
|
|
338
|
-
`Document retrieved from database:\n${JSON.stringify(result, null, 2)}`
|
|
339
|
-
);
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
if (require.main === module) {
|
|
343
|
-
getInfoFromExistingDatabase();
|
|
344
|
-
}
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
</details>
|
|
348
|
-
|
|
349
|
-
When you run the code, you see a result similar to the following output.
|
|
350
|
-
|
|
351
|
-
[embedmd]:# (test/examples/output/GetInfoFromExistingDatabase.txt)
|
|
352
|
-
```txt
|
|
353
|
-
Server version 2.1.1
|
|
354
|
-
Document count in "animaldb" database is 11.
|
|
355
|
-
Document retrieved from database:
|
|
356
|
-
{
|
|
357
|
-
"_id": "zebra",
|
|
358
|
-
"_rev": "3-750dac460a6cc41e6999f8943b8e603e",
|
|
359
|
-
"wiki_page": "http://en.wikipedia.org/wiki/Plains_zebra",
|
|
360
|
-
"min_length": 2,
|
|
361
|
-
"max_length": 2.5,
|
|
362
|
-
"min_weight": 175,
|
|
363
|
-
"max_weight": 387,
|
|
364
|
-
"class": "mammal",
|
|
365
|
-
"diet": "herbivore"
|
|
366
|
-
}
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
#### 2. Create your own database and add a document
|
|
370
|
-
|
|
371
|
-
**Note:** This example code assumes that `orders` database does not exist in your account.
|
|
372
|
-
|
|
373
|
-
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
|
|
374
|
-
[Basic](#basic-authentication) service credentials.
|
|
375
|
-
|
|
376
|
-
<details>
|
|
377
|
-
<summary>Create code example</summary>
|
|
378
|
-
|
|
379
|
-
<details open>
|
|
380
|
-
<summary>TypeScript:</summary>
|
|
381
|
-
|
|
382
244
|
```ts
|
|
383
245
|
import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
384
246
|
```
|
|
@@ -532,8 +394,6 @@ if (require.main === module) {
|
|
|
532
394
|
```
|
|
533
395
|
</details>
|
|
534
396
|
|
|
535
|
-
|
|
536
|
-
</details>
|
|
537
397
|
When you run the code, you see a result similar to the following output.
|
|
538
398
|
|
|
539
399
|
[embedmd]:# (test/examples/output/CreateDbAndDoc.txt)
|
|
@@ -548,11 +408,139 @@ You have created the document:
|
|
|
548
408
|
}
|
|
549
409
|
```
|
|
550
410
|
|
|
411
|
+
#### 2. Retrieve information from an existing database
|
|
412
|
+
|
|
413
|
+
**Note**: This example code assumes that you have created both the `orders`
|
|
414
|
+
database and the `example` document by
|
|
415
|
+
[running the previous example code](#1-create-a-database-and-add-a-document)
|
|
416
|
+
successfully. Otherwise, the following error message occurs, "Cannot delete document because either 'orders'
|
|
417
|
+
database or 'example' document was not found."
|
|
418
|
+
|
|
419
|
+
<details>
|
|
420
|
+
<summary>Gather database information example</summary>
|
|
421
|
+
|
|
422
|
+
<details open>
|
|
423
|
+
<summary>TypeScript:</summary>
|
|
424
|
+
|
|
425
|
+
```ts
|
|
426
|
+
import {CloudantV1} from "@ibm-cloud/cloudant";
|
|
427
|
+
```
|
|
428
|
+
[embedmd]:# (test/examples/src/ts/GetInfoFromExistingDatabase.ts /\/\/ 1./ $)
|
|
429
|
+
```ts
|
|
430
|
+
// 1. Create a client with `CLOUDANT` default service name =====================
|
|
431
|
+
const client = CloudantV1.newInstance({});
|
|
432
|
+
|
|
433
|
+
// 2. Get server information ====================================================
|
|
434
|
+
// call service without parameters:
|
|
435
|
+
client.getServerInformation().then((serverInformation) => {
|
|
436
|
+
const { version } = serverInformation.result;
|
|
437
|
+
console.log(`Server version ${version}`);
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
// 3. Get database information for "orders" ===================================
|
|
441
|
+
const dbName = 'orders';
|
|
442
|
+
|
|
443
|
+
// call service with embedded parameters:
|
|
444
|
+
client.getDatabaseInformation({ db: dbName }).then((dbInfo) => {
|
|
445
|
+
const documentCount = dbInfo.result.doc_count;
|
|
446
|
+
const dbNameResult = dbInfo.result.db_name;
|
|
447
|
+
|
|
448
|
+
// 4. Show document count in database =========================================
|
|
449
|
+
console.log(
|
|
450
|
+
`Document count in "${dbNameResult}" database is ${documentCount}.`
|
|
451
|
+
);
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
// 5. Get "example" document out of the database by document id =====================
|
|
455
|
+
const getDocParams: CloudantV1.GetDocumentParams = {
|
|
456
|
+
db: dbName,
|
|
457
|
+
docId: 'example',
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
// call service with predefined parameters:
|
|
461
|
+
client.getDocument(getDocParams).then((documentExample) => {
|
|
462
|
+
// result object is defined as a Document here:
|
|
463
|
+
const { result } = documentExample;
|
|
464
|
+
console.log(
|
|
465
|
+
`Document retrieved from database:\n${JSON.stringify(result, null, 2)}`
|
|
466
|
+
);
|
|
467
|
+
});
|
|
468
|
+
```
|
|
469
|
+
</details>
|
|
470
|
+
|
|
471
|
+
<details>
|
|
472
|
+
<summary>JavaScript:</summary>
|
|
473
|
+
|
|
474
|
+
```js
|
|
475
|
+
const { CloudantV1 } = require('@ibm-cloud/cloudant');
|
|
476
|
+
```
|
|
477
|
+
[embedmd]:# (test/examples/src/js/GetInfoFromExistingDatabase.js /const getInfoFromExistingDatabase/ /getInfoFromExistingDatabase\(\);\n\}/)
|
|
478
|
+
```js
|
|
479
|
+
const getInfoFromExistingDatabase = async () => {
|
|
480
|
+
// 1. Create a client with `CLOUDANT` default service name ===================
|
|
481
|
+
const client = CloudantV1.newInstance({});
|
|
482
|
+
|
|
483
|
+
// 2. Get server information ==================================================
|
|
484
|
+
// call service without parameters:
|
|
485
|
+
const { version } = (await client.getServerInformation()).result;
|
|
486
|
+
console.log(`Server version ${version}`);
|
|
487
|
+
|
|
488
|
+
// 3. Get database information for "orders" =================================
|
|
489
|
+
const dbName = 'orders';
|
|
490
|
+
|
|
491
|
+
// call service with embedded parameters:
|
|
492
|
+
const dbInfo = await client.getDatabaseInformation({ db: dbName });
|
|
493
|
+
const documentCount = dbInfo.result.doc_count;
|
|
494
|
+
const dbNameResult = dbInfo.result.db_name;
|
|
495
|
+
|
|
496
|
+
// 4. Show document count in database =========================================
|
|
497
|
+
console.log(
|
|
498
|
+
`Document count in "${dbNameResult}" database is ${documentCount}.`
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
// 5. Get "example" document out of the database by document id ===================
|
|
502
|
+
const getDocParams = { db: dbName, docId: 'example' };
|
|
503
|
+
|
|
504
|
+
// call service with predefined parameters:
|
|
505
|
+
const documentExample = await client.getDocument(getDocParams);
|
|
506
|
+
|
|
507
|
+
// result object is defined as a Document here:
|
|
508
|
+
const { result } = documentExample;
|
|
509
|
+
|
|
510
|
+
console.log(
|
|
511
|
+
`Document retrieved from database:\n${JSON.stringify(result, null, 2)}`
|
|
512
|
+
);
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
if (require.main === module) {
|
|
516
|
+
getInfoFromExistingDatabase();
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
</details>
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
</details>
|
|
524
|
+
When you run the code, you see a result similar to the following output.
|
|
525
|
+
|
|
526
|
+
[embedmd]:# (test/examples/output/GetInfoFromExistingDatabase.txt)
|
|
527
|
+
```txt
|
|
528
|
+
Server version 2.1.1
|
|
529
|
+
Document count in "orders" database is 1.
|
|
530
|
+
Document retrieved from database:
|
|
531
|
+
{
|
|
532
|
+
"_id": "example",
|
|
533
|
+
"_rev": "1-1b403633540686aa32d013fda9041a5d",
|
|
534
|
+
"name": "Bob Smith",
|
|
535
|
+
"joined": "2019-01-24T10:42:59.000Z"
|
|
536
|
+
}
|
|
537
|
+
```
|
|
538
|
+
|
|
551
539
|
#### 3. Update your previously created document
|
|
552
540
|
|
|
553
541
|
**Note**: This example code assumes that you have created both the `orders`
|
|
554
542
|
database and the `example` document by
|
|
555
|
-
[running the previous example code](#
|
|
543
|
+
[running the previous example code](#1-create-a-database-and-add-a-document)
|
|
556
544
|
successfully. Otherwise, the following error message occurs, "Cannot update document because either 'orders'
|
|
557
545
|
database or 'example' document was not found."
|
|
558
546
|
|
|
@@ -771,7 +759,7 @@ You have updated the document:
|
|
|
771
759
|
|
|
772
760
|
**Note**: This example code assumes that you have created both the `orders`
|
|
773
761
|
database and the `example` document by
|
|
774
|
-
[running the previous example code](#
|
|
762
|
+
[running the previous example code](#1-create-a-database-and-add-a-document)
|
|
775
763
|
successfully. Otherwise, the following error message occurs, "Cannot delete document because either 'orders'
|
|
776
764
|
database or 'example' document was not found."
|
|
777
765
|
|