redux-persist-sqlite-storage icon indicating copy to clipboard operation
redux-persist-sqlite-storage copied to clipboard

Can this work with Cordova?

Open YCMitch opened this issue 6 years ago • 8 comments

I'm losing a fair amount of hair trying to get iOS to not just delete all my localStorage whenever it feels like it, and I guess SQLite is my best bet.

I'm trying to get this to work with Cordova's SQLite storage, but it really doesn't want to. The bottom of this page seems to imply it's achievable, though.

I'm creating the SQLite DB like so:

        window.db = window.sqlitePlugin.openDatabase({
            name: 'my.db',
            location: 'default',
        });

then in my reducers, I'm declaring storage like this:

import SQLiteStorage from 'redux-persist-sqlite-storage'
const config = {
	key: 'primary',
	storage: SQLiteStorage(window.db, {
		name: 'my.db',
		location: 'default',
	})
}

Predictably enough, nothing in life is that easy. The project currently won't even build, with the error:

Module parse failed: /Servers/hassl/app/node_modules/redux-persist-sqlite-storage/index.js Unexpected token (26:27)
You may need an appropriate loader to handle this file type.
|   const dbResolver = (() => {
|     return new Promise((resolve, reject) => {
|       SQLite.openDatabase({...defaultConfig, ...config}, (db) => {
|         db.transaction( tx => {
|           tx.executeSql(`CREATE TABLE IF NOT EXISTS store (key, value)`);

Is this something anyone's familiar with? Does anyone know if it's possible?

YCMitch avatar Nov 30 '18 07:11 YCMitch

Hi @MitchEff ,

I never tried directly with Cordova's SQLite storage, always tested on the wrapper one react-native-sqlite-storage. Let me try with it, will get back to you soon.

Meanwhile, can you please inform which version of react-native you are using?

prsn avatar Dec 01 '18 04:12 prsn

@prsn any news on this?

ksendra avatar Apr 29 '19 11:04 ksendra

Hi @ntoka ,

I haven't yet tried with Cordova, could you please tell me which version of react-native and Cordova you are using?

prsn avatar Apr 30 '19 03:04 prsn

@prsn If I/we were using react-native, we wouldn't need to use Cordova.

@ntoka For what it's worth, I'm using Mozilla's localForage (instead of localStorage), and specifying WebSQL - seems to be working great so far.

YCMitch avatar Apr 30 '19 03:04 YCMitch

Thanks for the heads up @MitchEff ,

prsn avatar Apr 30 '19 04:04 prsn

@MitchEff Hey, thanks for the hint. Could you provide a bit more detail on how you've hooked up localforage to redux-persist to be used with cordova?

EDIT: Even though websql looks nice, I will need more storage space available than it's given to websql (~5MB). @prsn I'm using cordova v 9.0.0 and would like to hook up this package to https://github.com/xpbrew/cordova-sqlite-storage#readme

ksendra avatar Apr 30 '19 07:04 ksendra

@ntoka you can change WebSQL to any other localForage-supported database pretty easily. Perhaps IndexedDB, or something?

Anyways in my index.html, I just include localForage like:

<script src="lib/localforage/dist/localforage.js"></script>

In my onDeviceReady, I've got the following:

        if(localforage){
            window.localForage = localforage

            window.localForage.config({
                driver      : localforage.WEBSQL, // Force WebSQL; same as using setDriver()
                name        : 'MyApp',
                version     : 1.0,
                size        : 4980736, // Size of database, in bytes. WebSQL-only for now.
                storeName   : 'my_app', // Should be alphanumeric, with underscores.
                description : 'none'
            });
        }

and then in my reducer definition I have:

const config = {
	key: 'primary',
	blacklist: ['form', 'modal', 'dashboard'],
	transforms: [ accountDataFilter, activeChatFilter ],
	storage: window.localForage ? window.localForage : storage
}

Seems to work pretty good.

YCMitch avatar May 01 '19 00:05 YCMitch

Sorry to dig up an old issue, but the problem for Cordova users is that the OS can randomly delete data stored using any of the localForage storage systems, because they are all considered to be temp data. The OP's question remains valid, and is also my use-case. Using the cordova SQL lite plugin would let us have our redux store persist to the native platform sqlite implementation, thereby getting around the data persistence issue.

jthrilly avatar Nov 09 '20 23:11 jthrilly