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
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [9.2.3](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.2.2...v9.2.3) (2023-01-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **security:** patch forest-express dependency vulnerability ([#1064](https://github.com/ForestAdmin/forest-express-sequelize/issues/1064)) ([66a5f68](https://github.com/ForestAdmin/forest-express-sequelize/commit/66a5f68a810b9eefaf1edbf26c0326f6156009a2))
7
+
8
+ ## [9.2.2](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.2.1...v9.2.2) (2023-01-19)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **security:** patch dependencies of dev dependencies vulnerabilities ([#1063](https://github.com/ForestAdmin/forest-express-sequelize/issues/1063)) ([64d17a6](https://github.com/ForestAdmin/forest-express-sequelize/commit/64d17a654f949d45c0f1bc8e96dfe38aca5b8706))
14
+
1
15
  ## [9.2.1](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.2.0...v9.2.1) (2023-01-17)
2
16
 
3
17
 
@@ -1,41 +1,29 @@
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 P = require('bluebird');
8
-
9
6
  const Interface = require('forest-express');
10
-
11
7
  const ApimapFieldBuilder = require('../services/apimap-field-builder');
12
-
13
8
  const ApimapFieldTypeDetector = require('../services/apimap-field-type-detector');
14
-
15
9
  const isPrimaryKeyAForeignKey = require('../utils/is-primary-key-a-foreign-key');
16
-
17
10
  module.exports = function (model, opts) {
18
11
  const fields = [];
19
12
  const fieldNamesToExclude = [];
20
-
21
13
  function getTypeForAssociation(association) {
22
14
  const attribute = association.target.rawAttributes[association.targetKey];
23
15
  const type = attribute ? new ApimapFieldTypeDetector(attribute, opts).perform() : 'Number';
24
-
25
16
  switch (association.associationType) {
26
17
  case 'BelongsTo':
27
18
  case 'HasOne':
28
19
  return type;
29
-
30
20
  case 'HasMany':
31
21
  case 'BelongsToMany':
32
22
  return [type];
33
-
34
23
  default:
35
24
  return null;
36
25
  }
37
26
  }
38
-
39
27
  function getInverseOf(association) {
40
28
  // Notice: get inverse relation field
41
29
  // return null if not found
@@ -47,14 +35,11 @@ module.exports = function (model, opts) {
47
35
  const field = association.associationType === 'BelongsToMany' ? foreignIdentifierField : identifierField;
48
36
  return a.identifierField === field && association.source.name === a.target.name;
49
37
  });
50
-
51
38
  if (remoteAssociation) {
52
39
  return remoteAssociation.associationAccessor;
53
40
  }
54
-
55
41
  return null;
56
42
  }
57
-
58
43
  function getSchemaForAssociation(association) {
59
44
  const schema = {
60
45
  field: association.associationAccessor,
@@ -62,26 +47,24 @@ module.exports = function (model, opts) {
62
47
  relationship: association.associationType,
63
48
  reference: `${association.target.name}.${association.target.primaryKeyField}`,
64
49
  inverseOf: getInverseOf(association)
65
- }; // NOTICE: Detect potential foreign keys that should be excluded, if a
66
- // constraints property is set for example.
50
+ };
67
51
 
52
+ // NOTICE: Detect potential foreign keys that should be excluded, if a
53
+ // constraints property is set for example.
68
54
  if (association.associationType === 'BelongsTo') {
69
55
  fieldNamesToExclude.push(association.identifierField);
70
56
  }
71
-
72
57
  return schema;
73
- } // FIXME: In `model.rawAttributes`, TEXT default values loose their inclosing quotes.
74
- // eg: "'quoted string'" => "quoted string"
75
-
58
+ }
76
59
 
60
+ // FIXME: In `model.rawAttributes`, TEXT default values loose their inclosing quotes.
61
+ // eg: "'quoted string'" => "quoted string"
77
62
  const columns = P.each(_.values(model.rawAttributes), function (column) {
78
63
  try {
79
64
  if (column.references && !column.primaryKey) {
80
65
  return;
81
66
  }
82
-
83
67
  const schema = new ApimapFieldBuilder(model, column, opts).perform();
84
-
85
68
  if (schema.type) {
86
69
  fields.push(schema);
87
70
  }
@@ -99,34 +82,26 @@ module.exports = function (model, opts) {
99
82
  });
100
83
  return P.all([columns, associations]).then(function () {
101
84
  let isCompositePrimary = false;
102
-
103
85
  const primaryKeys = _.keys(model.primaryKeys);
104
-
105
86
  let idField = primaryKeys[0];
106
-
107
87
  if (_.keys(model.primaryKeys).length > 1) {
108
88
  isCompositePrimary = true;
109
89
  idField = 'forestCompositePrimary';
110
90
  }
111
-
112
91
  Object.entries(model.associations).forEach(function ([, association]) {
113
92
  const primaryKeyIsAForeignKey = isPrimaryKeyAForeignKey(association);
114
-
115
93
  if (primaryKeyIsAForeignKey) {
116
94
  const FieldWithForeignKey = fields.find(function (field) {
117
95
  return field.reference === `${association.target.name}.${association.target.primaryKeyField}`;
118
96
  });
119
-
120
97
  if (FieldWithForeignKey) {
121
98
  FieldWithForeignKey.foreignAndPrimaryKey = true;
122
99
  }
123
100
  }
124
101
  });
125
-
126
102
  _.remove(fields, function (field) {
127
103
  return _.includes(fieldNamesToExclude, field.columnName) && !field.isPrimaryKey;
128
104
  });
129
-
130
105
  return {
131
106
  name: model.name,
132
107
  idField,
package/dist/index.js CHANGED
@@ -1,69 +1,40 @@
1
1
  "use strict";
2
2
 
3
3
  require("core-js/modules/es.promise.js");
4
-
5
4
  require("core-js/modules/es.regexp.exec.js");
6
-
7
5
  require("core-js/modules/web.url.to-json.js");
8
-
9
6
  const P = require('bluebird');
10
-
11
7
  const Interface = require('forest-express');
12
-
13
8
  const orm = require('./utils/orm');
14
-
15
9
  const lianaPackage = require('../package.json');
16
-
17
10
  const SchemaAdapter = require('./adapters/sequelize');
18
-
19
11
  const ResourcesGetter = require('./services/resources-getter');
20
-
21
12
  const ResourceGetter = require('./services/resource-getter');
22
-
23
13
  const ResourceCreator = require('./services/resource-creator');
24
-
25
14
  const ResourceUpdater = require('./services/resource-updater');
26
-
27
15
  const ResourceRemover = require('./services/resource-remover');
28
-
29
16
  const ResourcesExporter = require('./services/resources-exporter');
30
-
31
17
  const ResourcesRemover = require('./services/resources-remover');
32
-
33
18
  const HasManyGetter = require('./services/has-many-getter');
34
-
35
19
  const HasManyAssociator = require('./services/has-many-associator');
36
-
37
20
  const HasManyDissociator = require('./services/has-many-dissociator');
38
-
39
21
  const BelongsToUpdater = require('./services/belongs-to-updater');
40
-
41
22
  const ValueStatGetter = require('./services/value-stat-getter');
42
-
43
23
  const PieStatGetter = require('./services/pie-stat-getter');
44
-
45
24
  const LineStatGetter = require('./services/line-stat-getter');
46
-
47
25
  const LeaderboardStatGetter = require('./services/leaderboard-stat-getter');
48
-
49
26
  const QueryStatGetter = require('./services/query-stat-getter');
50
-
51
27
  const FiltersParser = require('./services/filters-parser');
52
-
53
28
  const RecordsDecorator = require('./utils/records-decorator');
54
-
55
29
  const makeParseFilter = require('./public/parse-filter');
56
-
57
30
  const REGEX_VERSION = /(\d+\.)?(\d+\.)?(\*|\d+)/;
58
31
  exports.collection = Interface.collection;
59
32
  exports.ensureAuthenticated = Interface.ensureAuthenticated;
60
-
61
33
  exports.errorHandler = function () {
62
34
  return Interface.errorHandler({
63
35
  logger: Interface.logger
64
36
  });
65
37
  };
66
-
67
38
  exports.StatSerializer = Interface.StatSerializer;
68
39
  exports.ResourceSerializer = Interface.ResourceSerializer;
69
40
  exports.ResourceDeserializer = Interface.ResourceDeserializer;
@@ -83,52 +54,40 @@ exports.RecordSerializer = Interface.RecordSerializer;
83
54
  exports.BaseOperatorDateParser = Interface.BaseOperatorDateParser;
84
55
  exports.parseFilter = makeParseFilter(FiltersParser, exports);
85
56
  exports.PUBLIC_ROUTES = Interface.PUBLIC_ROUTES;
86
-
87
57
  exports.init = function init(opts) {
88
58
  exports.opts = opts;
89
-
90
59
  if (!opts.objectMapping) {
91
60
  Interface.logger.error('The objectMapping option appears to be missing. Please make sure it is set correctly.');
92
61
  return Promise.resolve(function () {});
93
62
  }
94
-
95
63
  if (opts.sequelize) {
96
64
  Interface.logger.warn('The sequelize option is not supported anymore. Please remove this option.');
97
65
  }
98
-
99
66
  opts.Sequelize = opts.objectMapping;
100
67
  opts.useMultipleDatabases = Object.keys(opts.connections).length > 1;
101
-
102
68
  exports.getLianaName = function getLianaName() {
103
69
  return 'forest-express-sequelize';
104
70
  };
105
-
106
71
  exports.getLianaVersion = function getLianaVersion() {
107
72
  const lianaVersion = lianaPackage.version.match(REGEX_VERSION);
108
-
109
73
  if (lianaVersion && lianaVersion[0]) {
110
74
  return lianaVersion[0];
111
75
  }
112
-
113
76
  return null;
114
77
  };
115
-
116
78
  exports.getOrmVersion = function getOrmVersion() {
117
79
  return orm.getVersion(opts.Sequelize);
118
80
  };
119
-
120
81
  exports.getDatabaseType = function getDatabaseType() {
121
82
  if (opts.useMultipleDatabases) return 'multiple';
122
83
  return Object.values(opts.connections)[0].options.dialect;
123
84
  };
124
-
125
85
  exports.SchemaAdapter = SchemaAdapter;
126
-
127
86
  exports.getModelName = function getModelName(model) {
128
87
  return model.name;
129
- }; // TODO: Remove nameOld attribute once the lianas versions older than 2.0.0 are minority
130
-
88
+ };
131
89
 
90
+ // TODO: Remove nameOld attribute once the lianas versions older than 2.0.0 are minority
132
91
  exports.getModelNameOld = exports.getModelName;
133
92
  exports.ResourcesGetter = ResourcesGetter;
134
93
  exports.ResourceGetter = ResourceGetter;
@@ -154,11 +113,9 @@ exports.init = function init(opts) {
154
113
  if (customer && customer[customerField]) {
155
114
  return customer.toJSON();
156
115
  }
157
-
158
116
  return P.reject();
159
117
  });
160
118
  }
161
-
162
119
  return P.resolve();
163
120
  },
164
121
  getCustomerByUserField: function (customerModel, customerField, userField) {
@@ -167,7 +124,6 @@ exports.init = function init(opts) {
167
124
  return resolve();
168
125
  });
169
126
  }
170
-
171
127
  const query = {};
172
128
  query[customerField] = userField;
173
129
  return customerModel.findOne({
@@ -176,7 +132,6 @@ exports.init = function init(opts) {
176
132
  if (!customer) {
177
133
  return null;
178
134
  }
179
-
180
135
  return customer.toJSON();
181
136
  });
182
137
  }
@@ -199,11 +154,9 @@ exports.init = function init(opts) {
199
154
  if (!customer || !customer[customerField]) {
200
155
  return reject();
201
156
  }
202
-
203
157
  return resolve(customer);
204
158
  });
205
159
  }
206
-
207
160
  return resolve();
208
161
  });
