How to limit PouchDB revisions or permanently delete revisions
Asked Answered
G

2

9

I'm using PouchDB with websql adapter or a cordova app (I'm using the sqlite plugin).

N.B. this database is completely local and I have no need for it to sync.

For my specific application (the database is used to track API calls), I can update the same document many times.

My database keeps growing even after I delete documents and compact the database.

I have tried compacting after deleting a document, and as much as it makes a difference - is still saves record of the revision's existance - so my database still grows indefinitely.

How can I stop the database from growing indefinitely?

Deleting (by setting doc._deleted = true and then db.put(doc)) and compacting doesn't seem to work.

See below a one record which has continued to grow with all its revisions even despite my deleting it and compacting the database:

{
  "id": "STATUS_TYPE_WEB_BROADCAST_RESULT_APICALL_5",
  "deleted": true,
  "rev_tree": [
    {
      "pos": 1,
      "ids": [
        "561567a6abccf9e25063be1cff906862",
        {
          "status": "missing"
        },
        [
          [
            "76a29a5ea586f54b86d9f7671fa0938a",
            {
              "status": "missing"
            },
            [
              [
                "00945a90f36790a6e25c3c8c99ecb008",
                {
                  "status": "missing",
                  "deleted": true
                },
                [
                  [
                    "1844208b140a98fe83ccf547f3da1516",
                    {
                      "status": "missing"
                    },
                    [
                      [
                        "dd56c4b8dfae01cf3eabfad3ea21bf84",
                        {
                          "status": "missing"
                        },
                        [
                          [
                            "9152aac8de2c87c2265cc34ef1e992a9",
                            {
                              "status": "missing",
                              "deleted": true
                            },
                            [
                              [
                                "6ac6a3e5593250b2f6f3b9de5f2b80d0",
                                {
                                  "status": "missing"
                                },
                                [
                                  [
                                    "1c400fcbabe3fe9ee6b923e99ab87b3e",
                                    {
                                      "status": "missing"
                                    },
                                    [
                                      [
                                        "6d3b88e73b6f880152f4e0418ab3a67d",
                                        {
                                          "status": "missing",
                                          "deleted": true
                                        },
                                        [
                                          [
                                            "b2af3344fd9888653777e24bfd15dae4",
                                            {
                                              "status": "missing"
                                            },
                                            [
                                              [
                                                "d70cd32d571868ee2a49b14eb9ff3cf1",
                                                {
                                                  "status": "missing"
                                                },
                                                [
                                                  [
                                                    "3ece08f0894f93859915812aba9d294b",
                                                    {
                                                      "status": "missing",
                                                      "deleted": true
                                                    },
                                                    [
                                                      [
                                                        "7f31761497f509e6e5b0815b659405dd",
                                                        {
                                                          "status": "missing"
                                                        },
                                                        [
                                                          [
                                                            "c3db1ae3c411389ac9e099c5739420af",
                                                            {
                                                              "status": "missing"
                                                            },
                                                            [
                                                              [
                                                                "a0c86c197187b210a4030742da03416c",
                                                                {
                                                                  "status": "missing",
                                                                  "deleted": true
                                                                },
                                                                [
                                                                  [
                                                                    "d89180025074fc48c28fbc785e034218",
                                                                    {
                                                                      "status": "missing"
                                                                    },
                                                                    [
                                                                      [
                                                                        "00eb69a55afea63ac30b9fa9038e3f1e",
                                                                        {
                                                                          "status": "missing"
                                                                        },
                                                                        [
                                                                          [
                                                                            "b8c051e94c65e485b2129464b0b4830b",
                                                                            {
                                                                              "status": "missing",
                                                                              "deleted": true
                                                                            },
                                                                            [
                                                                              [
                                                                                "2832699d4da2bec534a129ade793bcb8",
                                                                                {
                                                                                  "status": "missing"
                                                                                },
                                                                                [
                                                                                  [
                                                                                    "f8314cb28d7d7466b7e15ff7756f703a",
                                                                                    {
                                                                                      "status": "missing"
                                                                                    },
                                                                                    [
                                                                                      [
                                                                                        "398e27cf400f815c04eb5059900949d8",
                                                                                        {
                                                                                          "status": "missing",
                                                                                          "deleted": true
                                                                                        },
                                                                                        [
                                                                                          [
                                                                                            "747af489d66690927251e71ad4608500",
                                                                                            {
                                                                                              "status": "missing"
                                                                                            },
                                                                                            [
                                                                                              [
                                                                                                "4f2766536562894054c6220ef84fb782",
                                                                                                {
                                                                                                  "status": "missing"
                                                                                                },
                                                                                                [
                                                                                                  [
                                                                                                    "536677ee9decf93b77d2d4b2a4243936",
                                                                                                    {
                                                                                                      "status": "missing",
                                                                                                      "deleted": true
                                                                                                    },
                                                                                                    [
                                                                                                      [
                                                                                                        "82d9b89e3ac956f5bc64d27a32ba3d5b",
                                                                                                        {
                                                                                                          "status": "missing"
                                                                                                        },
                                                                                                        [
                                                                                                          [
                                                                                                            "23fe3cfd7bce5a6e0d279c708dc3565c",
                                                                                                            {
                                                                                                              "status": "missing"
                                                                                                            },
                                                                                                            [
                                                                                                              [
                                                                                                                "3c7f3d5b774f0549ca3c80700e84e074",
                                                                                                                {
                                                                                                                  "status": "missing",
                                                                                                                  "deleted": true
                                                                                                                },
                                                                                                                [
                                                                                                                  [
                                                                                                                    "5b0db6e212bbf58e4d162389dc67800b",
                                                                                                                    {
                                                                                                                      "status": "missing"
                                                                                                                    },
                                                                                                                    [
                                                                                                                      [
                                                                                                                        "94b5b06de69d7b02bfa0800a4a895113",
                                                                                                                        {
                                                                                                                          "status": "missing"
                                                                                                                        },
                                                                                                                        [
                                                                                                                          [
                                                                                                                            "bb35cf47fa3da85eb2941c667c7ab40e",
                                                                                                                            {
                                                                                                                              "status": "missing",
                                                                                                                              "deleted": true
                                                                                                                            },
                                                                                                                            [
                                                                                                                              [
                                                                                                                                "432ea39039c355f9b1512c40b237ec38",
                                                                                                                                {
                                                                                                                                  "status": "missing"
                                                                                                                                },
                                                                                                                                [
                                                                                                                                  [
                                                                                                                                    "f09f2f15dde4bd327f20c1beabc2ae13",
                                                                                                                                    {
                                                                                                                                      "status": "missing"
                                                                                                                                    },
                                                                                                                                    [
                                                                                                                                      [
                                                                                                                                        "b4a21b8562c085e0a2a91ca9ba5e6385",
                                                                                                                                        {
                                                                                                                                          "status": "available",
                                                                                                                                          "deleted": true
                                                                                                                                        },
                                                                                                                                        []
                                                                                                                                      ]
                                                                                                                                    ]
                                                                                                                                  ]
                                                                                                                                ]
                                                                                                                              ]
                                                                                                                            ]
                                                                                                                          ]
                                                                                                                        ]
                                                                                                                      ]
                                                                                                                    ]
                                                                                                                  ]
                                                                                                                ]
                                                                                                              ]
                                                                                                            ]
                                                                                                          ]
                                                                                                        ]
                                                                                                      ]
                                                                                                    ]
                                                                                                  ]
                                                                                                ]
                                                                                              ]
                                                                                            ]
                                                                                          ]
                                                                                        ]
                                                                                      ]
                                                                                    ]
                                                                                  ]
                                                                                ]
                                                                              ]
                                                                            ]
                                                                          ]
                                                                        ]
                                                                      ]
                                                                    ]
                                                                  ]
                                                                ]
                                                              ]
                                                            ]
                                                          ]
                                                        ]
                                                      ]
                                                    ]
                                                  ]
                                                ]
                                              ]
                                            ]
                                          ]
                                        ]
                                      ]
                                    ]
                                  ]
                                ]
                              ]
                            ]
                          ]
                        ]
                      ]
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    }
  ],
  "seq": 230
}
Genoa answered 22/3, 2016 at 7:42 Comment(0)
G
19

I got the answer by opening an issue on the pouchDb's github:link to github issue.

Below is the answer from @daleyharvey on github. As you can see the main thing was adding revs_limit: 1 to my initialisation. That stopped my db from growing without bounds.

new PouchDB('name', {revs_limit: 1, auto_compaction: true}) should do what you want, revs_limit is the number of revisions we keep track off (which is what your seeing) compaction deletes the contents of those revisions


Just as a side-note, for my use case I eventually found out that there was no need to use an advanced db such as PouchDB, in the end end - I needed either plain WebSQL or local storage.

Genoa answered 4/4, 2016 at 3:43 Comment(1)
quick note: PouchDB uses WebSQL and local storage, it can use one or the other whether you're in a compatible browser.Spoilage
E
1

Have you tried auto compaction?

var db = new PouchDB('mydb', {auto_compaction: true});
db.put({_id: 'foo', version: 1}).then(function () {
  return db.get('foo');
}).then(function (doc) {
  doc.version = 2;
  return db.put(doc);
}).then(function () {
  // Revision 1 is already unavailable!
}).catch(function (err) {
  // handle errors
});
Erdah answered 3/4, 2016 at 20:40 Comment(1)
This was a step towards the right direction, but the missing sauce was the revs_limit. See accepted answer below.Genoa

© 2022 - 2024 — McMap. All rights reserved.