4

I am creating a PouchDb like so :

var db = new PouchDB('my_db', 
        { auto_compaction: true, revs_limit: 1, adapter: 'websql' });

Then I create and delete a number of records :

db.put({ _id: '1'});
db.put({ _id: '2'});
db.put({ _id: '3'});

db.get('1')
.then(function(doc) {
    db.remove(doc)
});

db.get('2')
.then(function(doc) {
    db.remove(doc)
});

db.get('3')
.then(function(doc) {
    db.remove(doc)
});

From my reading of the documentation, this is the correct way to delete and remove records.
And this SO question and answer seems to suggest also that this is the way to do things.

However, if I use the Chrome inspector to look at my Web SQL DB, the records are still there :

The records are still visible in my Web SQL database

I don't believe this is not a timing issue or anything like that, as I can refresh with just the delete code and then get a 404 not_found error


My application creates and keeps records in a local pouchDb until they have been synced to central server, at which time I want to clear them from the local database.
I'm creating lots of records and if I cannot clear them out then eventually I'm going to run out of space on the device (it is hybrid HTML5 mobile app).

Is it even possible to actually remove records from a local PouchDB?

If so, how do I do it?

If not, what is a good solution that I can easily swap in place of PouchDB?

(I'm really hoping it is possible because I've gone down this path of development, so if the answer to the first question is No, then I need a good answer to the third question)

Community
  • 1
  • 1
kris
  • 11,868
  • 9
  • 88
  • 110
  • Not yet but it's in the works https://github.com/pouchdb/pouchdb/issues/4987#issuecomment-226517481 and here's the main issue tracking this https://github.com/pouchdb/pouchdb/issues/802 – Kul Sep 13 '16 at 14:32
  • Thanks for the answer Kul -- is there any new info on the .purge() implementation ? – kris Sep 13 '16 at 16:16

1 Answers1

4

As mentioned in the comments above, this is not yet possible but is being worked on (source 1 source 2). However, there is a work around which you might be able to use.

The workaround is to replicate the database locally to another PouchDB database and once the replication is complete, delete the original database. Deleted documents won't be replicated (source)


Here is a working demo:

(() => {
  // DECLARATION
  const dbName = 'testdb';
  const tmpDBName = 'tmpdb';
  const deleteFilter = (doc, req) => !doc._deleted;
  const doc1 = { _id: 'd1' };
  const doc2 = { _id: 'd2' };

  //  CREATION
  //  create database
  const maindb = new PouchDB(dbName);
  //  insert two documents
  maindb.post(doc1)
    .then(() => maindb.post(doc2))
    //  query for one document
    .then(() => maindb.get(doc1._id))
    //  delete this document
    .then((doc) => { console.log(doc); return maindb.remove(doc) })
    //  query for the same document
    .then(() => maindb.get(doc1._id))
    .catch((err) => { console.log(err) });

  //  CLEANUP
  //  delete a database with tmpdb name
  new PouchDB(tmpDBName).destroy()
    //  create a database with tmpdb name
    .then(() => Promise.resolve(new PouchDB(tmpDBName)))
    //  replicate original database to tmpdb with filter
    .then((tmpDB) => new Promise((resolve, reject) => {
      maindb.replicate.to(tmpDB, { filter: deleteFilter })
        .on('complete', () => { resolve(tmpDB) })
        .on('denied', reject)
        .on('error', reject)
    }))
    //  destroy the original db
    .then((tmpDB) => {
      console.log(tmpDB.name);
      return maindb.destroy().then(() => Promise.resolve(tmpDB))
    })
    //  create the original db
    .then((tmpDB) => new Promise((resolve, reject) => {
      console.log(tmpDB.name);
      try {
        resolve({ db: new PouchDB(dbName), tmpDB: tmpDB })
      } catch (e) {
        reject(e)
      }
    }))
    //  replicate the tmpdb to original db
    .then(({db, tmpDB}) => new Promise((resolve, reject) => {
      tmpDB.replicate.to(db)
        .on('complete', () => { resolve(tmpDB) })
        .on('denied', reject)
        .on('error', reject)
    }))
    //  destroy the tmpdb
    .then((tmpDB) => tmpDB.destroy())
    .then(() => { console.log('Cleanup complete') })
    .catch((err) => { console.log(err) });

})()

If you check the state of the database after executing this code, it'll contain only one document. Note that at times, I had to refresh the browser to be able to see the latest state of the database (a right click + Refresh IndexedDB wasn't enough).

If you want to cleanup the database while testing this, you can use this snippet:

['testdb', 'tmpdb'].forEach((d) => { new PouchDB(d).destroy() })
Community
  • 1
  • 1
Kul
  • 1,239
  • 8
  • 18
  • OK - again thank you for this answer -- I think I will go with this solution -- but how do I go about doing a local replication to a new PouchDB? – kris Sep 13 '16 at 16:23
  • create another db with `new PouchDB()` using a new dbname and sync like you normally would. You could use the `memory` adapter to avoid creating another db (but then you'd have to sync twice. So your workflow would be something like: (1) sync from original db to in memory db (2) delete original db & create it again (3) sync from the in memory db to the original db. – Kul Sep 13 '16 at 16:35
  • I shouldn't have used CouchDB - I just needed a local database and I've coded the syncing to a central DB myself - so I don't have a "normal" sync - could you point me to some code for doing a sync from one local DB to another ? – kris Sep 13 '16 at 16:59
  • https://pouchdb.com/guides/replication.html. Instead of creating a remote db (as done on the linked page) create another local db – Kul Sep 13 '16 at 17:03
  • sweet! thank you - I will give this a go - if successful, are you happy if I update your answer with the code and then mark it as correct? – kris Sep 13 '16 at 17:06
  • Sure, that'd be great! – Kul Sep 13 '16 at 17:24
  • Hmmmm ... I have completed this ... and it seems to "work", in that my original database is there, and there is an empty "temp" database, but my deleted lines are still in there ! (SO has prompted me to move this discussion to chat, which would be great, but then it says "moving the discussion to chat failed" -- I'm going to go ahead and past my code into your answer to show you what I have done, please let me know if you see if I'm missing something) – kris Sep 13 '16 at 17:40
  • We can discuss this on PouchDB's slack channel if you want – Kul Sep 13 '16 at 17:41
  • that is a great idea - I'd just finished doing the update to the post laying it all out - but please send me a link to the pouchdb slack channel – kris Sep 13 '16 at 17:51
  • this is not my night -- I get "502 Bad Gateway" following that link – kris Sep 13 '16 at 18:03
  • I really appreciate all your help with this -- I'm going to need to call it a night here now -- it is just after 4am where I am -- if you see anything wrong in the code please let me know -- maybe i'll be able to talk to you some way tomorrow -- if you happened to be up for that --- regardless; thanks for your time here so far – kris Sep 13 '16 at 18:11