IndexedDB - 向嵌套对象添加键

发布于 2025-01-13 00:30:04 字数 658 浏览 1 评论 0原文

我有一个具有以下结构的 IDB:

id: 1
name: "test"
operations: [
  {
    column: "active"
    operator: "="
    value: true
  }
]

这里添加 ID,如下所示:

request.onupgradeneeded = e => {
            let db = e.target.result
            db.createObjectStore('filters', { autoIncrement : true, keyPath: 'id'})
        }

用户可以添加一条新记录(在这种情况下它将具有 id:2 等),或者向现有记录添加操作。

我应该怎么做才能为每个操作添加自动递增键?

预期结果:

id: 1
name: "test"
operations: [
  {
    id: 1
    column: "active"
    operator: "="
    value: true
  },
  {
    id: 2
    column: "age"
    operator: ">"
    value: 32
  },
]

I have an IDB with the following structure :

id: 1
name: "test"
operations: [
  {
    column: "active"
    operator: "="
    value: true
  }
]

Here the ID is added like so :

request.onupgradeneeded = e => {
            let db = e.target.result
            db.createObjectStore('filters', { autoIncrement : true, keyPath: 'id'})
        }

Users can either add a new record, in which case it will have id: 2 and so on, or add an operation to an existing record.

What should I do to add an auto incrementing key to each operations ?

Expected result :

id: 1
name: "test"
operations: [
  {
    id: 1
    column: "active"
    operator: "="
    value: true
  },
  {
    id: 2
    column: "age"
    operator: ">"
    value: 32
  },
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2025-01-20 00:30:04

IndexedDB 不会帮助您解决这个问题,您必须在应用程序代码中处理它。 IndexedDB中的自增键仅适用于对象的主键。

IndexedDB won't help you with that, you'll have to handle it in your application code. Auto incrementing keys in IndexedDB are only for the primary key of the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文