From 6de2633adc888765a58c0e2be4db6f02769b8aa4 Mon Sep 17 00:00:00 2001 From: maxeljkin Date: Thu, 10 Oct 2019 22:21:52 +0300 Subject: [PATCH] refactor(migrator): refactor batchInsert from bluebird --- lib/util/batchInsert.js | 75 ++++++++++++++++++----------------------- lib/util/delay.js | 3 ++ 2 files changed, 36 insertions(+), 42 deletions(-) create mode 100644 lib/util/delay.js diff --git a/lib/util/batchInsert.js b/lib/util/batchInsert.js index cc1e3fb1d7..5cceaa09ea 100644 --- a/lib/util/batchInsert.js +++ b/lib/util/batchInsert.js @@ -1,5 +1,5 @@ const { isNumber, chunk, flatten } = require('lodash'); -const Bluebird = require('bluebird'); +const delay = require('./delay'); module.exports = function batchInsert( client, @@ -11,60 +11,51 @@ module.exports = function batchInsert( let autoTransaction = true; let transaction = null; - const getTransaction = () => - new Bluebird((resolve, reject) => { - if (transaction) { - autoTransaction = false; - return resolve(transaction); - } + const getTransaction = async () => { + if (transaction) { + autoTransaction = false; + return transaction; + } - autoTransaction = true; - client.transaction(resolve).catch(reject); - }); + autoTransaction = true; + return client.transaction(); + }; const wrapper = Object.assign( - new Bluebird((resolve, reject) => { - const chunks = chunk(batch, chunkSize); - + Promise.resolve().then(async () => { if (!isNumber(chunkSize) || chunkSize < 1) { - return reject(new TypeError(`Invalid chunkSize: ${chunkSize}`)); + throw new TypeError(`Invalid chunkSize: ${chunkSize}`); } if (!Array.isArray(batch)) { - return reject( - new TypeError(`Invalid batch: Expected array, got ${typeof batch}`) + throw new TypeError( + `Invalid batch: Expected array, got ${typeof batch}` ); } - //Next tick to ensure wrapper functions are called if needed - return Bluebird.delay(1) - .then(getTransaction) - .then((tr) => { - return Bluebird.mapSeries(chunks, (items) => - tr(tableName).insert(items, returning) - ) - .then((result) => { - result = flatten(result || []); + const chunks = chunk(batch, chunkSize); - if (autoTransaction) { - //TODO: -- Oracle tr.commit() does not return a 'thenable' !? Ugly hack for now. - return (tr.commit(result) || Bluebird.resolve()).then( - () => result - ); - } + //Next tick to ensure wrapper functions are called if needed + await delay(1); + const tr = await getTransaction(); - return result; - }) - .catch((error) => { - if (autoTransaction) { - return tr.rollback(error).then(() => Bluebird.reject(error)); - } + try { + const chunksResults = []; + for (const items of chunks) { + chunksResults.push(await tr(tableName).insert(items, returning)); + } + const result = flatten(chunksResults); + if (autoTransaction) { + await tr.commit(); + } + return result; + } catch (error) { + if (autoTransaction) { + await tr.rollback(error); + } - return Bluebird.reject(error); - }); - }) - .then(resolve) - .catch(reject); + throw error; + } }), { returning(columns) { diff --git a/lib/util/delay.js b/lib/util/delay.js new file mode 100644 index 0000000000..3a0376b875 --- /dev/null +++ b/lib/util/delay.js @@ -0,0 +1,3 @@ +const { promisify } = require('util'); + +module.exports = promisify(setTimeout);