interface.js 3.2 KB

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