storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.15k stars 716 forks source link

PouchDB error: you must install a SQLite plugin in order for PouchDB to work on this platform #929

Open marionava opened 4 years ago

marionava commented 4 years ago

Hi,

On Ionic have installed the cordova-sqlite-storage running the command npm install --save cordova-sqlite-storage but still getting this error.

PouchDB error: you must install a SQLite plugin in order for PouchDB to work on this platform. Options:

https://github.com/nolanlawson/cordova-plugin-sqlite-2 https://github.com/litehelpers/Cordova-sqlite-storage https://github.com/Microsoft/cordova-plugin-websql

This is my code on a service

import { Injectable } from '@angular/core';
import PouchDB from 'pouchdb';
import { Observable } from 'rxjs';
import { Item } from '../interfaces/item.interfase';
import SQLitePlugin from 'pouchdb-adapter-cordova-sqlite';

@Injectable({
    providedIn: 'root'
})
export class ItemsService {
    public db;

    constructor() {
        this.createPouchDB();
    }

    createPouchDB() {
        PouchDB.plugin(SQLitePlugin);
        this.db = new PouchDB<Item>('items.db', { adapter: 'cordova-sqlite' });
    }
marionava commented 4 years ago

I hope this helps you. SQLite Plugin does not work on the browser. Until I tested it with my iOS device and made a few slight changes to my code it worked perfectly, but only on a real device. This is what I changed on my code:

createPouchDB() {
        PouchDB.plugin(SQLitePlugin);
        this.db = new PouchDB<Item>('items.db', {
            adapter: 'cordova-sqlite',
            iosDatabaseLocation: 'Library',
            androidDatabaseImplementation: 2
        });
    }
brodycj commented 4 years ago

SQLite Plugin does not work on the browser.

Correct. It would be ideal if PouchDB could be adapted to use IndexedDB on the browser and plugin on Cordova.

The code changes look fine but do have some limitations. My understanding is that the Library directory would be backed up to iCloud, which is generally not desired for reasons that I have already documented. The androidDatabaseImplementation option was renamed, and the old name was deprecated. I generally recommend avoiding this option anyway for multiple reasons including the risk of missing recent security updates and the likely lack of extra safety that I have implemented against potential database corruption.