How to use Dexie.js inside of dedicated worker?

I have a function that fetch a JSON data via window.fetch and put it inside IndexedDB table via db.table.bulkPut(array_of_data). It takes more than 10 seconds to do that because of large amount of data and it blocks UI and makes UX bad. I have decided to run this task in a Worker, but I have not found any examples how can I make that with Dexie.js or directly with IndexedDB

Thank you for any advice :)

Upvotes: 10

Views: 2737

Answers (1)

Parsa_Gholipour
Parsa_Gholipour

Reputation: 960

I am the developer of this library. You can use this:

https://github.com/parsagholipour/dexie-worker

It receives a Dexie.js instance and provides a web worker instance.

import { getWebWorkerDB } from "dexie-worker";

// db is a Dexie.js instance 
const workerDB = getWebWorkerDB(db);
const users = await workerDB.users.toArray(); // This will be executed in web worker

Upvotes: 1

Related Questions