Back to Top

Learn about Object store and Index in HTML5 IndexedDB

html5 Object Store and index

In the first article of Indexed DB series, I told you all about the basics of IndexedDB in HTML5.In this tutorial, I will continue to introduce how to use IndexedDB,Object store, Index, Transactions in IndexedDB.

Let’s start with the Object store in IndexedDB

Object store in INDEXEDDB:

Now I learn how to store data and in IndexedDB have the concept called Object store.The object will be stored in the data with a keypath and an optional set of indexes. To create a new object that is stored in the existing database, you need an existing database for version control. To do this, first open the database you want version control. In addition to the database name, Open method also accepts a version number as the second argument. If you want to create a new version of the database, to create or modify a stored object or simply open the database with existing database with the higher version.

To create an Object store, you need to call the createObjectStore method, as shown in following line of code:

Creating an object store

html5_explain
[sociallocker]
The above code, a look at onupgradeneeded event, first obtained by variable currentTarget to get the result and create an object.Here In createObjectStore function,keyPath is the optional argument to create objects. If you try to create an object that already exists in the database, then the error will be thrown out.

I have checked the object storage here and how it works. Next, let’s look at an index and how reference object store index contains the data.

Index in INDEXED DB

Furthermore, You can use keys to retrieve an object in a stored record, you can also use the indexed fields to retrieve the records on behalf of the key. Object store may have one or more indexes. The index is a special kind of object storage which refers to an object that contains the data stored in the object storage and change when reference (ie, add, modify or delete records) is automatically updated.

To create an index, you can use the method shown in above with the database version control. Indexes can be unique or it can be not unique. The only requires is that all values ​in the index are unique such as the use of an e-mail address field.

When a there is duplicate or repeated value, you need to use a non-unique index, such as city, state. The following code snippet shows how to create an object in the state field of a non-user unique index, create a non-unique index for the ZIP code field, and the email address field to create a unique index.

SYNTAX:

The first parameter is the name of the index, the second parameter is the columns. And the use of unique designate a column is unique.

So how do you use the Index in INDEXED DB? Once you get the objectStore in the transaction, you can go to get the data by an index, let’s understand with an example as follows:

Next, I will use the transaction to perform some operation on an object store.

Transaction in INDEXED DB

You need to use the transaction to perform all read and write operations on the object store in Indexed DB. The transaction is similar to a relational database.IndexedDB transaction provides database writes a collection of atoms.

To add data

IndexedDB allows you to save objects.This means that you can even save a Javascript object.The transaction requires two parameters, the first is an array of tables to be processed and the second parameter is the type of transaction.

There are two types of transaction:

  1. read-only :Provide read-only access to an object store, is used when a query object store.
  2. read-write: It provides read and write access to an object store.

The default transaction mode in Indexed DB is readonly. You can open multiple concurrent at any time with the readonly mode, but can not open multiple modes with a read-write transactions. Because of this reason, when you update the data, consider using read-write transactions. So that they can not open any other concurrent transactions. In the onupgradeneeded method, use the event handler versionchange transaction to create, modify or delete an object store, or to add an index to the object store.

To readwrite mode for the users and to create a transactional storage objects, you can use the following statement:

Above line of code pointed out the need to set up storage objects users to read and write operations and then use the stored objects with method objectStore

Next is the JavaScript function which will show how to use a transaction and using a key to retrieve the particular user record.

Example

Now, It’s time to check the real time example using IndexedDB in HTML5 which will show you how you can use in your application.

Key to get a specific record

Here is the one more JavaScript function which show how to use a transaction for email using Index instead of object store key to retrieve the specific user record.

Index to obtain specific records

Now, Let’s see the code create an example of read-write operation for new user record using transaction.

Creating a new user record

After adding a user data, now it’s time to update the data using same read-write transaction which will update an added record. Below example changes the record ID for the 53 user’s e-mail address.

Update the existing user records

However, I have updated record and in CRUD operation, next operation is but obvious delete. To delete stored objects from all records using read-write transaction.As like asynchronous transactions, like, Clear method will check whether the object store has been cleared, if yes it will call onsuccess or any error occurred, onerror callback will be called.

Clear Object Storage Services

Above examples demonstrate some common uses of the transaction. Next you will see Cursor in IndexedDB and how it works.

Conclusion

So after this article I hope you will be able to create an Object store and Index in Indexed DB. Now on next article I will go​ to cover about creating the cursor in IndexedDB.

To get notified about the next article in this series, subscribe to our RSS feed via email, so you will not miss out any article from us. [/sociallocker]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Branching and Merging in Git

Posted on 6 years ago

Bhumi

How to use Session in Joomla

Posted on 12 years ago

Bhumi