interface.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _each2 = require('lodash/each');
  4. var _each3 = _interopRequireDefault(_each2);
  5. var _clone2 = require('lodash/clone');
  6. var _clone3 = _interopRequireDefault(_clone2);
  7. var _map2 = require('lodash/map');
  8. var _map3 = _interopRequireDefault(_map2);
  9. var _isArray2 = require('lodash/isArray');
  10. var _isArray3 = _interopRequireDefault(_isArray2);
  11. var _isEmpty2 = require('lodash/isEmpty');
  12. var _isEmpty3 = _interopRequireDefault(_isEmpty2);
  13. exports.default = function (Target) {
  14. Target.prototype.toQuery = function (tz) {
  15. var _this = this;
  16. var data = this.toSQL(this._method, tz);
  17. if (!(0, _isArray3.default)(data)) data = [data];
  18. return (0, _map3.default)(data, function (statement) {
  19. return _this.client._formatQuery(statement.sql, statement.bindings, tz);
  20. }).join(';\n');
  21. };
  22. // Create a new instance of the `Runner`, passing in the current object.
  23. Target.prototype.then = function () /* onFulfilled, onRejected */{
  24. var result = this.client.runner(this).run();
  25. return result.then.apply(result, arguments);
  26. };
  27. // Add additional "options" to the builder. Typically used for client specific
  28. // items, like the `mysql` and `sqlite3` drivers.
  29. Target.prototype.options = function (opts) {
  30. this._options = this._options || [];
  31. this._options.push((0, _clone3.default)(opts) || {});
  32. return this;
  33. };
  34. // Sets an explicit "connnection" we wish to use for this query.
  35. Target.prototype.connection = function (connection) {
  36. this._connection = connection;
  37. return this;
  38. };
  39. // Set a debug flag for the current schema query stack.
  40. Target.prototype.debug = function (enabled) {
  41. this._debug = arguments.length ? enabled : true;
  42. return this;
  43. };
  44. // Set the transaction object for this query.
  45. Target.prototype.transacting = function (t) {
  46. if (t && t.client) {
  47. if (!t.client.transacting) {
  48. helpers.warn('Invalid transaction value: ' + t.client);
  49. } else {
  50. this.client = t.client;
  51. }
  52. }
  53. if ((0, _isEmpty3.default)(t)) {
  54. helpers.error('Invalid value on transacting call, potential bug');
  55. throw Error('Invalid transacting value (null, undefined or empty object)');
  56. }
  57. return this;
  58. };
  59. // Initializes a stream.
  60. Target.prototype.stream = function (options) {
  61. return this.client.runner(this).stream(options);
  62. };
  63. // Initialize a stream & pipe automatically.
  64. Target.prototype.pipe = function (writable, options) {
  65. return this.client.runner(this).pipe(writable, options);
  66. };
  67. // Creates a method which "coerces" to a promise, by calling a
  68. // "then" method on the current `Target`
  69. (0, _each3.default)(['bind', 'catch', 'finally', 'asCallback', 'spread', 'map', 'reduce', 'tap', 'thenReturn', 'return', 'yield', 'ensure', 'reflect', 'get', 'mapSeries', 'delay'], function (method) {
  70. Target.prototype[method] = function () {
  71. var promise = this.then();
  72. return promise[method].apply(promise, arguments);
  73. };
  74. });
  75. };
  76. var _helpers = require('./helpers');
  77. var helpers = _interopRequireWildcard(_helpers);
  78. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  79. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  80. module.exports = exports['default'];