indexeddb中多列的自动递增

AutoIncrement of Multiple columns in indexeddb

有谁知道 - 我们如何为 indexeddb 中的两列指定自动增量。

我知道 - 我们可以在创建 table 时为一列指定自动增量 -

var objectStore = thisDb.createObjectStore("note", { keyPath: "id", autoIncrement:true });

但无法找到如何对多列执行相同的操作。据我所知-我们无法获得自动增量的价值。当我们插入数据时,该值将自动增加并添加。所以如果我能以某种方式获得自动增量值,那也是解决方案。

您不能在商店中创建两个自动递增的属性。该功能仅适用于定义为关键路径的 属性。

您可以轻松获取自增值。该值作为插入新对象的 putadd 请求的 result 提供。

例如:

function addThing(db, thing) {
  return new Promise((resolve, reject) => {
    let id = undefined;

    const transaction = db.transaction('things', 'readwrite');
    const store = transaction.objectStore('things');

    // wait to resolve the promise until the transaction is completed
    // so that we do not prematurely pretend the operation succeeded. resolve 
    // to the value of the new id
    transaction.oncomplete = event => resolve(id);

    transaction.onerror = event => reject(event.target.error);

    // store.add also works here
    const request = store.put(thing);

    // listen for the add event and grab the value of the new id
    // that indexedDB generated and store it in the id variable
    request.onsuccess = event => id = event.target.result;
  });
}


async function doSomething() {
  const something = {bar: 'baz'};

  const db = await open(...);
  const id = await addThing(db, something);
  db.close();

  something.id = id;
  console.log(something);
}