make-knex.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _defineProperties = require('babel-runtime/core-js/object/define-properties');
  4. var _defineProperties2 = _interopRequireDefault(_defineProperties);
  5. var _assign2 = require('lodash/assign');
  6. var _assign3 = _interopRequireDefault(_assign2);
  7. exports.default = makeKnex;
  8. var _events = require('events');
  9. var _migrate = require('../migrate');
  10. var _migrate2 = _interopRequireDefault(_migrate);
  11. var _seed = require('../seed');
  12. var _seed2 = _interopRequireDefault(_seed);
  13. var _functionhelper = require('../functionhelper');
  14. var _functionhelper2 = _interopRequireDefault(_functionhelper);
  15. var _methods = require('../query/methods');
  16. var _methods2 = _interopRequireDefault(_methods);
  17. var _helpers = require('../helpers');
  18. var helpers = _interopRequireWildcard(_helpers);
  19. var _batchInsert = require('./batchInsert');
  20. var _batchInsert2 = _interopRequireDefault(_batchInsert);
  21. 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; } }
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. function makeKnex(client) {
  24. // The object we're potentially using to kick off an initial chain.
  25. function knex(tableName, options) {
  26. var qb = knex.queryBuilder();
  27. if (!tableName) helpers.warn('calling knex without a tableName is deprecated. Use knex.queryBuilder() instead.');
  28. return tableName ? qb.table(tableName, options) : qb;
  29. }
  30. (0, _assign3.default)(knex, {
  31. Promise: require('bluebird'),
  32. // A new query builder instance.
  33. queryBuilder: function queryBuilder() {
  34. return client.queryBuilder();
  35. },
  36. raw: function raw() {
  37. return client.raw.apply(client, arguments);
  38. },
  39. batchInsert: function batchInsert(table, batch) {
  40. var chunkSize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1000;
  41. return new _batchInsert2.default(this, table, batch, chunkSize);
  42. },
  43. // Runs a new transaction, taking a container and returning a promise
  44. // for when the transaction is resolved.
  45. transaction: function transaction(container, config) {
  46. return client.transaction(container, config);
  47. },
  48. // Typically never needed, initializes the pool for a knex client.
  49. initialize: function initialize(config) {
  50. return client.initialize(config);
  51. },
  52. // Convenience method for tearing down the pool.
  53. destroy: function destroy(callback) {
  54. return client.destroy(callback);
  55. }
  56. });
  57. // Hook up the "knex" object as an EventEmitter.
  58. var ee = new _events.EventEmitter();
  59. for (var key in ee) {
  60. knex[key] = ee[key];
  61. }
  62. // Allow chaining methods from the root object, before
  63. // any other information is specified.
  64. _methods2.default.forEach(function (method) {
  65. knex[method] = function () {
  66. var builder = knex.queryBuilder();
  67. return builder[method].apply(builder, arguments);
  68. };
  69. });
  70. knex.client = client;
  71. var VERSION = '0.12.6';
  72. (0, _defineProperties2.default)(knex, {
  73. __knex__: {
  74. get: function get() {
  75. helpers.warn('knex.__knex__ is deprecated, you can get the module version' + "by running require('knex/package').version");
  76. return VERSION;
  77. }
  78. },
  79. VERSION: {
  80. get: function get() {
  81. helpers.warn('knex.VERSION is deprecated, you can get the module version' + "by running require('knex/package').version");
  82. return VERSION;
  83. }
  84. },
  85. schema: {
  86. get: function get() {
  87. return client.schemaBuilder();
  88. }
  89. },
  90. migrate: {
  91. get: function get() {
  92. return new _migrate2.default(knex);
  93. }
  94. },
  95. seed: {
  96. get: function get() {
  97. return new _seed2.default(knex);
  98. }
  99. },
  100. fn: {
  101. get: function get() {
  102. return new _functionhelper2.default(client);
  103. }
  104. }
  105. });
  106. // Passthrough all "start" and "query" events to the knex object.
  107. client.on('start', function (obj) {
  108. knex.emit('start', obj);
  109. });
  110. client.on('query', function (obj) {
  111. knex.emit('query', obj);
  112. });
  113. client.on('query-error', function (err, obj) {
  114. knex.emit('query-error', err, obj);
  115. });
  116. client.on('query-response', function (response, obj, builder) {
  117. knex.emit('query-response', response, obj, builder);
  118. });
  119. client.makeKnex = makeKnex;
  120. return knex;
  121. }
  122. module.exports = exports['default'];