Member-only story
IndexedDB Manipulation with Dexie — Queries and Indexes
IndexedDB is a way to store data in the browser.
It lets us store larger amounts of data than local storage in an asynchronous way.
Dexie makes working with IndexedDB easier.
In this article, we’ll take a look at how to start working with IndexedDB with Dexie.
Queries
We can retrieve objects from our table with the get
and where
methods.
get
retrieves an object by its primary key.
where
does an advanced query.
For example, we can use get
by writing:
const db = new Dexie("friend_database");
(async () => {
try {
await db.version(1).stores({
friends: '++id,name,age'
});
await db.friends.put({
name: "mary",
age: 28
})
await db.friends.put({
name: "james",
age: 22
})
const friend = await db.friends.get(1)
console.log(friend)
} catch (error) {
console.log(error);
}
})()
We call get
with the id
value of the entry to get.
Then that returns a promise with the result we want to get.
To make more complex queries, we can use the where
query.