【问题标题】:IndexedDB taking up a lot of space for simple objectIndexedDB 为简单对象占用大量空间
【发布时间】:2020-03-14 02:49:29
【问题描述】:

我存储一个 JSON 对象,包括 IndexedDB 中的 18 个图像 - 每个图像最多 50KB,但不知何故它需要 118 MB 在 indexedDB 中? - 不知道为什么这么重?

除了图像之外,它都是纯 JSON,主要是带有文本的键/值对...

查看附件截图

Size of indexedDB

Item in IndexedDB

我正在使用 DexieJS 来处理 IndexedDB

保存到数据库的函数如下所示:

  export const savePendingSurvey = (id, currentSurvey, surveyAnswers, surveyFiles) => {
const updatedSurvey = {
    id: id,
    createdAt: new Date(),
    status: 'UNSUBMITTED',
    surveyVersion: currentSurvey.survey.version,
    currentSurvey: {
        ...currentSurvey.survey
    },
    currentSurveyAnswers: [
        ...surveyAnswers
    ],
    currentSurveyFiles: [
        ...surveyFiles
    ]
};

db.open().then(() => {
    db.pendingSurveys.put(updatedSurvey).then((response) => {
        console.log('done adding pending survey', response);
    }).then(() => {
        return db.pendingSurveys.toArray();
    }).then((data) => {
        console.log('pendings surveys in db', data);
    }).catch((e) => {
        if ((e.name === 'QuotaExceededError') ||
            (e.inner && e.inner.name === 'QuotaExceededError')) {
            // QuotaExceededError may occur as the inner error of an AbortError
            alert('QuotaExceeded error! - There are not enough space available on your device');
        } else {
            // Any other error
            console.error(e);
        }
    });
});
};

似乎每次我对对象进行最简单的更新,即使是简单的文本更改,每次都会为项目增加 100-300kbs :/

【问题讨论】:

    标签: javascript ecmascript-6 indexeddb dexie


    【解决方案1】:

    在 Dexie 问题604641 中有类似的问题。似乎 IndexedDB 的大多数实现都像大多数数据库一样 - 它为每次更新添加新行并标记旧版本以供以后删除。当 DB 达到一定大小时,它将对旧行进行垃圾收集。

    【讨论】:

    • 有什么方法可以保持较小的数据库大小,还是会“自动”发生?
    • @nickycdk 不,不幸的是,据我所知,没有 API 可以控制它。
    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2019-03-28
    • 2015-03-21
    • 2018-02-14
    相关资源
    最近更新 更多