My name is Manish Sharma I am a manager in think way company I do one other work. My name is Manish Sharma I am a manager in think way company I do one other work.
To create and open a database in IndexedDB, you use the indexedDB.open() method. This is
asynchronous, and you interact with it via event handlers like
onsuccess, onerror, and onupgradeneeded.
Basic Steps to Create and Open an IndexedDB Database
version: A number (e.g., 1) — used to trigger schema upgrades (like adding object stores)
Example: Create and Open a DB
const request = indexedDB.open("MyAppDB", 1);
request.onupgradeneeded = function(event) {
const db = event.target.result;
// Create an object store named "users" with "id" as the keyPath
const store = db.createObjectStore("users", {
keyPath: "id",
autoIncrement: true
});
// Optional: create an index on "email"
store.createIndex("emailIndex", "email", { unique: true });
console.log("Object store and index created.");
};
request.onsuccess = function(event) {
const db = event.target.result;
console.log("Database opened successfully", db);
// Now you can use `db.transaction(...)` to read/write
};
request.onerror = function(event) {
console.error("Database error:", event.target.error);
};
Summary of Events
Event
Purpose
onupgradeneeded
Called when DB is created or upgraded (new version)
onsuccess
Called when DB is successfully opened
onerror
Called if opening DB fails
Tips
Use onupgradeneeded to:
Create object stores (db.createObjectStore(...))
Define indexes
Migrate data between versions
If the DB already exists with the same version, onupgradeneeded is
not triggered.
You must perform all schema setup in onupgradeneeded.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To create and open a database in IndexedDB, you use the
indexedDB.open()method. This is asynchronous, and you interact with it via event handlers likeonsuccess,onerror, andonupgradeneeded.Basic Steps to Create and Open an IndexedDB Database
Syntax
databaseName: A string, like"MyAppDB"version: A number (e.g.,1) — used to trigger schema upgrades (like adding object stores)Example: Create and Open a DB
Summary of Events
onupgradeneededonsuccessonerrorTips
onupgradeneededto:db.createObjectStore(...))onupgradeneededis not triggered.onupgradeneeded.