forest-express-sequelize 8.4.6 → 8.4.10

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.
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ npx --no-install commitlint --edit "$1"
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ node ./.eslint-bin/pre-commit-hook.js
package/CHANGELOG.md CHANGED
@@ -1,3 +1,31 @@
1
+ ## [8.4.10](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.9...v8.4.10) (2022-01-31)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * fix association with primary key different of 'id' ([#878](https://github.com/ForestAdmin/forest-express-sequelize/issues/878)) ([27bcac5](https://github.com/ForestAdmin/forest-express-sequelize/commit/27bcac53f9f657559531ba9e2d621dfd77bc6e8f))
7
+
8
+ ## [8.4.9](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.8...v8.4.9) (2022-01-31)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * return data of field who have the same name as model ([#880](https://github.com/ForestAdmin/forest-express-sequelize/issues/880)) ([cee9327](https://github.com/ForestAdmin/forest-express-sequelize/commit/cee9327ac9fb5dec1e2c6293a4955390db059ff8))
14
+
15
+ ## [8.4.8](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.7...v8.4.8) (2021-11-30)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * add searchFields type in the collection options ([#875](https://github.com/ForestAdmin/forest-express-sequelize/issues/875)) ([8a9a84a](https://github.com/ForestAdmin/forest-express-sequelize/commit/8a9a84a263741fc3ed1814eb954dec6ca8bb5626))
21
+
22
+ ## [8.4.7](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.6...v8.4.7) (2021-10-25)
23
+
24
+
25
+ ### Bug Fixes
26
+
27
+ * add warning message when connections does not have any models ([#866](https://github.com/ForestAdmin/forest-express-sequelize/issues/866)) ([4c41bd7](https://github.com/ForestAdmin/forest-express-sequelize/commit/4c41bd765f156234053634e98df59c779c8ecd0e))
28
+
1
29
  ## [8.4.6](https://github.com/ForestAdmin/forest-express-sequelize/compare/v8.4.5...v8.4.6) (2021-10-25)
2
30
 
3
31
 
@@ -22,14 +22,15 @@ class BelongsToUpdater {
22
22
 
23
23
  async _getTargetKey(association) {
24
24
  const pk = this.data.data.id;
25
+ const targetKeyIsPrimaryKey = association.targetKey === association.target.primaryKeyAttribute;
25
26
  let targetKey = pk;
26
27
 
27
- if (association.associationType === 'HasOne' || association.targetKey !== 'id') {
28
+ if (association.associationType === 'HasOne' || !targetKeyIsPrimaryKey) {
28
29
  const record = await associationRecord.get(association.target, pk);
29
30
 
30
31
  if (association.associationType === 'HasOne') {
31
32
  targetKey = record;
32
- } else if (association.targetKey !== 'id') {
33
+ } else if (!targetKeyIsPrimaryKey) {
33
34
  // NOTICE: special use case with foreign key non pointing to a primary key
34
35
  targetKey = record[association.targetKey];
35
36
  }
@@ -78,7 +78,7 @@ function extractRequestedFields(requestedFields, modelOrAssociation, schemas) {
78
78
  return extractRequestedFieldsForAssociation(associationName, requestedFields && requestedFields[associationName] ? requestedFields[associationName].split(',') : [], schemas[modelOrAssociation.associations[associationName].target.name]);
79
79
  }).flat();
80
80
  const modelFields = requestedFields[modelOrAssociation.name].split(',').filter(function (fieldName) {
81
- return !requestedFields[fieldName];
81
+ return Object.prototype.hasOwnProperty.call(modelOrAssociation.rawAttributes, fieldName);
82
82
  });
83
83
  const smartFields = extractRequestedSmartField(requestedFields, schemas[modelOrAssociation.name]);
84
84
  return Array.from(new Set([...primaryKeyArray, ...modelFields, ...smartFields, ...allAssociationFields]));
@@ -43,7 +43,7 @@ class ResourceCreator {
43
43
  const primaryKey = this.body[name];
44
44
  let targetKey = primaryKey;
45
45
 
46
- if (typeof primaryKey !== 'undefined' && association.targetKey !== 'id') {
46
+ if (primaryKey && association.targetKey !== association.target.primaryKeyAttribute) {
47
47
  const record = await associationRecord.get(association.target, primaryKey);
48
48
  targetKey = record[association.targetKey];
49
49
  }
@@ -2,7 +2,7 @@ postgres :
2
2
  image : postgres:9.4.5
3
3
  container_name : forest_express_sequelize_postgres
4
4
  ports :
5
- - "5436:5432"
5
+ - "5437:5432"
6
6
  environment:
7
7
  - POSTGRES_DB=forest-express-sequelize-test
8
8
  - POSTGRES_USER=forest
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": "8.4.6",
4
+ "version": "8.4.10",
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": "9.3.1",
31
+ "forest-express": "9.3.2",
32
32
  "http-errors": "1.6.1",
33
33
  "lodash": "4.17.21",
34
34
  "moment": "2.19.4",
@@ -57,14 +57,14 @@
57
57
  "eslint-plugin-import": "2.18.2",
58
58
  "eslint-plugin-jest": "23.0.4",
59
59
  "eslint-plugin-sonarjs": "0.5.0",
60
- "husky": "4.2.5",
60
+ "husky": "7.0.4",
61
61
  "jest": "26.6.3",
62
62
  "jest-extended": "0.11.5",
63
63
  "mysql2": "2.1.0",
64
64
  "onchange": "6.0.0",
65
65
  "pg": "8.4.2",
66
66
  "semantic-release": "17.4.7",
67
- "semantic-release-npm-deprecate-old-versions": "1.1.2",
67
+ "semantic-release-npm-deprecate-old-versions": "1.3.2",
68
68
  "semantic-release-slack-bot": "2.1.1",
69
69
  "sequelize": "5.21.3",
70
70
  "sequelize-fixtures": "1.1.1",
@@ -76,6 +76,7 @@
76
76
  "build": "babel src --out-dir dist",
77
77
  "build:watch": "onchange 'src/**/*.js' 'node_modules/forest-express/dist/*' --no-exclude -i -- babel --source-maps inline --out-dir dist src",
78
78
  "lint": "./node_modules/eslint/bin/eslint.js .eslint-bin src test types/index.d.ts",
79
+ "prepare": "husky install",
79
80
  "test": "jest",
80
81
  "test:coverage": "jest --coverage"
81
82
  }
package/test/databases.js CHANGED
@@ -42,7 +42,7 @@ class ConnectionManager {
42
42
  * @type {Record<string, ConnectionManager>}
43
43
  */
44
44
  module.exports = {
45
- sequelizePostgres: new ConnectionManager('Postgresql 9.4', 'postgres://forest:secret@localhost:5436/forest-express-sequelize-test'),
45
+ sequelizePostgres: new ConnectionManager('Postgresql 9.4', 'postgres://forest:secret@localhost:5437/forest-express-sequelize-test'),
46
46
  sequelizeMySQLMin: new ConnectionManager('MySQL 5.6', 'mysql://forest:secret@localhost:8998/forest-express-sequelize-test'),
47
47
  sequelizeMySQLMax: new ConnectionManager('MySQL 8.0', 'mysql://forest:secret@localhost:8999/forest-express-sequelize-test'),
48
48
  };
@@ -8,7 +8,7 @@ const databaseOptions = {
8
8
  };
9
9
 
10
10
  const sequelize = new Sequelize(
11
- 'postgres://forest:secret@localhost:5436/forest-express-sequelize-test',
11
+ 'postgres://forest:secret@localhost:5437/forest-express-sequelize-test',
12
12
  databaseOptions,
13
13
  );
14
14
 
@@ -0,0 +1,119 @@
1
+ import Sequelize from 'sequelize';
2
+ import associationRecord from '../../src/utils/association-record';
3
+ import BelongsToUpdater from '../../src/services/belongs-to-updater';
4
+
5
+ describe('services > belongs-to-updater', () => {
6
+ const params = { timezone: 'Europe/Paris' };
7
+
8
+ const buildModelMock = () => {
9
+ // Sequelize is created here without connection to a database
10
+ const sequelize = new Sequelize({ dialect: 'postgres' });
11
+
12
+ const Actor = sequelize.define('actor', {
13
+ Id: {
14
+ type: Sequelize.DataTypes.INTEGER,
15
+ primaryKey: true,
16
+ },
17
+ });
18
+ const Author = sequelize.define('author', {
19
+ id: {
20
+ type: Sequelize.DataTypes.INTEGER,
21
+ primaryKey: true,
22
+ },
23
+ name: {
24
+ type: Sequelize.DataTypes.STRING,
25
+ unique: true,
26
+ },
27
+ });
28
+ const Film = sequelize.define('film', {});
29
+
30
+ Film.belongsTo(Actor);
31
+ Film.belongsTo(Author, {
32
+ targetKey: 'name',
33
+ });
34
+
35
+ return { Actor, Author, Film };
36
+ };
37
+
38
+ describe('_getTargetKey', () => {
39
+ describe('when association does not have entry in data', () => {
40
+ it('should return null', async () => {
41
+ expect.assertions(2);
42
+
43
+ const { Film, Actor } = buildModelMock();
44
+
45
+ const data = {};
46
+
47
+ const spy = jest.spyOn(associationRecord, 'get');
48
+
49
+ const belongsToUpdater = new BelongsToUpdater(Film, null, null, params, { data });
50
+ const targetKey = await belongsToUpdater._getTargetKey(
51
+ Film.associations[Actor.name],
52
+ );
53
+
54
+ expect(spy).not.toHaveBeenCalled();
55
+ expect(targetKey).toBeNil();
56
+ });
57
+ });
58
+
59
+ describe('when association does not have value in body', () => {
60
+ it('should return null', async () => {
61
+ expect.assertions(2);
62
+
63
+ const { Film, Actor } = buildModelMock();
64
+
65
+ const data = { id: null };
66
+
67
+ const spy = jest.spyOn(associationRecord, 'get');
68
+
69
+ const belongsToUpdater = new BelongsToUpdater(Film, null, null, params, { data });
70
+ const targetKey = await belongsToUpdater._getTargetKey(
71
+ Film.associations[Actor.name],
72
+ );
73
+
74
+ expect(spy).not.toHaveBeenCalled();
75
+ expect(targetKey).toBeNil();
76
+ });
77
+ });
78
+
79
+ describe('when association target key is the primary key', () => {
80
+ it('should return the body value', async () => {
81
+ expect.assertions(2);
82
+
83
+ const { Film, Actor } = buildModelMock();
84
+
85
+ const data = { id: 2 };
86
+
87
+ const spy = jest.spyOn(associationRecord, 'get');
88
+
89
+ const belongsToUpdater = new BelongsToUpdater(Film, null, null, params, { data });
90
+ const targetKey = await belongsToUpdater._getTargetKey(
91
+ Film.associations[Actor.name],
92
+ );
93
+
94
+ expect(spy).not.toHaveBeenCalled();
95
+ expect(targetKey).toStrictEqual(2);
96
+ });
97
+ });
98
+
99
+ describe('when association target key is not the primary key', () => {
100
+ it('should return the right value', async () => {
101
+ expect.assertions(2);
102
+
103
+ const { Film, Author } = buildModelMock();
104
+
105
+ const data = { id: 2 };
106
+
107
+ const spy = jest.spyOn(associationRecord, 'get').mockResolvedValue({ id: 2, name: 'Scorsese' });
108
+
109
+ const belongsToUpdater = new BelongsToUpdater(Film, null, null, params, { data });
110
+ const targetKey = await belongsToUpdater._getTargetKey(
111
+ Film.associations[Author.name],
112
+ );
113
+
114
+ expect(spy).toHaveBeenCalledWith(Author, 2);
115
+ expect(targetKey).toStrictEqual('Scorsese');
116
+ });
117
+ });
118
+ });
119
+ });
@@ -23,6 +23,11 @@ describe('services > requested-fields-extractor', () => {
23
23
  name: 'user',
24
24
  primaryKeys: { id: null, uid: null },
25
25
  associations: {},
26
+ rawAttributes: {
27
+ id: {},
28
+ uid: {},
29
+ name: {},
30
+ },
26
31
  };
27
32
 
28
33
  const schemas = {
@@ -51,6 +56,11 @@ describe('services > requested-fields-extractor', () => {
51
56
  name: 'user',
52
57
  primaryKeys: { id: null, uid: null },
53
58
  associations: {},
59
+ rawAttributes: {
60
+ id: {},
61
+ uid: {},
62
+ name: {},
63
+ },
54
64
  };
55
65
 
56
66
  const schemas = {
@@ -68,6 +78,38 @@ describe('services > requested-fields-extractor', () => {
68
78
  expect(result).toStrictEqual(['id', 'name']);
69
79
  });
70
80
 
81
+ it('should include field with same name as the model', () => {
82
+ expect.assertions(1);
83
+
84
+ const fields = {
85
+ user: 'id,user',
86
+ };
87
+
88
+ const model = {
89
+ name: 'user',
90
+ primaryKeys: { id: null },
91
+ associations: {},
92
+ rawAttributes: {
93
+ id: {},
94
+ user: {},
95
+ },
96
+ };
97
+
98
+ const schemas = {
99
+ user: {
100
+ name: 'user',
101
+ fields: [{
102
+ field: 'user',
103
+ isVirtual: false,
104
+ }],
105
+ },
106
+ };
107
+
108
+ const result = extractRequestedFields(fields, model, schemas);
109
+
110
+ expect(result).toStrictEqual(['id', 'user']);
111
+ });
112
+
71
113
  it('should include all associations\' requested fields', () => {
72
114
  expect.assertions(1);
73
115
 
@@ -87,6 +129,11 @@ describe('services > requested-fields-extractor', () => {
87
129
  },
88
130
  },
89
131
  },
132
+ rawAttributes: {
133
+ id: {},
134
+ uid: {},
135
+ name: {},
136
+ },
90
137
  };
91
138
 
92
139
  const schemas = {
@@ -130,6 +177,12 @@ describe('services > requested-fields-extractor', () => {
130
177
  },
131
178
  },
132
179
  },
180
+ rawAttributes: {
181
+ id: {},
182
+ uid: {},
183
+ name: {},
184
+ account: {},
185
+ },
133
186
  };
134
187
 
135
188
  const schemas = {
@@ -180,6 +233,12 @@ describe('services > requested-fields-extractor', () => {
180
233
  },
181
234
  },
182
235
  },
236
+ rawAttributes: {
237
+ id: {},
238
+ uid: {},
239
+ name: {},
240
+ account: {},
241
+ },
183
242
  };
184
243
 
185
244
  const schemas = {
@@ -222,6 +281,10 @@ describe('services > requested-fields-extractor', () => {
222
281
  name: 'user',
223
282
  primaryKeys: { id: null, uid: null },
224
283
  associations: {},
284
+ rawAttributes: {
285
+ id: {},
286
+ uid: {},
287
+ },
225
288
  };
226
289
 
227
290
  const schemas = {
@@ -0,0 +1,128 @@
1
+ import Interface from 'forest-express';
2
+ import Sequelize from 'sequelize';
3
+ import associationRecord from '../../src/utils/association-record';
4
+ import ResourceCreator from '../../src/services/resource-creator';
5
+
6
+ describe('services > resource-creator', () => {
7
+ const user = { renderingId: 1 };
8
+ const params = { timezone: 'Europe/Paris' };
9
+
10
+ const buildModelMock = () => {
11
+ // Sequelize is created here without connection to a database
12
+ const sequelize = new Sequelize({ dialect: 'postgres' });
13
+
14
+ const Actor = sequelize.define('actor', {
15
+ Id: {
16
+ type: Sequelize.DataTypes.INTEGER,
17
+ primaryKey: true,
18
+ },
19
+ });
20
+ const Author = sequelize.define('author', {
21
+ id: {
22
+ type: Sequelize.DataTypes.INTEGER,
23
+ primaryKey: true,
24
+ },
25
+ name: {
26
+ type: Sequelize.DataTypes.STRING,
27
+ unique: true,
28
+ },
29
+ });
30
+ const Film = sequelize.define('film', {});
31
+
32
+ Film.belongsTo(Actor);
33
+ Film.belongsTo(Author, {
34
+ targetKey: 'name',
35
+ });
36
+
37
+ Interface.Schemas.schemas[Actor.name] = {};
38
+ Interface.Schemas.schemas[Film.name] = {};
39
+
40
+ return { Actor, Author, Film };
41
+ };
42
+
43
+ describe('_getTargetKey', () => {
44
+ describe('when association does not have entry in body', () => {
45
+ it('should return null', async () => {
46
+ expect.assertions(2);
47
+
48
+ const { Film, Actor } = buildModelMock();
49
+
50
+ const body = {};
51
+
52
+ const spy = jest.spyOn(associationRecord, 'get');
53
+
54
+ const resourceCreator = new ResourceCreator(Film, params, body, user);
55
+ const targetKey = await resourceCreator._getTargetKey(
56
+ Actor.name,
57
+ Film.associations[Actor.name],
58
+ );
59
+
60
+ expect(spy).not.toHaveBeenCalled();
61
+ expect(targetKey).toBeNil();
62
+ });
63
+ });
64
+
65
+ describe('when association does not have value in body', () => {
66
+ it('should return null', async () => {
67
+ expect.assertions(2);
68
+
69
+ const { Film, Actor } = buildModelMock();
70
+
71
+ const body = { [Actor.name]: null };
72
+
73
+ const spy = jest.spyOn(associationRecord, 'get');
74
+
75
+ const resourceCreator = new ResourceCreator(Film, params, body, user);
76
+ const targetKey = await resourceCreator._getTargetKey(
77
+ Actor.name,
78
+ Film.associations[Actor.name],
79
+ );
80
+
81
+ expect(spy).not.toHaveBeenCalled();
82
+ expect(targetKey).toBeNil();
83
+ });
84
+ });
85
+
86
+ describe('when association target key is the primary key', () => {
87
+ it('should return the body value', async () => {
88
+ expect.assertions(2);
89
+
90
+ const { Film, Actor } = buildModelMock();
91
+
92
+ const body = { [Actor.name]: 2 };
93
+
94
+ const spy = jest.spyOn(associationRecord, 'get');
95
+
96
+ const resourceCreator = new ResourceCreator(Film, params, body, user);
97
+ const targetKey = await resourceCreator._getTargetKey(
98
+ Actor.name,
99
+ Film.associations[Actor.name],
100
+ );
101
+
102
+ expect(spy).not.toHaveBeenCalled();
103
+ expect(targetKey).toStrictEqual(2);
104
+ });
105
+ });
106
+
107
+ describe('when association target key is not the primary key', () => {
108
+ it('should return the right value', async () => {
109
+ expect.assertions(2);
110
+
111
+ const { Film, Author } = buildModelMock();
112
+
113
+ const body = { [Author.name]: 2 };
114
+
115
+ const spy = jest.spyOn(associationRecord, 'get').mockResolvedValue({ id: 2, name: 'Scorsese' });
116
+
117
+ const resourceCreator = new ResourceCreator(Film, params, body, user);
118
+ const targetKey = await resourceCreator._getTargetKey(
119
+ Author.name,
120
+ Film.associations[Author.name],
121
+ );
122
+
123
+ expect(spy).toHaveBeenCalledWith(Author, 2);
124
+ expect(targetKey).toStrictEqual('Scorsese');
125
+ });
126
+ });
127
+ });
128
+ });
package/types/index.d.ts CHANGED
@@ -321,6 +321,7 @@ export interface CollectionOptions {
321
321
  fields?: SmartFieldOptions[];
322
322
  actions?: SmartActionOptions[];
323
323
  segments?: SmartSegmentOptions[];
324
+ searchFields?: string[];
324
325
  }
325
326
 
326
327
  export function collection(name: string, options: CollectionOptions): void;
package/husky.config.js DELETED
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- hooks: {
3
- 'pre-commit': 'node ./.eslint-bin/pre-commit-hook.js',
4
- 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
5
- },
6
- };