如何在 IndexedDB 中更新基于自身的列?

How to update a column based on itself in IndexedDB?

sql : 更新表A 设置 column01 = column01 + 3, column02 = column01 + column02

如何在 IndexedDB 中执行此操作?

像这样:

function patch(db, store, id, mutate) {
  return new Promise((resolve, reject) => {
    let dirtied = false;
    const tx = db.transaction(store, 'readwrite');
    tx.onerror = event => reject(event.target.error);
    tx.oncomplete = () => resolve(dirtied);
    const store = tx.objectStore(store);
    const request = store.get(id);
    request.onsuccess = (event) => {
      const object = event.target.result;

      if (!object) {
        reject(new Error(`Unable to patch ${id} because id not found`));
        return;
      }

      dirtied = mutate(object);
      if (dirtied) {
        store.put(object);
      }
    };
  });
}

async function updateAColumnBasedOnItself() {
  let db;
  try {
    db = await open('mydb', 1);
    const dirtied = await patch(db, 'tableA', 123, object => {
      let dirtied = false;
      if (object.column01 !== object.column01 + object.column03) {
        object.column01 = object.column01 + object.column03;
        dirtied = true;
      }

      if (object.column02 !== object.column01 + object.column02) {
        object.column02 = object.column01 + object.column02;
        dirtied = true;
      }

      return dirtied;
    });
    console.log('patch completed, affected %d objects', dirtied ? 1 : 0);
  } finally (error) {
    db?.close();
  }
}