forest-express-sequelize 9.2.0 → 9.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/adapters/sequelize.js +6 -31
- package/dist/index.js +2 -50
- package/dist/public/parse-filter.js +0 -1
- package/dist/services/apimap-field-builder.js +12 -40
- package/dist/services/apimap-field-type-detector.js +2 -15
- package/dist/services/belongs-to-updater.js +5 -16
- package/dist/services/errors.js +0 -5
- package/dist/services/filters-parser.js +9 -55
- package/dist/services/has-many-associator.js +3 -6
- package/dist/services/has-many-dissociator.js +0 -13
- package/dist/services/has-many-getter.js +2 -22
- package/dist/services/leaderboard-stat-getter.js +1 -22
- package/dist/services/line-stat-getter.js +2 -59
- package/dist/services/pie-stat-getter.js +0 -33
- package/dist/services/primary-keys-manager.js +5 -28
- package/dist/services/query-builder.js +4 -13
- package/dist/services/query-options.js +21 -78
- package/dist/services/query-stat-getter.js +3 -8
- package/dist/services/requested-fields-extractor.js +9 -14
- package/dist/services/resource-creator.js +10 -37
- package/dist/services/resource-getter.js +0 -11
- package/dist/services/resource-remover.js +0 -4
- package/dist/services/resource-updater.js +0 -15
- package/dist/services/resources-exporter.js +0 -13
- package/dist/services/resources-getter.js +8 -28
- package/dist/services/resources-remover.js +0 -9
- package/dist/services/search-builder.js +6 -71
- package/dist/services/value-stat-getter.js +7 -30
- package/dist/utils/association-record.js +0 -6
- package/dist/utils/database.js +0 -3
- package/dist/utils/object-tools.js +1 -8
- package/dist/utils/operators.js +0 -9
- package/dist/utils/orm.js +0 -12
- package/dist/utils/query.js +5 -16
- package/dist/utils/records-decorator.js +0 -10
- package/dist/utils/sequelize-compatibility.js +9 -38
- package/package.json +1 -1
|
@@ -1,169 +1,121 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.regexp.exec.js");
|
|
8
|
-
|
|
9
6
|
require("core-js/modules/es.string.replace.js");
|
|
10
|
-
|
|
11
7
|
require("core-js/modules/es.array.iterator.js");
|
|
12
|
-
|
|
13
8
|
require("core-js/modules/es.promise.js");
|
|
14
|
-
|
|
15
9
|
var _forestExpress = require("forest-express");
|
|
16
|
-
|
|
17
10
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
18
|
-
|
|
19
11
|
var _moment = _interopRequireDefault(require("moment"));
|
|
20
|
-
|
|
21
12
|
var _database = require("../utils/database");
|
|
22
|
-
|
|
23
13
|
var _orm = _interopRequireDefault(require("../utils/orm"));
|
|
24
|
-
|
|
25
14
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
26
|
-
|
|
27
15
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
28
|
-
|
|
29
16
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
30
|
-
|
|
31
17
|
function LineStatGetter(model, params, options, user) {
|
|
32
18
|
const schema = _forestExpress.Schemas.schemas[model.name];
|
|
33
19
|
const timeRange = params.timeRange.toLowerCase();
|
|
34
|
-
|
|
35
20
|
function getAggregateField() {
|
|
36
21
|
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
|
|
37
22
|
const fieldName = params.aggregateFieldName || schema.primaryKeys[0] || schema.fields[0].field;
|
|
38
23
|
return `${schema.name}.${_orm.default.getColumnName(schema, fieldName)}`;
|
|
39
24
|
}
|
|
40
|
-
|
|
41
25
|
function getGroupByDateField() {
|
|
42
26
|
return `${schema.name}.${_orm.default.getColumnName(schema, params.groupByFieldName)}`;
|
|
43
27
|
}
|
|
44
|
-
|
|
45
28
|
const groupByDateField = getGroupByDateField();
|
|
46
|
-
|
|
47
29
|
function getGroupByDateFieldFormatedForMySQL(currentTimeRange) {
|
|
48
30
|
const groupByDateFieldFormated = `\`${groupByDateField.replace('.', '`.`')}\``;
|
|
49
|
-
|
|
50
31
|
switch (currentTimeRange) {
|
|
51
32
|
case 'day':
|
|
52
33
|
return options.Sequelize.fn('DATE_FORMAT', options.Sequelize.col(groupByDateField), '%Y-%m-%d 00:00:00');
|
|
53
|
-
|
|
54
34
|
case 'week':
|
|
55
35
|
return options.Sequelize.literal(`DATE_FORMAT(DATE_SUB(${groupByDateFieldFormated}, \
|
|
56
36
|
INTERVAL ((7 + WEEKDAY(${groupByDateFieldFormated})) % 7) DAY), '%Y-%m-%d 00:00:00')`);
|
|
57
|
-
|
|
58
37
|
case 'month':
|
|
59
38
|
return options.Sequelize.fn('DATE_FORMAT', options.Sequelize.col(groupByDateField), '%Y-%m-01 00:00:00');
|
|
60
|
-
|
|
61
39
|
case 'year':
|
|
62
40
|
return options.Sequelize.fn('DATE_FORMAT', options.Sequelize.col(groupByDateField), '%Y-01-01 00:00:00');
|
|
63
|
-
|
|
64
41
|
default:
|
|
65
42
|
return null;
|
|
66
43
|
}
|
|
67
44
|
}
|
|
68
|
-
|
|
69
45
|
function getGroupByDateFieldFormatedForMSSQL(currentTimeRange) {
|
|
70
46
|
const groupByDateFieldFormated = `[${groupByDateField.replace('.', '].[')}]`;
|
|
71
|
-
|
|
72
47
|
switch (currentTimeRange) {
|
|
73
48
|
case 'day':
|
|
74
49
|
return options.Sequelize.fn('FORMAT', options.Sequelize.col(groupByDateField), 'yyyy-MM-dd 00:00:00');
|
|
75
|
-
|
|
76
50
|
case 'week':
|
|
77
51
|
return options.Sequelize.literal(`FORMAT(DATEADD(DAY, -DATEPART(dw,${groupByDateFieldFormated}),\
|
|
78
52
|
${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
79
|
-
|
|
80
53
|
case 'month':
|
|
81
54
|
return options.Sequelize.fn('FORMAT', options.Sequelize.col(groupByDateField), 'yyyy-MM-01 00:00:00');
|
|
82
|
-
|
|
83
55
|
case 'year':
|
|
84
56
|
return options.Sequelize.fn('FORMAT', options.Sequelize.col(groupByDateField), 'yyyy-01-01 00:00:00');
|
|
85
|
-
|
|
86
57
|
default:
|
|
87
58
|
return null;
|
|
88
59
|
}
|
|
89
60
|
}
|
|
90
|
-
|
|
91
61
|
function getGroupByDateFieldFormatedForSQLite(currentTimeRange) {
|
|
92
62
|
switch (currentTimeRange) {
|
|
93
63
|
case 'day':
|
|
94
64
|
{
|
|
95
65
|
return options.Sequelize.fn('STRFTIME', '%Y-%m-%d', options.Sequelize.col(groupByDateField));
|
|
96
66
|
}
|
|
97
|
-
|
|
98
67
|
case 'week':
|
|
99
68
|
{
|
|
100
69
|
return options.Sequelize.fn('STRFTIME', '%Y-%W', options.Sequelize.col(groupByDateField));
|
|
101
70
|
}
|
|
102
|
-
|
|
103
71
|
case 'month':
|
|
104
72
|
{
|
|
105
73
|
return options.Sequelize.fn('STRFTIME', '%Y-%m-01', options.Sequelize.col(groupByDateField));
|
|
106
74
|
}
|
|
107
|
-
|
|
108
75
|
case 'year':
|
|
109
76
|
{
|
|
110
77
|
return options.Sequelize.fn('STRFTIME', '%Y-01-01', options.Sequelize.col(groupByDateField));
|
|
111
78
|
}
|
|
112
|
-
|
|
113
79
|
default:
|
|
114
80
|
return null;
|
|
115
81
|
}
|
|
116
82
|
}
|
|
117
|
-
|
|
118
83
|
function getGroupByDateInterval() {
|
|
119
84
|
if ((0, _database.isMySQL)(model.sequelize)) {
|
|
120
85
|
return [getGroupByDateFieldFormatedForMySQL(timeRange), 'date'];
|
|
121
86
|
}
|
|
122
|
-
|
|
123
87
|
if ((0, _database.isMSSQL)(model.sequelize)) {
|
|
124
88
|
return [getGroupByDateFieldFormatedForMSSQL(timeRange), 'date'];
|
|
125
89
|
}
|
|
126
|
-
|
|
127
90
|
if ((0, _database.isSQLite)(model.sequelize)) {
|
|
128
91
|
return [getGroupByDateFieldFormatedForSQLite(timeRange), 'date'];
|
|
129
92
|
}
|
|
130
|
-
|
|
131
93
|
return [options.Sequelize.fn('to_char', options.Sequelize.fn('date_trunc', params.timeRange, options.Sequelize.literal(`"${getGroupByDateField().replace('.', '"."')}" at time zone '${params.timezone}'`)), 'YYYY-MM-DD 00:00:00'), 'date'];
|
|
132
94
|
}
|
|
133
|
-
|
|
134
95
|
function getFormat() {
|
|
135
96
|
switch (timeRange) {
|
|
136
97
|
case 'day':
|
|
137
98
|
return 'DD/MM/YYYY';
|
|
138
|
-
|
|
139
99
|
case 'week':
|
|
140
100
|
return '[W]W-YYYY';
|
|
141
|
-
|
|
142
101
|
case 'month':
|
|
143
102
|
return 'MMM YY';
|
|
144
|
-
|
|
145
103
|
case 'year':
|
|
146
104
|
return 'YYYY';
|
|
147
|
-
|
|
148
105
|
default:
|
|
149
106
|
return null;
|
|
150
107
|
}
|
|
151
108
|
}
|
|
152
|
-
|
|
153
109
|
function fillEmptyDateInterval(records) {
|
|
154
110
|
if (records.length) {
|
|
155
111
|
let sqlFormat = 'YYYY-MM-DD 00:00:00';
|
|
156
|
-
|
|
157
112
|
if ((0, _database.isSQLite)(model.sequelize) && timeRange === 'week') {
|
|
158
113
|
sqlFormat = 'YYYY-WW';
|
|
159
114
|
}
|
|
160
|
-
|
|
161
115
|
const firstDate = (0, _moment.default)(records[0].label, sqlFormat);
|
|
162
116
|
const lastDate = (0, _moment.default)(records[records.length - 1].label, sqlFormat);
|
|
163
|
-
|
|
164
117
|
for (let i = firstDate; i.toDate() <= lastDate.toDate(); i = i.add(1, timeRange)) {
|
|
165
118
|
const label = i.format(sqlFormat);
|
|
166
|
-
|
|
167
119
|
if (!_lodash.default.find(records, {
|
|
168
120
|
label
|
|
169
121
|
})) {
|
|
@@ -175,7 +127,6 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
175
127
|
});
|
|
176
128
|
}
|
|
177
129
|
}
|
|
178
|
-
|
|
179
130
|
records = _lodash.default.sortBy(records, 'label');
|
|
180
131
|
return _lodash.default.map(records, function (record) {
|
|
181
132
|
return {
|
|
@@ -184,22 +135,17 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
184
135
|
};
|
|
185
136
|
});
|
|
186
137
|
}
|
|
187
|
-
|
|
188
138
|
return records;
|
|
189
139
|
}
|
|
190
|
-
|
|
191
140
|
function getAggregate() {
|
|
192
141
|
return [options.Sequelize.fn(params.aggregator.toLowerCase(), options.Sequelize.col(getAggregateField())), 'value'];
|
|
193
142
|
}
|
|
194
|
-
|
|
195
143
|
function getGroupBy() {
|
|
196
144
|
return (0, _database.isMSSQL)(model.sequelize) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.Sequelize.literal('1')];
|
|
197
145
|
}
|
|
198
|
-
|
|
199
146
|
function getOrder() {
|
|
200
147
|
return (0, _database.isMSSQL)(model.sequelize) ? [getGroupByDateFieldFormatedForMSSQL(timeRange)] : [options.Sequelize.literal('1')];
|
|
201
148
|
}
|
|
202
|
-
|
|
203
149
|
this.perform = async function () {
|
|
204
150
|
const {
|
|
205
151
|
filter,
|
|
@@ -211,15 +157,14 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
211
157
|
});
|
|
212
158
|
await queryOptions.filterByConditionTree(filter, timezone);
|
|
213
159
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
214
|
-
|
|
215
160
|
const sequelizeOptions = _objectSpread(_objectSpread({}, queryOptions.sequelizeOptions), {}, {
|
|
216
161
|
attributes: [getGroupByDateInterval(), getAggregate()],
|
|
217
162
|
group: getGroupBy(),
|
|
218
163
|
order: getOrder(),
|
|
219
164
|
raw: true
|
|
220
|
-
});
|
|
221
|
-
|
|
165
|
+
});
|
|
222
166
|
|
|
167
|
+
// do not load related properties
|
|
223
168
|
if (sequelizeOptions.include) {
|
|
224
169
|
sequelizeOptions.include = sequelizeOptions.include.map(function (includeProperties) {
|
|
225
170
|
return _objectSpread(_objectSpread({}, includeProperties), {}, {
|
|
@@ -227,7 +172,6 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
227
172
|
});
|
|
228
173
|
});
|
|
229
174
|
}
|
|
230
|
-
|
|
231
175
|
const records = await model.unscoped().findAll(sequelizeOptions);
|
|
232
176
|
return {
|
|
233
177
|
value: fillEmptyDateInterval(records.map(function (record) {
|
|
@@ -241,5 +185,4 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
241
185
|
};
|
|
242
186
|
};
|
|
243
187
|
}
|
|
244
|
-
|
|
245
188
|
module.exports = LineStatGetter;
|
|
@@ -1,39 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.regexp.exec.js");
|
|
8
|
-
|
|
9
6
|
require("core-js/modules/es.array.iterator.js");
|
|
10
|
-
|
|
11
7
|
require("core-js/modules/es.promise.js");
|
|
12
|
-
|
|
13
8
|
var _forestExpress = require("forest-express");
|
|
14
|
-
|
|
15
9
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
16
|
-
|
|
17
10
|
var _moment = _interopRequireDefault(require("moment"));
|
|
18
|
-
|
|
19
11
|
var _database = require("../utils/database");
|
|
20
|
-
|
|
21
12
|
var _orm = _interopRequireWildcard(require("../utils/orm"));
|
|
22
|
-
|
|
23
13
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
24
|
-
|
|
25
14
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
26
|
-
|
|
27
15
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
28
|
-
|
|
29
16
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
30
|
-
|
|
31
17
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
32
|
-
|
|
33
18
|
// NOTICE: These aliases are not camelcased to prevent issues with Sequelize.
|
|
34
19
|
const ALIAS_GROUP_BY = 'forest_alias_groupby';
|
|
35
20
|
const ALIAS_AGGREGATE = 'forest_alias_aggregate';
|
|
36
|
-
|
|
37
21
|
function PieStatGetter(model, params, options, user) {
|
|
38
22
|
const needsDateOnlyFormating = (0, _orm.isVersionLessThan)(options.Sequelize, '4.0.0');
|
|
39
23
|
const schema = _forestExpress.Schemas.schemas[model.name];
|
|
@@ -42,7 +26,6 @@ function PieStatGetter(model, params, options, user) {
|
|
|
42
26
|
let associationField;
|
|
43
27
|
let associationSchema;
|
|
44
28
|
let field;
|
|
45
|
-
|
|
46
29
|
if (params.groupByFieldName.indexOf(':') === -1) {
|
|
47
30
|
field = _lodash.default.find(schema.fields, function (currentField) {
|
|
48
31
|
return currentField.field === params.groupByFieldName;
|
|
@@ -56,55 +39,43 @@ function PieStatGetter(model, params, options, user) {
|
|
|
56
39
|
return currentField.field === associationField;
|
|
57
40
|
});
|
|
58
41
|
}
|
|
59
|
-
|
|
60
42
|
function getGroupByField() {
|
|
61
43
|
if (params.groupByFieldName.includes(':')) {
|
|
62
44
|
const [associationName, fieldName] = params.groupByFieldName.split(':');
|
|
63
45
|
return `${associationName}.${_orm.default.getColumnName(associationSchema, fieldName)}`;
|
|
64
46
|
}
|
|
65
|
-
|
|
66
47
|
return `${schema.name}.${_orm.default.getColumnName(schema, params.groupByFieldName)}`;
|
|
67
48
|
}
|
|
68
|
-
|
|
69
49
|
const groupByField = getGroupByField();
|
|
70
|
-
|
|
71
50
|
function getAggregate() {
|
|
72
51
|
return params.aggregator.toLowerCase();
|
|
73
52
|
}
|
|
74
|
-
|
|
75
53
|
function getAggregateField() {
|
|
76
54
|
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
|
|
77
55
|
const fieldName = params.aggregateFieldName || schema.primaryKeys[0] || schema.fields[0].field;
|
|
78
56
|
return `${schema.name}.${_orm.default.getColumnName(schema, fieldName)}`;
|
|
79
57
|
}
|
|
80
|
-
|
|
81
58
|
function getGroupBy() {
|
|
82
59
|
return (0, _database.isMSSQL)(model.sequelize) ? [options.Sequelize.col(groupByField)] : [ALIAS_GROUP_BY];
|
|
83
60
|
}
|
|
84
|
-
|
|
85
61
|
function formatResults(records) {
|
|
86
62
|
return records.map(function (record) {
|
|
87
63
|
let key;
|
|
88
|
-
|
|
89
64
|
if (field.type === 'Date') {
|
|
90
65
|
key = (0, _moment.default)(record[ALIAS_GROUP_BY]).format('DD/MM/YYYY HH:mm:ss');
|
|
91
66
|
} else if (field.type === 'Dateonly' && needsDateOnlyFormating) {
|
|
92
67
|
const offsetServer = (0, _moment.default)().utcOffset() / 60;
|
|
93
|
-
|
|
94
68
|
const dateonly = _moment.default.utc(record[ALIAS_GROUP_BY]).add(offsetServer, 'h');
|
|
95
|
-
|
|
96
69
|
key = dateonly.format('DD/MM/YYYY');
|
|
97
70
|
} else {
|
|
98
71
|
key = String(record[ALIAS_GROUP_BY]);
|
|
99
72
|
}
|
|
100
|
-
|
|
101
73
|
return {
|
|
102
74
|
key,
|
|
103
75
|
value: record[ALIAS_AGGREGATE]
|
|
104
76
|
};
|
|
105
77
|
});
|
|
106
78
|
}
|
|
107
|
-
|
|
108
79
|
this.perform = async function () {
|
|
109
80
|
const {
|
|
110
81
|
filter,
|
|
@@ -116,14 +87,12 @@ function PieStatGetter(model, params, options, user) {
|
|
|
116
87
|
});
|
|
117
88
|
await queryOptions.filterByConditionTree(filter, timezone);
|
|
118
89
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
119
|
-
|
|
120
90
|
const sequelizeOptions = _objectSpread(_objectSpread({}, queryOptions.sequelizeOptions), {}, {
|
|
121
91
|
attributes: [[options.Sequelize.col(groupByField), ALIAS_GROUP_BY], [options.Sequelize.fn(getAggregate(), options.Sequelize.col(getAggregateField())), ALIAS_AGGREGATE]],
|
|
122
92
|
group: getGroupBy(),
|
|
123
93
|
order: [[options.Sequelize.literal(ALIAS_AGGREGATE), 'DESC']],
|
|
124
94
|
raw: true
|
|
125
95
|
});
|
|
126
|
-
|
|
127
96
|
if (sequelizeOptions.include) {
|
|
128
97
|
sequelizeOptions.include = sequelizeOptions.include.map(function (includeProperties) {
|
|
129
98
|
return _objectSpread(_objectSpread({}, includeProperties), {}, {
|
|
@@ -131,12 +100,10 @@ function PieStatGetter(model, params, options, user) {
|
|
|
131
100
|
});
|
|
132
101
|
});
|
|
133
102
|
}
|
|
134
|
-
|
|
135
103
|
const records = await model.unscoped().findAll(sequelizeOptions);
|
|
136
104
|
return {
|
|
137
105
|
value: formatResults(records)
|
|
138
106
|
};
|
|
139
107
|
};
|
|
140
108
|
}
|
|
141
|
-
|
|
142
109
|
module.exports = PieStatGetter;
|
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.array.iterator.js");
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.regexp.exec.js");
|
|
8
|
-
|
|
9
6
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
10
|
-
|
|
11
7
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
12
|
-
|
|
13
8
|
var _operators = _interopRequireDefault(require("../utils/operators"));
|
|
14
|
-
|
|
15
9
|
/**
|
|
16
10
|
* This helper class allows abstracting away the complexity
|
|
17
11
|
* of using collection which have composite primary keys.
|
|
@@ -21,89 +15,72 @@ class PrimaryKeysManager {
|
|
|
21
15
|
this._primaryKeys = _lodash.default.keys(model.primaryKeys);
|
|
22
16
|
this._Sequelize = model.sequelize.constructor;
|
|
23
17
|
}
|
|
24
|
-
/** Build sequelize where condition from a list of packed recordIds */
|
|
25
|
-
|
|
26
18
|
|
|
19
|
+
/** Build sequelize where condition from a list of packed recordIds */
|
|
27
20
|
getRecordsConditions(recordIds) {
|
|
28
21
|
if (recordIds.length === 0) {
|
|
29
22
|
return this._Sequelize.literal('(0=1)');
|
|
30
23
|
}
|
|
31
|
-
|
|
32
24
|
switch (this._primaryKeys.length) {
|
|
33
25
|
case 0:
|
|
34
26
|
throw new Error('No primary key was found');
|
|
35
|
-
|
|
36
27
|
case 1:
|
|
37
28
|
return this._getRecordsConditionsSimple(recordIds);
|
|
38
|
-
|
|
39
29
|
default:
|
|
40
30
|
return this._getRecordsConditionComposite(recordIds);
|
|
41
31
|
}
|
|
42
32
|
}
|
|
43
|
-
/* Annotate records with their packed primary key */
|
|
44
|
-
|
|
45
33
|
|
|
34
|
+
/* Annotate records with their packed primary key */
|
|
46
35
|
annotateRecords(records) {
|
|
47
36
|
var _this = this;
|
|
48
|
-
|
|
49
37
|
if (this._primaryKeys.length > 1) {
|
|
50
38
|
records.forEach(function (record) {
|
|
51
39
|
record.forestCompositePrimary = _this._createCompositePrimary(record);
|
|
52
40
|
});
|
|
53
41
|
}
|
|
54
42
|
}
|
|
55
|
-
|
|
56
43
|
_getRecordsConditionsSimple(recordIds) {
|
|
57
44
|
return {
|
|
58
45
|
[this._primaryKeys[0]]: recordIds.length === 1 ? recordIds[0] : recordIds
|
|
59
46
|
};
|
|
60
47
|
}
|
|
61
|
-
|
|
62
48
|
_getRecordsConditionComposite(recordIds) {
|
|
63
49
|
var _this2 = this;
|
|
64
|
-
|
|
65
50
|
const Ops = _operators.default.getInstance({
|
|
66
51
|
Sequelize: this._Sequelize
|
|
67
52
|
});
|
|
68
|
-
|
|
69
53
|
return recordIds.length === 1 ? this._getRecordConditions(recordIds[0]) : {
|
|
70
54
|
[Ops.OR]: recordIds.map(function (id) {
|
|
71
55
|
return _this2._getRecordConditions(id);
|
|
72
56
|
})
|
|
73
57
|
};
|
|
74
58
|
}
|
|
75
|
-
/** Build sequelize where condition from a single packed recordId */
|
|
76
|
-
|
|
77
59
|
|
|
60
|
+
/** Build sequelize where condition from a single packed recordId */
|
|
78
61
|
_getRecordConditions(recordId) {
|
|
79
62
|
return _lodash.default.zipObject(this._primaryKeys, this._getPrimaryKeyValues(recordId));
|
|
80
63
|
}
|
|
81
|
-
/** Create packed recordId from record */
|
|
82
|
-
|
|
83
64
|
|
|
65
|
+
/** Create packed recordId from record */
|
|
84
66
|
_createCompositePrimary(record) {
|
|
85
67
|
return this._primaryKeys.map(function (field) {
|
|
86
68
|
return record[field] === null ? 'null' : record[field];
|
|
87
69
|
}).join(PrimaryKeysManager._GLUE);
|
|
88
70
|
}
|
|
89
|
-
/** Unpack recordId into an array */
|
|
90
|
-
|
|
91
71
|
|
|
72
|
+
/** Unpack recordId into an array */
|
|
92
73
|
_getPrimaryKeyValues(recordId) {
|
|
93
74
|
// Prevent liana to crash when a composite primary keys is null,
|
|
94
75
|
// this behaviour should be avoid instead of fixed.
|
|
95
76
|
const unpacked = recordId.split(PrimaryKeysManager._GLUE).map(function (key) {
|
|
96
77
|
return key === 'null' ? null : key;
|
|
97
78
|
});
|
|
98
|
-
|
|
99
79
|
if (unpacked.length !== this._primaryKeys.length) {
|
|
100
80
|
throw new Error('Invalid packed primary key');
|
|
101
81
|
}
|
|
102
|
-
|
|
103
82
|
return unpacked;
|
|
104
83
|
}
|
|
105
|
-
|
|
106
84
|
}
|
|
107
|
-
|
|
108
85
|
(0, _defineProperty2.default)(PrimaryKeysManager, "_GLUE", '|');
|
|
109
86
|
module.exports = PrimaryKeysManager;
|
|
@@ -1,55 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
require("core-js/modules/es.array.iterator.js");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.regexp.exec.js");
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.string.replace.js");
|
|
8
|
-
|
|
9
6
|
const HAS_ONE = 'HasOne';
|
|
10
7
|
const BELONGS_TO = 'BelongsTo';
|
|
8
|
+
|
|
11
9
|
/**
|
|
12
10
|
* @param {string[]} values
|
|
13
11
|
* @returns {string[]}
|
|
14
12
|
*/
|
|
15
|
-
|
|
16
13
|
function uniqueValues(values) {
|
|
17
14
|
return Array.from(new Set(values));
|
|
18
15
|
}
|
|
16
|
+
|
|
19
17
|
/**
|
|
20
18
|
* @param {string} key
|
|
21
19
|
* @param {import('sequelize').Association} association
|
|
22
20
|
* @returns {string}
|
|
23
21
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
22
|
function getTargetFieldName(key, association) {
|
|
27
23
|
// Defensive programming
|
|
28
24
|
if (key && association.target.tableAttributes[key]) {
|
|
29
25
|
return association.target.tableAttributes[key].fieldName;
|
|
30
26
|
}
|
|
31
|
-
|
|
32
27
|
return undefined;
|
|
33
28
|
}
|
|
29
|
+
|
|
34
30
|
/**
|
|
35
31
|
* @param {import('sequelize').HasOne|import('sequelize').BelongsTo} association
|
|
36
32
|
* @returns {string[]}
|
|
37
33
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
34
|
function getMandatoryFields(association) {
|
|
41
35
|
return association.target.primaryKeyAttributes.map(function (attribute) {
|
|
42
36
|
return getTargetFieldName(attribute, association);
|
|
43
37
|
});
|
|
44
38
|
}
|
|
39
|
+
|
|
45
40
|
/**
|
|
46
41
|
* Compute "includes" parameter which is expected by sequelize from a list of fields.
|
|
47
42
|
* The list of fields can contain fields from relations in the form 'author.firstname'
|
|
48
43
|
*
|
|
49
44
|
* @param {string[]} fieldNames model and relationship field names
|
|
50
45
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
46
|
function QueryBuilder() {
|
|
54
47
|
this.getIncludes = function (modelForIncludes, fieldNamesRequested) {
|
|
55
48
|
const includes = [];
|
|
@@ -66,7 +59,6 @@ function QueryBuilder() {
|
|
|
66
59
|
}).filter(function (fieldName) {
|
|
67
60
|
return targetFields.includes(fieldName);
|
|
68
61
|
});
|
|
69
|
-
|
|
70
62
|
if (fieldNamesRequested !== null && fieldNamesRequested !== void 0 && fieldNamesRequested.includes(association.as) || explicitAttributes.length) {
|
|
71
63
|
// NOTICE: For performance reasons, we only request the keys
|
|
72
64
|
// as they're the only needed fields for the interface
|
|
@@ -82,5 +74,4 @@ function QueryBuilder() {
|
|
|
82
74
|
return includes;
|
|
83
75
|
};
|
|
84
76
|
}
|
|
85
|
-
|
|
86
77
|
module.exports = QueryBuilder;
|