{"action":"created","comment":{"url":"https://api.github.com/repos/basho/basho_docs/pulls/comments/27812762","id":27812762,"diff_hunk":"@@ -0,0 +1,176 @@\n+---\n+title: \"Taste of Riak: NodeJS\"\n+project: riak\n+version: 1.4.0+\n+document: guide\n+toc: true\n+audience: beginner\n+keywords: [developers, client, javascript, nodejs]\n+---\n+\n+If you haven't set up a Riak Node and started it, please visit the [[Prerequisites|Taste of Riak: Prerequisites]] first.\n+\n+To try this flavor of Riak, a working installation of NodeJS 0.12 or later is required.\n+\n+Code for these examples is available [here](https://github.com/basho/riak-nodejs-client-examples/blob/master/dev/taste-of-riak/introduction.js). To run, follow these directions:\n+\n+```bash\n+git clone git://github.com/basho/riak-nodejs-client-examples\n+cd riak-nodejs-client-examples\n+npm install\n+node ./app.js\n+```\n+\n+### Client Setup\n+\n+Install [the Riak NodeJS Client](https://github.com/basho/riak-nodejs-client/wiki/Installation) through [NPM](https://www.npmjs.com/package/basho-riak-client).\n+\n+### Connecting to Riak\n+\n+Connecting to Riak with the Riak NodeJS Client requires creating a new client object.\n+\n+```javascript\n+var Riak = require('basho-riak-client');\n+var client = new Riak.Client([\n+    'riak-test:10017',\n+    'riak-test:10027',\n+    'riak-test:10037',\n+    'riak-test:10047'\n+]);\n+```\n+\n+This creates a new `Riak.Client` object which handles all the details of tracking active nodes and also provides load balancing. The `Riak.Client` object is used to send commands to Riak. When your application is completely done with Riak communications, the following method can be used to gracefully shut the client down and exit NodeJS:\n+\n+```javascript\n+function client_shutdown() {\n+    client.shutdown(function (state) {\n+        if (state === Riak.Cluster.State.SHUTDOWN) {\n+            process.exit();\n+        }\n+    });\n+}\n+```\n+\n+Let's make sure the cluster is online with a `Ping` request:\n+\n+```javascript\n+var assert = require('assert');\n+\n+client.ping(function (err, rslt) {\n+    if (err) {\n+        throw new Error(err);\n+    } else {\n+        // On success, ping returns true\n+        assert(rslt === true);\n+    }\n+});\n+```\n+\n+This is some simple code to test that a node in a Riak cluster is online - we send a simple ping message. Even if the cluster isn't present, the Riak NodeJS Client will return a response message. In the callback it is important to check that your activity was successful by checking the `err` variable.\n+\n+### Saving Objects to Riak\n+\n+Pinging a Riak cluster sounds like a lot of fun, but eventually someone is going to want us to do productive work. Let's create some data to save in Riak.\n+\n+The Riak NodeJS Client makes use of a `RiakObject` class to encapsulate Riak key/value objects. At the most basic, a `RiakObject` is responsible for identifying your object and for translating it into a format that can be easily saved to Riak.\n+\n+```javascript\n+var async = require('async');\n+\n+var people = [\n+    {\n+        emailAddress: \"bashoman@basho.com\",\n+        firstName: \"Basho\",\n+        lastName: \"Man\"\n+    },\n+    {\n+        emailAddress: \"johndoe@gmail.com\",\n+        firstName: \"John\",\n+        lastName: \"Doe\"\n+    }\n+];\n+\n+var storeFuncs = [];\n+people.forEach(function (person) {\n+    // Create functions to execute in parallel to store people\n+    storeFuncs.push(function (async_cb) {\n+        client.storeValue({\n+                bucket: 'contributors',\n+                key: person.emailAddress,\n+                value: person\n+            },\n+            function(err, rslt) {\n+                async_cb(err, rslt);\n+            }\n+        );\n+    });\n+};\n+\n+async.parallel(storeFuncs, function (err, rslts) {\n+    if (err) {\n+        throw new Error(err);\n+    }\n+});\n+```\n+\n+In this sample, we create a collection of `Person` objects and then save each `Person` to Riak. Once again, we check the response from Riak.\n+\n+### Reading from Riak\n+\n+Let's find a person!\n+\n+```javascript\n+var logger = require('winston');\n+\n+client.fetchValue({ bucket: 'contributors', key: 'bashoman@basho.com', convertToJs: true },\n+    function (err, rslt) {\n+        if (err) {\n+            throw new Error(err);\n+        } else {\n+            var riakObj = rslt.values.shift();\n+            var bashoman = riakObj.value;\n+            logger.info(\"I found %s in 'contributors'\", bashoman.emailAddress);\n+        }\n+    }\n+);\n+```\n+\n+We use `client.fetchValue` to retrieve an object from Riak. This returns an array of `RiakObject` objects which helpfully encapsulates the communication with Riak.\n+\n+After verifying that we've been able to communicate with Riak *and* that we have a successful result, we use the `value` property to get the object, which has already been converted to a javascript object due to the use of `convertToJs: true` in the options.\n+\n+### Modifying Existing Data\n+\n+Let's say that Basho Man has decided to be known as Riak Man:\n+\n+```javascript\n+bashoman.FirstName = \"Riak\";\n+riakObj.setValue(bashoman);\n+\n+client.storeValue({ value: riakObj }, function (err, rslt) {\n+    if (err) {\n+        throw new Error(err);\n+    }\n+});\n+```\n+\n+Updating an object involves modifying a `RiakObject` then using `client.storeValue` to save the existing object. \n+\n+### Deleting Data\n+\n+```javascript\n+client.deleteValue({ bucket: 'contributors', key: 'johndoe@gmail.com' }, function (err, rslt) {\n+    if (err) {\n+        throw new Error(err);\n+    }\n+};\n+```\n+\n+Just like other operations, we check the results that have come back from Riak to make sure the object was successfully deleted. Of course, if you don't care about that, you can just ignore the result.\n+\n+The Riak NodeJS Client has a lot of additional functionality that makes it easy to build rich, complex applications with Riak. Check out the [documentation](https://github.com/basho/riak-dotnet-client/wiki) to learn more about working with the Riak NodeJS Client and Riak.\n+\n+## Next Steps\n+\n+More complex use cases can be composed from these initial create, read, update, and delete (CRUD) operations. [[In the next chapter|Taste of Riak: Querying]], we will look at how to store and query more complicated and interconnected data, such as documents.","path":"source/languages/en/riak/dev/taste-of-riak/nodejs.md","position":175,"original_position":175,"commit_id":"aff5658a96fa38e5aa4ae0db2caea22257e31c8d","original_commit_id":"aff5658a96fa38e5aa4ae0db2caea22257e31c8d","user":{"login":"TheOnlyRew","id":361216,"avatar_url":"https://avatars.githubusercontent.com/u/361216?v=3","gravatar_id":"","url":"https://api.github.com/users/TheOnlyRew","html_url":"https://github.com/TheOnlyRew","followers_url":"https://api.github.com/users/TheOnlyRew/followers","following_url":"https://api.github.com/users/TheOnlyRew/following{/other_user}","gists_url":"https://api.github.com/users/TheOnlyRew/gists{/gist_id}","starred_url":"https://api.github.com/users/TheOnlyRew/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheOnlyRew/subscriptions","organizations_url":"https://api.github.com/users/TheOnlyRew/orgs","repos_url":"https://api.github.com/users/TheOnlyRew/repos","events_url":"https://api.github.com/users/TheOnlyRew/events{/privacy}","received_events_url":"https://api.github.com/users/TheOnlyRew/received_events","type":"User","site_admin":false},"body":"It looks like the `title` for the linked page is `Taste of Riak: Querying with NodeJS`. It would probably be worth testing out all the links before merging these changes.","created_at":"2015-04-06T17:30:27Z","updated_at":"2015-04-06T17:30:27Z","html_url":"https://github.com/basho/basho_docs/pull/1609#discussion_r27812762","pull_request_url":"https://api.github.com/repos/basho/basho_docs/pulls/1609","_links":{"self":{"href":"https://api.github.com/repos/basho/basho_docs/pulls/comments/27812762"},"html":{"href":"https://github.com/basho/basho_docs/pull/1609#discussion_r27812762"},"pull_request":{"href":"https://api.github.com/repos/basho/basho_docs/pulls/1609"}}},"pull_request":{"url":"https://api.github.com/repos/basho/basho_docs/pulls/1609","id":32621703,"html_url":"https://github.com/basho/basho_docs/pull/1609","diff_url":"https://github.com/basho/basho_docs/pull/1609.diff","patch_url":"https://github.com/basho/basho_docs/pull/1609.patch","issue_url":"https://api.github.com/repos/basho/basho_docs/issues/1609","number":1609,"state":"open","locked":false,"title":"Ready: Taste of Riak - NodeJS Introduction","user":{"login":"lukebakken","id":514926,"avatar_url":"https://avatars.githubusercontent.com/u/514926?v=3","gravatar_id":"","url":"https://api.github.com/users/lukebakken","html_url":"https://github.com/lukebakken","followers_url":"https://api.github.com/users/lukebakken/followers","following_url":"https://api.github.com/users/lukebakken/following{/other_user}","gists_url":"https://api.github.com/users/lukebakken/gists{/gist_id}","starred_url":"https://api.github.com/users/lukebakken/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukebakken/subscriptions","organizations_url":"https://api.github.com/users/lukebakken/orgs","repos_url":"https://api.github.com/users/lukebakken/repos","events_url":"https://api.github.com/users/lukebakken/events{/privacy}","received_events_url":"https://api.github.com/users/lukebakken/received_events","type":"User","site_admin":false},"body":"Introductory document for Taste of Riak: NodeJS.\r\n\r\nI'll be doing a PR per-document to speed up publishing these.","created_at":"2015-04-03T22:42:43Z","updated_at":"2015-04-06T17:30:27Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c95aa87784c14cff5fc225533b540dbdfe5d3dc1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/basho/basho_docs/pulls/1609/commits","review_comments_url":"https://api.github.com/repos/basho/basho_docs/pulls/1609/comments","review_comment_url":"https://api.github.com/repos/basho/basho_docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/basho/basho_docs/issues/1609/comments","statuses_url":"https://api.github.com/repos/basho/basho_docs/statuses/aff5658a96fa38e5aa4ae0db2caea22257e31c8d","head":{"label":"basho:features/lrb/nodejs","ref":"features/lrb/nodejs","sha":"aff5658a96fa38e5aa4ae0db2caea22257e31c8d","user":{"login":"basho","id":176293,"avatar_url":"https://avatars.githubusercontent.com/u/176293?v=3","gravatar_id":"","url":"https://api.github.com/users/basho","html_url":"https://github.com/basho","followers_url":"https://api.github.com/users/basho/followers","following_url":"https://api.github.com/users/basho/following{/other_user}","gists_url":"https://api.github.com/users/basho/gists{/gist_id}","starred_url":"https://api.github.com/users/basho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basho/subscriptions","organizations_url":"https://api.github.com/users/basho/orgs","repos_url":"https://api.github.com/users/basho/repos","events_url":"https://api.github.com/users/basho/events{/privacy}","received_events_url":"https://api.github.com/users/basho/received_events","type":"Organization","site_admin":false},"repo":{"id":2401217,"name":"basho_docs","full_name":"basho/basho_docs","owner":{"login":"basho","id":176293,"avatar_url":"https://avatars.githubusercontent.com/u/176293?v=3","gravatar_id":"","url":"https://api.github.com/users/basho","html_url":"https://github.com/basho","followers_url":"https://api.github.com/users/basho/followers","following_url":"https://api.github.com/users/basho/following{/other_user}","gists_url":"https://api.github.com/users/basho/gists{/gist_id}","starred_url":"https://api.github.com/users/basho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basho/subscriptions","organizations_url":"https://api.github.com/users/basho/orgs","repos_url":"https://api.github.com/users/basho/repos","events_url":"https://api.github.com/users/basho/events{/privacy}","received_events_url":"https://api.github.com/users/basho/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/basho/basho_docs","description":"Basho Products Documentation","fork":false,"url":"https://api.github.com/repos/basho/basho_docs","forks_url":"https://api.github.com/repos/basho/basho_docs/forks","keys_url":"https://api.github.com/repos/basho/basho_docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/basho/basho_docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/basho/basho_docs/teams","hooks_url":"https://api.github.com/repos/basho/basho_docs/hooks","issue_events_url":"https://api.github.com/repos/basho/basho_docs/issues/events{/number}","events_url":"https://api.github.com/repos/basho/basho_docs/events","assignees_url":"https://api.github.com/repos/basho/basho_docs/assignees{/user}","branches_url":"https://api.github.com/repos/basho/basho_docs/branches{/branch}","tags_url":"https://api.github.com/repos/basho/basho_docs/tags","blobs_url":"https://api.github.com/repos/basho/basho_docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/basho/basho_docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/basho/basho_docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/basho/basho_docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/basho/basho_docs/statuses/{sha}","languages_url":"https://api.github.com/repos/basho/basho_docs/languages","stargazers_url":"https://api.github.com/repos/basho/basho_docs/stargazers","contributors_url":"https://api.github.com/repos/basho/basho_docs/contributors","subscribers_url":"https://api.github.com/repos/basho/basho_docs/subscribers","subscription_url":"https://api.github.com/repos/basho/basho_docs/subscription","commits_url":"https://api.github.com/repos/basho/basho_docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/basho/basho_docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/basho/basho_docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/basho/basho_docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/basho/basho_docs/contents/{+path}","compare_url":"https://api.github.com/repos/basho/basho_docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/basho/basho_docs/merges","archive_url":"https://api.github.com/repos/basho/basho_docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/basho/basho_docs/downloads","issues_url":"https://api.github.com/repos/basho/basho_docs/issues{/number}","pulls_url":"https://api.github.com/repos/basho/basho_docs/pulls{/number}","milestones_url":"https://api.github.com/repos/basho/basho_docs/milestones{/number}","notifications_url":"https://api.github.com/repos/basho/basho_docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/basho/basho_docs/labels{/name}","releases_url":"https://api.github.com/repos/basho/basho_docs/releases{/id}","created_at":"2011-09-16T18:47:02Z","updated_at":"2015-04-06T17:23:35Z","pushed_at":"2015-04-06T17:23:36Z","git_url":"git://github.com/basho/basho_docs.git","ssh_url":"git@github.com:basho/basho_docs.git","clone_url":"https://github.com/basho/basho_docs.git","svn_url":"https://github.com/basho/basho_docs","homepage":"http://docs.basho.com","size":43974,"stargazers_count":74,"watchers_count":74,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":109,"mirror_url":null,"open_issues_count":68,"forks":109,"open_issues":68,"watchers":74,"default_branch":"master"}},"base":{"label":"basho:master","ref":"master","sha":"4416ce7f10120b85f38c26fca233a89e7c30036a","user":{"login":"basho","id":176293,"avatar_url":"https://avatars.githubusercontent.com/u/176293?v=3","gravatar_id":"","url":"https://api.github.com/users/basho","html_url":"https://github.com/basho","followers_url":"https://api.github.com/users/basho/followers","following_url":"https://api.github.com/users/basho/following{/other_user}","gists_url":"https://api.github.com/users/basho/gists{/gist_id}","starred_url":"https://api.github.com/users/basho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basho/subscriptions","organizations_url":"https://api.github.com/users/basho/orgs","repos_url":"https://api.github.com/users/basho/repos","events_url":"https://api.github.com/users/basho/events{/privacy}","received_events_url":"https://api.github.com/users/basho/received_events","type":"Organization","site_admin":false},"repo":{"id":2401217,"name":"basho_docs","full_name":"basho/basho_docs","owner":{"login":"basho","id":176293,"avatar_url":"https://avatars.githubusercontent.com/u/176293?v=3","gravatar_id":"","url":"https://api.github.com/users/basho","html_url":"https://github.com/basho","followers_url":"https://api.github.com/users/basho/followers","following_url":"https://api.github.com/users/basho/following{/other_user}","gists_url":"https://api.github.com/users/basho/gists{/gist_id}","starred_url":"https://api.github.com/users/basho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/basho/subscriptions","organizations_url":"https://api.github.com/users/basho/orgs","repos_url":"https://api.github.com/users/basho/repos","events_url":"https://api.github.com/users/basho/events{/privacy}","received_events_url":"https://api.github.com/users/basho/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/basho/basho_docs","description":"Basho Products Documentation","fork":false,"url":"https://api.github.com/repos/basho/basho_docs","forks_url":"https://api.github.com/repos/basho/basho_docs/forks","keys_url":"https://api.github.com/repos/basho/basho_docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/basho/basho_docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/basho/basho_docs/teams","hooks_url":"https://api.github.com/repos/basho/basho_docs/hooks","issue_events_url":"https://api.github.com/repos/basho/basho_docs/issues/events{/number}","events_url":"https://api.github.com/repos/basho/basho_docs/events","assignees_url":"https://api.github.com/repos/basho/basho_docs/assignees{/user}","branches_url":"https://api.github.com/repos/basho/basho_docs/branches{/branch}","tags_url":"https://api.github.com/repos/basho/basho_docs/tags","blobs_url":"https://api.github.com/repos/basho/basho_docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/basho/basho_docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/basho/basho_docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/basho/basho_docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/basho/basho_docs/statuses/{sha}","languages_url":"https://api.github.com/repos/basho/basho_docs/languages","stargazers_url":"https://api.github.com/repos/basho/basho_docs/stargazers","contributors_url":"https://api.github.com/repos/basho/basho_docs/contributors","subscribers_url":"https://api.github.com/repos/basho/basho_docs/subscribers","subscription_url":"https://api.github.com/repos/basho/basho_docs/subscription","commits_url":"https://api.github.com/repos/basho/basho_docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/basho/basho_docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/basho/basho_docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/basho/basho_docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/basho/basho_docs/contents/{+path}","compare_url":"https://api.github.com/repos/basho/basho_docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/basho/basho_docs/merges","archive_url":"https://api.github.com/repos/basho/basho_docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/basho/basho_docs/downloads","issues_url":"https://api.github.com/repos/basho/basho_docs/issues{/number}","pulls_url":"https://api.github.com/repos/basho/basho_docs/pulls{/number}","milestones_url":"https://api.github.com/repos/basho/basho_docs/milestones{/number}","notifications_url":"https://api.github.com/repos/basho/basho_docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/basho/basho_docs/labels{/name}","releases_url":"https://api.github.com/repos/basho/basho_docs/releases{/id}","created_at":"2011-09-16T18:47:02Z","updated_at":"2015-04-06T17:23:35Z","pushed_at":"2015-04-06T17:23:36Z","git_url":"git://github.com/basho/basho_docs.git","ssh_url":"git@github.com:basho/basho_docs.git","clone_url":"https://github.com/basho/basho_docs.git","svn_url":"https://github.com/basho/basho_docs","homepage":"http://docs.basho.com","size":43974,"stargazers_count":74,"watchers_count":74,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":109,"mirror_url":null,"open_issues_count":68,"forks":109,"open_issues":68,"watchers":74,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/basho/basho_docs/pulls/1609"},"html":{"href":"https://github.com/basho/basho_docs/pull/1609"},"issue":{"href":"https://api.github.com/repos/basho/basho_docs/issues/1609"},"comments":{"href":"https://api.github.com/repos/basho/basho_docs/issues/1609/comments"},"review_comments":{"href":"https://api.github.com/repos/basho/basho_docs/pulls/1609/comments"},"review_comment":{"href":"https://api.github.com/repos/basho/basho_docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/basho/basho_docs/pulls/1609/commits"},"statuses":{"href":"https://api.github.com/repos/basho/basho_docs/statuses/aff5658a96fa38e5aa4ae0db2caea22257e31c8d"}}}}