Migrate from LocalStorage/IndexedDB to SQLite

Hello there, we have an already productive Capacitor Ionic App.
We, unfortunately, don’t included the plugin cordova-plugin-sqlite so the user currently stored the data inside indexeddb, websql or localstorage as we just use the default config of ionic/storage.

We have a few storage problems on android devices with android 8, which is why we wanted to include the sqlite plugin. The problem now is, that the users data is lost because the data is stored with another driver.

Does anyone know how to migrate the data from one of the other technologies to sqlite?
Thank you very much!

Edit: As suggested the solution is to directly use the localforage to have a look what data is stored. This is the code that helped me:

import { Storage } from "@ionic/storage";
import * as localforage from "localforage";  

...

  constructor(private ionicStorage: Storage) {
    this.storage = ionicStorage;

    localforage.config({
      name: "_ionicstorage",
      storeName: "_ionickv",
      driver: [
        localforage.INDEXEDDB,
        localforage.WEBSQL,
        localforage.LOCALSTORAGE,
      ],
    });
  }

public async get(key: string): Promise<string> {
    let result: string | null = null;

    try {
      result = await this.storage.get(key);
    } catch (error) {
      result = null;
    }

    if (!!result) {
      this.logger.warn("Found data in ionic/storage for ", key);
      return result;
    }

    this.logger.warn("Found no data in ionic/storage for ", key);
    try {
      const dataFromForage = await localforage.getItem<string>(key);
      if (!!dataFromForage) {
        this.logger.info("Found data in forage for ", key);
        await this.set(key, dataFromForage);
        return dataFromForage;
      }
    } catch (error) {
      this.logger.error("Received error on checking localforage", error);
    }

    this.logger.warn("Found no data at all anywhere");
    return null;
  }

...

Hi
Ionic Storage uses LocalForage under the hood.
If you manage to access the current data using a instance \in Localforage, then you can do the migration to the newly created sqlite db which you access through your Ionic Storage API

Haven’t done it myself, but this would be my first try
Tom

1 Like

Thank you very much, I don’t know why I don’t think about that by myself … :smiley:
It works perfectly. I updated my question with the actual code that fixed my problem.

1 Like

Is there anyway you could provide more info on the solution for this? I’ve rewritten a Ionic/Cordova app with Capacitor. The Cordova app used Ionic Storage to store its data. I’ve followed you code and can’t seem to retrieve any data from the original database. It doesn’t matter what values I put in ‘name’ and ‘storeName’, it always comes back empty. Where did you come up with those values? Is there any other things you did that you didn’t mention in the post?

Hi there, the problem you have is totally different from the topic described here.
Here I just wanted to add the sqlite-storage to my existing capacitor-app. You want to migrate from cordova to capacitor, so my workaround won’t work.

Ionic has a documentation for the migration where you need to set a scheme to have the same storage address as before with cordova: Capacitor - build cross platform apps with the web.
But this sadly doesn’t work for my migration a few months ago. There I actually lost the complete data.