helpers.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _isTypedArray2 = require('lodash/isTypedArray');
  4. var _isTypedArray3 = _interopRequireDefault(_isTypedArray2);
  5. var _isArray2 = require('lodash/isArray');
  6. var _isArray3 = _interopRequireDefault(_isArray2);
  7. var _isPlainObject2 = require('lodash/isPlainObject');
  8. var _isPlainObject3 = _interopRequireDefault(_isPlainObject2);
  9. var _isUndefined2 = require('lodash/isUndefined');
  10. var _isUndefined3 = _interopRequireDefault(_isUndefined2);
  11. var _isFunction2 = require('lodash/isFunction');
  12. var _isFunction3 = _interopRequireDefault(_isFunction2);
  13. var _keys2 = require('lodash/keys');
  14. var _keys3 = _interopRequireDefault(_keys2);
  15. var _pick2 = require('lodash/pick');
  16. var _pick3 = _interopRequireDefault(_pick2);
  17. var _map2 = require('lodash/map');
  18. var _map3 = _interopRequireDefault(_map2);
  19. exports.skim = skim;
  20. exports.normalizeArr = normalizeArr;
  21. exports.debugLog = debugLog;
  22. exports.error = error;
  23. exports.deprecate = deprecate;
  24. exports.warn = warn;
  25. exports.exit = exit;
  26. exports.containsUndefined = containsUndefined;
  27. exports.addQueryContext = addQueryContext;
  28. var _chalk = require('chalk');
  29. var _chalk2 = _interopRequireDefault(_chalk);
  30. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  31. // Pick off the attributes from only the current layer of the object.
  32. function skim(data) {
  33. return (0, _map3.default)(data, function (obj) {
  34. return (0, _pick3.default)(obj, (0, _keys3.default)(obj));
  35. });
  36. }
  37. // Check if the first argument is an array, otherwise uses all arguments as an
  38. // array.
  39. /* eslint no-console:0 */
  40. function normalizeArr() {
  41. var args = new Array(arguments.length);
  42. for (var i = 0; i < args.length; i++) {
  43. args[i] = arguments[i];
  44. }
  45. if (Array.isArray(args[0])) {
  46. return args[0];
  47. }
  48. return args;
  49. }
  50. function debugLog(msg) {
  51. console.log(msg);
  52. }
  53. function error(msg) {
  54. console.log(_chalk2.default.red('Knex:Error ' + msg));
  55. }
  56. // Used to signify deprecated functionality.
  57. function deprecate(method, alternate) {
  58. warn(method + ' is deprecated, please use ' + alternate);
  59. }
  60. // Used to warn about incorrect use, without error'ing
  61. function warn(msg) {
  62. console.log(_chalk2.default.yellow('Knex:warning - ' + msg));
  63. }
  64. function exit(msg) {
  65. console.log(_chalk2.default.red(msg));
  66. process.exit(1);
  67. }
  68. function containsUndefined(mixed) {
  69. var argContainsUndefined = false;
  70. if ((0, _isTypedArray3.default)(mixed)) return false;
  71. if (mixed && (0, _isFunction3.default)(mixed.toSQL)) {
  72. //Any QueryBuilder or Raw will automatically be validated during compile.
  73. return argContainsUndefined;
  74. }
  75. if ((0, _isArray3.default)(mixed)) {
  76. for (var i = 0; i < mixed.length; i++) {
  77. if (argContainsUndefined) break;
  78. argContainsUndefined = this.containsUndefined(mixed[i]);
  79. }
  80. } else if ((0, _isPlainObject3.default)(mixed)) {
  81. for (var key in mixed) {
  82. if (mixed.hasOwnProperty(key)) {
  83. if (argContainsUndefined) break;
  84. argContainsUndefined = this.containsUndefined(mixed[key]);
  85. }
  86. }
  87. } else {
  88. argContainsUndefined = (0, _isUndefined3.default)(mixed);
  89. }
  90. return argContainsUndefined;
  91. }
  92. function addQueryContext(Target) {
  93. // Stores or returns (if called with no arguments) context passed to
  94. // wrapIdentifier and postProcessResponse hooks
  95. Target.prototype.queryContext = function (context) {
  96. if ((0, _isUndefined3.default)(context)) {
  97. return this._queryContext;
  98. }
  99. this._queryContext = context;
  100. return this;
  101. };
  102. }