Dexie.js icon indicating copy to clipboard operation
Dexie.js copied to clipboard

Is there a reason why bulkUpdate doesn't exist?

Open mcfarljw opened this issue 6 years ago • 9 comments

I just want to update several things by key and of course I can just write my own Promise.all function that calls table.update, but is there a specific reason this wasn't included?

mcfarljw avatar Jan 06 '18 14:01 mcfarljw

Depending on how you update you might be able to use http://dexie.org/docs/Table/Table.bulkPut(). http://dexie.org/docs/Table/Table.update() explains the difference between put and update.

In case you use table.update you should put the calls in a transaction and react when it's done (not when the individual updates are done). If I remember correctly bulkPut does just that with table.put.

No clue why bulkUpdate is not implemented.

nponiros avatar Jan 06 '18 19:01 nponiros

Right, in this particular case I'm just updating one single property and don't want to overwrite the other properties for a few specific keys. Thanks for the heads up about wrapping it all using a transaction!

mcfarljw avatar Jan 07 '18 00:01 mcfarljw

The only reason bulkUpdate isn't updated has been lack of time ;) The bulk methods are, as @nponiros said, a transaction block with operations, but there is a great performance gain using the bulk methods as they ignore success events from indexedDB, which makes a great difference when working with large arrays of objects.

The plan is to implement Table.bulkUpdate(), wich will be more performant than several individual updates.

dfahlander avatar Jan 07 '18 22:01 dfahlander

Interesting and thanks for clearing that up! In my case my bulk updates generally include less than 100 things at a time so for now the performance of updating individually seems negligible.

mcfarljw avatar Jan 08 '18 00:01 mcfarljw

Hi there, looking for some feedback... what do you guys think about this bulkUpdate implementation, i.e. am i missing something?

MyDexie.prototype = Object.create(Dexie.prototype);

MyDexie.prototype.bulkUpdate = promisify(function(resolve, reject, table, bulkData) {
    'use strict';

    if (typeof table !== 'string') {
        bulkData = table;
        table = this.tables;
        table = table.length === 1 && table[0].name;
    }

    if (!bulkData.length) {
        return resolve(bulkData);
    }
    table = this.table(table);

    var i;
    var keyPath;
    var anyOf = [];
    var schema = table.schema;
    var indexes = schema.indexes;

    for (i = 0; i < indexes.length; ++i) {
        if (indexes[i].unique) {
            keyPath = indexes[i].keyPath;
            break;
        }
    }

    for (i = bulkData.length; i--;) {
        var v = bulkData[i][keyPath || schema.primKey.keyPath];

        if (MyDexie.exists(anyOf, v)) {
            bulkData.splice(i, 1);
        }
        else {
            anyOf.push(v);
        }
    }

    (keyPath ? table.where(keyPath).anyOf(anyOf).toArray() : table.bulkGet(anyOf))
        .then(function(r) {
            var toUpdate = [];

            keyPath = keyPath || schema.primKey.keyPath;
            for (var i = r.length; i--;) {
                for (var j = r[i] && bulkData.length; j--;) {
                    if (MyDexie.equal(r[i][keyPath], bulkData[j][keyPath])) {
                        delete bulkData[j][keyPath];
                        toUpdate.push([r[i], bulkData.splice(j, 1)[0]]);
                        break;
                    }
                }
            }

            var tasks = toUpdate.map(function(u) {
                return table.where(":id").equals(u[0][schema.primKey.keyPath]).modify(u[1]);
            });
            if (bulkData.length) {
                tasks.push(table.bulkPut(bulkData));
            }
            return Promise.all(tasks);
        })
        .then(resolve)
        .catch(reject);
});

Thanks in advance :)

diegocr avatar May 12 '20 10:05 diegocr

Has there been any updates on this?

I am also looking for something similar to a bulkUpdate method but something that bulkAdds as well as bulkUpdates. Similar to bulkPut but instead of replacing the object it updates the values.

