Pouchdb: Add `key` support for SQLite Plugin

Created on 17 Mar 2016  路  5Comments  路  Source: pouchdb/pouchdb

Hi,

I am using https://github.com/litehelpers/Cordova-sqlcipher-adapter plugin for sqlite support with encryption and I am trying the following code and PouchDB is not opening db as encrypted and displayed the following on console.

OPEN database: _pouch_userDB.txt
open full db path: /var/mobile/Containers/Data/Application/99BA28C6-03BB-4636-9224-9EF708B79C94/Documents/_pouch_userDB.db
Open DB with NO encryption

Here is the code !

      var db2 = new PouchDB('userDB.db', {adapter: 'websql', key: 'password'});
      db2.info().then(console.log.bind(console));
      db2.put({_id: '1', name: 'username'})
        .then(usr => {
          console.log(`${usr} saved`);
        }).
        catch(ex=> {
          console.log(`${ex} occurred`)
        });

Where as the following works fine in the same app

      var db = window.sqlitePlugin.openDatabase({name: "my.db", key: "your-password-here"});
      db.transaction(function (tx) {
        tx.executeSql('DROP TABLE IF EXISTS test_table');
        tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');

        tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function (tx, res) {
          console.log("insertId: " + res.insertId + " -- probably 1");
          console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");

          tx.executeSql("select count(id) as cnt from test_table;", [], function (tx, res) {
            console.log("res.rows.length: " + res.rows.length + " -- should be 1");
            console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
          });

        }, function (e) {
          console.log("ERROR: " + e.message);
        });
      });

And here is the console output!

OPEN database: my.db
open full db path: /var/mobile/Containers/Data/Application/99BA28C6-03BB-4636-9224-9EF708B79C94/Documents/my.db
Open DB with encryption

It will be really helpful if someone please point out the mistake. What am I missing to pass to PouchDB constructor ?

enhancement

Most helpful comment

All 5 comments

Thanks for reporting! Similar to https://github.com/pouchdb/pouchdb/issues/3617, we just need to pass the key parameter along (ala location) and then document it.

I just added a new iosDatabaseLocation option which should eventually replace location and am planning to add another option to specify an internal or external Android database option.

I wonder if we can just have a single options object that PouchDB passes verbatim to sqlitePlugin.openDatabase? (I would be open to discussion whether the database name should be part of the options object or handled separately.)

Another neat option would be if users could specify this on the window.sqlitePlugin itself (e.g. sqlitePlugin.setDatabaseLocation(...). I'm kinda wary to add a generic new PouchDB(dbname, {sqliteOptions: {...}}) API since it will create inconsistency and confusion for existing users (and we'd probably have to support both APIs).

any plans for a new version?

Was this page helpful?
0 / 5 - 0 ratings