forest-express-sequelize 9.3.20 → 9.3.22
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.
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [9.3.22](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.3.21...v9.3.22) (2024-05-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* create and update with a scope when it is not found ([#1107](https://github.com/ForestAdmin/forest-express-sequelize/issues/1107)) ([9a138c7](https://github.com/ForestAdmin/forest-express-sequelize/commit/9a138c77e857d2ad756e32145f89b4d769b2b3cc))
|
|
7
|
+
|
|
8
|
+
## [9.3.21](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.3.20...v9.3.21) (2024-05-17)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Performance Improvements
|
|
12
|
+
|
|
13
|
+
* improve memory usage when exporting ([#1114](https://github.com/ForestAdmin/forest-express-sequelize/issues/1114)) ([cc17621](https://github.com/ForestAdmin/forest-express-sequelize/commit/cc17621c961405d52bfcc05cb36242e88f6d72e3))
|
|
14
|
+
|
|
1
15
|
## [9.3.20](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.3.19...v9.3.20) (2024-04-25)
|
|
2
16
|
|
|
3
17
|
|
|
@@ -76,6 +76,7 @@ class ResourceCreator {
|
|
|
76
76
|
|
|
77
77
|
// handleAssociationsBeforeSave
|
|
78
78
|
await this._handleSave(recordCreated, this._makePromisesBeforeSave);
|
|
79
|
+
const scopeFilters = await Interface.scopeManager.getScopeForUser(this.user, this.model.name, true);
|
|
79
80
|
|
|
80
81
|
// saveInstance (validate then save)
|
|
81
82
|
try {
|
|
@@ -92,11 +93,16 @@ class ResourceCreator {
|
|
|
92
93
|
|
|
93
94
|
// appendCompositePrimary
|
|
94
95
|
new PrimaryKeysManager(this.model).annotateRecords([record]);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
96
|
+
try {
|
|
97
|
+
return await new ResourceGetter(this.model, _objectSpread(_objectSpread({}, this.params), {}, {
|
|
98
|
+
recordId: record[this.schema.idField]
|
|
99
|
+
}), this.user).perform();
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error.statusCode === 404 && scopeFilters) {
|
|
102
|
+
return record;
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
100
106
|
}
|
|
101
107
|
}
|
|
102
108
|
module.exports = ResourceCreator;
|
|
@@ -4,6 +4,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
4
4
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
5
5
|
require("core-js/modules/es.promise.js");
|
|
6
6
|
var _forestExpress = require("forest-express");
|
|
7
|
+
var _httpErrors = _interopRequireDefault(require("http-errors"));
|
|
7
8
|
var _errors = require("./errors");
|
|
8
9
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
9
10
|
var _resourceGetter = _interopRequireDefault(require("./resource-getter"));
|
|
@@ -33,10 +34,19 @@ class ResourceUpdater {
|
|
|
33
34
|
} catch (error) {
|
|
34
35
|
throw new _errors.ErrorHTTP422(error.message);
|
|
35
36
|
}
|
|
37
|
+
} else {
|
|
38
|
+
throw (0, _httpErrors.default)(404, `The ${this._model.name} #${this._params.recordId} does not exist.`);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
return await new _resourceGetter.default(this._model, _objectSpread(_objectSpread({}, this._params), {}, {
|
|
42
|
+
recordId: this._params.recordId
|
|
43
|
+
}), this._user).perform();
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (error.statusCode === 404 && scopeFilters) {
|
|
46
|
+
return record;
|
|
47
|
+
}
|
|
48
|
+
throw error;
|
|
36
49
|
}
|
|
37
|
-
return new _resourceGetter.default(this._model, _objectSpread(_objectSpread({}, this._params), {}, {
|
|
38
|
-
recordId: this._params.recordId
|
|
39
|
-
}), this._user).perform();
|
|
40
50
|
}
|
|
41
51
|
}
|
|
42
52
|
module.exports = ResourceUpdater;
|
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.3.
|
|
4
|
+
"version": "9.3.22",
|
|
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.2.
|
|
31
|
+
"forest-express": "10.2.20",
|
|
32
32
|
"http-errors": "1.6.1",
|
|
33
33
|
"lodash": "4.17.21",
|
|
34
34
|
"moment": "2.29.4",
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import Interface from 'forest-express';
|
|
1
|
+
import Interface, { scopeManager } from 'forest-express';
|
|
2
2
|
import Sequelize from 'sequelize';
|
|
3
3
|
import associationRecord from '../../src/utils/association-record';
|
|
4
4
|
import ResourceCreator from '../../src/services/resource-creator';
|
|
5
|
+
import ResourceGetter from '../../src/services/resource-getter';
|
|
5
6
|
|
|
6
7
|
describe('services > resource-creator', () => {
|
|
7
8
|
const user = { renderingId: 1 };
|
|
@@ -40,6 +41,55 @@ describe('services > resource-creator', () => {
|
|
|
40
41
|
return { Actor, Author, Film };
|
|
41
42
|
};
|
|
42
43
|
|
|
44
|
+
describe('perform', () => {
|
|
45
|
+
describe('when the getter does not found the record', () => {
|
|
46
|
+
it('should catch the 404 error and return the record', async () => {
|
|
47
|
+
expect.assertions(1);
|
|
48
|
+
|
|
49
|
+
const { Film } = buildModelMock();
|
|
50
|
+
const record = { dataValues: { id: 1, title: 'The Godfather' } };
|
|
51
|
+
|
|
52
|
+
const error = new Error('Record not found');
|
|
53
|
+
error.statusCode = 404;
|
|
54
|
+
jest
|
|
55
|
+
.spyOn(ResourceGetter.prototype, 'perform')
|
|
56
|
+
.mockRejectedValue(error);
|
|
57
|
+
|
|
58
|
+
jest.spyOn(Film.prototype, 'save').mockReturnValue(record);
|
|
59
|
+
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({});
|
|
60
|
+
|
|
61
|
+
const body = { actor: 2 };
|
|
62
|
+
|
|
63
|
+
const resourceCreator = new ResourceCreator(Film, params, body, user);
|
|
64
|
+
const result = await resourceCreator.perform();
|
|
65
|
+
expect(result).toStrictEqual(record);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('when there is a scope and ResourcesGetter throw an error', () => {
|
|
70
|
+
it('should throw the error', async () => {
|
|
71
|
+
expect.assertions(1);
|
|
72
|
+
|
|
73
|
+
const { Film } = buildModelMock();
|
|
74
|
+
const record = { dataValues: { id: 1, title: 'The Godfather' } };
|
|
75
|
+
|
|
76
|
+
const error = new Error('Unauthorized');
|
|
77
|
+
error.statusCode = 401;
|
|
78
|
+
jest
|
|
79
|
+
.spyOn(ResourceGetter.prototype, 'perform')
|
|
80
|
+
.mockRejectedValue(error);
|
|
81
|
+
|
|
82
|
+
jest.spyOn(Film.prototype, 'save').mockReturnValue(record);
|
|
83
|
+
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({});
|
|
84
|
+
|
|
85
|
+
const body = { actor: 2 };
|
|
86
|
+
|
|
87
|
+
const resourceCreator = new ResourceCreator(Film, params, body, user);
|
|
88
|
+
await expect(resourceCreator.perform()).rejects.toThrow(error);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
43
93
|
describe('_getTargetKey', () => {
|
|
44
94
|
describe('when association does not have entry in body', () => {
|
|
45
95
|
it('should return null', async () => {
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { scopeManager } from 'forest-express';
|
|
2
|
+
import Sequelize from 'sequelize';
|
|
3
|
+
import createError from 'http-errors';
|
|
4
|
+
import ResourceUpdater from '../../src/services/resource-updater';
|
|
5
|
+
import ResourceGetter from '../../src/services/resource-getter';
|
|
6
|
+
import QueryOptions from '../../src/services/query-options';
|
|
7
|
+
|
|
8
|
+
describe('services > resources-updater', () => {
|
|
9
|
+
const user = { renderingId: 1 };
|
|
10
|
+
const params = { timezone: 'Europe/Paris' };
|
|
11
|
+
|
|
12
|
+
const buildModelMock = () => {
|
|
13
|
+
// Sequelize is created here without connection to a database
|
|
14
|
+
const sequelize = new Sequelize({ dialect: 'postgres' });
|
|
15
|
+
|
|
16
|
+
const Actor = sequelize.define('actor', {});
|
|
17
|
+
const Film = sequelize.define('film', {});
|
|
18
|
+
const ActorFilm = sequelize.define('ActorFilem', {
|
|
19
|
+
actorId: {
|
|
20
|
+
type: Sequelize.DataTypes.INTEGER,
|
|
21
|
+
primaryKey: true,
|
|
22
|
+
},
|
|
23
|
+
filmId: {
|
|
24
|
+
type: Sequelize.DataTypes.INTEGER,
|
|
25
|
+
primaryKey: true,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
ActorFilm.belongsTo(Actor);
|
|
30
|
+
ActorFilm.belongsTo(Film);
|
|
31
|
+
|
|
32
|
+
return { Actor, Film, ActorFilm };
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe('when it update with a scope and it is not in scope anymore', () => {
|
|
36
|
+
it('should still return the record', async () => {
|
|
37
|
+
expect.assertions(1);
|
|
38
|
+
|
|
39
|
+
const { Film } = buildModelMock();
|
|
40
|
+
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] });
|
|
41
|
+
|
|
42
|
+
const record = { dataValues: { id: 1, title: 'The Godfather' }, validate: () => {}, save: () => {} };
|
|
43
|
+
|
|
44
|
+
jest.spyOn(record, 'validate');
|
|
45
|
+
jest.spyOn(record, 'save');
|
|
46
|
+
|
|
47
|
+
const error = new Error('Record not found');
|
|
48
|
+
error.statusCode = 404;
|
|
49
|
+
jest
|
|
50
|
+
.spyOn(ResourceGetter.prototype, 'perform')
|
|
51
|
+
.mockRejectedValue(error);
|
|
52
|
+
|
|
53
|
+
jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue();
|
|
54
|
+
|
|
55
|
+
const resourceUpdater = new ResourceUpdater(Film, params, { name: 'new name' }, user);
|
|
56
|
+
jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record);
|
|
57
|
+
|
|
58
|
+
const result = await resourceUpdater.perform();
|
|
59
|
+
|
|
60
|
+
expect(result).toStrictEqual(record);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('when it update with a scope but the record does not exist', () => {
|
|
65
|
+
it('should throw 404', async () => {
|
|
66
|
+
expect.assertions(1);
|
|
67
|
+
|
|
68
|
+
const { Film } = buildModelMock();
|
|
69
|
+
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] });
|
|
70
|
+
|
|
71
|
+
const record = null;
|
|
72
|
+
|
|
73
|
+
jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue();
|
|
74
|
+
|
|
75
|
+
const resourceUpdater = new ResourceUpdater(Film, { ...params, recordId: 2 }, { name: 'new name' }, user);
|
|
76
|
+
jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record);
|
|
77
|
+
|
|
78
|
+
await expect(resourceUpdater.perform()).rejects.toThrow(createError(404, 'The film #2 does not exist.'));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe('when there is a scope and ResourcesGetter throw an error', () => {
|
|
83
|
+
it('should throw the error', async () => {
|
|
84
|
+
expect.assertions(1);
|
|
85
|
+
|
|
86
|
+
const { Film } = buildModelMock();
|
|
87
|
+
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] });
|
|
88
|
+
|
|
89
|
+
const record = { dataValues: { id: 1, title: 'The Godfather' }, validate: () => {}, save: () => {} };
|
|
90
|
+
|
|
91
|
+
jest.spyOn(record, 'validate');
|
|
92
|
+
jest.spyOn(record, 'save');
|
|
93
|
+
|
|
94
|
+
const error = new Error('Unauthorized');
|
|
95
|
+
error.statusCode = 401;
|
|
96
|
+
jest
|
|
97
|
+
.spyOn(ResourceGetter.prototype, 'perform')
|
|
98
|
+
.mockRejectedValue(error);
|
|
99
|
+
|
|
100
|
+
jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue();
|
|
101
|
+
|
|
102
|
+
const resourceUpdater = new ResourceUpdater(Film, { ...params, recordId: 2 }, { name: 'new name' }, user);
|
|
103
|
+
jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record);
|
|
104
|
+
|
|
105
|
+
await expect(resourceUpdater.perform()).rejects.toThrow(error);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|