only-cliches/Nano-SQL

Make it runnable in a webworker

lennardv2 opened this issue · 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

use postMessage() to communicate back to localStorage

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

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.