How to add a reliable storage to cordova framework7 app?

I’m trying to replace localstorage with reliable storage as some point out os may clean it, although i never experienced it. I look on web and found many options:-
1)localforage
2)cordova-sqlite-storage:- This is the most persistent storage, out of all options but i’m unable to get it running or found tutorials specifically due to sql queries.
3) PouchdB:- it also offers a cordova-sqlite-storage adapter but i don’t want to add another dependency specifically for a plugin.
It would be great if someone can point out to any tutorial or can provide a reference snippet or refer to any other library.

I used localforage. Quite successful. But it can be very complicated in complex applications.I am using Svelte right now. Svelte store and localstorage have been a great solution for me.

example;

store.js

import { writable,get  } from "svelte/store";

const createWritableStore = (key, startValue) => {
    const { subscribe, set } = writable(startValue);
     return {
      subscribe,
      set,
      useLocalStorage: () => {
        const json = localStorage.getItem(key);
        if (json) {
          set(JSON.parse(json));
        }         

        subscribe(current => {
          localStorage.setItem(key, JSON.stringify(current));
        });
      }
    };
  }

export let staff = createWritableStore('staff', null );

app.svelte

import { staff} from '../js/store';

staff.subscribe(value => {  
     console.log(value);
});

You can check this plugin: https://www.npmjs.com/package/cordova-plugin-nativestorage

Hi there,

I use the cordova-sqlite-storage plugin. Getting it working in my browser (emulator) was a piece of cake, but it took me a few days to get it running on a real iOS device:

  • like all plugins, this plugin must be initialized at startup;
  • the syntax is a little different, the examples used on the Github page of the plugin work.

But once it worked, I found it more convenient than localstorage.

Where did it go wrong @Gagan?

Peter

Hi Peter, actually i’m working with localstorage while developing the app, now i’m trying to replace it with cordova-sqlite-storage , SQL queries are the main reason for which i’m avoiding this plugin. As you pointed out that github examples of the repo work, i will try to set it on android device and let you know about the outcome. I’m quite confused of whole array of storage options available in cordova and which one to prefer?
Thanks

Thanks for pointing it out to this plugin. It looks promising. I would let you know about the results after setting it up.

I will let you know after testing localforage but as far as i know it also works like adapter over native storage plugin (if you are using nativestorage) otherwie, it uses webSql or indexdb(depends on which one is supported).

Some hints… the complex part was the insert data… my app will soon be open source, I can give you the complete code at that point :slight_smile:

determine the platform and create the database if not exist

 function onDeviceReady() {
        if (window.cordova.platformId === 'browser') DiaryDatabase=openDatabase('DiaryTables','1.0','Database containing all diary data', 2 * 1024 * 1024);
        else DiaryDatabase = window.sqlitePlugin.openDatabase({name: 'DiaryDatabase.db', location: 'default',
        androidDatabaseProvider: 'system'}); 
    }

create tables

tx.executeSql('CREATE TABLE IF NOT EXISTS surveyStructure (consent, manual, moduleAname, moduleBname, moduleCname, moduleDname)');

download data from the server in a json file

surveyStructure = JSON.parse(data).data0;

insert data

 DiaryDatabase.transaction(function(tx) {
                            var query = "INSERT INTO surveyStructure (consent, manual, moduleAname, moduleBname, moduleCname, moduleDname) VALUES (?, ?, ?, ?, ?, ?)";
                            tx.executeSql(query,[surveyStructure[0].data.consent,surveyStructure[0].data.manual,surveyStructure[0].data.moduleAname,surveyStructure[0].data.moduleBname,surveyStructure[0].data.moduleCname,surveyStructure[0].data.moduleDname])
                        })

I would say it’s more than enough. Thanks, @peterk . I would also advise you to check cordova-plugin-nativestorage as mentioned by @pmsgz . This plugin also looks promising.

True, but there are too many sunk costs right now :slight_smile: