Skip to content

Commit b818a95

Browse files
Merge branch 'master' into beta (#564)
2 parents f51823d + e002d42 commit b818a95

File tree

8 files changed

+3163
-3032
lines changed

8 files changed

+3163
-3032
lines changed

CHANGELOG.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55

66
* fix incorrect usage of the new connections parameter ([#557](https://github.com/ForestAdmin/forest-express-sequelize/issues/557)) ([2840e41](https://github.com/ForestAdmin/forest-express-sequelize/commit/2840e418bb782a83b861dda217d45b3d70175253))
77

8+
# [6.5.0](https://github.com/ForestAdmin/forest-express-sequelize/compare/v6.4.2...v6.5.0) (2020-11-27)
9+
10+
11+
### Features
12+
13+
* **smart actions:** endpoint that handle forms' load hooks ([0902ef4](https://github.com/ForestAdmin/forest-express-sequelize/commit/0902ef458abf41efb25f0dc024549702a50645fe))
14+
15+
## [6.4.2](https://github.com/ForestAdmin/forest-express-sequelize/compare/v6.4.1...v6.4.2) (2020-11-26)
16+
17+
18+
### Bug Fixes
19+
20+
* export error handler middleware ([#552](https://github.com/ForestAdmin/forest-express-sequelize/issues/552)) ([a87591b](https://github.com/ForestAdmin/forest-express-sequelize/commit/a87591b8d5cffc5041fedcb654be2fadfb6dd78f))
21+
22+
## [6.4.1](https://github.com/ForestAdmin/forest-express-sequelize/compare/v6.4.0...v6.4.1) (2020-11-26)
23+
24+
25+
### Bug Fixes
26+
27+
* always return primary keys of associated records ([#550](https://github.com/ForestAdmin/forest-express-sequelize/issues/550)) ([57bd84b](https://github.com/ForestAdmin/forest-express-sequelize/commit/57bd84bc992bbe5fd703b2e36c02bb431885535d))
28+
829
# [7.0.0-beta.1](https://github.com/ForestAdmin/forest-express-sequelize/compare/v6.4.0...v7.0.0-beta.1) (2020-11-24)
930

1031

@@ -19,8 +40,6 @@
1940
connections and objectMapping is now required on Liana.init().
2041
Update forest-express dependency to 8.0.0-beta.1 (See https://github.com/ForestAdmin/forest-express/tree/v8.0.0-beta.1)
2142

22-
Co-authored-by: jeffladiray <[email protected]>
23-
2443
# [6.4.0](https://github.com/ForestAdmin/forest-express-sequelize/compare/v6.3.15...v6.4.0) (2020-11-17)
2544

2645

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
"@babel/plugin-transform-runtime": "7.10.1",
4141
"@babel/preset-env": "7.10.1",
4242
"@babel/register": "7.10.1",
43-
"@commitlint/cli": "8.3.5",
44-
"@commitlint/config-conventional": "8.3.4",
45-
"@commitlint/travis-cli": "8.3.5",
43+
"@commitlint/cli": "11.0.0",
44+
"@commitlint/config-conventional": "11.0.0",
45+
"@commitlint/travis-cli": "11.0.0",
4646
"@semantic-release/changelog": "5.0.1",
4747
"@semantic-release/git": "9.0.0",
4848
"@types/jest": "26.0.9",
@@ -57,7 +57,7 @@
5757
"mysql2": "2.1.0",
5858
"onchange": "6.0.0",
5959
"pg": "8.4.2",
60-
"semantic-release": "17.0.7",
60+
"semantic-release": "17.3.0",
6161
"semantic-release-slack-bot": "1.6.2",
6262
"sequelize": "5.21.3",
6363
"sequelize-fixtures": "1.1.1",

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const REGEX_VERSION = /(\d+\.)?(\d+\.)?(\*|\d+)/;
3030

3131
exports.collection = Interface.collection;
3232
exports.ensureAuthenticated = Interface.ensureAuthenticated;
33+
exports.errorHandler = () => Interface.errorHandler({ logger: Interface.logger });
3334
exports.StatSerializer = Interface.StatSerializer;
3435
exports.ResourceSerializer = Interface.ResourceSerializer;
3536
exports.ResourceDeserializer = Interface.ResourceDeserializer;

src/services/query-builder.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
11
import { Schemas } from 'forest-express';
22
import Orm from '../utils/orm';
33

4+
const HAS_ONE = 'HasOne';
5+
const BELONGS_TO = 'BelongsTo';
6+
47
const { getReferenceField } = require('../utils/query');
58

9+
/**
10+
* @param {string[]} values
11+
* @returns {string[]}
12+
*/
13+
function uniqueValues(values) {
14+
return Array.from(new Set(values));
15+
}
16+
17+
/**
18+
* @param {string} key
19+
* @param {import('sequelize').Association} association
20+
* @returns {string}
21+
*/
22+
function getTargetFieldName(key, association) {
23+
// Defensive programming
24+
if (key && association.target.tableAttributes[key]) {
25+
return association.target.tableAttributes[key].fieldName;
26+
}
27+
28+
return undefined;
29+
}
30+
31+
/**
32+
* @param {import('sequelize').HasOne|import('sequelize').BelongsTo} association
33+
* @returns {string[]}
34+
*/
35+
function getMandatoryFields(association) {
36+
return association.target.primaryKeyAttributes
37+
.map((attribute) => getTargetFieldName(attribute, association));
38+
}
39+
640
function QueryBuilder(model, opts, params) {
741
const schema = Schemas.schemas[model.name];
842

@@ -28,7 +62,7 @@ function QueryBuilder(model, opts, params) {
2862
const includes = [];
2963

3064
Object.values(modelForIncludes.associations)
31-
.filter((association) => ['HasOne', 'BelongsTo'].includes(association.associationType))
65+
.filter((association) => [HAS_ONE, BELONGS_TO].includes(association.associationType))
3266
.forEach((association) => {
3367
const targetFields = Object.values(association.target.tableAttributes)
3468
.map((attribute) => attribute.fieldName);
@@ -43,10 +77,10 @@ function QueryBuilder(model, opts, params) {
4377
|| explicitAttributes.length) {
4478
// NOTICE: For performance reasons, we only request the keys
4579
// as they're the only needed fields for the interface
46-
const uniqueExplicitAttributes = Array.from(new Set([
47-
association.targetKey,
80+
const uniqueExplicitAttributes = uniqueValues([
81+
...getMandatoryFields(association),
4882
...explicitAttributes,
49-
].filter(Boolean)));
83+
].filter(Boolean));
5084

5185
const attributes = explicitAttributes.length
5286
? uniqueExplicitAttributes

0 commit comments

Comments
 (0)