{"id":756,"date":"2014-11-26T00:00:24","date_gmt":"2014-11-25T18:30:24","guid":{"rendered":"http:\/\/codeforgeek.com\/?p=756"},"modified":"2021-06-20T06:05:58","modified_gmt":"2021-06-20T00:35:58","slug":"indexeddb-tutorial","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/indexeddb-tutorial\/","title":{"rendered":"IndexedDB : Database in browser"},"content":{"rendered":"<p>IndexedDB is a JavaScript-based object-oriented database. It provides key -value data management within browser. IndexedDB Api\u00a0wraps the SQL like features to perform CRUD operation.<\/p>\n<p>In this tutorial i am going to explain basics about <strong>IndexedDB<\/strong> Api with sample code.<\/p>\n<h2>Sample code to get started :<\/h2>\n<p>Look over the code below, and observe the output which it generates.<br \/>\n<code lang=\"javascript\"><br \/>\nvar request = indexedDB.open(\"synker\");<br \/>\nvar db;<br \/>\nrequest.onupgradeneeded = function() {<br \/>\n\/\/ The database did not previously exist, so create object stores and indexes.<br \/>\ndb = request.result;<br \/>\nvar store = db.createObjectStore(\"notes\", {keyPath: \"ID\"});<br \/>\nvar ourindex = store.createIndex(\"content\",\"user_content\");<br \/>\n};<\/p>\n<p>request.onsuccess = function() {<br \/>\ndb = request.result;<br \/>\n};<br \/>\n<\/code><\/p>\n<p>For testing purpose, open up the chrome developer console and paste the code in &#8220;console&#8221; section. It will execute and generate the Index DB in your browser. See screenshot below for reference.<br \/>\n<figure id=\"attachment_881\" aria-describedby=\"caption-attachment-881\" style=\"width: 805px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152114.png\"><img decoding=\"async\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152114.png\" alt=\"Execute code from Developer console.\" width=\"805\" height=\"389\" class=\"size-full wp-image-881\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152114.png 805w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152114-768x371.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152114-300x145.png 300w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/a><figcaption id=\"caption-attachment-881\" class=\"wp-caption-text\">Execute code from Developer console.<\/figcaption><\/figure><br \/>\nOnce you executed the code, go to <strong>Resources<\/strong> tab then <strong>IndexedDB<\/strong> and see the name <strong>&#8220;synker&#8221;<\/strong> in it.<br \/>\n<figure id=\"attachment_882\" aria-describedby=\"caption-attachment-882\" style=\"width: 805px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152910.png\"><img decoding=\"async\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152910.png\" alt=\"IndexedDB created.\" width=\"805\" height=\"389\" class=\"size-full wp-image-882\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152910.png 805w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152910-768x371.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-152910-300x145.png 300w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/a><figcaption id=\"caption-attachment-882\" class=\"wp-caption-text\">IndexedDB created.<\/figcaption><\/figure><br \/>\nThis was just to get you started !<\/p>\n<h3>Explanation :<\/h3>\n<p>Above code creates Index database name as <strong>&#8220;synker&#8221;<\/strong>. In index database, you must create <strong>objectStore<\/strong> to store your information. We create one for us named <strong>&#8220;notes&#8221;<\/strong>. Now inside <strong>&#8220;notes&#8221;<\/strong> we can create our index and which in turn store our information.<\/p>\n<h3>Insert operation in indexeDB:<\/h3>\n<p>To put some information in our index database you need to create <strong>&#8220;transaction&#8221;<\/strong> object which will points to your <strong>objectStore<\/strong>. Here is sample code.<br \/>\n<code lang=\"javascript\"><br \/>\nfunction addData(data){<\/p>\n<p>var tx      =   db.transaction(\"notes\", \"readwrite\");<br \/>\nvar store   =   tx.objectStore(\"notes\");<\/p>\n<p>store.put({content: data, ID:1});<br \/>\n}<br \/>\n<\/code><br \/>\n<strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-163251.png\"><img decoding=\"async\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-163251.png\" alt=\"Screenshot from 2014-11-25 16:32:51\" width=\"805\" height=\"324\" class=\"alignnone size-full wp-image-886\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-163251.png 805w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-163251-768x309.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-163251-300x121.png 300w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/a><\/p>\n<h3>Select operation in IndexedDB: <\/h3>\n<p>To perform SELECT operation you need to create transaction object which points to your ObjectStore. Then you have to assign a <strong>&#8220;cursor&#8221;<\/strong> which will loop through the information in your ObjectStore, you just have to write a logic to store it in any data type. Here is sample code.<br \/>\n<code lang=\"javascript\"><br \/>\nfunction getalldata()<br \/>\n{<br \/>\n                  var all_content;<br \/>\n                  var self=this;<br \/>\n                  var tx = db.transaction(\"notes\", \"readonly\");<br \/>\n                  var store = tx.objectStore(\"notes\");<\/p>\n<p>                  var request = store.openCursor();<br \/>\n                  request.onsuccess = function() {<br \/>\n                    var cursor = request.result;<br \/>\n                    if (cursor) {<br \/>\n                      self.all_content=cursor.value.content;<br \/>\n                      cursor.continue();<br \/>\n                    }<br \/>\n                  };<br \/>\n              return self.all_content;<br \/>\n}<br \/>\n<\/code><br \/>\n<strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-164218-e1416914011160.png\"><img decoding=\"async\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-164218-e1416914011160.png\" alt=\"Screenshot from 2014-11-25 16:42:18\" width=\"805\" height=\"239\" class=\"alignnone size-full wp-image-887\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-164218-e1416914011160.png 805w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-164218-e1416914011160-768x228.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/Screenshot-from-2014-11-25-164218-e1416914011160-300x89.png 300w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/a><\/p>\n<h3>Delete operation in IndexedDB: <\/h3>\n<p>You can delete particular index or some part of data which matches using <strong>deleteindex<\/strong> and <strong>delete<\/strong> methods. <\/p>\n<h3>Update operation in IndexedDB:<\/h3>\n<p>To update the information in particular you can use same PUT function which we have used to insert data, if that data exists already then it will update it else add it as new value. <\/p>\n<h3>Conclusion:<\/h3>\n<p>IndexedDB is very useful for developing structure web application where we need to store information on client end. You can also assign auto generated key for managing index as well as do almost every simple transnational operation using IndexedDB.<\/p>\n<h3>Further reading:<\/h3>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/IDBObjectStore\" title=\"MDN API\" target=\"_blank\" rel=\"nofollow noopener\">Official documentation on MDN<\/a><\/li>\n<li><a href=\"https:\/\/softwareengineering.stackexchange.com\/questions\/219953\/how-is-localstorage-different-from-indexeddb\" title=\"How its different\" target=\"_blank\" rel=\"nofollow noopener\">How indexedDB and localstorage is different?<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>IndexedDB is a JavaScript-based object-oriented database. It provides key -value data management within browser. IndexedDB Api\u00a0wraps the SQL like features to perform CRUD operation. In this tutorial i am going to explain basics about IndexedDB Api with sample code. Sample code to get started : Look over the code below, and observe the output which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":893,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[9,18],"tags":[],"class_list":["post-756","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-js","category-tutorial"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner5.png",800,212,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner5-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner5-300x80.png",300,80,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner5-768x204.png",768,204,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner5.png",800,212,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner5.png",800,212,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/11\/banner5.png",800,212,false]},"uagb_author_info":{"display_name":"Shahid","author_link":"https:\/\/codeforgeek.com\/author\/shahid\/"},"uagb_comment_info":0,"uagb_excerpt":"IndexedDB is a JavaScript-based object-oriented database. It provides key -value data management within browser. IndexedDB Api\u00a0wraps the SQL like features to perform CRUD operation. In this tutorial i am going to explain basics about IndexedDB Api with sample code. Sample code to get started : Look over the code below, and observe the output which&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=756"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/756\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/893"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}