forest-express-sequelize 9.2.1 → 9.2.3

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 +2 -2
@@ -1,17 +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.array.iterator.js");
6
-
7
5
  var _lodash = _interopRequireDefault(require("lodash"));
8
-
9
6
  /** Do object1 and object2 have at least one common key or Symbol? */
10
7
  exports.plainObjectsShareNoKeys = function (object1, object2) {
11
8
  if (!_lodash.default.isPlainObject(object1) || !_lodash.default.isPlainObject(object2)) {
12
9
  return false;
13
10
  }
14
-
15
11
  const keys1 = [...Object.getOwnPropertyNames(object1), ...Object.getOwnPropertySymbols(object1)];
16
12
  const keys2 = [...Object.getOwnPropertyNames(object2), ...Object.getOwnPropertySymbols(object2)];
17
13
  const commonKeys = keys1.filter(function (key) {
@@ -19,6 +15,7 @@ exports.plainObjectsShareNoKeys = function (object1, object2) {
19
15
  });
20
16
  return commonKeys.length === 0;
21
17
  };
18
+
22
19
  /**
23
20
  * Clone object recursively while rewriting keys with the callback function.
24
21
  * Symbols are copied without modification (Sequelize.Ops are javascript symbols).
@@ -27,15 +24,12 @@ exports.plainObjectsShareNoKeys = function (object1, object2) {
27
24
  * mapKeysDeep({a: {b: 1}}, key => `_${key}_`);
28
25
  * => {_a_: {_b_: 1}}
29
26
  */
30
-
31
-
32
27
  exports.mapKeysDeep = function (object, callback) {
33
28
  if (Array.isArray(object)) {
34
29
  return object.map(function (child) {
35
30
  return exports.mapKeysDeep(child, callback);
36
31
  });
37
32
  }
38
-
39
33
  if (_lodash.default.isPlainObject(object)) {
40
34
  const newObject = {};
41
35
  Object.getOwnPropertyNames(object).forEach(function (name) {
@@ -46,6 +40,5 @@ exports.mapKeysDeep = function (object, callback) {
46
40
  });
47
41
  return newObject;
48
42
  }
49
-
50
43
  return object;
51
44
  };
@@ -1,14 +1,11 @@
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
  class Operators {
8
6
  static _isNewSequelizeOp(options) {
9
7
  return !!(options && options.Sequelize && options.Sequelize.Op);
10
8
  }
11
-
12
9
  _setupNewSequelizeOp(options) {
13
10
  const {
14
11
  Op
@@ -27,7 +24,6 @@ class Operators {
27
24
  this.NOT_LIKE = Op.notLike;
28
25
  this.OR = Op.or;
29
26
  }
30
-
31
27
  _setupOldSequelizeOp() {
32
28
  this.AND = '$and';
33
29
  this.CONTAINS = '$contains';
@@ -43,22 +39,17 @@ class Operators {
43
39
  this.NOT_LIKE = '$notLike';
44
40
  this.OR = '$or';
45
41
  }
46
-
47
42
  constructor(options) {
48
43
  if (Operators._isNewSequelizeOp(options)) {
49
44
  this._setupNewSequelizeOp(options);
50
45
  } else {
51
46
  this._setupOldSequelizeOp();
52
47
  }
53
-
54
48
  Operators._instance = this;
55
49
  }
56
-
57
50
  static getInstance(options) {
58
51
  return Operators._instance || new Operators(options);
59
52
  }
60
-
61
53
  }
62
-
63
54
  (0, _defineProperty2.default)(Operators, "_instance", null);
64
55
  module.exports = Operators;
package/dist/utils/orm.js CHANGED
@@ -1,25 +1,18 @@
1
1
  "use strict";
2
2
 
3
3
  require("core-js/modules/es.regexp.exec.js");
4
-
5
4
  const {
6
5
  SchemaUtils
7
6
  } = require('forest-express');
8
-
9
7
  const semver = require('semver');
10
-
11
8
  const REGEX_VERSION = /(\d+\.)?(\d+\.)?(\*|\d+)/;
12
-
13
9
  const getVersion = function (sequelize) {
14
10
  const version = sequelize.version.match(REGEX_VERSION);
15
-
16
11
  if (version && version[0]) {
17
12
  return version[0];
18
13
  }
19
-
20
14
  return null;
21
15
  };
22
-
23
16
  const isVersionLessThan = function (sequelize, target) {
24
17
  try {
25
18
  return semver.lt(getVersion(sequelize), target);
@@ -27,24 +20,19 @@ const isVersionLessThan = function (sequelize, target) {
27
20
  return true;
28
21
  }
29
22
  };
30
-
31
23
  const findRecord = function (model, recordId, options) {
32
24
  if (model.findByPk) {
33
25
  return model.findByPk(recordId, options);
34
26
  }
35
-
36
27
  return model.findById(recordId, options);
37
28
  };
38
-
39
29
  const getColumnName = function (schema, fieldName) {
40
30
  const schemaField = SchemaUtils.getField(schema, fieldName);
41
31
  return schemaField && schemaField.columnName ? schemaField.columnName : fieldName;
42
32
  };
43
-
44
33
  const isUUID = function (DataTypes, fieldType) {
45
34
  return fieldType instanceof DataTypes.UUID || fieldType instanceof DataTypes.UUIDV1 || fieldType instanceof DataTypes.UUIDV4;
46
35
  };
47
-
48
36
  exports.getVersion = getVersion;
49
37
  exports.isVersionLessThan = isVersionLessThan;
50
38
  exports.findRecord = findRecord;
@@ -1,52 +1,41 @@
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.array.iterator.js");
8
-
9
6
  require("core-js/modules/es.regexp.exec.js");
10
-
11
7
  var _objectTools = _interopRequireDefault(require("./object-tools"));
12
-
13
8
  var _orm = _interopRequireDefault(require("./orm"));
14
-
15
9
  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; }
16
-
17
10
  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; }
18
-
19
11
  exports.getReferenceSchema = function (schemas, modelSchema, associationName) {
20
12
  const schemaField = modelSchema.fields.find(function (field) {
21
13
  return field.field === associationName;
22
- }); // NOTICE: No reference field found, no name transformation tried.
14
+ });
23
15
 
16
+ // NOTICE: No reference field found, no name transformation tried.
24
17
  if (!schemaField || !schemaField.reference) {
25
18
  return null;
26
19
  }
27
-
28
20
  const [tableName] = schemaField.reference.split('.');
29
21
  return schemas[tableName];
30
22
  };
31
-
32
23
  exports.getReferenceField = function (schemas, modelSchema, associationName, fieldName) {
33
- const associationSchema = exports.getReferenceSchema(schemas, modelSchema, associationName, fieldName); // NOTICE: No association schema found, no name transformation tried.
24
+ const associationSchema = exports.getReferenceSchema(schemas, modelSchema, associationName, fieldName);
34
25
 
26
+ // NOTICE: No association schema found, no name transformation tried.
35
27
  if (!associationSchema) {
36
28
  return `${associationName}.${fieldName}`;
37
29
  }
38
-
39
30
  const belongsToColumnName = _orm.default.getColumnName(associationSchema, fieldName);
40
-
41
31
  return `${associationName}.${belongsToColumnName}`;
42
32
  };
33
+
43
34
  /**
44
35
  * When they don't have common keys, merge objects together.
45
36
  * This is used to avoid having too many nested 'AND' conditions on sequelize queries, which
46
37
  * makes debugging and testing more painful than it could be.
47
38
  */
48
-
49
-
50
39
  exports.mergeWhere = function (operators, ...wheres) {
51
40
  return wheres.filter(Boolean).reduce(function (where1, where2) {
52
41
  return _objectTools.default.plainObjectsShareNoKeys(where1, where2) ? _objectSpread(_objectSpread({}, where1), where2) : {
@@ -1,24 +1,18 @@
1
1
  "use strict";
2
2
 
3
3
  require("core-js/modules/es.regexp.exec.js");
4
-
5
4
  require("core-js/modules/es.string.replace.js");
6
-
7
5
  require("core-js/modules/es.regexp.constructor.js");
8
-
9
6
  const _ = require('lodash');
10
-
11
7
  function decorateForSearch(records, fields, searchValue) {
12
8
  let matchFields = {};
13
9
  records.forEach(function (record, index) {
14
10
  fields.forEach(function (fieldName) {
15
11
  const value = record[fieldName];
16
-
17
12
  if (value) {
18
13
  const searchEscaped = searchValue.replace(/[-[\]{}()*+!<=:?./\\^$|#\s,]/g, '\\$&');
19
14
  const searchHighlight = new RegExp(searchEscaped, 'i');
20
15
  const match = value.toString().match(searchHighlight);
21
-
22
16
  if (match) {
23
17
  if (!matchFields[index]) {
24
18
  matchFields[index] = {
@@ -26,18 +20,14 @@ function decorateForSearch(records, fields, searchValue) {
26
20
  search: []
27
21
  };
28
22
  }
29
-
30
23
  matchFields[index].search.push(fieldName);
31
24
  }
32
25
  }
33
26
  });
34
27
  });
35
-
36
28
  if (_.isEmpty(matchFields)) {
37
29
  matchFields = null;
38
30
  }
39
-
40
31
  return matchFields;
41
32
  }
42
-
43
33
  exports.decorateForSearch = decorateForSearch;
@@ -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.array.sort.js");
8
-
9
6
  require("core-js/modules/es.array.iterator.js");
10
-
11
7
  var _objectTools = _interopRequireDefault(require("./object-tools"));
12
-
13
8
  var _operators = _interopRequireDefault(require("./operators"));
14
-
15
9
  var _query = _interopRequireDefault(require("./query"));
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
  /**
22
13
  * Extract all where conditions along the include tree, and bubbles them up to the top in-place.
23
14
  * This allows to work around a sequelize quirk that cause nested 'where' to fail when they
@@ -30,30 +21,27 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
30
21
  */
31
22
  function bubbleWheresInPlace(operators, options) {
32
23
  var _options$include;
33
-
34
24
  const parentIncludeList = (_options$include = options.include) !== null && _options$include !== void 0 ? _options$include : [];
35
25
  parentIncludeList.forEach(function (include) {
36
26
  bubbleWheresInPlace(operators, include);
37
-
38
27
  if (include.where) {
39
28
  const newWhere = _objectTools.default.mapKeysDeep(include.where, function (key) {
40
29
  var _include$model$rawAtt, _include$model, _include$model$rawAtt2, _include$model$rawAtt3;
41
-
42
30
  // Targeting a nested field, simply nest it deeper.
43
31
  if (key[0] === '$' && key[key.length - 1] === '$') {
44
32
  return `$${include.as}.${key.substring(1)}`;
45
- } // Targeting a simple field.
46
- // Try to resolve the column name, as sequelize does not allow using model aliases here.
47
-
33
+ }
48
34
 
35
+ // Targeting a simple field.
36
+ // Try to resolve the column name, as sequelize does not allow using model aliases here.
49
37
  return `$${include.as}.${(_include$model$rawAtt = (_include$model = include.model) === null || _include$model === void 0 ? void 0 : (_include$model$rawAtt2 = _include$model.rawAttributes) === null || _include$model$rawAtt2 === void 0 ? void 0 : (_include$model$rawAtt3 = _include$model$rawAtt2[key]) === null || _include$model$rawAtt3 === void 0 ? void 0 : _include$model$rawAtt3.field) !== null && _include$model$rawAtt !== void 0 ? _include$model$rawAtt : key}$`;
50
38
  });
51
-
52
39
  options.where = _query.default.mergeWhere(operators, options.where, newWhere);
53
40
  delete include.where;
54
41
  }
55
42
  });
56
43
  }
44
+
57
45
  /**
58
46
  * Includes can be expressed in different ways in sequelize, which is inconvenient to
59
47
  * remove duplicate associations.
@@ -69,8 +57,6 @@ function bubbleWheresInPlace(operators, options) {
69
57
  * - { include: [[{ as: 'book' }]] }
70
58
  * - { include: [[{ model: Book }]] }
71
59
  */
72
-
73
-
74
60
  function normalizeInclude(model, include) {
75
61
  if (include.sequelize) {
76
62
  return {
@@ -80,33 +66,29 @@ function normalizeInclude(model, include) {
80
66
  })
81
67
  };
82
68
  }
83
-
84
69
  if (typeof include === 'string' && model.associations[include]) {
85
70
  return {
86
71
  as: include,
87
72
  model: model.associations[include].target
88
73
  };
89
74
  }
90
-
91
75
  if (typeof include === 'object') {
92
76
  if (typeof include.association === 'string' && model.associations[include.association]) {
93
77
  include.as = include.association;
94
78
  delete include.association;
95
79
  }
96
-
97
80
  if (typeof include.as === 'string' && !include.model && model.associations[include.as]) {
98
81
  const includeModel = model.associations[include.as].target;
99
82
  include.model = includeModel;
100
83
  }
101
-
102
84
  if (include.model && !include.as) {
103
85
  include.as = Object.keys(model.associations).find(function (association) {
104
86
  return model.associations[association].target.name === include.model.name;
105
87
  });
106
88
  }
107
- } // Recurse
108
-
89
+ }
109
90
 
91
+ // Recurse
110
92
  if (include.include) {
111
93
  if (Array.isArray(include.include)) {
112
94
  include.include = include.include.map(function (childInclude) {
@@ -116,62 +98,51 @@ function normalizeInclude(model, include) {
116
98
  include.include = [normalizeInclude(include.model, include.include)];
117
99
  }
118
100
  }
119
-
120
101
  return include;
121
102
  }
103
+
122
104
  /**
123
105
  * Remove duplications in a queryOption.include array in-place.
124
106
  * Using multiple times the same association yields invalid SQL when using sequelize <= 4.x
125
107
  */
126
-
127
-
128
108
  function removeDuplicateAssociations(model, includeList) {
129
109
  // Remove duplicates
130
110
  includeList.sort(function (include1, include2) {
131
111
  return include1.as < include2.as ? -1 : 1;
132
112
  });
133
-
134
113
  for (let i = 1; i < includeList.length; i += 1) {
135
114
  if (includeList[i - 1].as === includeList[i].as) {
136
115
  const newInclude = _objectSpread(_objectSpread({}, includeList[i - 1]), includeList[i]);
137
-
138
116
  if (includeList[i - 1].attributes && includeList[i].attributes) {
139
117
  // Keep 'attributes' only when defined on both sides.
140
118
  newInclude.attributes = [...new Set([...includeList[i - 1].attributes, ...includeList[i].attributes])].sort();
141
119
  } else {
142
120
  delete newInclude.attributes;
143
121
  }
144
-
145
122
  if (includeList[i - 1].include || includeList[i].include) {
146
123
  var _includeList$include, _includeList$i$includ;
147
-
148
124
  newInclude.include = [...((_includeList$include = includeList[i - 1].include) !== null && _includeList$include !== void 0 ? _includeList$include : []), ...((_includeList$i$includ = includeList[i].include) !== null && _includeList$i$includ !== void 0 ? _includeList$i$includ : [])];
149
125
  }
150
-
151
126
  includeList[i - 1] = newInclude;
152
127
  includeList.splice(i, 1);
153
128
  i -= 1;
154
129
  }
155
- } // Recurse
156
-
130
+ }
157
131
 
132
+ // Recurse
158
133
  includeList.forEach(function (include) {
159
134
  const association = model.associations[include.as];
160
-
161
135
  if (include.include && association) {
162
136
  removeDuplicateAssociations(association.target, include.include);
163
137
  }
164
138
  });
165
139
  }
166
-
167
140
  exports.postProcess = function (model, rawOptions) {
168
141
  if (!rawOptions.include) return rawOptions;
169
142
  const options = rawOptions;
170
-
171
143
  const operators = _operators.default.getInstance({
172
144
  Sequelize: model.sequelize.constructor
173
145
  });
174
-
175
146
  options.include = options.include.map(function (include) {
176
147
  return normalizeInclude(model, include);
177
148
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "forest-express-sequelize",
3
3
  "description": "Official Express/Sequelize Liana for Forest",
4
- "version": "9.2.1",
4
+ "version": "9.2.3",
5
5
  "author": "Sandro Munda <sandro@munda.me>",
6
6
  "contributors": [
7
7
  "Arnaud Besnier <arnaudibesnier@gmail.com>",
@@ -28,7 +28,7 @@
28
28
  "@babel/runtime": "7.15.4",
29
29
  "bluebird": "2.9.25",
30
30
  "core-js": "3.6.5",
31
- "forest-express": "10.1.2",
31
+ "forest-express": "10.1.6",
32
32
  "http-errors": "1.6.1",
33
33
  "lodash": "4.17.21",
34
34
  "moment": "2.29.4",