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