forest-express-sequelize 9.6.2 → 9.6.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.
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [9.6.3](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.2...v9.6.3) (2026-05-12)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **resources-remover:** support scopes targeting relation fields ([#1142](https://github.com/ForestAdmin/forest-express-sequelize/issues/1142)) ([5717ab8](https://github.com/ForestAdmin/forest-express-sequelize/commit/5717ab811746ea95c1d8a423dcdf26809c027092))
|
|
7
|
+
|
|
1
8
|
## [9.6.2](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.1...v9.6.2) (2026-02-03)
|
|
2
9
|
|
|
3
10
|
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
4
|
require("core-js/modules/es.promise.js");
|
|
5
|
+
require("core-js/modules/es.array.iterator.js");
|
|
5
6
|
var _forestExpress = require("forest-express");
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
8
|
+
var _operators = _interopRequireDefault(require("../utils/operators"));
|
|
6
9
|
var _errors = require("./errors");
|
|
7
10
|
var _queryOptions = _interopRequireDefault(require("./query-options"));
|
|
8
11
|
class ResourcesRemover {
|
|
@@ -13,6 +16,7 @@ class ResourcesRemover {
|
|
|
13
16
|
this._user = user;
|
|
14
17
|
}
|
|
15
18
|
async perform() {
|
|
19
|
+
var _options$include;
|
|
16
20
|
if (!Array.isArray(this._ids) || !this._ids.length) {
|
|
17
21
|
throw new _errors.InvalidParameterError('`ids` must be a non-empty array.');
|
|
18
22
|
}
|
|
@@ -23,7 +27,47 @@ class ResourcesRemover {
|
|
|
23
27
|
const queryOptions = new _queryOptions.default(this._model);
|
|
24
28
|
await queryOptions.filterByIds(this._ids);
|
|
25
29
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
26
|
-
|
|
30
|
+
const options = queryOptions.sequelizeOptions;
|
|
31
|
+
|
|
32
|
+
// `Model.destroy` ignores `include`, so a where clause referencing a
|
|
33
|
+
// joined column (e.g. `$product.id$`) produces invalid SQL. Resolve the
|
|
34
|
+
// matching primary keys with a SELECT first, then delete by PK.
|
|
35
|
+
if ((_options$include = options.include) !== null && _options$include !== void 0 && _options$include.length) {
|
|
36
|
+
return this._destroyByPrimaryKey(options);
|
|
37
|
+
}
|
|
38
|
+
return this._model.destroy(options);
|
|
39
|
+
}
|
|
40
|
+
async _destroyByPrimaryKey(options) {
|
|
41
|
+
const pkAttributes = Object.keys(this._model.primaryKeys);
|
|
42
|
+
const matches = await this._model.findAll({
|
|
43
|
+
attributes: pkAttributes,
|
|
44
|
+
where: options.where,
|
|
45
|
+
include: options.include
|
|
46
|
+
});
|
|
47
|
+
if (matches.length === 0) return 0;
|
|
48
|
+
return this._model.destroy({
|
|
49
|
+
where: ResourcesRemover._buildPrimaryKeyWhere(this._model, pkAttributes, matches)
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
static _buildPrimaryKeyWhere(model, pkAttributes, records) {
|
|
53
|
+
if (pkAttributes.length === 1) {
|
|
54
|
+
const [pk] = pkAttributes;
|
|
55
|
+
return {
|
|
56
|
+
[pk]: records.map(function (record) {
|
|
57
|
+
return record.get(pk);
|
|
58
|
+
})
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const OPERATORS = _operators.default.getInstance({
|
|
62
|
+
Sequelize: model.sequelize.constructor
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
[OPERATORS.OR]: records.map(function (record) {
|
|
66
|
+
return _lodash.default.fromPairs(pkAttributes.map(function (pk) {
|
|
67
|
+
return [pk, record.get(pk)];
|
|
68
|
+
}));
|
|
69
|
+
})
|
|
70
|
+
};
|
|
27
71
|
}
|
|
28
72
|
}
|
|
29
73
|
module.exports = ResourcesRemover;
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { scopeManager } from 'forest-express';
|
|
1
|
+
import Interface, { scopeManager } from 'forest-express';
|
|
2
2
|
import Sequelize, { Op } from 'sequelize';
|
|
3
3
|
import { InvalidParameterError } from '../../src/services/errors';
|
|
4
4
|
import ResourcesRemover from '../../src/services/resources-remover';
|
|
@@ -85,4 +85,116 @@ describe('services > resources-remover', () => {
|
|
|
85
85
|
});
|
|
86
86
|
});
|
|
87
87
|
});
|
|
88
|
+
|
|
89
|
+
describe('when scope targets a relation field', () => {
|
|
90
|
+
const scope = { field: 'film:id', operator: 'present', value: null };
|
|
91
|
+
|
|
92
|
+
const setup = () => {
|
|
93
|
+
const { Actor, Film, ActorFilm } = buildModelMock('postgres');
|
|
94
|
+
Actor.belongsTo(Film);
|
|
95
|
+
|
|
96
|
+
Interface.Schemas.schemas = {
|
|
97
|
+
actor: {
|
|
98
|
+
name: 'actor',
|
|
99
|
+
fields: [
|
|
100
|
+
{ field: 'id', type: 'Number' },
|
|
101
|
+
{ field: 'film', reference: 'film.id' },
|
|
102
|
+
],
|
|
103
|
+
primaryKeys: ['id'],
|
|
104
|
+
idField: 'id',
|
|
105
|
+
},
|
|
106
|
+
ActorFilem: {
|
|
107
|
+
name: 'ActorFilem',
|
|
108
|
+
fields: [
|
|
109
|
+
{ field: 'actorId', type: 'Number' },
|
|
110
|
+
{ field: 'filmId', type: 'Number' },
|
|
111
|
+
{ field: 'film', reference: 'film.id' },
|
|
112
|
+
],
|
|
113
|
+
primaryKeys: ['actorId', 'filmId'],
|
|
114
|
+
idField: 'actorId|filmId',
|
|
115
|
+
},
|
|
116
|
+
film: {
|
|
117
|
+
name: 'film',
|
|
118
|
+
fields: [{ field: 'id', type: 'Number' }],
|
|
119
|
+
primaryKeys: ['id'],
|
|
120
|
+
idField: 'id',
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const scopeSpy = jest.spyOn(scopeManager, 'getScopeForUser').mockResolvedValue(scope);
|
|
125
|
+
const restore = () => {
|
|
126
|
+
scopeSpy.mockRestore();
|
|
127
|
+
Interface.Schemas.schemas = {};
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return { Actor, ActorFilm, restore };
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
it('should resolve PKs via findAll, then destroy by Op.in for single-PK models', async () => {
|
|
134
|
+
expect.assertions(4);
|
|
135
|
+
|
|
136
|
+
const { Actor, restore } = setup();
|
|
137
|
+
const findAllSpy = jest.spyOn(Actor, 'findAll').mockResolvedValue([
|
|
138
|
+
{ get: () => 7 },
|
|
139
|
+
{ get: () => 42 },
|
|
140
|
+
]);
|
|
141
|
+
const destroySpy = jest.spyOn(Actor, 'destroy').mockResolvedValue(2);
|
|
142
|
+
|
|
143
|
+
await new ResourcesRemover(Actor, params, ['7', '42'], user).perform();
|
|
144
|
+
|
|
145
|
+
expect(findAllSpy).toHaveBeenCalledTimes(1);
|
|
146
|
+
expect(findAllSpy.mock.calls[0][0].include).toStrictEqual(
|
|
147
|
+
expect.arrayContaining([expect.objectContaining({ as: 'film' })]),
|
|
148
|
+
);
|
|
149
|
+
expect(destroySpy).toHaveBeenCalledTimes(1);
|
|
150
|
+
expect(destroySpy).toHaveBeenCalledWith({ where: { id: [7, 42] } });
|
|
151
|
+
|
|
152
|
+
findAllSpy.mockRestore();
|
|
153
|
+
destroySpy.mockRestore();
|
|
154
|
+
restore();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should destroy by Op.or for composite-PK models', async () => {
|
|
158
|
+
expect.assertions(1);
|
|
159
|
+
|
|
160
|
+
const { ActorFilm, restore } = setup();
|
|
161
|
+
const findAllSpy = jest.spyOn(ActorFilm, 'findAll').mockResolvedValue([
|
|
162
|
+
{ get: (key) => ({ actorId: 1, filmId: 2 }[key]) },
|
|
163
|
+
{ get: (key) => ({ actorId: 3, filmId: 4 }[key]) },
|
|
164
|
+
]);
|
|
165
|
+
const destroySpy = jest.spyOn(ActorFilm, 'destroy').mockResolvedValue(2);
|
|
166
|
+
|
|
167
|
+
await new ResourcesRemover(ActorFilm, params, ['1|2', '3|4'], user).perform();
|
|
168
|
+
|
|
169
|
+
expect(destroySpy).toHaveBeenCalledWith({
|
|
170
|
+
where: {
|
|
171
|
+
[Op.or]: [
|
|
172
|
+
{ actorId: 1, filmId: 2 },
|
|
173
|
+
{ actorId: 3, filmId: 4 },
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
findAllSpy.mockRestore();
|
|
179
|
+
destroySpy.mockRestore();
|
|
180
|
+
restore();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should not call destroy when no record matches the scope', async () => {
|
|
184
|
+
expect.assertions(2);
|
|
185
|
+
|
|
186
|
+
const { ActorFilm, restore } = setup();
|
|
187
|
+
const findAllSpy = jest.spyOn(ActorFilm, 'findAll').mockResolvedValue([]);
|
|
188
|
+
const destroySpy = jest.spyOn(ActorFilm, 'destroy');
|
|
189
|
+
|
|
190
|
+
const removed = await new ResourcesRemover(ActorFilm, params, ['1|2'], user).perform();
|
|
191
|
+
|
|
192
|
+
expect(destroySpy).not.toHaveBeenCalled();
|
|
193
|
+
expect(removed).toBe(0);
|
|
194
|
+
|
|
195
|
+
findAllSpy.mockRestore();
|
|
196
|
+
destroySpy.mockRestore();
|
|
197
|
+
restore();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
88
200
|
});
|