UNPKG

4.08 kBJavaScriptView Raw
1'use strict';
2
3var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
4
5var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9/* eslint no-console: 0 */
10
11// Helpers
12// ---------------
13var _ = require('lodash');
14var chalk = require('chalk');
15
16var helpers = {
17
18 // Sets the constraints necessary during a `model.save` call.
19 saveConstraints: function saveConstraints(model, relatedData) {
20 var data = {};
21 if (relatedData && !relatedData.isThrough() && relatedData.type !== 'belongsToMany' && relatedData.type !== 'belongsTo') {
22 data[relatedData.key('foreignKey')] = relatedData.parentFk || model.get(relatedData.key('foreignKey'));
23 if (relatedData.isMorph()) data[relatedData.key('morphKey')] = relatedData.key('morphValue');
24 }
25 return model.set(model.parse(data));
26 },
27
28 // Finds the specific `morphTo` table we should be working with, or throws
29 // an error if none is matched.
30 morphCandidate: function morphCandidate(candidates, morphValue) {
31 var _$find = _.find(candidates, function (_ref) {
32 var _ref2 = (0, _slicedToArray3.default)(_ref, 2),
33 tableName = _ref2[1];
34
35 return tableName === morphValue;
36 }),
37 _$find2 = (0, _slicedToArray3.default)(_$find, 1),
38 Target = _$find2[0];
39
40 if (!Target) {
41 throw new Error('The target polymorphic model was not found');
42 }
43 return Target;
44 },
45
46 // If there are no arguments, return the current object's
47 // query builder (or create and return a new one). If there are arguments,
48 // call the query builder with the first argument, applying the rest.
49 // If the first argument is an object, assume the keys are query builder
50 // methods, and the values are the arguments for the query.
51 query: function query(obj, args) {
52
53 // Ensure the object has a query builder.
54 if (!obj._knex) {
55 var tableName = _.result(obj, 'tableName');
56 obj._knex = obj._builder(tableName);
57 }
58
59 // If there are no arguments, return the query builder.
60 if (args.length === 0) return obj._knex;
61
62 var method = args[0];
63
64 if (_.isFunction(method)) {
65
66 // `method` is a query builder callback. Call it on the query builder
67 // object.
68 method.call(obj._knex, obj._knex);
69 } else if (_.isObject(method)) {
70
71 // `method` is an object. Use keys as methods and values as arguments to
72 // the query builder.
73 for (var key in method) {
74 var target = _.isArray(method[key]) ? method[key] : [method[key]];
75 obj._knex[key].apply(obj._knex, target);
76 }
77 } else {
78
79 // Otherwise assume that the `method` is string name of a query builder
80 // method, and use the remaining args as arguments to that method.
81 obj._knex[method].apply(obj._knex, args.slice(1));
82 }
83 return obj;
84 },
85
86 error: function error(msg) {
87 console.log(chalk.red(msg));
88 },
89
90 warn: function warn(msg) {
91 console.log(chalk.yellow(msg));
92 },
93
94 deprecate: function deprecate(a, b) {
95 helpers.warn(a + ' has been deprecated, please use ' + b + ' instead');
96 },
97
98 orderBy: function orderBy(obj, sort, order) {
99
100 var tableName = void 0;
101 var idAttribute = void 0;
102
103 if (obj.model) {
104 tableName = obj.model.prototype.tableName;
105 idAttribute = obj.model.prototype.idAttribute ? obj.model.prototype.idAttribute : 'id';
106 } else {
107 tableName = obj.constructor.prototype.tableName;
108 idAttribute = obj.constructor.prototype.idAttribute ? obj.constructor.prototype.idAttribute : 'id';
109 }
110
111 var _sort = void 0;
112
113 if (sort && sort.indexOf('-') === 0) {
114 _sort = sort.slice(1);
115 } else if (sort) {
116 _sort = sort;
117 } else {
118 _sort = idAttribute;
119 }
120
121 var _order = order || (sort && sort.indexOf('-') === 0 ? 'DESC' : 'ASC');
122
123 if (_sort.indexOf('.') === -1) {
124 _sort = tableName + '.' + _sort;
125 }
126
127 return obj.query(function (qb) {
128 qb.orderBy(_sort, _order);
129 });
130 }
131};
132
133module.exports = helpers;
\No newline at end of file