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.
Files changed (38) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/adapters/sequelize.js +6 -31
  3. package/dist/index.js +2 -50
  4. package/dist/public/parse-filter.js +0 -1
  5. package/dist/services/apimap-field-builder.js +12 -40
  6. package/dist/services/apimap-field-type-detector.js +2 -15
  7. package/dist/services/belongs-to-updater.js +5 -16
  8. package/dist/services/errors.js +0 -5
  9. package/dist/services/filters-parser.js +9 -55
  10. package/dist/services/has-many-associator.js +3 -6
  11. package/dist/services/has-many-dissociator.js +0 -13
  12. package/dist/services/has-many-getter.js +2 -22
  13. package/dist/services/leaderboard-stat-getter.js +1 -22
  14. package/dist/services/line-stat-getter.js +2 -59
  15. package/dist/services/pie-stat-getter.js +0 -33
  16. package/dist/services/primary-keys-manager.js +5 -28
  17. package/dist/services/query-builder.js +4 -13
  18. package/dist/services/query-options.js +21 -78
  19. package/dist/services/query-stat-getter.js +3 -8
  20. package/dist/services/requested-fields-extractor.js +9 -14
  21. package/dist/services/resource-creator.js +10 -37
  22. package/dist/services/resource-getter.js +0 -11
  23. package/dist/services/resource-remover.js +0 -4
  24. package/dist/services/resource-updater.js +0 -15
  25. package/dist/services/resources-exporter.js +0 -13
  26. package/dist/services/resources-getter.js +8 -28
  27. package/dist/services/resources-remover.js +0 -9
  28. package/dist/services/search-builder.js +6 -71
  29. package/dist/services/value-stat-getter.js +7 -30
  30. package/dist/utils/association-record.js +0 -6
  31. package/dist/utils/database.js +0 -3
  32. package/dist/utils/object-tools.js +1 -8
  33. package/dist/utils/operators.js +0 -9
  34. package/dist/utils/orm.js +0 -12
  35. package/dist/utils/query.js +5 -16
  36. package/dist/utils/records-decorator.js +0 -10
  37. package/dist/utils/sequelize-compatibility.js +9 -38
  38. package/package.json +1 -1
@@ -1,129 +1,98 @@
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
  require("core-js/modules/es.regexp.exec.js");
8
-
9
6
  require("core-js/modules/es.string.trim.js");
10
-
11
7
  require("core-js/modules/es.array.iterator.js");
12
-
13
8
  var _forestExpress = require("forest-express");
14
-
15
9
  var _operators = _interopRequireDefault(require("../utils/operators"));
16
-
17
10
  var _errors = require("./errors");
18
-
19
11
  const {
20
12
  getReferenceSchema,
21
13
  getReferenceField
22
14
  } = require('../utils/query');
