batchInsert.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _typeof2 = require('babel-runtime/helpers/typeof');
  4. var _typeof3 = _interopRequireDefault(_typeof2);
  5. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  6. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  7. var _flatten2 = require('lodash/flatten');
  8. var _flatten3 = _interopRequireDefault(_flatten2);
  9. var _chunk2 = require('lodash/chunk');
  10. var _chunk3 = _interopRequireDefault(_chunk2);
  11. var _isArray2 = require('lodash/isArray');
  12. var _isArray3 = _interopRequireDefault(_isArray2);
  13. var _isString2 = require('lodash/isString');
  14. var _isString3 = _interopRequireDefault(_isString2);
  15. var _isNumber2 = require('lodash/isNumber');
  16. var _isNumber3 = _interopRequireDefault(_isNumber2);
  17. var _bluebird = require('bluebird');
  18. var _bluebird2 = _interopRequireDefault(_bluebird);
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. var BatchInsert = function () {
  21. function BatchInsert(client, tableName, batch) {
  22. var chunkSize = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1000;
  23. (0, _classCallCheck3.default)(this, BatchInsert);
  24. if (!(0, _isNumber3.default)(chunkSize) || chunkSize < 1) {
  25. throw new TypeError('Invalid chunkSize: ' + chunkSize);
  26. }
  27. if (!(0, _isArray3.default)(batch)) {
  28. throw new TypeError('Invalid batch: Expected array, got ' + (typeof batch === 'undefined' ? 'undefined' : (0, _typeof3.default)(batch)));
  29. }
  30. this.client = client;
  31. this.tableName = tableName;
  32. this.batch = (0, _chunk3.default)(batch, chunkSize);
  33. this._returning = void 0;
  34. this._transaction = null;
  35. this._autoTransaction = true;
  36. if (client.transacting) {
  37. this.transacting(client);
  38. }
  39. }
  40. /**
  41. * Columns to return from the batch operation.
  42. * @param returning
  43. */
  44. BatchInsert.prototype.returning = function returning(_returning) {
  45. if ((0, _isArray3.default)(_returning) || (0, _isString3.default)(_returning)) {
  46. this._returning = _returning;
  47. }
  48. return this;
  49. };
  50. /**
  51. * User may supply their own transaction. If this is the case,
  52. * `autoTransaction = false`, meaning we don't automatically commit/rollback
  53. * the transaction. The responsibility instead falls on the user.
  54. *
  55. * @param transaction
  56. */
  57. BatchInsert.prototype.transacting = function transacting(transaction) {
  58. this._transaction = transaction;
  59. this._autoTransaction = false;
  60. return this;
  61. };
  62. BatchInsert.prototype._getTransaction = function _getTransaction() {
  63. var _this = this;
  64. return new _bluebird2.default(function (resolve) {
  65. if (_this._transaction) {
  66. return resolve(_this._transaction);
  67. }
  68. _this.client.transaction(function (tr) {
  69. return resolve(tr);
  70. });
  71. });
  72. };
  73. BatchInsert.prototype.then = function then() {
  74. var _this2 = this;
  75. var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
  76. return this._getTransaction().then(function (transaction) {
  77. return _bluebird2.default.all(_this2.batch.map(function (items) {
  78. return transaction(_this2.tableName).insert(items, _this2._returning);
  79. })).then(function (result) {
  80. if (_this2._autoTransaction) {
  81. transaction.commit();
  82. }
  83. return callback((0, _flatten3.default)(result || []));
  84. }).catch(function (error) {
  85. if (_this2._autoTransaction) {
  86. transaction.rollback(error);
  87. }
  88. throw error;
  89. });
  90. });
  91. };
  92. return BatchInsert;
  93. }();
  94. exports.default = BatchInsert;
  95. module.exports = exports['default'];