What Is an Object Store?

Learn about the object store in IndexedDB.

Overview

An object store is where data is stored. We can store any kind of data in the object store, including primitive values, objects, blobs, etc. It’s similar to tables in the SQL database.

A database contains multiple object stores. Each object store in the database is identified with a unique name. For example, the School database will have a separate object store for Students, Teachers, and Marks. The data that can be stored in IndexedDB must be serializableThis refers to an object that can be translated into a format suitable for transfer over a storage (e.g., in an array buffer or file format)..

Note: Object stores can only be created, modified, or deleted while updating the database version. These operations related to the object store should be handled inside the upgradeneeded event.

In the object store, the data is stored as a key-value pair, with each pair representing a record. The records are sorted by keys in ascending order. Each record must have a unique key to identify the value. The unique key can be defined in two ways:

  • keyPath: A key (property) that’s present in the object to be stored as a value. This indicates that the provided key will always be present in the value, and the value of the key will be unique. For example, in the Employee object store, every employee will have an id, which will be unique.

  • autoIncrement: A key generator will create new keys for every new record. The key is an ordered number sequence.

Note: If the autoIncrement option isn’t provided, we must pass the keyPath when storing a record.

Real-world examples

Example 1:

Let’s say we have a School database, and in this database, we have a Student object store to store student-related data like name, ID, and class.

Press + to interact
School database with student object store
School database with student object store

The illustration above demonstrates a School database with three object stores, Student, Teacher, and Hostel.

Conclusion

Object stores can be used to store data in IndexedDB. They are used in a variety of real-world applications, including web browsers and e-commerce websites.