209
162
  }
@@ -215,7 +168,6 @@ exports.init = function init(opts) {
215
168
  return user.toJSON();
216
169
  });
217
170
  }
218
-
219
171
  return P.resolve();
220
172
  }
221
173
  };
@@ -13,5 +13,4 @@ function makeParseFilter(FiltersParser, publicExports) {
13
13
  return parser.perform(JSON.stringify(filter));
14
14
  };
15
15
  }
16
-
17
16
  module.exports = makeParseFilter;
@@ -1,40 +1,32 @@
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
  const _ = require('lodash');
8
-
9
6
  const ApimapFieldTypeDetector = require('./apimap-field-type-detector');
10
-
11
7
  function ApimapFieldBuilder(model, column, options) {
12
8
  const DataTypes = options.Sequelize;
13
-
14
9
  function isRequired() {
15
10
  // eslint-disable-next-line
16
11
  return column._autoGenerated !== true && (column.allowNull === false || Boolean(column.primaryKey));
17
12
  }
18
-
19
13
  function getValidations(automaticValue) {
20
- const validations = []; // NOTICE: Do not inspect validation for autogenerated fields, it would
14
+ const validations = [];
15
+
16
+ // NOTICE: Do not inspect validation for autogenerated fields, it would
21
17
  // block the record creation/update.
22
18
  // eslint-disable-next-line
23
-
24
19
  if (automaticValue || column._autoGenerated === true) {
25
20
  return validations;
26
21
  }
27
-
28
22
  if (column.allowNull === false) {
29
23
  validations.push({
30
24
  type: 'is present'
31
25
  });
32
26
  }
33
-
34
27
  if (!column.validate) {
35
28
  return validations;
36
29
  }
37
-
38
30
  if (column.validate.min) {
39
31
  validations.push({
40
32
  type: 'is greater than',
@@ -42,7 +34,6 @@ function ApimapFieldBuilder(model, column, options) {
42
34
  message: column.validate.min.msg
43
35
  });
44
36
  }
45
-
46
37
  if (column.validate.max) {
47
38
  validations.push({
48
39
  type: 'is less than',
@@ -50,7 +41,6 @@ function ApimapFieldBuilder(model, column, options) {
50
41
  message: column.validate.max.msg
51
42
  });
52
43
  }
53
-
54
44
  if (column.validate.isBefore) {
55
45
  validations.push({
56
46
  type: 'is before',
@@ -58,7 +48,6 @@ function ApimapFieldBuilder(model, column, options) {
58
48
  message: column.validate.isBefore.msg
59
49
  });
60
50
  }
61
-
62
51
  if (column.validate.isAfter) {
63
52
  validations.push({
64
53
  type: 'is after',
@@ -66,17 +55,14 @@ function ApimapFieldBuilder(model, column, options) {
66
55
  message: column.validate.isAfter.msg
67
56
  });
68
57
  }
69
-
70
58
  if (column.validate.len) {
71
59
  const length = column.validate.len.args || column.validate.len;
72
-
73
60
  if (_.isArray(length) && !_.isNull(length[0]) && !_.isUndefined(length[0])) {
74
61
  validations.push({
75
62
  type: 'is longer than',
76
63
  value: length[0],
77
64
  message: column.validate.len.msg
78
65
  });
79
-
80
66
  if (length[1]) {
81
67
  validations.push({
82
68
  type: 'is shorter than',
@@ -92,7 +78,6 @@ function ApimapFieldBuilder(model, column, options) {
92
78
  });
93
79
  }
94
80
  }
95
-
96
81
  if (column.validate.contains) {
97
82
  validations.push({
98
83
  type: 'contains',
@@ -100,7 +85,6 @@ function ApimapFieldBuilder(model, column, options) {
100
85
  message: column.validate.contains.msg
101
86
  });
102
87
  }
103
-
104
88
  if (column.validate.is && !_.isArray(column.validate.is)) {
105
89
  const value = column.validate.is.args || column.validate.is;
106
90
  validations.push({
@@ -109,16 +93,14 @@ function ApimapFieldBuilder(model, column, options) {
109
93
  message: column.validate.is.msg
110
94
  });
111
95
  }
112
-
113
96
  return validations;
114
- } // NOTICE: Remove Sequelize.Utils.Literal wrapper to display actual value in UI.
97
+ }
98
+
99
+ // NOTICE: Remove Sequelize.Utils.Literal wrapper to display actual value in UI.
115
100
  // Keep only simple values, and hide expressions.
116
101
  // Do not export literal values to UI by default.
117
-
118
-
119
102
  function unwrapLiteral(literalValue, columnType) {
120
103
  let value;
121
-
122
104
  if (_.isString(literalValue)) {
123
105
  if (['true', 'false'].includes(literalValue.toLowerCase())) {
124
106
  value = Boolean(literalValue);
@@ -127,18 +109,16 @@ function ApimapFieldBuilder(model, column, options) {
127
109
  value = _.toNumber(literalValue);
128
110
  } else {
129
111
  value = literalValue;
130
- } // NOTICE: Only single quotes are widely considered valid to delimitate string values.
131
-
112
+ }
113
+ // NOTICE: Only single quotes are widely considered valid to delimitate string values.
132
114
  } else if (literalValue.match(/^'.*'$/)) {
133
115
  value = literalValue.substring(1, literalValue.length - 1);
134
116
  }
135
117
  } else if (_.isBoolean(literalValue) || _.isNumber(literalValue)) {
136
118
  value = literalValue;
137
119
  }
138
-
139
120
  return value;
140
121
  }
141
-
142
122
  this.perform = function () {
143
123
  const schema = {
144
124
  field: column.fieldName,
@@ -147,32 +127,28 @@ function ApimapFieldBuilder(model, column, options) {
147
127
  // column names
148
128
  columnName: column.field
149
129
  };
150
-
151
130
  if (column.primaryKey === true) {
152
131
  schema.isPrimaryKey = true;
153
132
  }
154
-
155
133
  if (schema.type === 'Enum') {
156
134
  schema.enums = column.values;
157
- } // NOTICE: Create enums from sub-type (for ['Enum'] type).
158
-
135
+ }
159
136
 
137
+ // NOTICE: Create enums from sub-type (for ['Enum'] type).
160
138
  if (Array.isArray(schema.type) && schema.type[0] === 'Enum') {
161
139
  schema.enums = column.type.type.values;
162
140
  }
163
-
164
141
  if (isRequired()) {
165
142
  schema.isRequired = true;
166
143
  }
167
-
168
144
  const canHaveDynamicDefaultValue = ['Date', 'Dateonly'].indexOf(schema.type) !== -1 || column.type instanceof DataTypes.UUID;
169
145
  const isDefaultValueFunction = typeof column.defaultValue === 'function' || canHaveDynamicDefaultValue && typeof column.defaultValue === 'object';
170
-
171
146
  if (!_.isNull(column.defaultValue) && !_.isUndefined(column.defaultValue)) {
172
147
  // NOTICE: Prevent sequelize.Sequelize.NOW to be defined as the default value as the client
173
148
  // does not manage it properly so far.
174
149
  if (isDefaultValueFunction) {
175
- schema.isRequired = false; // NOTICE: Do not use the primary keys default values to prevent issues with UUID fields
150
+ schema.isRequired = false;
151
+ // NOTICE: Do not use the primary keys default values to prevent issues with UUID fields
176
152
  // (defaultValue: DataTypes.UUIDV4).
177
153
  } else if (!_.includes(_.keys(model.primaryKeys), column.fieldName)) {
178
154
  // FIXME: `column.defaultValue instanceof Sequelize.Utils.Literal` fails for unknown reason.
@@ -183,15 +159,11 @@ function ApimapFieldBuilder(model, column, options) {
183
159
  }
184
160
  }
185
161
  }
186
-
187
162
  schema.validations = getValidations(isDefaultValueFunction);
188
-
189
163
  if (schema.validations.length === 0) {
190
164
  delete schema.validations;
191
165
  }
192
-
193
166
  return schema;
194
167
  };
195
168
  }
196
-
197
169
  module.exports = ApimapFieldBuilder;
@@ -2,58 +2,45 @@
2
2
 
3
3
  function ApimapFieldTypeDetector(column, options) {
4
4
  const DataTypes = options.Sequelize;
5
-
6
5
  this.perform = function () {
7
6
  if (column.type instanceof DataTypes.STRING || column.type instanceof DataTypes.TEXT || DataTypes.CITEXT && column.type instanceof DataTypes.CITEXT || column.type === 'citext') {
8
7
  // TODO: Remove 'citext' once Sequelize 4 has been deprecated.
9
8
  return 'String';
10
9
  }
11
-
12
10
  if (column.type instanceof DataTypes.ENUM) {
13
11
  return 'Enum';
14
12
  }
15
-
16
13
  if (column.type instanceof DataTypes.BOOLEAN) {
17
14
  return 'Boolean';
18
15
  }
19
-
20
16
  if (column.type instanceof DataTypes.DATEONLY) {
21
17
  return 'Dateonly';
22
18
  }
23
-
24
19
  if (column.type instanceof DataTypes.DATE) {
25
20
  return 'Date';
26
21
  }
27
-
28
22
  if (column.type instanceof DataTypes.INTEGER || column.type instanceof DataTypes.FLOAT || column.type instanceof DataTypes['DOUBLE PRECISION'] || column.type instanceof DataTypes.BIGINT || column.type instanceof DataTypes.DECIMAL) {
29
23
  return 'Number';
30
24
  }
31
-
32
25
  if (column.type instanceof DataTypes.JSONB || column.type instanceof DataTypes.JSON) {
33
26
  return 'Json';
34
27
  }
35
-
36
28
  if (column.type instanceof DataTypes.TIME) {
37
29
  return 'Time';
38
30
  }
39
-
40
31
  if (column.type instanceof DataTypes.GEOMETRY && column.type.type === 'POINT') {
41
32
  return 'Point';
42
33
  }
43
-
44
34
  if (column.type instanceof DataTypes.UUID || column.type instanceof DataTypes.UUIDV1 || column.type instanceof DataTypes.UUIDV4) {
45
35
  return 'Uuid';
46
- } // NOTICE: Detect Array types (Array(String), Array(Integer), ...)
47
-
48
-
36
+ }
37
+ // NOTICE: Detect Array types (Array(String), Array(Integer), ...)
49
38
  if (column.type.type) {
50
39
  return [new ApimapFieldTypeDetector({
51
40
  type: column.type.type
52
41
  }, options).perform()];
53
42
  }
54
-
55
43
  return null;
56
44
  };
57
45
  }
58
-
59
46
  module.exports = ApimapFieldTypeDetector;
@@ -1,13 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  require("core-js/modules/es.promise.js");
4
-
5
4
  const _ = require('lodash');
6
-
7
5
  const orm = require('../utils/orm');
8
-
9
6
  const associationRecord = require('../utils/association-record');
10
-
11
7
  class BelongsToUpdater {
12
8
  constructor(model, assoc, opts, params, data) {
13
9
  this.model = model;
@@ -15,19 +11,17 @@ class BelongsToUpdater {
15
11
  this.opts = opts;
16
12
  this.params = params;
17
13
  this.data = data;
18
- } // WORKAROUND: Make the hasOne associations update work while waiting
14
+ }
15
+
16
+ // WORKAROUND: Make the hasOne associations update work while waiting
19
17
  // for the Sequelize 4 release with the fix of the following
20
18
  // issue: https://github.com/sequelize/sequelize/issues/6069
21
-
22
-
23
19
  async _getTargetKey(association) {
24
20
  const pk = this.data.data.id;
25
21
  const targetKeyIsPrimaryKey = association.targetKey === association.target.primaryKeyAttribute;
26
22
  let targetKey = pk;
27
-
28
23
  if (association.associationType === 'HasOne' || !targetKeyIsPrimaryKey) {
29
24
  const record = await associationRecord.get(association.target, pk);
30
-
31
25
  if (association.associationType === 'HasOne') {
32
26
  targetKey = record;
33
27
  } else if (!targetKeyIsPrimaryKey) {
@@ -35,10 +29,8 @@ class BelongsToUpdater {
35
29
  targetKey = record[association.targetKey];
36
30
  }
37
31
  }
38
-
39
32
  return targetKey;
40
33
  }
41
-
42
34
  async perform() {
43
35
  const {
44
36
  associationName,
@@ -52,16 +44,13 @@ class BelongsToUpdater {
52
44
  const options = {
53
45
  fields: null
54
46
  };
55
-
56
47
  if (association && this.data.data) {
57
- const targetKey = await this._getTargetKey(association); // NOTICE: Enable model hooks to change fields values during an association update.
48
+ const targetKey = await this._getTargetKey(association);
58
49
 
50
+ // NOTICE: Enable model hooks to change fields values during an association update.
59
51
  return record[setterName](targetKey, options);
60
52
  }
61
-
62
53
  return record[setterName](null, options);
63
54
  }
64
-
65
55
  }
66
-
67
56
  module.exports = BelongsToUpdater;
@@ -6,25 +6,20 @@ function ErrorHTTP422(message) {
6
6
  this.status = 422;
7
7
  this.stack = new Error().stack;
8
8
  }
9
-
10
9
  ErrorHTTP422.prototype = new Error();
11
-
12
10
  function NoMatchingOperatorError(message) {
13
11
  this.name = 'NoMatchingOperatorError';
14
12
  this.message = message || 'The given operator is not handled.';
15
13
  this.status = 422;
16
14
  this.stack = new Error().stack;
17
15
  }
18
-
19
16
  NoMatchingOperatorError.prototype = new Error();
20
-
21
17
  function InvalidParameterError(message) {
22
18
  this.name = 'InvalidParameterError';
23
19
  this.message = message || 'The given parameter is invalid.';
24
20
  this.status = 422;
25
21
  this.stack = new Error().stack;
26
22
  }
27
-
28
23
  InvalidParameterError.prototype = new Error();
29
24
  exports.ErrorHTTP422 = ErrorHTTP422;
30
25
  exports.NoMatchingOperatorError = NoMatchingOperatorError;