forest-express-sequelize 9.6.13 → 9.6.14
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.14](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.13...v9.6.14) (2026-10-30)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **line-chart:** validate the line chart timezone and pass it as a value ([#1154](https://github.com/ForestAdmin/forest-express-sequelize/issues/1154)) ([61b3269](https://github.com/ForestAdmin/forest-express-sequelize/commit/61b3269355e105fba65373fb12d03dcd47b94d25))
|
|
7
|
+
|
|
1
8
|
## [9.6.13](https://github.com/ForestAdmin/forest-express-sequelize/compare/v9.6.12...v9.6.13) (2026-10-28)
|
|
2
9
|
|
|
3
10
|
|
|
@@ -32,6 +32,7 @@ function assertValidAggregator(aggregator) {
|
|
|
32
32
|
throw (0, _httpErrors.default)(400, `Invalid aggregator ${describeInput(aggregator)}`);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
+
const VALID_TIMEZONES = new Set();
|
|
35
36
|
function LineStatGetter(model, params, options, user) {
|
|
36
37
|
const schema = _forestExpress.Schemas.schemas[model.name];
|
|
37
38
|
const timeRange = params.timeRange.toLowerCase();
|
|
@@ -99,7 +100,28 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
99
100
|
return null;
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
|
-
function
|
|
103
|
+
function getTimezoneName() {
|
|
104
|
+
var _params$timezone;
|
|
105
|
+
const timezone = (_params$timezone = params.timezone) === null || _params$timezone === void 0 ? void 0 : _params$timezone.toString();
|
|
106
|
+
if (!timezone) {
|
|
107
|
+
throw (0, _httpErrors.default)(400, 'Missing timezone');
|
|
108
|
+
}
|
|
109
|
+
if (!VALID_TIMEZONES.has(timezone)) {
|
|
110
|
+
try {
|
|
111
|
+
Intl.DateTimeFormat('en-US', {
|
|
112
|
+
timeZone: timezone
|
|
113
|
+
});
|
|
114
|
+
} catch (_unused) {
|
|
115
|
+
throw (0, _httpErrors.default)(400, `Invalid timezone: "${timezone}"`);
|
|
116
|
+
}
|
|
117
|
+
VALID_TIMEZONES.add(timezone);
|
|
118
|
+
}
|
|
119
|
+
return timezone;
|
|
120
|
+
}
|
|
121
|
+
function isTimezoneInQuery() {
|
|
122
|
+
return !(0, _database.isMySQL)(model.sequelize) && !(0, _database.isMSSQL)(model.sequelize) && !(0, _database.isSQLite)(model.sequelize);
|
|
123
|
+
}
|
|
124
|
+
function getGroupByDateInterval(timezone) {
|
|
103
125
|
if ((0, _database.isMySQL)(model.sequelize)) {
|
|
104
126
|
return [getGroupByDateFieldFormatedForMySQL(timeRange), 'date'];
|
|
105
127
|
}
|
|
@@ -109,7 +131,7 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
109
131
|
if ((0, _database.isSQLite)(model.sequelize)) {
|
|
110
132
|
return [getGroupByDateFieldFormatedForSQLite(timeRange), 'date'];
|
|
111
133
|
}
|
|
112
|
-
return [options.Sequelize.fn('to_char', options.Sequelize.fn('date_trunc', params.timeRange, options.Sequelize.literal(`"${
|
|
134
|
+
return [options.Sequelize.fn('to_char', options.Sequelize.fn('date_trunc', params.timeRange, options.Sequelize.fn('timezone', timezone, options.Sequelize.literal(`"${groupByDateField.replace('.', '"."')}"`))), 'YYYY-MM-DD 00:00:00'), 'date'];
|
|
113
135
|
}
|
|
114
136
|
function getFormat() {
|
|
115
137
|
switch (timeRange) {
|
|
@@ -169,9 +191,9 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
169
191
|
assertValidGroupByField(schema, params.groupByFieldName);
|
|
170
192
|
assertValidAggregator(params.aggregator);
|
|
171
193
|
const {
|
|
172
|
-
filter
|
|
173
|
-
timezone
|
|
194
|
+
filter
|
|
174
195
|
} = params;
|
|
196
|
+
const timezone = isTimezoneInQuery() ? getTimezoneName() : params.timezone;
|
|
175
197
|
const scopeFilters = await _forestExpress.scopeManager.getScopeForUser(user, model.name, true);
|
|
176
198
|
const queryOptions = new _queryOptions.default(model, {
|
|
177
199
|
includeRelations: true
|
|
@@ -179,7 +201,7 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
|
|
|
179
201
|
await queryOptions.filterByConditionTree(filter, timezone);
|
|
180
202
|
await queryOptions.filterByConditionTree(scopeFilters, timezone);
|
|
181
203
|
const sequelizeOptions = _objectSpread(_objectSpread({}, queryOptions.sequelizeOptions), {}, {
|
|
182
|
-
attributes: [getGroupByDateInterval(), getAggregate()],
|
|
204
|
+
attributes: [getGroupByDateInterval(timezone), getAggregate()],
|
|
183
205
|
group: getGroupBy(),
|
|
184
206
|
order: getOrder(),
|
|
185
207
|
raw: true
|
package/package.json
CHANGED
package/test/databases.test.js
CHANGED
|
@@ -685,6 +685,80 @@ const user = { renderingId: 1 };
|
|
|
685
685
|
});
|
|
686
686
|
});
|
|
687
687
|
|
|
688
|
+
describe('a line chart per day in each time zone the front sends', () => {
|
|
689
|
+
['Europe/Paris', 'America/New_York', 'UTC', 'Etc/GMT+2'].forEach((timezone) => {
|
|
690
|
+
it(`should return a single daily point with the ${timezone} time zone`, async () => {
|
|
691
|
+
expect.assertions(1);
|
|
692
|
+
const { models, options } = initializeSequelize();
|
|
693
|
+
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
694
|
+
try {
|
|
695
|
+
const stat = await new LineStatGetter(models.user, {
|
|
696
|
+
timezone,
|
|
697
|
+
type: 'Line',
|
|
698
|
+
sourceCollectionName: 'user',
|
|
699
|
+
groupByFieldName: 'createdAt',
|
|
700
|
+
aggregator: 'Count',
|
|
701
|
+
timeRange: 'Day',
|
|
702
|
+
filter: null,
|
|
703
|
+
}, options, user).perform();
|
|
704
|
+
expect(stat.value).toHaveLength(1);
|
|
705
|
+
} finally {
|
|
706
|
+
spy.mockRestore();
|
|
707
|
+
connectionManager.closeConnection();
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
});
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
if (connectionManager === sequelizePostgres) {
|
|
714
|
+
describe('a line chart with an unknown timezone', () => {
|
|
715
|
+
it('should be refused with a bad request error', async () => {
|
|
716
|
+
expect.assertions(1);
|
|
717
|
+
const { models, options } = initializeSequelize();
|
|
718
|
+
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
719
|
+
try {
|
|
720
|
+
await expect(new LineStatGetter(models.user, {
|
|
721
|
+
timezone: 'Europe/Atlantis',
|
|
722
|
+
type: 'Line',
|
|
723
|
+
sourceCollectionName: 'user',
|
|
724
|
+
groupByFieldName: 'createdAt',
|
|
725
|
+
aggregator: 'Count',
|
|
726
|
+
timeRange: 'Day',
|
|
727
|
+
filter: null,
|
|
728
|
+
}, options, user).perform()).rejects.toMatchObject({
|
|
729
|
+
name: 'BadRequestError',
|
|
730
|
+
status: 400,
|
|
731
|
+
});
|
|
732
|
+
} finally {
|
|
733
|
+
spy.mockRestore();
|
|
734
|
+
connectionManager.closeConnection();
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
});
|
|
738
|
+
} else {
|
|
739
|
+
describe('a line chart without timezone', () => {
|
|
740
|
+
it('should still return the points', async () => {
|
|
741
|
+
expect.assertions(1);
|
|
742
|
+
const { models, options } = initializeSequelize();
|
|
743
|
+
const spy = jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue(null);
|
|
744
|
+
try {
|
|
745
|
+
const stat = await new LineStatGetter(models.user, {
|
|
746
|
+
type: 'Line',
|
|
747
|
+
sourceCollectionName: 'user',
|
|
748
|
+
groupByFieldName: 'createdAt',
|
|
749
|
+
aggregator: 'Count',
|
|
750
|
+
timeRange: 'Day',
|
|
751
|
+
filter: null,
|
|
752
|
+
}, options, user).perform();
|
|
753
|
+
expect(stat.value).toHaveLength(1);
|
|
754
|
+
} finally {
|
|
755
|
+
spy.mockRestore();
|
|
756
|
+
connectionManager.closeConnection();
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
|
|
688
762
|
describe('chart with a filter', () => {
|
|
689
763
|
it('should generate a valid SQL query', async () => {
|
|
690
764
|
expect.assertions(1);
|
|
@@ -174,10 +174,10 @@ describe('services > line-stat-getter > chart inputs', () => {
|
|
|
174
174
|
});
|
|
175
175
|
|
|
176
176
|
[
|
|
177
|
-
['postgres', 'Day', 'to_char(date_trunc(\'Day\',
|
|
178
|
-
+ '
|
|
179
|
-
['postgres', 'Week', 'to_char(date_trunc(\'Week\',
|
|
180
|
-
+ '
|
|
177
|
+
['postgres', 'Day', 'to_char(date_trunc(\'Day\', timezone(\'Europe/Paris\', '
|
|
178
|
+
+ '"user"."createdAt")), \'YYYY-MM-DD 00:00:00\') AS "date"'],
|
|
179
|
+
['postgres', 'Week', 'to_char(date_trunc(\'Week\', timezone(\'Europe/Paris\', '
|
|
180
|
+
+ '"user"."createdAt")), \'YYYY-MM-DD 00:00:00\') AS "date"'],
|
|
181
181
|
['mysql', 'Day', 'DATE_FORMAT(`user`.`createdAt`, \'%Y-%m-%d 00:00:00\') AS `date`'],
|
|
182
182
|
['mysql', 'Week', 'DATE_FORMAT(DATE_SUB(`user`.`createdAt`, INTERVAL '
|
|
183
183
|
+ '((7 + WEEKDAY(`user`.`createdAt`)) % 7) DAY), \'%Y-%m-%d 00:00:00\') AS `date`'],
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import Sequelize from 'sequelize';
|
|
2
|
+
import Interface, { scopeManager } from 'forest-express';
|
|
3
|
+
import LineStatGetter from '../../src/services/line-stat-getter';
|
|
4
|
+
|
|
5
|
+
describe('services > line-stat-getter', () => {
|
|
6
|
+
const user = { renderingId: 1 };
|
|
7
|
+
const supportedTimezones = ['Europe/Paris', 'America/New_York', 'UTC', 'Etc/GMT+2'];
|
|
8
|
+
|
|
9
|
+
const setup = (dialect) => {
|
|
10
|
+
jest.restoreAllMocks();
|
|
11
|
+
const dialectModule = ['sqlite', 'mariadb'].includes(dialect) ? {} : undefined;
|
|
12
|
+
const sequelize = new Sequelize({ dialect, dialectModule });
|
|
13
|
+
const model = sequelize.define('user', {
|
|
14
|
+
id: { type: Sequelize.INTEGER, primaryKey: true },
|
|
15
|
+
createdAt: { type: Sequelize.DATE },
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
Interface.Schemas = {
|
|
19
|
+
schemas: {
|
|
20
|
+
user: {
|
|
21
|
+
name: 'user',
|
|
22
|
+
idField: 'id',
|
|
23
|
+
primaryKeys: ['id'],
|
|
24
|
+
fields: [{ field: 'id', type: 'Number' }, { field: 'createdAt', type: 'Date' }],
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const findAll = jest.spyOn(model, 'findAll').mockResolvedValue([]);
|
|
30
|
+
jest.spyOn(scopeManager, 'getScopeForUser').mockResolvedValue(null);
|
|
31
|
+
|
|
32
|
+
const buildSql = () => {
|
|
33
|
+
const [[findAllOptions]] = findAll.mock.calls;
|
|
34
|
+
return sequelize.getQueryInterface().queryGenerator
|
|
35
|
+
.selectQuery(model.getTableName(), findAllOptions, model);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
return { model, findAll, buildSql };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const lineParams = (extra) => ({
|
|
42
|
+
type: 'Line',
|
|
43
|
+
sourceCollectionName: 'user',
|
|
44
|
+
groupByFieldName: 'createdAt',
|
|
45
|
+
aggregator: 'Count',
|
|
46
|
+
timeRange: 'Day',
|
|
47
|
+
filter: null,
|
|
48
|
+
...extra,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('with postgres', () => {
|
|
52
|
+
[...supportedTimezones, 'America/Ciudad_Juarez', 'America/Coyhaique'].forEach((timezone) => {
|
|
53
|
+
it(`groups the dates in the ${timezone} time zone`, async () => {
|
|
54
|
+
expect.assertions(2);
|
|
55
|
+
const { model, findAll, buildSql } = setup('postgres');
|
|
56
|
+
|
|
57
|
+
await new LineStatGetter(model, lineParams({ timezone }), { Sequelize }, user).perform();
|
|
58
|
+
|
|
59
|
+
expect(findAll).toHaveBeenCalledTimes(1);
|
|
60
|
+
expect(buildSql()).toContain(
|
|
61
|
+
`to_char(date_trunc('Day', timezone('${timezone}', "user"."createdAt")), 'YYYY-MM-DD 00:00:00') AS "date"`,
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('accepts a zone name whatever its case', async () => {
|
|
67
|
+
expect.assertions(1);
|
|
68
|
+
const { model, buildSql } = setup('postgres');
|
|
69
|
+
|
|
70
|
+
await new LineStatGetter(model, lineParams({ timezone: 'europe/paris' }), { Sequelize }, user).perform();
|
|
71
|
+
|
|
72
|
+
expect(buildSql()).toContain('timezone(\'europe/paris\', "user"."createdAt")');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('groups the dates in the requested zone on a host whose own zone Intl cannot name', async () => {
|
|
76
|
+
expect.assertions(1);
|
|
77
|
+
const { model, buildSql } = setup('postgres');
|
|
78
|
+
const { DateTimeFormat } = Intl;
|
|
79
|
+
// Node reports no host zone when started with TZ=UTC0 or TZ=:/etc/localtime.
|
|
80
|
+
jest.spyOn(Intl, 'DateTimeFormat').mockImplementation((...args) => (args.length
|
|
81
|
+
? new DateTimeFormat(...args)
|
|
82
|
+
: { resolvedOptions: () => ({ timeZone: undefined }) }));
|
|
83
|
+
|
|
84
|
+
await new LineStatGetter(model, lineParams({ timezone: 'Asia/Tokyo' }), { Sequelize }, user).perform();
|
|
85
|
+
|
|
86
|
+
expect(buildSql()).toContain('timezone(\'Asia/Tokyo\', "user"."createdAt")');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('reads a one-item timezone list as that zone', async () => {
|
|
90
|
+
expect.assertions(1);
|
|
91
|
+
const { model, buildSql } = setup('postgres');
|
|
92
|
+
|
|
93
|
+
await new LineStatGetter(model, lineParams({ timezone: ['Europe/Paris'] }), { Sequelize }, user).perform();
|
|
94
|
+
|
|
95
|
+
expect(buildSql()).toContain('timezone(\'Europe/Paris\', "user"."createdAt")');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
[
|
|
99
|
+
['no timezone', undefined, 'Missing timezone'],
|
|
100
|
+
['an empty timezone', '', 'Missing timezone'],
|
|
101
|
+
['an unknown zone name', 'Europe/Atlantis', 'Invalid timezone: "Europe/Atlantis"'],
|
|
102
|
+
['a timezone containing a space', 'Europe Paris', 'Invalid timezone: "Europe Paris"'],
|
|
103
|
+
['several timezones', ['Europe/Paris', 'UTC'], 'Invalid timezone: "Europe/Paris,UTC"'],
|
|
104
|
+
['a timezone that is not a string', { name: 'Europe/Paris' }, 'Invalid timezone: "[object Object]"'],
|
|
105
|
+
].forEach(([description, timezone, message]) => {
|
|
106
|
+
it(`refuses ${description} without querying the database`, async () => {
|
|
107
|
+
expect.assertions(2);
|
|
108
|
+
const { model, findAll } = setup('postgres');
|
|
109
|
+
|
|
110
|
+
await expect(
|
|
111
|
+
new LineStatGetter(model, lineParams({ timezone }), { Sequelize }, user).perform(),
|
|
112
|
+
).rejects.toThrow(message);
|
|
113
|
+
expect(findAll).not.toHaveBeenCalled();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('answers "Missing timezone" with a 400 status when a date filter comes without timezone', async () => {
|
|
118
|
+
expect.assertions(2);
|
|
119
|
+
const { model, findAll } = setup('postgres');
|
|
120
|
+
const filter = { field: 'createdAt', operator: 'today', value: null };
|
|
121
|
+
|
|
122
|
+
await expect(
|
|
123
|
+
new LineStatGetter(model, lineParams({ filter }), { Sequelize }, user).perform(),
|
|
124
|
+
).rejects.toMatchObject({ message: 'Missing timezone', status: 400 });
|
|
125
|
+
expect(findAll).not.toHaveBeenCalled();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('answers "Missing timezone" with a 400 status when a date scope comes without timezone', async () => {
|
|
129
|
+
expect.assertions(2);
|
|
130
|
+
const { model, findAll } = setup('postgres');
|
|
131
|
+
scopeManager.getScopeForUser.mockResolvedValue({ field: 'createdAt', operator: 'today', value: null });
|
|
132
|
+
|
|
133
|
+
await expect(
|
|
134
|
+
new LineStatGetter(model, lineParams({}), { Sequelize }, user).perform(),
|
|
135
|
+
).rejects.toMatchObject({ message: 'Missing timezone', status: 400 });
|
|
136
|
+
expect(findAll).not.toHaveBeenCalled();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('answers an invalid timezone with a 400 status', async () => {
|
|
140
|
+
expect.assertions(1);
|
|
141
|
+
const { model } = setup('postgres');
|
|
142
|
+
|
|
143
|
+
await expect(
|
|
144
|
+
new LineStatGetter(model, lineParams({ timezone: 'Europe/Atlantis' }), { Sequelize }, user).perform(),
|
|
145
|
+
).rejects.toMatchObject({ name: 'BadRequestError', status: 400 });
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('answers a missing timezone with a 400 status', async () => {
|
|
149
|
+
expect.assertions(1);
|
|
150
|
+
const { model } = setup('postgres');
|
|
151
|
+
|
|
152
|
+
await expect(
|
|
153
|
+
new LineStatGetter(model, lineParams({}), { Sequelize }, user).perform(),
|
|
154
|
+
).rejects.toMatchObject({ name: 'BadRequestError', status: 400 });
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
['mysql', 'mariadb', 'mssql', 'sqlite'].forEach((dialect) => {
|
|
159
|
+
describe(`with ${dialect}`, () => {
|
|
160
|
+
[...supportedTimezones, undefined, 'Europe Paris'].forEach((timezone) => {
|
|
161
|
+
it(`builds the chart query when the timezone is ${JSON.stringify(timezone)}`, async () => {
|
|
162
|
+
expect.assertions(2);
|
|
163
|
+
const { model, findAll, buildSql } = setup(dialect);
|
|
164
|
+
|
|
165
|
+
await new LineStatGetter(model, lineParams({ timezone }), { Sequelize }, user).perform();
|
|
166
|
+
|
|
167
|
+
expect(findAll).toHaveBeenCalledTimes(1);
|
|
168
|
+
expect(buildSql()).not.toContain('timezone(');
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|