Skip to content

Instantly share code, notes, and snippets.

@CosmoAbdon
Created November 6, 2020 14:53
Show Gist options
  • Save CosmoAbdon/764da3d7296876f2cd1f7b7115faf363 to your computer and use it in GitHub Desktop.
Save CosmoAbdon/764da3d7296876f2cd1f7b7115faf363 to your computer and use it in GitHub Desktop.

Revisions

  1. CosmoAbdon created this gist Nov 6, 2020.
    15 changes: 15 additions & 0 deletions bulkUpdate.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    const bulkUpdate = async (model, dataValues, conditional) => {
    const transformValue = (...values) => values.map((v) => JSON.stringify(v));
    const checkValue = ([A, B]) => A === B;

    const promises = dataValues.map((value) =>
    model.update({ ...value }, { where: { [conditional]: value[conditional] } })
    );

    const results = await Promise.all(promises);
    const size = results.filter((r) => checkValue(transformValue(r, [0]))).length;

    return size;
    };

    export default bulkUpdate;