avatarJohn Au-Yeung

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2379

Abstract

the <code>where</code> query.</p><p id="a611">For instance, we can write:</p><div id="8cba"><pre>const db = new Dexie(<span class="hljs-string">"friend_database"</span>)<span class="hljs-comment">;</span> (<span class="hljs-name">async</span> () => { try { await db.version(<span class="hljs-number">1</span>).stores({ friends: '++id,name,age' })<span class="hljs-comment">;</span> await db.friends.put({ name: <span class="hljs-string">"mary"</span>, age: <span class="hljs-number">28</span> }) await db.friends.put({ name: <span class="hljs-string">"james"</span>, age: <span class="hljs-number">22</span> }) const friendCount = await db.friends.where('age').above(<span class="hljs-number">25</span>).count() console.log(<span class="hljs-name">friendCount</span>) } catch (<span class="hljs-name">error</span>) { console.log(<span class="hljs-name">error</span>)<span class="hljs-comment">;</span> } })()</pre></div><p id="93ed">We add 2 entries to the <code>friends</code> table.</p><p id="e286">Then we call <code>where</code> with the column to search for.</p><p id="a7c0"><code>above</code> searches for anything with the value above a given value.</p><p id="0863"><code>count</code> returns the count of the results.</p><p id="1dee">We can call more methods to make more advanced queries:</p><div id="ab9d"><pre>const db = new Dexie(<span class="hljs-string">"friend_database"</span>)<span class="hljs-comment">;</span> (<span class="hljs-name">async</span> () => { try { await db.version(<span class="hljs-number">1</span>).stores({ friends: '++id,name,age' })<span class="hljs-comment">;</span> await db.friends.put({ name: <span class="hljs-string">"mary"</span>, age: <span class="hljs-number">28</span>, isCloseFriend: true }) await db.friends.put({ name: <span class="hljs-string">"james"</span>, age: <span class="hljs-number">22</span> }) const friendCount = await db.friends .where('age') .between(<span class="hljs-number">37</span>, <span class="hljs-number">40</span>) .or('name') .anyOf(['mary', 'james']) .and((<span class="hljs-name">friend</span>) => { return friend.isCloseFriend<span class="hljs-comment">;</span> }) .limit(<span class="hljs-number">10</

Options

span>) .each((<span class="hljs-name">friend</span>) => { console.log(<span class="hljs-name">friend</span>)<span class="hljs-comment">;</span> })<span class="hljs-comment">;</span> } catch (<span class="hljs-name">error</span>) { console.log(<span class="hljs-name">error</span>)<span class="hljs-comment">;</span> } })()</pre></div><p id="c0d2"><code>or</code> lets us combine one or more conditions with an OR operator.</p><p id="b961"><code>anyOf</code> searches for any of the values in the array.</p><p id="c867"><code>and</code> combines one or more conditions with an AND operator.</p><p id="bb2a"><code>limit</code> limits the number of items returned.</p><p id="7aca"><code>each</code> lets us iterate through each result.</p><h1 id="cc5f">Detailed Schema Syntax</h1><p id="3971">We can define a schema with Dexie’s schema syntax.</p><p id="1aeb">Parts of the syntax include:</p><ul><li><code>++keyPath</code> — autoincrement primary key.</li><li><code>++</code> — hidden autoincrement primary key.</li><li><code>keyPath</code> — non-autoincrement primary key.</li><li><code>(blank)</code> — hidden primary key.</li><li><code>keyPath</code> — the <code>keyPath</code> is indexed</li><li><code>&keyPath</code><code>keyPath</code> is indexed and the keys must be unique.</li><li><code>*keyPath</code> — the key is an array and eah array value is regarded as a key to the object</li><li><code>[keyPath1+keyPath2]</code> — compound index for <code>keyPath1</code> and <code>keyPath2</code> .</li></ul><p id="cde0">We can use them to add indexes by writing:</p><div id="1252"><pre>(<span class="hljs-name">async</span> () => { const db = new Dexie(<span class="hljs-symbol">'MyDatabase</span>')<span class="hljs-comment">;</span> db.version(<span class="hljs-name">1</span>).stores({ friends: <span class="hljs-symbol">'++id</span>,name,age', pets: <span class="hljs-symbol">'id</span>, name, kind', cars: <span class="hljs-symbol">'++</span>, name', enemies: ',name,*weaknesses', users: <span class="hljs-symbol">'meta.ssn</span>, addr.city', people: '[<span class="hljs-name">name+id</span>], &id' })<span class="hljs-comment">;</span> })()</pre></div><h1 id="fa14">Conclusion</h1><p id="8c85">We can make queries and create indexes easily in our IndexedDB database with Dexie.</p></article></body>

IndexedDB Manipulation with Dexie — Queries and Indexes

Photo by Gilly Stewart on Unsplash

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.

For instance, we can write:

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 friendCount = await db.friends.where('age').above(25).count()
    console.log(friendCount)
  } catch (error) {
    console.log(error);
  }
})()

We add 2 entries to the friends table.

Then we call where with the column to search for.

above searches for anything with the value above a given value.

count returns the count of the results.

We can call more methods to make more advanced queries:

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,
      isCloseFriend: true
    })
    await db.friends.put({
      name: "james",
      age: 22
    })
    const friendCount = await db.friends
      .where('age')
      .between(37, 40)
      .or('name')
      .anyOf(['mary', 'james'])
      .and((friend) => {
        return friend.isCloseFriend;
      })
      .limit(10)
      .each((friend) => {
        console.log(friend);
      });
  } catch (error) {
    console.log(error);
  }
})()

or lets us combine one or more conditions with an OR operator.

anyOf searches for any of the values in the array.

and combines one or more conditions with an AND operator.

limit limits the number of items returned.

each lets us iterate through each result.

Detailed Schema Syntax

We can define a schema with Dexie’s schema syntax.

Parts of the syntax include:

  • ++keyPath — autoincrement primary key.
  • ++ — hidden autoincrement primary key.
  • keyPath — non-autoincrement primary key.
  • (blank) — hidden primary key.
  • keyPath — the keyPath is indexed
  • &keyPathkeyPath is indexed and the keys must be unique.
  • *keyPath — the key is an array and eah array value is regarded as a key to the object
  • [keyPath1+keyPath2] — compound index for keyPath1 and keyPath2 .

We can use them to add indexes by writing:

(async () => {
  const db = new Dexie('MyDatabase');
  db.version(1).stores({
    friends: '++id,name,age',
    pets: 'id, name, kind',
    cars: '++, name',
    enemies: ',name,*weaknesses',
    users: 'meta.ssn, addr.city',
    people: '[name+id], &id'
  });
})()

Conclusion

We can make queries and create indexes easily in our IndexedDB database with Dexie.

Programming
Web Development
Technology
Software Development
JavaScript
Recommended from ReadMedium