23
-
24
15
  function FiltersParser(modelSchema, timezone, options) {
25
16
  var _this = this;
26
-
27
17
  this.OPERATORS = _operators.default.getInstance(options);
28
18
  this.operatorDateParser = new _forestExpress.BaseOperatorDateParser({
29
19
  operators: this.OPERATORS,
30
20
  timezone
31
21
  });
32
-
33
22
  this.perform = async function (filtersString) {
34
23
  return _forestExpress.BaseFiltersParser.perform(filtersString, _this.formatAggregation, _this.formatCondition, modelSchema);
35
24
  };
36
-
37
25
  this.formatAggregation = async function (aggregator, formattedConditions) {
38
26
  const aggregatorOperator = _this.formatAggregatorOperator(aggregator);
39
-
40
27
  return {
41
28
  [aggregatorOperator]: formattedConditions
42
29
  };
43
30
  };
44
-
45
31
  this.formatCondition = async function (condition, isSmartField = false) {
46
32
  const isTextField = _this.isTextField(condition.field);
47
-
48
33
  if (isSmartField) {
49
34
  return _this.formatOperatorValue(condition.operator, condition.value, isTextField);
50
35
  }
51
-
52
36
  const formattedField = _this.formatField(condition.field);
53
-
54
37
  if (_this.operatorDateParser.isDateOperator(condition.operator)) {
55
38
  return {
56
39
  [formattedField]: _this.operatorDateParser.getDateFilter(condition.operator, condition.value)
57
40
  };
58
41
  }
59
-
60
42
  return {
61
43
  [formattedField]: _this.formatOperatorValue(condition.operator, condition.value, isTextField)
62
44
  };
63
45
  };
64
-
65
46
  this.formatAggregatorOperator = function (aggregatorOperator) {
66
47
  switch (aggregatorOperator) {
67
48
  case 'and':
68
49
  return _this.OPERATORS.AND;
69
-
70
50
  case 'or':
71
51
  return _this.OPERATORS.OR;
72
-
73
52
  default:
74
53
  throw new _errors.NoMatchingOperatorError();
75
54
  }
76
55
  };
77
-
78
56
  this.formatOperatorValue = function (operator, value, isTextField = false) {
79
57
  switch (operator) {
80
58
  case 'not':
81
59
  return {
82
60
  [_this.OPERATORS.NOT]: value
83
61
  };
84
-
85
62
  case 'greater_than':
86
63
  case 'after':
87
64
  return {
88
65
  [_this.OPERATORS.GT]: value
89
66
  };
90
-
91
67
  case 'less_than':
92
68
  case 'before':
93
69
  return {
94
70
  [_this.OPERATORS.LT]: value
95
71
  };
96
-
97
72
  case 'contains':
98
73
  return {
99
74
  [_this.OPERATORS.LIKE]: `%${value}%`
100
75
  };
101
-
102
76
  case 'starts_with':
103
77
  return {
104
78
  [_this.OPERATORS.LIKE]: `${value}%`
105
79
  };
106
-
107
80
  case 'ends_with':
108
81
  return {
109
82
  [_this.OPERATORS.LIKE]: `%${value}`
110
83
  };
111
-
112
84
  case 'not_contains':
113
85
  return {
114
86
  [_this.OPERATORS.NOT_LIKE]: `%${value}%`
115
87
  };
116
-
117
88
  case 'present':
118
89
  return {
119
90
  [_this.OPERATORS.NE]: null
120
91
  };
121
-
122
92
  case 'not_equal':
123
93
  return {
124
94
  [_this.OPERATORS.NE]: value
125
95
  };
126
-
127
96
  case 'blank':
128
97
  return isTextField ? {
129
98
  [_this.OPERATORS.OR]: [{
@@ -134,17 +103,14 @@ function FiltersParser(modelSchema, timezone, options) {
134
103
  } : {
135
104
  [_this.OPERATORS.EQ]: null
136
105
  };
137
-
138
106
  case 'equal':
139
107
  return {
140
108
  [_this.OPERATORS.EQ]: value
141
109
  };
142
-
143
110
  case 'includes_all':
144
111
  return {
145
112
  [_this.OPERATORS.CONTAINS]: value
146
113
  };
147
-
148
114
  case 'in':
149
115
  return typeof value === 'string' ? {
150
116
  [_this.OPERATORS.IN]: value.split(',').map(function (elem) {
@@ -153,80 +119,68 @@ function FiltersParser(modelSchema, timezone, options) {
153
119
  } : {
154
120
  [_this.OPERATORS.IN]: value
155
121
  };
156
-
157
122
  default:
158
123
  throw new _errors.NoMatchingOperatorError();
159
124
  }
160
125
  };
161
-
162
126
  this.formatField = function (field) {
163
127
  if (field.includes(':')) {
164
128
  const [associationName, fieldName] = field.split(':');
165
129
  return `$${getReferenceField(_forestExpress.Schemas.schemas, modelSchema, associationName, fieldName)}$`;
166
130
  }
167
-
168
131
  return field;
169
132
  };
170
-
171
133
  this.isTextField = function (field) {
172
134
  if (field.includes(':')) {
173
135
  const [associationName, fieldName] = field.split(':');
174
136
  const associationSchema = getReferenceSchema(_forestExpress.Schemas.schemas, modelSchema, associationName, fieldName);
175
-
176
137
  if (associationSchema) {
177
138
  return _forestExpress.SchemaUtils.getFieldType(associationSchema, field) === 'String';
178
139
  }
179
-
180
140
  return false;
181
141
  }
182
-
183
142
  return _forestExpress.SchemaUtils.getFieldType(modelSchema, field) === 'String';
184
- }; // NOTICE: Look for a previous interval condition matching the following:
143
+ };
144
+
145
+ // NOTICE: Look for a previous interval condition matching the following:
185
146
  // - If the filter is a simple condition at the root the check is done right away.
186
147
  // - There can't be a previous interval condition if the aggregator is 'or' (no meaning).
187
148
  // - The condition's operator has to be elligible for a previous interval.
188
149
  // - There can't be two previous interval condition.
189
-
190
-
191
150
  this.getPreviousIntervalCondition = function (filtersString) {
192
151
  const filters = _forestExpress.BaseFiltersParser.parseFiltersString(filtersString);
152
+ let currentPreviousInterval = null;
193
153
 
194
- let currentPreviousInterval = null; // NOTICE: Leaf condition at root
195
-
154
+ // NOTICE: Leaf condition at root
196
155
  if (filters && !filters.aggregator) {
197
156
  if (_this.operatorDateParser.hasPreviousDateInterval(filters.operator)) {
198
157
  return filters;
199
158
  }
200
-
201
159
  return null;
202
- } // NOTICE: No previous interval condition when 'or' aggregator
203
-
160
+ }
204
161
 
162
+ // NOTICE: No previous interval condition when 'or' aggregator
205
163
  if (filters.aggregator === 'and') {
206
164
  for (let i = 0; i < filters.conditions.length; i += 1) {
207
- const condition = filters.conditions[i]; // NOTICE: Nested filters
165
+ const condition = filters.conditions[i];
208
166
 
167
+ // NOTICE: Nested filters
209
168
  if (condition.aggregator) {
210
169
  return null;
211
170
  }
212
-
213
171
  if (_this.operatorDateParser.hasPreviousDateInterval(condition.operator)) {
214
172
  // NOTICE: There can't be two previousInterval.
215
173
  if (currentPreviousInterval) {
216
174
  return null;
217
175
  }
218
-
219
176
  currentPreviousInterval = condition;
220
177
  }
221
178
  }
222
179
  }
223
-
224
180
  return currentPreviousInterval;
225
181
  };
226
-
227
182
  this.getAssociations = async function (filtersString) {
228
183
  return _forestExpress.BaseFiltersParser.getAssociations(filtersString);
229
184
  };
230
185
  }
231
-
232
186
  module.exports = FiltersParser;
@@ -1,24 +1,21 @@
1
1
  "use strict";
2
2
 
3
3
  const _ = require('lodash');
4
-
5
4
  const orm = require('../utils/orm');
6
-
7
5
  function HasManyAssociator(model, association, opts, params, data) {
8
6
  this.perform = function perform() {
9
7
  return orm.findRecord(model, params.recordId).then(function (record) {
10
8
  const associatedIds = _.map(data.data, function (value) {
11
9
  return value.id;
12
- }); // NOTICE: Deactivate validation to prevent potential issues with custom model validations.
10
+ });
11
+
12
+ // NOTICE: Deactivate validation to prevent potential issues with custom model validations.
13
13
  // In this case, the full record attributes are missing which may raise an
14
14
  // unexpected validation error.
15
-
16
-
17
15
  return record[`add${_.upperFirst(params.associationName)}`](associatedIds, {
18
16
  validate: false
19
17
  });
20
18
  });
21
19
  };
22
20
  }
23
-
24
21
  module.exports = HasManyAssociator;
@@ -1,29 +1,21 @@
1
1
  "use strict";
2
2
 
3
3
  require("core-js/modules/es.array.iterator.js");
4
-
5
4
  const _ = require('lodash');
6
-
7
5
  const Operators = require('../utils/operators');
8
-
9
6
  const orm = require('../utils/orm');
10
-
11
7
  const {
12
8
  ErrorHTTP422
13
9
  } = require('./errors');
14
-
15
10
  function HasManyDissociator(model, association, options, params, data) {
16
11
  const OPERATORS = Operators.getInstance(options);
17
12
  const isDelete = Boolean(params.delete);
18
-
19
13
  this.perform = function () {
20
14
  const associatedIds = _.map(data.data, function (value) {
21
15
  return value.id;
22
16
  });
23
-
24
17
  return orm.findRecord(model, params.recordId).then(function (record) {
25
18
  let removeAssociation = false;
26
-
27
19
  if (isDelete) {
28
20
  _.each(model.associations, function (innerAssociation, associationName) {
29
21
  if (associationName === params.associationName) {
@@ -33,16 +25,13 @@ function HasManyDissociator(model, association, options, params, data) {
33
25
  } else {
34
26
  removeAssociation = true;
35
27
  }
36
-
37
28
  if (removeAssociation) {
38
29
  return record[`remove${_.upperFirst(params.associationName)}`](associatedIds);
39
30
  }
40
-
41
31
  return null;
42
32
  }).then(function () {
43
33
  if (isDelete) {
44
34
  const primaryKeys = _.keys(association.primaryKeys);
45
-
46
35
  const [idField] = primaryKeys;
47
36
  const condition = {
48
37
  [idField]: {
@@ -53,12 +42,10 @@ function HasManyDissociator(model, association, options, params, data) {
53
42
  where: condition
54
43
  });
55
44
  }
56
-
57
45
  return null;
58
46
  }).catch(function (error) {
59
47
  throw new ErrorHTTP422(error.message);
60
48
  });
61
49
  };
62
50
  }
63
-
64
51
  module.exports = HasManyDissociator;
@@ -1,48 +1,34 @@
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
  var _lodash = require("lodash");
12
-
13
8
  var _sequelizeCompatibility = _interopRequireDefault(require("../utils/sequelize-compatibility"));
14
-
15
9
  var _primaryKeysManager = _interopRequireDefault(require("./primary-keys-manager"));
16
-
17
10
  var _resourcesGetter = _interopRequireDefault(require("./resources-getter"));
18
-
19
11
  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; }
20
-
21
12
  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; }
22
-
23
13
  class HasManyGetter extends _resourcesGetter.default {
24
14
  constructor(model, association, lianaOptions, params, user) {
25
15
  super(association, lianaOptions, params, user);
26
16
  this._parentModel = model.unscoped();
27
17
  }
28
-
29
18
  async _getRecords() {
30
19
  var _parentRecord$this$_p;
31
-
32
20
  const options = await this._buildQueryOptions();
33
21
  const parentRecord = await this._parentModel.findOne(options);
34
22
  const records = (_parentRecord$this$_p = parentRecord === null || parentRecord === void 0 ? void 0 : parentRecord[this._params.associationName]) !== null && _parentRecord$this$_p !== void 0 ? _parentRecord$this$_p : [];
35
23
  new _primaryKeysManager.default(this._model).annotateRecords(records);
36
24
  return records;
37
25
  }
38
-
39
26
  async count() {
40
27
  const options = await this._buildQueryOptions({
41
28
  forCount: true
42
29
  });
43
30
  return this._parentModel.count(options);
44
31
  }
45
-
46
32
  async _buildQueryOptions(buildOptions = {}) {
47
33
  const {
48
34
  associationName,
@@ -51,7 +37,6 @@ class HasManyGetter extends _resourcesGetter.default {
51
37
  const [model, options] = await super._buildQueryOptions(_objectSpread(_objectSpread({}, buildOptions), {}, {
52
38
  tableAlias: associationName
53
39
  }));
54
-
55
40
  const parentOptions = _sequelizeCompatibility.default.postProcess(this._parentModel, {
56
41
  where: new _primaryKeysManager.default(this._parentModel).getRecordsConditions([recordId]),
57
42
  include: [_objectSpread({
@@ -61,25 +46,20 @@ class HasManyGetter extends _resourcesGetter.default {
61
46
  required: !!buildOptions.forCount
62
47
  }, (0, _lodash.pick)(options, ['attributes', 'where', 'include']))]
63
48
  });
64
-
65
49
  if (!buildOptions.forCount) {
66
50
  parentOptions.subQuery = false; // Why?
67
-
68
51
  parentOptions.attributes = []; // Don't fetch parent attributes (perf)
69
-
70
52
  parentOptions.offset = options.offset;
71
- parentOptions.limit = options.limit; // Order with the relation (https://github.com/sequelize/sequelize/issues/4553)
53
+ parentOptions.limit = options.limit;
72
54
 
55
+ // Order with the relation (https://github.com/sequelize/sequelize/issues/4553)
73
56
  if (options.order) {
74
57
  parentOptions.order = options.order.map(function (fields) {
75
58
  return [associationName, ...fields];
76
59
  });
77
60
  }
78
61
  }
79
-
80
62
  return parentOptions;
81
63
  }
82
-
83
64
  }
84
-
85
65
  module.exports = HasManyGetter;
@@ -1,23 +1,14 @@
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
  require("core-js/modules/es.array.iterator.js");
8
-
9
6
  var _lodash = _interopRequireDefault(require("lodash"));
10
-
11
7
  var _forestExpress = require("forest-express");
12
-
13
8
  var _orm = _interopRequireDefault(require("../utils/orm"));
14
-
15
9
  var _sequelizeCompatibility = _interopRequireDefault(require("../utils/sequelize-compatibility"));
16
-
17
10
  var _errors = require("./errors");
18
-
19
11
  var _queryOptions = _interopRequireDefault(require("./query-options"));
20
-
21
12
  function getAggregateField({
22
13
  aggregateField,
23
14
  parentSchema,
@@ -27,13 +18,13 @@ function getAggregateField({
27
18
  const fieldName = aggregateField || parentSchema.primaryKeys[0] || parentSchema.fields[0].field;
28
19
  return `${parentModel.name}.${_orm.default.getColumnName(parentSchema, fieldName)}`;
29
20
  }
30
-
31
21
  async function getSequelizeOptionsForModel(model, user, timezone) {
32
22
  const queryOptions = new _queryOptions.default(model);
33
23
  const scopeFilters = await _forestExpress.scopeManager.getScopeForUser(user, model.name, true);
34
24
  await queryOptions.filterByConditionTree(scopeFilters, timezone);
35
25
  return queryOptions.sequelizeOptions;
36
26
  }
27
+
37
28
  /**
38
29
  * @param {import('sequelize').Model} childModel
39
30
  * @param {import('sequelize').Model} parentModel
@@ -44,8 +35,6 @@ async function getSequelizeOptionsForModel(model, user, timezone) {
44
35
  * limit: number;
45
36
  * }} params
46
37
  */
47
-
48
-
49
38
  function LeaderboardStatGetter(childModel, parentModel, params, user) {
50
39
  const labelField = params.labelFieldName;
51
40
  const aggregate = params.aggregator.toUpperCase();
@@ -55,36 +44,28 @@ function LeaderboardStatGetter(childModel, parentModel, params, user) {
55
44
  const childSchema = _forestExpress.Schemas.schemas[childModel.name];
56
45
  const parentSchema = _forestExpress.Schemas.schemas[parentModel.name];
57
46
  let associationAs = childSchema.name;
58
-
59
47
  const associationFound = _lodash.default.find(parentModel.associations, function (association) {
60
48
  return association.target.name === childModel.name;
61
49
  });
62
-
63
50
  const aggregateField = getAggregateField({
64
51
  aggregateField: params.aggregateFieldName,
65
52
  parentSchema,
66
53
  parentModel
67
54
  });
68
-
69
55
  if (!associationFound) {
70
56
  throw new _errors.InvalidParameterError(`Association ${childModel.name} not found`);
71
57
  }
72
-
73
58
  if (associationFound.as) {
74
59
  associationAs = associationFound.as;
75
60
  }
76
-
77
61
  const labelColumn = _orm.default.getColumnName(childSchema, labelField);
78
-
79
62
  const groupBy = `${associationAs}.${labelColumn}`;
80
-
81
63
  this.perform = async function () {
82
64
  const {
83
65
  timezone
84
66
  } = params;
85
67
  const parentSequelizeOptions = await getSequelizeOptionsForModel(parentModel, user, timezone);
86
68
  const childSequelizeOptions = await getSequelizeOptionsForModel(childModel, user, timezone);
87
-
88
69
  const queryOptions = _sequelizeCompatibility.default.postProcess(parentModel, {
89
70
  attributes: [[childModel.sequelize.col(groupBy), 'key'], [childModel.sequelize.fn(aggregate, childModel.sequelize.col(aggregateField)), 'value']],
90
71
  where: parentSequelizeOptions.where,
@@ -103,7 +84,6 @@ function LeaderboardStatGetter(childModel, parentModel, params, user) {
103
84
  limit,
104
85
  raw: true
105
86
  });
106
-
107
87
  const records = await parentModel.findAll(queryOptions);
108
88
  return {
109
89
  value: records.map(function (data) {
@@ -115,5 +95,4 @@ function LeaderboardStatGetter(childModel, parentModel, params, user) {
115
95
  };
116
96
  };
117
97
  }
118
-
119
98
  module.exports = LeaderboardStatGetter;