【问题标题】:How to limit PouchDB revisions or permanently delete revisions如何限制 PouchDB 修订或永久删除修订
【发布时间】:2016-07-09 00:04:10
【问题描述】:

我将PouchDBwebsql 适配器或cordova 应用程序一起使用(我使用的是sqlite 插件)。

注意这个数据库完全是本地的,我不需要同步它。

对于我的特定应用程序(数据库用于跟踪 API 调用),我可以多次更新同一个文档。

即使在我删除文档并压缩数据库后,我的数据库仍在增长

我尝试在删除文档后进行压缩,尽管它有所作为 - 仍然保存修订存在的记录 - 所以我的数据库仍然无限增长。

如何阻止数据库无限增长?

删除(通过设置doc._deleted = true 然后db.put(doc))和压缩似乎不起作用。

请参阅下面的一条记录,尽管我删除了它并压缩了数据库,但它的所有修订版仍在继续增长:

{
  "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
}

【问题讨论】:

    标签: database sqlite cordova pouchdb


    【解决方案1】:

    我通过在 pouchDb 的 github 上打开一个问题得到了答案:link to github issue

    以下是来自 github 上@daleyharvey 的答案。如您所见,主要是将revs_limit: 1 添加到我的初始化中。这阻止了我的数据库无限增长。

    new PouchDB('name', {revs_limit: 1, auto_compaction: true}) 应该做你想做的,revs_limit 是我们跟踪的修订数量(这是你看到的)压缩删除这些修订的内容


    顺便说一句,对于我的用例,我最终发现不需要使用 PouchDB 等高级数据库,最终我需要普通的 WebSQL 或本地存储。

    【讨论】:

    • 快速说明:PouchDB 使用 WebSQL 和本地存储,无论您在兼容的浏览器中,它都可以使用其中一种。
    【解决方案2】:

    你试过自动压缩吗?

    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
    });
    

    【讨论】:

    • 这是朝着正确方向迈出的一步,但缺少的酱汁是 revs_limit。请参阅下面接受的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2012-11-29
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多