Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 429

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T10:48:07+00:00 2024-11-25T10:48:07+00:00

Svelte Stores x Dexie 2.0

  • 62k

My first post was about connecting a svelte store to the indexedDB. This one will be an update on the same topic. Keep reading if you want a completely different approach that is more practial in most cases!

What I want to achieve

I want to save data in the indexedDB and use that data in my app.

The problem

As the idexedDB works asynchronously, most of the time the page will be loaded before the data, which results in an ugly flicker when the elements are updated due to svelte's reactive nature. So, the data should be loaded from a store rather than the indexedDB itself whenever possible. Obviously, it has to be loaded once on the initial page load — but with an SPA that is it!

The solution

A svelte store with a special function that gets the data from the indexedDB. That is basically it but there are some important subtleties that I will explain in a minute.

Install Dexie

In order to use this, you need to install Dexie.js:

npm install dexie 
Enter fullscreen mode Exit fullscreen mode

Create db.js

This is the very minimalistic set-up for your indexedDB that sits in your lib folder:

import Dexie from 'dexie';  export const db = new Dexie("user");  db.version(1).stores({   user: "key, value" }); 
Enter fullscreen mode Exit fullscreen mode

Create stores.js

This is the actual store that you can use in your +page.svelte. As you can see, it has a function called sync which gets the data from the indexedDB with Dexie and sets the data of userData. It also returns a promise which will be very handy in a second.

import { writable } from "svelte/store"; import { db } from "$lib/db";  export const userData = writable([]);  userData.sync = function() {     return new Promise (async (resolve, reject) => {         try {             const data = await db.user.toArray();             userData.set(data);              resolve();         } catch (error) {             console.error(error);              reject (error);         }     }) } 
Enter fullscreen mode Exit fullscreen mode

How To Load

import { userData } from "@stores"; // I am using an alias here  <script>     function handleMount() {         userData.sync()     }      onMount(handleMount); </script>  <main>     {#each $userData as data}         <p>{data.name}, {data.age}</p>     {/each} </main> 
Enter fullscreen mode Exit fullscreen mode

Do Stuff When The Data Has Been Loaded

Sometimes you need to wait for the data until you can call a function. This is no longer a problem, as you can just use .then after the sync-Function.

import { userData } from "@stores"; // I am using an alias here  <script>     function handleMount() {         userData.sync()         .then(() => {             // do stuff!         })     }      onMount(handleMount); </script>  <main>     {#each $userData as data}         <p>{data.name}, {data.age}</p>     {/each} </main> 
Enter fullscreen mode Exit fullscreen mode

How To Save

To save data in the indexedDB, simply use the Dexie API and then update the store:

    function saveData() {         db.user.put({ name, inputValue });         userData.sync()     } 
Enter fullscreen mode Exit fullscreen mode

I really like this work flow, as it gives me way more control than the last solution (and Dexie's liveQuery) but is still really, really simple!

Cheers,

Nico

javascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.