ionic-team / ionic-storage

Ionic Storage module for Ionic apps
MIT License
434 stars 99 forks source link

Where is the iOS sqlite .db file located? #242

Open garbit opened 3 years ago

garbit commented 3 years ago

Hi there,

I'm trying to use sqlite on iOS to persist data on the iOS device however I can't seem to locate the .db file.

Where can I find the .db sqlite database file on iOS?

I'm using the following code that seems to be adding/retrieving the relevant data and Safari debugger doesn't show the database in the "Storage" section whilst running an Ionic/Vue app on an iPad.

Installation steps:

npm install @ionic/storage
npm install cordova-sqlite-storage
npm install localforage-cordovasqlitedriver

Code to interact with DB (which is successful, from what I can tell but I can't find the file)

import { Drivers, Storage } from '@ionic/storage';
import * as CordovaSQLiteDriver from 'localforage-cordovasqlitedriver';

const sqliteStorage = new Storage({
      name: '__mydb',
      driverOrder: [CordovaSQLiteDriver._driver, Drivers.IndexedDB, Drivers.LocalStorage]
});

await sqliteStorage.defineDriver(CordovaSQLiteDriver)
await sqliteStorage.create();
await sqliteStorage.set(media.name, JSON.stringify(media)) 
garbit commented 3 years ago

Looking more closely at this problem. Ionic Storage doesn't support location as a property of the StorageConfig. If it was possible to add this and you were using localForage-cordovaSQLiteDriver you could then pass in a location id as found in the underlaying cordova-sqlite-storage package (see below):

To work out what options to add as a location I looked in the cordova-sqlite-storage repo to understand how this errors and also the location options you can pass for iOS (permalink):

In cordova-sqlite-storage there's two lists of iOS specific keys that can be passed to store the db:

dblocations = ["docs", "libs", "nosync"];

  iosLocationMap = {
    'default': 'nosync',
    'Documents': 'docs',
    'Library': 'libs'
  };

In ionic/storage I've modified the StorageConfig object to allow for the location ID to be passed to cordova-sqlite-storage:

export interface StorageConfig {
    name?: string;
    version?: number;
    size?: number;
    storeName?: string;
    description?: string;
    driverOrder?: Driver[];
    dbKey?: string;
    location?: number; <- I've added this
}

I then can use the following in my Ionic app (passing in the location index that denotes Documents on iOS):

const ionicStorage = new Storage({
    name: '__mydb.db',
    driverOrder: [CordovaSQLiteDriver._driver, Drivers.IndexedDB, Drivers.LocalStorage],
    location: 0 <- this is passed in by editing the StorageConfig in ionic/storage (see above) - the 0 refers to the index position of capacitor-sqlite-storage package [here](https://github.com/storesafe/cordova-sqlite-storage/blob/c256701caf8affbe0779e8ee95abaa746ecc051f/www/SQLitePlugin.js#L543)
});

location should really be available to be passed to specify the location output of the database.