Categories
JavaScript APIs

IndexedDB Manipulation with Dexie — Result Count and Iteration

Spread the love

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.

Collection Count

We can get the number of results from a collection with the count method.

For example, we can write:

const db = new Dexie("friends");

db.version(1).stores({
  friends: "id, name, age"
});

(async () => {
  await db.friends.put({
    id: 1,
    name: "jane",
    age: 78
  });
  await db.friends.put({
    id: 2,
    name: "mary",
    age: 76
  });

  const count = await db.friends
    .where("name")
    .equalsIgnoreCase("jane")
    .count()
  console.log(count)
})()

to get the query results and then call count on it to get the count of the results.

Delete Items

We can call the delete all objects in the query with the delete method.

For instance, we can write:

const db = new Dexie("friends");

db.version(1).stores({
  friends: "id, name, age"
});

(async () => {
  await db.friends.put({
    id: 1,
    name: "jane",
    age: 78
  });
  await db.friends.put({
    id: 2,
    name: "mary",
    age: 76
  });

  await db.friends
    .where("name")
    .equalsIgnoreCase("jane")
    .delete()
})()

We just call delete on the collection to delete all the items in the collection.

Sort Descending

We can sort items in descending order with the desc method.

For example, we can write:

const db = new Dexie("friends");

db.version(1).stores({
  friends: "id, name, age"
});

(async () => {
  await db.friends.put({
    id: 1,
    name: "jane",
    age: 78
  });
  await db.friends.put({
    id: 2,
    name: "mary",
    age: 76
  });

  const someFriends = await db.friends
    .where('age')
    .above(25)
    .desc()
    .toArray()
  console.log(someFriends)
})()

to sort the items in descending order.

Each

We can loop through any items returned in a collection with the each method.

For example, we can write:

const db = new Dexie("friends");

db.version(1).stores({
  friends: "id, name, age"
});

(async () => {
  await db.friends.put({
    id: 1,
    name: "jane",
    age: 78
  });
  await db.friends.put({
    id: 2,
    name: "jane",
    age: 76
  });

  await db.friends.each(friend => console.log(friend.name));
})()

to call each to loop through each item in the friends table.

Loop Through Keys in Collections

We can loop through each key returned in the collection with the eachKey method.

For instance, we can write:

const db = new Dexie("friends");

db.version(1).stores({
  friends: "id, name, age"
});

(async () => {
  await db.friends.put({
    id: 1,
    name: "jane",
    age: 78
  });
  await db.friends.put({
    id: 2,
    name: "jane",
    age: 76
  });

  await db.friends
    .where('age')
    .above(25)
    .eachKey(key => console.log(key));
})()

We make the query that queries the age field.

Then eachKey will have the age value for each result.

Loop Through Each Primary Key

We can loop through each primary key in a collection with the eachPrimaryKey method.

For example, we can write:

const db = new Dexie("friends");

db.version(1).stores({
  friends: "id, name, age"
});

(async () => {
  await db.friends.put({
    id: 1,
    name: "jane",
    age: 78
  });
  await db.friends.put({
    id: 2,
    name: "jane",
    age: 76
  });

  await db.friends
    .orderBy('name')
    .eachPrimaryKey(id => console.log(id));
})()

We call eachPrimaryKey to log the value of the id field in our collection.

Conclusion

We can work with collection with various methods provided by Dexie.

Leave a Reply

Your email address will not be published. Required fields are marked *