Nano-SQL icon indicating copy to clipboard operation
Nano-SQL copied to clipboard

Make it runnable in a webworker

Open lennardv2 opened this issue 6 years ago • 3 comments

Is your feature request related to a problem? Please describe. I would like to run nSQL in a webworker. This however is not possible due to nanosql referencing to localStorage (when using IndexedDB). LocalStorage is not available in a webworker.

Describe the solution you'd like Let nanosql no longer reference the localstorage when on IndexedDB

lennardv2 avatar Sep 09 '19 16:09 lennardv2

use postMessage() to communicate back to localStorage

neofuture avatar Sep 09 '19 19:09 neofuture

The problem is that nanosql is trying to access localstorage while its not available in the webworker.

lennardv2 avatar Sep 10 '19 08:09 lennardv2

I had this issue as well. To get around it you can mock localStorage on the global object in your web worker.

Example:

self.localStorage = {
    getItem: function() {},
    setItem: function() {},
    removeItem: function() {}
};

It looks like it's using localStorage to store some information about the database version and model hash if you don't supply a version number. It's also storing some information about auto increment if you are using that. At least that's my understanding from looking at this file: https://github.com/ClickSimply/Nano-SQL/blob/master/packages/Core/src/adapters/indexedDB.ts

If you aren't using any of these things, the mock above should work. If you do need these things, you'll have to add code to your mock to make it work, keeping in mind that nanosql expects these methods to be synchronous.

The indexeddb adapter could probably be modified to use a separate indexeddb database to store these values instead of localStorage.

danbritt avatar Jan 15 '20 13:01 danbritt