Cordova-sqlite-storage requests permission for 4.8G in Safari browser

I’m using the sqlite plugin and everything works great in the browser build, but I’ve just found that Safari requests authorization to store 4.8G as the page loads. It’s certainly not going to store that much but it’s not a great first impression and would likely (rightly so) scare most people off the page.

Anything I can do about that?

image

1 Like

How exactly are you using SQLite on desktop?
How is this app build you are running?

Here’s the relevant part of my database provider

import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Storage } from '@ionic/storage';

declare var window: any;

@Injectable()
export class Database {

    public db: any;
    public dbname: string = 'WTM.db';

    constructor(
        public storage: Storage,
        public platform: Platform
    ) {
    }

    /**
    * Init - init database etc. PS! Have to wait for Platform.ready
    */
    init(): Promise<any> {

        return new Promise((resolve, reject) => {
            if (typeof window.sqlitePlugin !== 'undefined' && !this.platform.is('core') && !this.platform.is('mobileweb')) {
                this.db = window.sqlitePlugin.openDatabase({ name: this.dbname, location: 'default' });
            } else {
                this.db = window.openDatabase(this.dbname, '1.0', 'Test DB', -1);
            };

            this.createDistanceTable().then(
                (res) => {
                    resolve(true);
                },
                (err) => {
                    reject(false);
                }
            );
        });
    }

    // use this database class to dole out an instance of storage
    // for simple key => value settings
    public getStorage() {
        return this.storage;
    }

    get(key: string): Promise <any> {
        return this.storage.get(key);
    }

    set(key: string, value: any): Promise <any> {
        return this.storage.set(key, value);
    }
}

I guess on the browser it’s actually using this to initialize the db:

this.db = window.openDatabase(this.dbname, '1.0', 'Test DB', -1);

Not sure what you mean by that, but it’s a full browser build: ionic build browser --prod

you can see it live at http://walktomordor.com

Actually I’ve answered my own question… sometimes I just need to get annoyed enough type it out to see it :slight_smile:

window.openDatabase(this.dbname, '1.0', 'Test DB', -1);

The -1 is the size parameter, so it’s requesting authorization for the absolute largest size possible. D’oh! At least it’s an easy fix.

1 Like

Yep, exactly what I was googling now:

https://www.w3.org/TR/webdatabase/#databases

interface WindowDatabase {
  Database openDatabase(in DOMString name, in DOMString version, in DOMString displayName, in unsigned long estimatedSize, in optional DatabaseCallback creationCallback);
};

A minor note, but you don’t have to directly create a Promise. Thus you can re-write your init() to look something like:

    init(): Promise<boolean> {
        if (typeof window.sqlitePlugin !== 'undefined' && !this.platform.is('core') && !this.platform.is('mobileweb')) {
            this.db = window.sqlitePlugin.openDatabase({ name: this.dbname, location: 'default' });
        } else {
            this.db = window.openDatabase(this.dbname, '1.0', 'Test DB', -1);
        };

        return this.createDistanceTable().then(
            (res) => true,
            (err) => false
        );
    }

I also changed it to return a Promise<boolean> as I’d suggest avoiding any at …any… cost.

2 Likes