forest-express-sequelize 9.6.9 → 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 CHANGED
@@ -1,3 +1,12 @@
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
+
8
+ ## [9.6.10](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.9...v9.6.10) (2026-08-28)
9
+
1
10
  ## [9.6.9](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.8...v9.6.9) (2026-08-28)
2
11
 
3
12
 
@@ -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
- * Multiple sorts are not supported
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
- const [sortField, order] = sortString[0] === '-' ? [sortString.substring(1), 'DESC'] : [sortString, 'ASC'];
274
- if (sortField.includes('.')) {
275
- // Sort on the belongsTo displayed field
276
- const [associationName, fieldName] = sortField.split('.');
277
- this._order.push([associationName, fieldName, order]);
278
- this._neededFields.add(sortField);
279
- } else {
280
- this._order.push([sortField, order]);
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "forest-express-sequelize",
3
3
  "description": "Official Express/Sequelize Liana for Forest",
4
- "version": "9.6.9",
4
+ "version": "9.6.11",
5
5
  "author": "Sandro Munda <sandro@munda.me>",
6
6
  "contributors": [
7
7
  "Arnaud Besnier <arnaudibesnier@gmail.com>",
@@ -31,7 +31,7 @@
31
31
  "@babel/runtime": "7.15.4",
32
32
  "bluebird": "2.9.25",
33
33
  "core-js": "3.6.5",
34
- "forest-express": "10.9.2",
34
+ "forest-express": "10.9.3",
35
35
  "http-errors": "1.6.1",
36
36
  "lodash": "4.18.1",
37
37
  "moment": "2.29.4",
@@ -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
  });