forest-express-sequelize 9.6.10 → 9.6.11
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 +7 -0
- package/dist/services/query-options.js +14 -11
- package/package.json +1 -1
- package/test/databases.test.js +24 -0
- package/test/services/query-options.test.js +27 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [9.6.11](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.10...v9.6.11) (2026-10-07)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **sort:** apply every comma-separated sort field ([#1151](https://github.com/ForestAdmin/forest-express-sequelize/issues/1151)) ([89b5f88](https://github.com/ForestAdmin/forest-express-sequelize/commit/89b5f88a4231976b01843ed3030adb2af1410fba))
|
|
7
|
+
|
|
1
8
|
## [9.6.10](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.9...v9.6.10) (2026-08-28)
|
|
2
9
|
|
|
3
10
|
## [9.6.9](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.8...v9.6.9) (2026-08-28)
|
|
@@ -264,21 +264,24 @@ class QueryOptions {
|
|
|
264
264
|
}
|
|
265
265
|
|
|
266
266
|
/**
|
|
267
|
-
* Apply sort instructions from a sort string in the form 'field', '-field' or 'field.subfield'
|
|
268
|
-
*
|
|
267
|
+
* Apply sort instructions from a sort string in the form 'field', '-field' or 'field.subfield',
|
|
268
|
+
* several of them comma-separated: 'field,-otherField'.
|
|
269
269
|
* @param {string} sortString a sort string
|
|
270
270
|
*/
|
|
271
271
|
async sort(sortString) {
|
|
272
|
+
var _this5 = this;
|
|
272
273
|
if (!sortString) return;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
274
|
+
sortString.split(',').filter(Boolean).forEach(function (sortChunk) {
|
|
275
|
+
const [sortField, order] = sortChunk[0] === '-' ? [sortChunk.substring(1), 'DESC'] : [sortChunk, 'ASC'];
|
|
276
|
+
if (sortField.includes('.')) {
|
|
277
|
+
// Sort on the belongsTo displayed field
|
|
278
|
+
const [associationName, fieldName] = sortField.split('.');
|
|
279
|
+
_this5._order.push([associationName, fieldName, order]);
|
|
280
|
+
_this5._neededFields.add(sortField);
|
|
281
|
+
} else {
|
|
282
|
+
_this5._order.push([sortField, order]);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
282
285
|
}
|
|
283
286
|
|
|
284
287
|
/**
|
package/package.json
CHANGED
package/test/databases.test.js
CHANGED
|
@@ -1210,6 +1210,30 @@ const user = { renderingId: 1 };
|
|
|
1210
1210
|
});
|
|
1211
1211
|
});
|
|
1212
1212
|
|
|
1213
|
+
describe('request on the resources getter with several comma-separated sorts', () => {
|
|
1214
|
+
it('should order by each sort in turn', async () => {
|
|
1215
|
+
expect.assertions(1);
|
|
1216
|
+
const { models } = initializeSequelize();
|
|
1217
|
+
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
1218
|
+
const params = {
|
|
1219
|
+
...baseParams,
|
|
1220
|
+
fields: { user: 'primaryId,age' },
|
|
1221
|
+
sort: 'age,-primaryId',
|
|
1222
|
+
page: { number: '1', size: '30' },
|
|
1223
|
+
};
|
|
1224
|
+
try {
|
|
1225
|
+
const [records] = await new ResourcesGetter(models.user, null, params, user).perform();
|
|
1226
|
+
const fixtureIds = records
|
|
1227
|
+
.map((record) => record.primaryId)
|
|
1228
|
+
.filter((primaryId) => [100, 102, 103].includes(primaryId));
|
|
1229
|
+
expect(fixtureIds).toStrictEqual([102, 103, 100]);
|
|
1230
|
+
} finally {
|
|
1231
|
+
spy.mockRestore();
|
|
1232
|
+
connectionManager.closeConnection();
|
|
1233
|
+
}
|
|
1234
|
+
});
|
|
1235
|
+
});
|
|
1236
|
+
|
|
1213
1237
|
describe('request on the resources getter with a sort on the primary key', () => {
|
|
1214
1238
|
it('should return the records for the specified page', async () => {
|
|
1215
1239
|
expect.assertions(1);
|
|
@@ -64,6 +64,33 @@ describe('services > query-options', () => {
|
|
|
64
64
|
await options.sort('-id');
|
|
65
65
|
expect(options.sequelizeOptions.order).toStrictEqual([['id', 'DESC']]);
|
|
66
66
|
});
|
|
67
|
+
|
|
68
|
+
it('should apply every comma-separated sort, in order, each with its own direction', async () => {
|
|
69
|
+
expect.assertions(1);
|
|
70
|
+
const options = new QueryOptions(model);
|
|
71
|
+
await options.sort('movieId,-id');
|
|
72
|
+
expect(options.sequelizeOptions.order).toStrictEqual([['movieId', 'ASC'], ['id', 'DESC']]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should join the association of a sort on a relation that is not the first one', async () => {
|
|
76
|
+
expect.assertions(2);
|
|
77
|
+
const options = new QueryOptions(model);
|
|
78
|
+
await options.sort('id,-movie.id');
|
|
79
|
+
const { order, include } = options.sequelizeOptions;
|
|
80
|
+
expect(order).toStrictEqual([['id', 'ASC'], ['movie', 'id', 'DESC']]);
|
|
81
|
+
expect(include).toStrictEqual([expect.objectContaining({ as: 'movie' })]);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('with mssql and several sorts', () => {
|
|
87
|
+
const model = buildModelMock('mssql');
|
|
88
|
+
|
|
89
|
+
it('should only drop the primary key from the sorts', async () => {
|
|
90
|
+
expect.assertions(1);
|
|
91
|
+
const options = new QueryOptions(model);
|
|
92
|
+
await options.sort('movieId,-id');
|
|
93
|
+
expect(options.sequelizeOptions.order).toStrictEqual([['movieId', 'ASC']]);
|
|
67
94
|
});
|
|
68
95
|
});
|
|
69
96
|
});
|