Search code examples
sqlitetypescriptstorageionic2web-sql

Ionic 2: RC.0 Storage no longer works in browser


Q) How can I get my app to work in the browser as before, using WebSQL/SQLite?

I've been using the ionic Storage module since forever, which enabled my app to run with WebSQL in the browser and SQLite on the device itself.

This was simple and working, now it's broken with RC.0.

My LocalStorageService.ts was like this:

export class LocalStorageService {

  constructor(...) { ... }

  query(sql: string, params?: any, infoMsg?: string): Promise<any> {
    return new Promise(resolve => {
      return this._db.executeSql(sql, params).then(
        (data) => {
          this._LogService.info(infoMsg);
          resolve(data);
        },
        (error) => {
          this._LogService.error(error);
          resolve(null);
        }
      );
    });
  }

}

Then I could call it from any component:

this.LocalStorageService.query('SELECT * FROM blah').then(data => {
    // do stuff with results.
});

Note: Key/value pair storage i.e. LocalStorage won't work, I need SQL querying capabilities in the browser+device.

Thanks.


Solution

  • Please refer to this gist:

    https://gist.github.com/NickStemerdink/64c782807fc31b7bc9b529ad4b1d56d5

    Original post here:

    https://github.com/driftyco/ionic/issues/8269#issuecomment-250756711