index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _assign2 = require('lodash/assign');
  4. var _assign3 = _interopRequireDefault(_assign2);
  5. var _map2 = require('lodash/map');
  6. var _map3 = _interopRequireDefault(_map2);
  7. var _isUndefined2 = require('lodash/isUndefined');
  8. var _isUndefined3 = _interopRequireDefault(_isUndefined2);
  9. var _bluebird = require('bluebird');
  10. var _bluebird2 = _interopRequireDefault(_bluebird);
  11. var _inherits = require('inherits');
  12. var _inherits2 = _interopRequireDefault(_inherits);
  13. var _client = require('../../client');
  14. var _client2 = _interopRequireDefault(_client);
  15. var _helpers = require('../../helpers');
  16. var helpers = _interopRequireWildcard(_helpers);
  17. var _compiler = require('./query/compiler');
  18. var _compiler2 = _interopRequireDefault(_compiler);
  19. var _compiler3 = require('./schema/compiler');
  20. var _compiler4 = _interopRequireDefault(_compiler3);
  21. var _columncompiler = require('./schema/columncompiler');
  22. var _columncompiler2 = _interopRequireDefault(_columncompiler);
  23. var _tablecompiler = require('./schema/tablecompiler');
  24. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  25. var _ddl = require('./schema/ddl');
  26. var _ddl2 = _interopRequireDefault(_ddl);
  27. 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; } }
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. function Client_SQLite3(config) {
  30. _client2.default.call(this, config);
  31. if ((0, _isUndefined3.default)(config.useNullAsDefault)) {
  32. helpers.warn('sqlite does not support inserting default values. Set the ' + '`useNullAsDefault` flag to hide this warning. ' + '(see docs http://knexjs.org/#Builder-insert).');
  33. }
  34. }
  35. // SQLite3
  36. // -------
  37. (0, _inherits2.default)(Client_SQLite3, _client2.default);
  38. (0, _assign3.default)(Client_SQLite3.prototype, {
  39. dialect: 'sqlite3',
  40. driverName: 'sqlite3',
  41. _driver: function _driver() {
  42. return require('sqlite3');
  43. },
  44. schemaCompiler: function schemaCompiler() {
  45. return new (Function.prototype.bind.apply(_compiler4.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  46. },
  47. queryCompiler: function queryCompiler() {
  48. return new (Function.prototype.bind.apply(_compiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  49. },
  50. columnCompiler: function columnCompiler() {
  51. return new (Function.prototype.bind.apply(_columncompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  52. },
  53. tableCompiler: function tableCompiler() {
  54. return new (Function.prototype.bind.apply(_tablecompiler2.default, [null].concat([this], Array.prototype.slice.call(arguments))))();
  55. },
  56. ddl: function ddl(compiler, pragma, connection) {
  57. return new _ddl2.default(this, compiler, pragma, connection);
  58. },
  59. // Get a raw connection from the database, returning a promise with the connection object.
  60. acquireRawConnection: function acquireRawConnection() {
  61. var _this = this;
  62. return new _bluebird2.default(function (resolve, reject) {
  63. var db = new _this.driver.Database(_this.connectionSettings.filename, function (err) {
  64. if (err) {
  65. return reject(err);
  66. }
  67. resolve(db);
  68. });
  69. });
  70. },
  71. // Used to explicitly close a connection, called internally by the pool when
  72. // a connection times out or the pool is shutdown.
  73. destroyRawConnection: function destroyRawConnection(connection) {
  74. var _this2 = this;
  75. connection.close(function (err) {
  76. if (err) {
  77. _this2.emit('error', err);
  78. }
  79. });
  80. },
  81. // Runs the query on the specified connection, providing the bindings and any
  82. // other necessary prep work.
  83. _query: function _query(connection, obj) {
  84. var method = obj.method;
  85. var callMethod = void 0;
  86. switch (method) {
  87. case 'insert':
  88. case 'update':
  89. case 'counter':
  90. case 'del':
  91. callMethod = 'run';
  92. break;
  93. default:
  94. callMethod = 'all';
  95. }
  96. return new _bluebird2.default(function (resolver, rejecter) {
  97. if (!connection || !connection[callMethod]) {
  98. return rejecter(new Error('Error calling ' + callMethod + ' on connection.'));
  99. }
  100. connection[callMethod](obj.sql, obj.bindings, function (err, response) {
  101. if (err) return rejecter(err);
  102. obj.response = response;
  103. // We need the context here, as it contains
  104. // the "this.lastID" or "this.changes"
  105. obj.context = this;
  106. return resolver(obj);
  107. });
  108. });
  109. },
  110. _stream: function _stream(connection, sql, stream) {
  111. var client = this;
  112. return new _bluebird2.default(function (resolver, rejecter) {
  113. stream.on('error', rejecter);
  114. stream.on('end', resolver);
  115. return client._query(connection, sql).then(function (obj) {
  116. return obj.response;
  117. }).map(function (row) {
  118. stream.write(row);
  119. }).catch(function (err) {
  120. stream.emit('error', err);
  121. }).then(function () {
  122. stream.end();
  123. });
  124. });
  125. },
  126. // Ensures the response is returned in the same format as other clients.
  127. processResponse: function processResponse(obj, runner) {
  128. var ctx = obj.context;
  129. var response = obj.response;
  130. if (obj.output) return obj.output.call(runner, response);
  131. switch (obj.method) {
  132. case 'select':
  133. case 'pluck':
  134. case 'first':
  135. response = helpers.skim(response);
  136. if (obj.method === 'pluck') response = (0, _map3.default)(response, obj.pluck);
  137. return obj.method === 'first' ? response[0] : response;
  138. case 'insert':
  139. return [ctx.lastID];
  140. case 'del':
  141. case 'update':
  142. case 'counter':
  143. return ctx.changes;
  144. default:
  145. return response;
  146. }
  147. },
  148. poolDefaults: function poolDefaults(config) {
  149. return (0, _assign3.default)(_client2.default.prototype.poolDefaults.call(this, config), {
  150. min: 1,
  151. max: 1
  152. });
  153. }
  154. });
  155. exports.default = Client_SQLite3;
  156. module.exports = exports['default'];