| copyright |
|
||
|---|---|---|---|
| lastupdated | 2018-08-20 |
{:new_window: target="_blank"} {:shortdesc: .shortdesc} {:screen: .screen} {:codeblock: .codeblock} {:pre: .pre} {:tip: .tip}
{: #cloudant}
{{site.data.keyword.cloudantfull}} is a document-oriented Database as a Service (DBaaS). It stores data as documents in JSON format. {{site.data.keyword.cloudant_short_notm}} is built with scalability, high availability, and durability in mind, and is easy to configure for use in Node.js applications. It comes with a wide variety of indexing options that includes MapReduce, {{site.data.keyword.cloudant_short_notm}} Query, full-text indexing, and geospatial indexing. The replication capabilities make it easy to keep data in sync between database clusters, desktop PCs, and mobile devices. {:shortdesc}
For more information, see {{site.data.keyword.cloudant_short_notm}} Basics{:new_window}.
{: #before}
Be sure that you have the following prerequisites ready to go:
- Nodejs-cloudant
2.3.0+ client library.
- To access {{site.data.keyword.cloudant_short_notm}}, you must have either an {{site.data.keyword.cloudant_short_notm}} account
or an {{site.data.keyword.cloud}} account
.
- The code snippets in these instructions use IAM authentication.
{: #enable_IAM}
Only new {{site.data.keyword.cloudant_short_notm}} service instances can be used with {{site.data.keyword.cloud_notm}} IAM.
All new {{site.data.keyword.cloudant_short_notm}} service instances are enabled to use {{site.data.keyword.cloud_notm}} Identity and Access Management (IAM) when provisioned. When you provision a new instance from the {{site.data.keyword.cloud_notm}} catalog, choose the Use only IAM authentication method. This mode means that only IAM credentials are provided by service binding and credential generation. You can find more information at {{site.data.keyword.cloud_notm}} Identity and Access Management (IAM).
{: #create-instance}
When you create an instance of {{site.data.keyword.cloudant_short_notm}}, you also create the database.
- Log in to your {{site.data.keyword.cloud}} account.
- From the {{site.data.keyword.cloud}} Dashboard, click Create resource. The {{site.data.keyword.cloud_notm}} catalog opens.
- In the {{site.data.keyword.cloud_notm}} catalog, select the Databases category, and then click {{site.data.keyword.cloudant_short_notm}}. The service configuration page opens.
- Complete the information in the following fields:
- Service name - Either type a name for your service instance, or use the preset name.
- Choose a region/location to deploy in - Select a region in which to deploy your service.
- Select a resource group - Select a resource group, or accept the default.
- Available authentication methods - Select Use only IAM for the authentication method.
- Select your pricing plan, and then click Create. The page for your service instance opens.
- To create a service credential, complete these steps:
- From the navigation menu, select Service credentials.
- Click New credential. The Add new credential page opens.
- In the Add new credential page, complete the fields, and then click Add. The new service credential is added to the service instance.
- If you want to view the service credential details, click View credentials in the Actions column of the new credential.
- From the navigation menu, select Manage, and then click Launch Cloudant Dashboard. The Monitoring page opens.
- From the navigation menu, click the Databases icon.
- Click Create Database, provide a database name, and then click Create. Your database page opens.
If you want to see related information about provisioning an instance of the {{site.data.keyword.cloud_notm}} service, see Creating an IBM Cloudant instance on IBM Cloud tutorial .
{: #install}
Begin with your own Node.js project, and define this work as your dependency. In other words, put {{site.data.keyword.cloudant_short_notm}} in your package.json dependencies. Use the npm package manager from the command line:
npm install --save @cloudant/cloudant
{: pre}
Notice that your package.json now shows this package.
{: #initialize}
After you initialize the SDK in your app, you can use {{site.data.keyword.cloudant_short_notm}} to store data. To initialize your connection, enter your credentials and provide a callback function to run when everything is ready.
- Load the client library by adding the following
requiredefinition to yourserver.jsfile.
var Cloudant = require('@cloudant/cloudant');{: codeblock}
- Initialize the client library by supplying your credentials. Use the
iamauthplug-in to create a database client with an IAM API key.
var cloudant = new Cloudant({ url: 'https://examples.cloudant.com', plugins: { iamauth: { iamApiKey: 'xxxxxxxxxx' } } });{: codeblock}
- List the databases by adding the following code to your
server.jsfile.
cloudant.db.list(function(err, body) {
body.forEach(function(db) {
console.log(db);
});
});{: codeblock}
Uppercase Cloudant is the package that you load by using require(). Lowercase cloudant is the authenticated connection to your {{site.data.keyword.cloudant_short_notm}} service.
{: tip}
{: #basic_operations}
These basic operations illustrate the actions to create, read, update, and delete your documents by using the initialized client.
var createDocument = function(callback) {
console.log("Creating document 'mydoc'");
// specify the id of the document so you can update and delete it later
db.insert({ _id: 'mydoc', a: 1, b: 'two' }, function(err, data) {
console.log('Error:', err);
console.log('Data:', data);
callback(err, data);
});
};{: codeblock}
var readDocument = function(callback) {
console.log("Reading document 'mydoc'");
db.get('mydoc', function(err, data) {
console.log('Error:', err);
console.log('Data:', data);
// keep a copy of the doc so you know its revision token
doc = data;
callback(err, data);
});
};{: codeblock}
var updateDocument = function(callback) {
console.log("Updating document 'mydoc'");
// make a change to the document, using the copy we kept from reading it back
doc.c = true;
db.insert(doc, function(err, data) {
console.log('Error:', err);
console.log('Data:', data);
// keep the revision of the update so we can delete it
doc._rev = data.rev;
callback(err, data);
});
};{: codeblock}
var deleteDocument = function(callback) {
console.log("Deleting document 'mydoc'");
// supply the id and revision to be deleted
db.destroy(doc._id, doc._rev, function(err, data) {
console.log('Error:', err);
console.log('Data:', data);
callback(err, data);
});
};{: codeblock}
{: #test}
Is everything set up correctly? Test it out!
- Run your application, making sure to start the initialization and respective operations, such as creating a document.
- Return to the {{site.data.keyword.cloudant_short_notm}} service instance that you previously created in your web browser, and open the service dashboard.
- Select the database that is used to see your newly created documents in the dashboard.
Having trouble? Check out the {{site.data.keyword.cloudant_short_notm}} API Reference{:new_window}.
{: #next notoc}
Great job! You added a level of secure persistence to your app. Keep the momentum by trying one of the following options:
- View the {{site.data.keyword.cloudant_short_notm}} SDK for Node.js
source code.
- Check out the example code for database and document operations
- Starter Kits are one of the fastest ways to use the capabilities of {{site.data.keyword.cloud}}. View the available starter kits in the Mobile developer dashboard
. Download the code. Run the app!
- To learn more about and take advantage of all of the features that {{site.data.keyword.cloudant_short_notm}} offers, check out the docs!