CKTIS / JavaScript
IDBDatabase — an IndexedDB database connection
IDBDatabase represents an open connection to an IndexedDB database. It lets you create stores during schema upgrades, start transactions, and close the connection.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const request = indexedDB.open(name, version);
// In upgradeneeded:
const store = request.result.createObjectStore(storeName, options);
// After open succeeds:
const db = request.result;
db.objectStoreNames;
const tx = db.transaction(storeNames, "readonly");
db.onversionchange = () => db.close();Obtain an IDBDatabase as request.result after indexedDB.open() succeeds. This is a Web API, not ECMAScript; Node 24 does not establish browser support. Detect indexedDB in the target environment; the cited specifications are not browser compatibility tables.
IDBDatabase.createObjectStore() works only within an active upgrade transaction, usually in upgradeneeded. IDBDatabase.objectStoreNames returns a sorted DOMStringList of names, not an array. Use .contains() before creating a store if needed.
IDBDatabase.transaction() specifies the store scope and mode: default readonly or readwrite. It cannot manually create a versionchange transaction. Queue operations while the transaction is active; do not assume it survives arbitrary await calls. Request success does not mean commit: wait for transaction complete.
In response to a request to open the database at a higher version or to delete it, IndexedDB sends the IDBDatabase.versionchange event to existing connections to the database except those whose close pending flag is set. Those connections do not receive the event even if they remain open while their existing transactions finish. The requester does not need an existing connection, and a deletion request does not open one. Handle the event using onversionchange. It is distinct from upgradeneeded. IDBDatabase.close() prevents new transactions and closes the connection after existing ones finish, without deleting data. Connections left open can block upgrades.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Using an IDBDatabase connection</title>
</head>
<body>
<script>
(() => {
const name = `cktis-connection-${Date.now()}-${Math.random()}`;
const unavailable = () => console.log('IndexedDB unavailable in this context.');
let open;
try {
// Opening a connection requires a context that permits IndexedDB.
open = indexedDB.open(name, 1);
} catch {
unavailable();
return;
}
open.onerror = unavailable;
open.onupgradeneeded = () => {
open.result.createObjectStore('notes', { keyPath: 'id' });
};
open.onsuccess = () => {
const db = open.result;
const tx = db.transaction('notes', 'readwrite');
const store = tx.objectStore('notes');
store.put({ id: 1, text: 'Hello' });
const read = store.get(1);
function cleanup() {
db.close();
indexedDB.deleteDatabase(name);
}
tx.oncomplete = () => {
console.log(read.result.text);
cleanup();
};
tx.onabort = () => {
cleanup();
console.log('Database transaction aborted.');
};
};
})();
</script>
</body>
</html>Result
Hello
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.