Skip to content

Using getAll() Functions

While you can retrieve objects one by one using get() or iterate with a cursor, a very common requirement is to fetch multiple records at once. The getAll() method is the modern, efficient way to retrieve all objects from a store or index that match a specific query.

const results = await objectStore.getAll([query], [count]);
const indexResults = await index.getAll([query], [count]);

Both objectStore and index objects support getAll(). The parameters are optional:

  • query: A key or an IDBKeyRange to filter the results. If omitted, all objects from the store/index are returned.
  • count: An integer specifying the maximum number of objects to retrieve.

The getAll() method is convenient but be mindful of its memory usage. It loads all matching objects into an array in memory. For extremely large datasets (e.g., hundreds of thousands of records), this could strain the browser. In such scenarios, using a cursor to process records one by one is a more memory-efficient approach.

Example: Retrieving All and a Subset of Data

Section titled “Example: Retrieving All and a Subset of Data”

In the following example, we create a ‘tasks’ database. We first use getAll() to retrieve every task, and then use it again with a query to retrieve only tasks from a specific project.

<!DOCTYPE html>
<html lang="en">
<head>
<title>IndexedDB getAll() Example</title>
</head>
<body>
<h1>Check the console for output</h1>
<script type="module">
import { openDB } from 'https://cdn.jsdelivr.net/npm/idb@7/build/index.js';
async function runDemo() {
const db = await openDB('taskManagerDB', 1, {
upgrade(db) {
const store = db.createObjectStore('tasks', { keyPath: 'id', autoIncrement: true });
store.createIndex('projectIdx', 'project');
},
});
const tx = db.transaction('tasks', 'readwrite');
await Promise.all([
tx.store.add({ text: 'Design UI mockups', project: 'Website' }),
tx.store.add({ text: 'Develop API endpoints', project: 'Backend' }),
tx.store.add({ text: 'Implement frontend logic', project: 'Website' }),
tx.store.add({ text: 'Write documentation', project: 'Backend' }),
]);
await tx.done;
// 1. Get ALL tasks from the object store
const allTasks = await db.getAll('tasks');
console.log('--- All Tasks ---');
console.table(allTasks);
// 2. Get all tasks for the 'Website' project using the index
const websiteTasks = await db.getAllFromIndex('tasks', 'projectIdx', 'Website');
console.log("--- 'Website' Project Tasks ---");
console.table(websiteTasks);
}
runDemo().catch(console.error);
</script>
</body>
</html>
--- All Tasks ---
(console.table of 4 tasks)
--- 'Website' Project Tasks ---
(console.table of 2 tasks: 'Design UI mockups' and 'Implement frontend logic')