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,23 +1,14 @@
|
|
|
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.promise.js");
|
|
8
|
-
|
|
9
6
|
var _forestExpress = require("forest-express");
|
|
10
|
-
|
|
11
7
|
var _errors = require("./errors");
|
|
12
|
-
|
|
13
8
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
14
|
-
|
|
15
9
|
var _resourceGetter = _interopRequireDefault(require("./resource-getter"));
|
|
16
|
-
|
|
17
10
|
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; }
|
|
18
|
-
|
|
19
11
|
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; }
|
|
20
|
-
|
|
21
12
|
class ResourceUpdater {
|
|
22
13
|
constructor(model, params, newRecord, user) {
|
|
23
14
|
this._model = model.unscoped();
|
|
@@ -25,7 +16,6 @@ class ResourceUpdater {
|
|
|
25
16
|
this._newRecord = newRecord;
|
|
26
17
|
this._user = user;
|
|
27
18
|
}
|
|
28
|
-
|
|
29
19
|
async perform() {
|
|
30
20
|
const {
|
|
31
21
|
timezone
|
|
@@ -35,10 +25,8 @@ class ResourceUpdater {
|
|
|
35
25
|
await queryOptions.filterByIds([this._params.recordId]);
|
|
36
26
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
37
27
|
const record = await this._model.findOne(queryOptions.sequelizeOptions);
|
|
38
|
-
|
|
39
28
|
if (record) {
|
|
40
29
|
Object.assign(record, this._newRecord);
|
|
41
|
-
|
|
42
30
|
try {
|
|
43
31
|
await record.validate();
|
|
44
32
|
await record.save();
|
|
@@ -46,12 +34,9 @@ class ResourceUpdater {
|
|
|
46
34
|
throw new _errors.ErrorHTTP422(error.message);
|
|
47
35
|
}
|
|
48
36
|
}
|
|
49
|
-
|
|
50
37
|
return new _resourceGetter.default(this._model, _objectSpread(_objectSpread({}, this._params), {}, {
|
|
51
38
|
recordId: this._params.recordId
|
|
52
39
|
}), this._user).perform();
|
|
53
40
|
}
|
|
54
|
-
|
|
55
41
|
}
|
|
56
|
-
|
|
57
42
|
module.exports = ResourceUpdater;
|
|
@@ -1,34 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
require("core-js/modules/es.array.iterator.js");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.array.sort.js");
|
|
6
|
-
|
|
7
5
|
const _ = require('lodash');
|
|
8
|
-
|
|
9
6
|
const ResourcesGetter = require('./resources-getter');
|
|
10
|
-
|
|
11
7
|
const HasManyGetter = require('./has-many-getter');
|
|
12
|
-
|
|
13
8
|
const BATCH_INITIAL_PAGE = 1;
|
|
14
9
|
const BATCH_SIZE = 1000;
|
|
15
|
-
|
|
16
10
|
function ResourcesExporter(model, options, params, association, user) {
|
|
17
11
|
const primaryKeys = _.keys((association || model).primaryKeys);
|
|
18
|
-
|
|
19
12
|
params.sort = primaryKeys[0] || 'id';
|
|
20
13
|
params.page = {
|
|
21
14
|
size: BATCH_SIZE
|
|
22
15
|
};
|
|
23
|
-
|
|
24
16
|
function getter() {
|
|
25
17
|
if (association) {
|
|
26
18
|
return new HasManyGetter(model, association, options, params, user);
|
|
27
19
|
}
|
|
28
|
-
|
|
29
20
|
return new ResourcesGetter(model, options, params, user);
|
|
30
21
|
}
|
|
31
|
-
|
|
32
22
|
function retrieveBatch(dataSender, pageNumber) {
|
|
33
23
|
params.page.number = pageNumber;
|
|
34
24
|
return getter().perform().then(function (results) {
|
|
@@ -37,15 +27,12 @@ function ResourcesExporter(model, options, params, association, user) {
|
|
|
37
27
|
if (records.length === BATCH_SIZE) {
|
|
38
28
|
return retrieveBatch(dataSender, pageNumber + 1);
|
|
39
29
|
}
|
|
40
|
-
|
|
41
30
|
return null;
|
|
42
31
|
});
|
|
43
32
|
});
|
|
44
33
|
}
|
|
45
|
-
|
|
46
34
|
this.perform = function (dataSender) {
|
|
47
35
|
return retrieveBatch(dataSender, BATCH_INITIAL_PAGE);
|
|
48
36
|
};
|
|
49
37
|
}
|
|
50
|
-
|
|
51
38
|
module.exports = ResourcesExporter;
|
|
@@ -1,31 +1,18 @@
|
|
|
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.promise.js");
|
|
8
|
-
|
|
9
6
|
require("core-js/modules/es.array.iterator.js");
|
|
10
|
-
|
|
11
7
|
require("core-js/modules/es.regexp.exec.js");
|
|
12
|
-
|
|
13
8
|
require("core-js/modules/es.array.sort.js");
|
|
14
|
-
|
|
15
9
|
var _forestExpress = require("forest-express");
|
|
16
|
-
|
|
17
10
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
18
|
-
|
|
19
11
|
var _primaryKeysManager = _interopRequireDefault(require("./primary-keys-manager"));
|
|
20
|
-
|
|
21
12
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
22
|
-
|
|
23
13
|
var _requestedFieldsExtractor = _interopRequireDefault(require("./requested-fields-extractor"));
|
|
24
|
-
|
|
25
14
|
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; }
|
|
26
|
-
|
|
27
15
|
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; }
|
|
28
|
-
|
|
29
16
|
class ResourcesGetter {
|
|
30
17
|
constructor(model, lianaOptions, params, user) {
|
|
31
18
|
// lianaOptions is kept for compatibility with forest-express-mongoose
|
|
@@ -33,34 +20,31 @@ class ResourcesGetter {
|
|
|
33
20
|
this._params = params;
|
|
34
21
|
this._user = user;
|
|
35
22
|
}
|
|
36
|
-
|
|
37
23
|
async perform() {
|
|
38
24
|
return [await this._getRecords(), await this._getFieldsSearched()];
|
|
39
25
|
}
|
|
40
|
-
/** Count records matching current query (wo/ pagination) */
|
|
41
|
-
|
|
42
26
|
|
|
27
|
+
/** Count records matching current query (wo/ pagination) */
|
|
43
28
|
async count() {
|
|
44
29
|
const [model, options] = await this._buildQueryOptions({
|
|
45
30
|
forCount: true
|
|
46
|
-
});
|
|
31
|
+
});
|
|
47
32
|
|
|
33
|
+
// If no primary key is found, use * as a fallback for Sequelize.
|
|
48
34
|
return model.count(_objectSpread(_objectSpread({}, options), {}, {
|
|
49
35
|
col: _lodash.default.isEmpty(this._model.primaryKeys) ? '*' : undefined
|
|
50
36
|
}));
|
|
51
37
|
}
|
|
52
|
-
/** Load records matching current query (w/ pagination) */
|
|
53
|
-
|
|
54
38
|
|
|
39
|
+
/** Load records matching current query (w/ pagination) */
|
|
55
40
|
async _getRecords() {
|
|
56
41
|
const [model, options] = await this._buildQueryOptions();
|
|
57
42
|
const records = await model.findAll(options);
|
|
58
43
|
new _primaryKeysManager.default(this._model).annotateRecords(records);
|
|
59
44
|
return records;
|
|
60
45
|
}
|
|
61
|
-
/** Get list of fields descriptors which are used when searching (for frontend highlighting). */
|
|
62
|
-
|
|
63
46
|
|
|
47
|
+
/** Get list of fields descriptors which are used when searching (for frontend highlighting). */
|
|
64
48
|
async _getFieldsSearched() {
|
|
65
49
|
const {
|
|
66
50
|
fields,
|
|
@@ -72,9 +56,8 @@ class ResourcesGetter {
|
|
|
72
56
|
await queryOptions.requireFields(requestedFields);
|
|
73
57
|
return queryOptions.search(search, searchExtended);
|
|
74
58
|
}
|
|
75
|
-
/** Compute query options (shared for count and getRecords) */
|
|
76
|
-
|
|
77
59
|
|
|
60
|
+
/** Compute query options (shared for count and getRecords) */
|
|
78
61
|
async _buildQueryOptions(buildOptions = {}) {
|
|
79
62
|
const {
|
|
80
63
|
forCount,
|
|
@@ -101,7 +84,6 @@ class ResourcesGetter {
|
|
|
101
84
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
102
85
|
await queryOptions.segment(segment);
|
|
103
86
|
await queryOptions.segmentQuery(segmentQuery);
|
|
104
|
-
|
|
105
87
|
if (!forCount) {
|
|
106
88
|
const {
|
|
107
89
|
sort,
|
|
@@ -110,13 +92,11 @@ class ResourcesGetter {
|
|
|
110
92
|
await queryOptions.sort(sort);
|
|
111
93
|
await queryOptions.paginate(page === null || page === void 0 ? void 0 : page.number, page === null || page === void 0 ? void 0 : page.size);
|
|
112
94
|
}
|
|
113
|
-
|
|
114
|
-
|
|
95
|
+
return [
|
|
96
|
+
// add scopes to model
|
|
115
97
|
queryOptions.sequelizeScopes.reduce(function (m, scope) {
|
|
116
98
|
return m.scope(scope);
|
|
117
99
|
}, this._model), queryOptions.sequelizeOptions];
|
|
118
100
|
}
|
|
119
|
-
|
|
120
101
|
}
|
|
121
|
-
|
|
122
102
|
module.exports = ResourcesGetter;
|
|
@@ -1,15 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.promise.js");
|
|
6
|
-
|
|
7
5
|
var _forestExpress = require("forest-express");
|
|
8
|
-
|
|
9
6
|
var _errors = require("./errors");
|
|
10
|
-
|
|
11
7
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
12
|
-
|
|
13
8
|
class ResourcesRemover {
|
|
14
9
|
constructor(model, params, ids, user) {
|
|
15
10
|
this._model = model.unscoped();
|
|
@@ -17,12 +12,10 @@ class ResourcesRemover {
|
|
|
17
12
|
this._ids = ids;
|
|
18
13
|
this._user = user;
|
|
19
14
|
}
|
|
20
|
-
|
|
21
15
|
async perform() {
|
|
22
16
|
if (!Array.isArray(this._ids) || !this._ids.length) {
|
|
23
17
|
throw new _errors.InvalidParameterError('`ids` must be a non-empty array.');
|
|
24
18
|
}
|
|
25
|
-
|
|
26
19
|
const {
|
|
27
20
|
timezone
|
|
28
21
|
} = this._params;
|
|
@@ -32,7 +25,5 @@ class ResourcesRemover {
|
|
|
32
25
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
33
26
|
return this._model.destroy(queryOptions.sequelizeOptions);
|
|
34
27
|
}
|
|
35
|
-
|
|
36
28
|
}
|
|
37
|
-
|
|
38
29
|
module.exports = ResourcesRemover;
|
|
@@ -1,107 +1,78 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
require("core-js/modules/es.regexp.exec.js");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.array.iterator.js");
|
|
6
|
-
|
|
7
5
|
require("core-js/modules/es.promise.js");
|
|
8
|
-
|
|
9
6
|
const _ = require('lodash');
|
|
10
|
-
|
|
11
7
|
const Interface = require('forest-express');
|
|
12
|
-
|
|
13
8
|
const Operators = require('../utils/operators');
|
|
14
|
-
|
|
15
9
|
const Database = require('../utils/database');
|
|
16
|
-
|
|
17
10
|
const {
|
|
18
11
|
isUUID
|
|
19
12
|
} = require('../utils/orm');
|
|
20
|
-
|
|
21
13
|
const REGEX_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
22
|
-
|
|
23
14
|
function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
24
15
|
var _this = this;
|
|
25
|
-
|
|
26
16
|
const schema = Interface.Schemas.schemas[model.name];
|
|
27
17
|
const DataTypes = opts.Sequelize;
|
|
28
|
-
|
|
29
18
|
const fields = _.clone(schema.fields);
|
|
30
|
-
|
|
31
19
|
let associations = _.clone(model.associations);
|
|
32
|
-
|
|
33
20
|
const hasSearchFields = schema.searchFields && _.isArray(schema.searchFields);
|
|
34
|
-
|
|
35
21
|
let searchAssociationFields;
|
|
36
22
|
const OPERATORS = Operators.getInstance(opts);
|
|
37
23
|
const fieldsSearched = [];
|
|
38
24
|
let hasExtendedConditions = false;
|
|
39
|
-
|
|
40
25
|
function lowerIfNecessary(entry) {
|
|
41
26
|
// NOTICE: MSSQL search is natively case insensitive, do not use the "lower" function for
|
|
42
27
|
// performance optimization.
|
|
43
28
|
if (Database.isMSSQL(model.sequelize)) {
|
|
44
29
|
return entry;
|
|
45
30
|
}
|
|
46
|
-
|
|
47
31
|
return opts.Sequelize.fn('lower', entry);
|
|
48
32
|
}
|
|
49
|
-
|
|
50
33
|
function selectSearchFields() {
|
|
51
34
|
const searchFields = _.clone(schema.searchFields);
|
|
52
|
-
|
|
53
35
|
searchAssociationFields = _.remove(searchFields, function (field) {
|
|
54
36
|
return field.indexOf('.') !== -1;
|
|
55
37
|
});
|
|
56
|
-
|
|
57
38
|
_.remove(fields, function (field) {
|
|
58
39
|
return !_.includes(schema.searchFields, field.field);
|
|
59
40
|
});
|
|
60
|
-
|
|
61
41
|
const searchAssociationNames = _.map(searchAssociationFields, function (association) {
|
|
62
42
|
return association.split('.')[0];
|
|
63
43
|
});
|
|
44
|
+
associations = _.pick(associations, searchAssociationNames);
|
|
64
45
|
|
|
65
|
-
|
|
46
|
+
// NOTICE: Compute warnings to help developers to configure the
|
|
66
47
|
// searchFields.
|
|
67
|
-
|
|
68
48
|
const fieldsSimpleNotFound = _.xor(searchFields, _.map(fields, function (field) {
|
|
69
49
|
return field.field;
|
|
70
50
|
}));
|
|
71
|
-
|
|
72
51
|
const fieldsAssociationNotFound = _.xor(_.map(searchAssociationFields, function (association) {
|
|
73
52
|
return association.split('.')[0];
|
|
74
53
|
}), _.keys(associations));
|
|
75
|
-
|
|
76
54
|
if (fieldsSimpleNotFound.length) {
|
|
77
55
|
Interface.logger.warn(`Cannot find the fields [${fieldsSimpleNotFound}] while searching records in model ${model.name}.`);
|
|
78
56
|
}
|
|
79
|
-
|
|
80
57
|
if (fieldsAssociationNotFound.length) {
|
|
81
58
|
Interface.logger.warn(`Cannot find the associations [${fieldsAssociationNotFound}] while searching records in model ${model.name}.`);
|
|
82
59
|
}
|
|
83
60
|
}
|
|
84
|
-
|
|
85
61
|
function getStringExtendedCondition(attributes, value, column) {
|
|
86
62
|
if (attributes && isUUID(DataTypes, attributes.type)) {
|
|
87
63
|
if (!value.match(REGEX_UUID)) {
|
|
88
64
|
return null;
|
|
89
65
|
}
|
|
90
|
-
|
|
91
66
|
return opts.Sequelize.where(column, '=', value);
|
|
92
67
|
}
|
|
93
|
-
|
|
94
68
|
return opts.Sequelize.where(lowerIfNecessary(column), ' LIKE ', lowerIfNecessary(`%${value}%`));
|
|
95
69
|
}
|
|
96
|
-
|
|
97
70
|
this.getFieldsSearched = function () {
|
|
98
71
|
return fieldsSearched;
|
|
99
72
|
};
|
|
100
|
-
|
|
101
73
|
this.hasExtendedSearchConditions = function () {
|
|
102
74
|
return hasExtendedConditions;
|
|
103
75
|
};
|
|
104
|
-
|
|
105
76
|
this.performWithSmartFields = async function (associationName) {
|
|
106
77
|
// Retrocompatibility: customers which implement search on smart fields are expected to
|
|
107
78
|
// inject their conditions at .where[Op.and][0][Op.or].push(searchCondition)
|
|
@@ -122,7 +93,6 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
122
93
|
const errorMessage = `Cannot search properly on Smart Field ${field.field}: `;
|
|
123
94
|
Interface.logger.error(errorMessage, error);
|
|
124
95
|
}
|
|
125
|
-
|
|
126
96
|
return Promise.resolve();
|
|
127
97
|
}));
|
|
128
98
|
return {
|
|
@@ -130,69 +100,51 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
130
100
|
conditions: query.where[OPERATORS.AND][0][OPERATORS.OR].length ? query.where[OPERATORS.AND][0] : null
|
|
131
101
|
};
|
|
132
102
|
};
|
|
133
|
-
|
|
134
103
|
this.perform = function (associationName) {
|
|
135
104
|
if (!params.search) {
|
|
136
105
|
return null;
|
|
137
106
|
}
|
|
138
|
-
|
|
139
107
|
if (hasSearchFields) {
|
|
140
108
|
selectSearchFields();
|
|
141
109
|
}
|
|
142
|
-
|
|
143
110
|
const aliasName = associationName || schema.name;
|
|
144
111
|
const or = [];
|
|
145
|
-
|
|
146
112
|
function pushCondition(condition, fieldName) {
|
|
147
113
|
or.push(condition);
|
|
148
114
|
fieldsSearched.push(fieldName);
|
|
149
115
|
}
|
|
150
|
-
|
|
151
116
|
function getConditionValueForNumber(search, keyType) {
|
|
152
117
|
const searchAsNumber = Number(search);
|
|
153
|
-
|
|
154
118
|
if (Number.isNaN(searchAsNumber)) {
|
|
155
119
|
return null;
|
|
156
120
|
}
|
|
157
|
-
|
|
158
121
|
if (Number.isSafeInteger(searchAsNumber) || !Number.isInteger(searchAsNumber)) {
|
|
159
122
|
return searchAsNumber;
|
|
160
|
-
}
|
|
123
|
+
}
|
|
124
|
+
// Integers higher than MAX_SAFE_INTEGER need to be handled as strings to circumvent
|
|
161
125
|
// precision problems only if the field type is a big int.
|
|
162
|
-
|
|
163
|
-
|
|
164
126
|
if (keyType instanceof DataTypes.BIGINT) {
|
|
165
127
|
return search;
|
|
166
128
|
}
|
|
167
|
-
|
|
168
129
|
return null;
|
|
169
130
|
}
|
|
170
|
-
|
|
171
131
|
_.each(fields, function (field) {
|
|
172
132
|
if (field.isVirtual) {
|
|
173
133
|
return;
|
|
174
134
|
} // NOTICE: Ignore Smart Fields.
|
|
175
|
-
|
|
176
|
-
|
|
177
135
|
if (field.integration) {
|
|
178
136
|
return;
|
|
179
137
|
} // NOTICE: Ignore integration fields.
|
|
180
|
-
|
|
181
|
-
|
|
182
138
|
if (field.reference) {
|
|
183
139
|
return;
|
|
184
140
|
} // NOTICE: Handle belongsTo search below.
|
|
185
141
|
|
|
186
|
-
|
|
187
142
|
let condition = {};
|
|
188
143
|
let columnName;
|
|
189
|
-
|
|
190
144
|
if (field.field === schema.idField) {
|
|
191
145
|
const primaryKeyType = model.primaryKeys[schema.idField].type;
|
|
192
|
-
|
|
193
146
|
if (primaryKeyType instanceof DataTypes.INTEGER) {
|
|
194
147
|
const conditionValue = getConditionValueForNumber(params.search, primaryKeyType);
|
|
195
|
-
|
|
196
148
|
if (conditionValue !== null) {
|
|
197
149
|
condition[field.field] = conditionValue;
|
|
198
150
|
pushCondition(condition, field.field);
|
|
@@ -208,13 +160,11 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
208
160
|
} else if (field.type === 'Enum') {
|
|
209
161
|
let enumValueFound;
|
|
210
162
|
const searchValue = params.search.toLowerCase();
|
|
211
|
-
|
|
212
163
|
_.each(field.enums, function (enumValue) {
|
|
213
164
|
if (enumValue.toLowerCase() === searchValue) {
|
|
214
165
|
enumValueFound = enumValue;
|
|
215
166
|
}
|
|
216
167
|
});
|
|
217
|
-
|
|
218
168
|
if (enumValueFound) {
|
|
219
169
|
condition[field.field] = enumValueFound;
|
|
220
170
|
pushCondition(condition, field.field);
|
|
@@ -232,15 +182,14 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
232
182
|
}
|
|
233
183
|
} else if (field.type === 'Number') {
|
|
234
184
|
const conditionValue = getConditionValueForNumber(params.search, model.rawAttributes[field.field].type);
|
|
235
|
-
|
|
236
185
|
if (conditionValue !== null) {
|
|
237
186
|
condition[field.field] = conditionValue;
|
|
238
187
|
pushCondition(condition, field.field);
|
|
239
188
|
}
|
|
240
189
|
}
|
|
241
|
-
});
|
|
242
|
-
|
|
190
|
+
});
|
|
243
191
|
|
|
192
|
+
// NOTICE: Handle search on displayed belongsTo
|
|
244
193
|
if (parseInt(params.searchExtended, 10)) {
|
|
245
194
|
_.each(associations, function (association) {
|
|
246
195
|
if (!fieldNamesRequested || fieldNamesRequested.includes(association.as) || fieldNamesRequested.find(function (fieldName) {
|
|
@@ -250,39 +199,28 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
250
199
|
const modelAssociation = association.target;
|
|
251
200
|
const schemaAssociation = Interface.Schemas.schemas[modelAssociation.name];
|
|
252
201
|
const fieldsAssociation = schemaAssociation.fields;
|
|
253
|
-
|
|
254
202
|
_.each(fieldsAssociation, function (field) {
|
|
255
203
|
if (field.isVirtual) {
|
|
256
204
|
return;
|
|
257
205
|
} // NOTICE: Ignore Smart Fields.
|
|
258
|
-
|
|
259
|
-
|
|
260
206
|
if (field.integration) {
|
|
261
207
|
return;
|
|
262
208
|
} // NOTICE: Ignore integration fields.
|
|
263
|
-
|
|
264
|
-
|
|
265
209
|
if (field.reference) {
|
|
266
210
|
return;
|
|
267
211
|
} // NOTICE: Ignore associations.
|
|
268
|
-
|
|
269
|
-
|
|
270
212
|
if (field.isSearchable === false) {
|
|
271
213
|
return;
|
|
272
214
|
}
|
|
273
|
-
|
|
274
215
|
if (hasSearchFields && !_.includes(searchAssociationFields, `${association.as}.${field.field}`)) {
|
|
275
216
|
return;
|
|
276
217
|
}
|
|
277
|
-
|
|
278
218
|
let condition = null;
|
|
279
219
|
const columnName = field.columnName || field.field;
|
|
280
220
|
const column = opts.Sequelize.col(`${association.as}.${columnName}`);
|
|
281
|
-
|
|
282
221
|
if (field.field === schemaAssociation.idField) {
|
|
283
222
|
if (field.type === 'Number') {
|
|
284
223
|
const value = parseInt(params.search, 10) || 0;
|
|
285
|
-
|
|
286
224
|
if (value) {
|
|
287
225
|
condition = opts.Sequelize.where(column, ' = ', value);
|
|
288
226
|
hasExtendedConditions = true;
|
|
@@ -295,7 +233,6 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
295
233
|
condition = getStringExtendedCondition(modelAssociation.rawAttributes[field.field], params.search, column);
|
|
296
234
|
hasExtendedConditions = !!condition;
|
|
297
235
|
}
|
|
298
|
-
|
|
299
236
|
if (condition) {
|
|
300
237
|
or.push(condition);
|
|
301
238
|
}
|
|
@@ -304,11 +241,9 @@ function SearchBuilder(model, opts, params, fieldNamesRequested) {
|
|
|
304
241
|
}
|
|
305
242
|
});
|
|
306
243
|
}
|
|
307
|
-
|
|
308
244
|
return {
|
|
309
245
|
[OPERATORS.OR]: or
|
|
310
246
|
};
|
|
311
247
|
};
|
|
312
248
|
}
|
|
313
|
-
|
|
314
249
|
module.exports = SearchBuilder;
|
|
@@ -1,29 +1,17 @@
|
|
|
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.promise.js");
|
|
8
|
-
|
|
9
6
|
var _bluebird = _interopRequireDefault(require("bluebird"));
|
|
10
|
-
|
|
11
7
|
var _forestExpress = require("forest-express");
|
|
12
|
-
|
|
13
8
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
14
|
-
|
|
15
9
|
var _operators = _interopRequireDefault(require("../utils/operators"));
|
|
16
|
-
|
|
17
10
|
var _orm = _interopRequireDefault(require("../utils/orm"));
|
|
18
|
-
|
|
19
11
|
var _filtersParser = _interopRequireDefault(require("./filters-parser"));
|
|
20
|
-
|
|
21
12
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
22
|
-
|
|
23
13
|
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; }
|
|
24
|
-
|
|
25
14
|
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; }
|
|
26
|
-
|
|
27
15
|
class ValueStatGetter {
|
|
28
16
|
constructor(model, params, options, user) {
|
|
29
17
|
this._model = model;
|
|
@@ -37,21 +25,18 @@ class ValueStatGetter {
|
|
|
37
25
|
timezone: params.timezone
|
|
38
26
|
});
|
|
39
27
|
}
|
|
40
|
-
/** Function used to aggregate results (count, sum, ...) */
|
|
41
|
-
|
|
42
28
|
|
|
29
|
+
/** Function used to aggregate results (count, sum, ...) */
|
|
43
30
|
get _aggregateFunction() {
|
|
44
31
|
return this._params.aggregator.toLowerCase();
|
|
45
32
|
}
|
|
46
|
-
/** Column name we're aggregating on */
|
|
47
|
-
|
|
48
33
|
|
|
34
|
+
/** Column name we're aggregating on */
|
|
49
35
|
get _aggregateField() {
|
|
50
36
|
// NOTICE: As MySQL cannot support COUNT(table_name.*) syntax, fieldName cannot be '*'.
|
|
51
37
|
const fieldName = this._params.aggregateFieldName || this._schema.primaryKeys[0] || this._schema.fields[0].field;
|
|
52
38
|
return `${this._schema.name}.${_orm.default.getColumnName(this._schema, fieldName)}`;
|
|
53
39
|
}
|
|
54
|
-
|
|
55
40
|
async perform() {
|
|
56
41
|
const {
|
|
57
42
|
filter,
|
|
@@ -62,8 +47,9 @@ class ValueStatGetter {
|
|
|
62
47
|
includeRelations: true
|
|
63
48
|
});
|
|
64
49
|
await queryOptions.filterByConditionTree(filter, timezone);
|
|
65
|
-
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
50
|
+
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
66
51
|
|
|
52
|
+
// No attributes should be retrieved from relations for the group by to work.
|
|
67
53
|
const options = queryOptions.sequelizeOptions;
|
|
68
54
|
options.include = options.include ? options.include.map(function (includeProperties) {
|
|
69
55
|
return _objectSpread(_objectSpread({}, includeProperties), {}, {
|
|
@@ -77,12 +63,13 @@ class ValueStatGetter {
|
|
|
77
63
|
})
|
|
78
64
|
};
|
|
79
65
|
}
|
|
80
|
-
|
|
81
66
|
async _getCount(options) {
|
|
82
|
-
const count = await this._model.unscoped().aggregate(this._aggregateField, this._aggregateFunction, options);
|
|
67
|
+
const count = await this._model.unscoped().aggregate(this._aggregateField, this._aggregateFunction, options);
|
|
83
68
|
|
|
69
|
+
// sequelize@4 returns NaN, while sequelize@5+ returns null
|
|
84
70
|
return count || 0;
|
|
85
71
|
}
|
|
72
|
+
|
|
86
73
|
/**
|
|
87
74
|
* Fetch the value for the previous period.
|
|
88
75
|
*
|
|
@@ -90,36 +77,26 @@ class ValueStatGetter {
|
|
|
90
77
|
* - when the 'rawPreviousInterval.field' appears twice
|
|
91
78
|
* - when scopes use the same field as the filter
|
|
92
79
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
80
|
async _getCountPrevious(options) {
|
|
96
81
|
const {
|
|
97
82
|
filter,
|
|
98
83
|
timezone
|
|
99
84
|
} = this._params;
|
|
100
|
-
|
|
101
85
|
if (!filter) {
|
|
102
86
|
return undefined;
|
|
103
87
|
}
|
|
104
|
-
|
|
105
88
|
const conditionsParser = new _filtersParser.default(this._schema, timezone, this._options);
|
|
106
89
|
const rawInterval = conditionsParser.getPreviousIntervalCondition(filter);
|
|
107
|
-
|
|
108
90
|
if (!rawInterval) {
|
|
109
91
|
return undefined;
|
|
110
92
|
}
|
|
111
|
-
|
|
112
93
|
const interval = this._operatorDateParser.getPreviousDateFilter(rawInterval.operator, rawInterval.value);
|
|
113
|
-
|
|
114
94
|
const newOptions = _lodash.default.cloneDeepWith(options, function (object) {
|
|
115
95
|
return object && object[rawInterval.field] ? _objectSpread(_objectSpread({}, object), {}, {
|
|
116
96
|
[rawInterval.field]: interval
|
|
117
97
|
}) : undefined;
|
|
118
98
|
});
|
|
119
|
-
|
|
120
99
|
return this._getCount(newOptions);
|
|
121
100
|
}
|
|
122
|
-
|
|
123
101
|
}
|
|
124
|
-
|
|
125
102
|
module.exports = ValueStatGetter;
|
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
require("core-js/modules/es.promise.js");
|
|
6
|
-
|
|
7
5
|
var _orm = _interopRequireDefault(require("./orm"));
|
|
8
|
-
|
|
9
6
|
async function get(model, pk) {
|
|
10
7
|
const record = await _orm.default.findRecord(model, pk);
|
|
11
|
-
|
|
12
8
|
if (!record) {
|
|
13
9
|
throw new Error(`related ${model.name} with pk ${pk} does not exist.`);
|
|
14
10
|
}
|
|
15
|
-
|
|
16
11
|
return record;
|
|
17
12
|
}
|
|
18
|
-
|
|
19
13
|
exports.get = get;
|
package/dist/utils/database.js
CHANGED
|
@@ -3,15 +3,12 @@
|
|
|
3
3
|
function getConnectionDialect(connection) {
|
|
4
4
|
return connection.options.dialect;
|
|
5
5
|
}
|
|
6
|
-
|
|
7
6
|
exports.isMySQL = function (connection) {
|
|
8
7
|
return ['mysql', 'mariadb'].includes(getConnectionDialect(connection));
|
|
9
8
|
};
|
|
10
|
-
|
|
11
9
|
exports.isMSSQL = function (connection) {
|
|
12
10
|
return getConnectionDialect(connection) === 'mssql';
|
|
13
11
|
};
|
|
14
|
-
|
|
15
12
|
exports.isSQLite = function (connection) {
|
|
16
13
|
return getConnectionDialect(connection) === 'sqlite';
|
|
17
14
|
};
|