jonathanadams avatar Dec 09 '20 14:12 jonathanadams

The implementation of bulkUpdate should do things similarily to how Collection.modify() is implemented but a much simpler as it will only apply a set of keyPaths and change to their new values. Basically do a dbCoreTable.getMany({trans, keys, cache: 'immutable'}).then(values => values.map((value, i) => applyUpdate(value, updates[i])).then(newValues => dbCoreTable.put({trans, keys, values: newValues}). applyUpdate should be done the same way as the modifyer function does it in Collection.modify() - to go through the keypaths and call setByKeyPath() on them. Must take care of both inbound and outbound tables.

I will implement this at some point or get a PR that does it.

dfahlander avatar Dec 09 '20 22:12 dfahlander

Hi! Prosit New Year! – I was wondering, whether this feature has been implemented already? Thanks for the info! @dfahlander

rowild avatar Jan 03 '22 14:01 rowild

It's not implemented.

dfahlander avatar Jan 03 '22 22:01 dfahlander

So is there any sample code showing how to achieve this before bulkUpdate is implemented?

Is this code alright?

const updateMulti = async (data: Array<TodoType>) => {
  await db.transaction("rw", db.todos, async () => {
    await Promise.all(
      data.map(
        async ({ id, ...rest }) => await db.todos.update(id, { ...rest, updatedAt: Date.now() })
      )
    );
  });
  return data.map((d) => d.id);
};

Toshinaki avatar Oct 03 '22 07:10 Toshinaki

So is there any sample code showing how to achieve this before bulkUpdate is implemented?

Is this code alright?

const updateMulti = async (data: Array<TodoType>) => {
  await db.transaction("rw", db.todos, async () => {
    await Promise.all(
      data.map(
        async ({ id, ...rest }) => await db.todos.update(id, { ...rest, updatedAt: Date.now() })
      )
    );
  });
  return data.map((d) => d.id);
};

No, this looks more like it tries to do what bulkPut() already does. bulkPut() is an upsert operation - it will add or replace the given object, while update will only apply changes on individual properties.

There is an implementation of bulkUpdate() in dexie-cloud-addon (not exported though) that you could copy/paste:

import Dexie, { Table, cmp } from 'dexie';

export async function bulkUpdate(
  table: Table,
  keys: any[],
  changeSpecs: { [keyPath: string]: any }[]
) {
  const objs = await table.bulkGet(keys);
  const resultKeys: any[] = [];
  const resultObjs: any[] = [];
  keys.forEach((key, idx) => {
    const obj = objs[idx];
    if (obj) {
      for (const [keyPath, value] of Object.entries(changeSpecs[idx])) {
        if (keyPath === table.schema.primKey.keyPath) {
          if (cmp(value, key) !== 0) {
            throw new Error(`Cannot change primary key`);
          }
        } else {
          Dexie.setByKeyPath(obj, keyPath, value);
        }
      }
      resultKeys.push(key);
      resultObjs.push(obj);
    }
  });
  await (table.schema.primKey.keyPath == null
    ? table.bulkPut(resultObjs, resultKeys)
    : table.bulkPut(resultObjs));
}

EDIT: You'd need dexie@^4.0.0-alpha.1 to import cmp. It's needed if your primary keys can be arrays, dates or typed arrays, but if you know your primary keys to be number or string, you could change it to if (value !== key) to get rid of the dependency of cmp.

dfahlander avatar Oct 03 '22 12:10 dfahlander

bulkUpdate is now in Dexie 4.0 and in the docs: https://dexie.org/docs/Table/Table.bulkUpdate()

dfahlander avatar Jan 30 '23 00:01 dfahlander

@dfahlander Great, thank you! ill try it ASAP!

rowild avatar Jan 30 '23 12:01 rowild

I can see it's really fast my bulkUpdate, in my case running 8k records updating via bulkUpdate reduce half of time. Thank you so much.

tinh1115 avatar Apr 16 '23 04:04 tinh1115