Skip to content

Commit c6a66b5

Browse files
authored
[*] Segment Live Query - Fix issue where an incorrect live query causes the server to crash. (#362)
1 parent 69ecf18 commit c6a66b5

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Fixed
88
- Technical - Ensure that all databases are initialized before running CI tests.
9+
- Segment Live Query - Fix issue where an incorrect live query causes the server to crash.
910

1011
## RELEASE 5.6.0 - 2020-01-14
1112
### Added

src/services/resources-getter.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,49 @@ function ResourcesGetter(model, options, params) {
6363
}
6464

6565
async function getWhere() {
66-
return new P(async (resolve, reject) => {
67-
const where = {};
68-
where[OPERATORS.AND] = [];
66+
const where = {};
67+
where[OPERATORS.AND] = [];
6968

70-
if (params.search) {
71-
where[OPERATORS.AND].push(getSearchBuilder().perform());
72-
}
69+
if (params.search) {
70+
where[OPERATORS.AND].push(getSearchBuilder().perform());
71+
}
7372

74-
if (params.filters) {
75-
where[OPERATORS.AND].push(await handleFilterParams());
76-
}
73+
if (params.filters) {
74+
where[OPERATORS.AND].push(await handleFilterParams());
75+
}
7776

78-
if (segmentWhere) {
79-
where[OPERATORS.AND].push(segmentWhere);
80-
}
77+
if (segmentWhere) {
78+
where[OPERATORS.AND].push(segmentWhere);
79+
}
8180

82-
if (params.segmentQuery) {
83-
const queryToFilterRecords = params.segmentQuery.trim();
84-
new LiveQueryChecker().perform(queryToFilterRecords);
81+
if (params.segmentQuery) {
82+
const queryToFilterRecords = params.segmentQuery.trim();
8583

86-
// WARNING: Choosing the first connection might generate issues if the model does not
87-
// belongs to this database.
88-
return options.connections[0]
84+
new LiveQueryChecker().perform(queryToFilterRecords);
85+
86+
// WARNING: Choosing the first connection might generate issues if the model does not
87+
// belongs to this database.
88+
try {
89+
const results = await options.connections[0]
8990
.query(queryToFilterRecords, {
9091
type: options.sequelize.QueryTypes.SELECT,
91-
})
92-
.then((results) => {
93-
const recordIds = results.map((result) => result[primaryKey] || result.id);
94-
const condition = { [primaryKey]: {} };
95-
condition[primaryKey][OPERATORS.IN] = recordIds;
96-
where[OPERATORS.AND].push(condition);
97-
98-
return resolve(where);
99-
}, (error) => {
100-
const errorMessage = `Invalid SQL query for this Live Query segment:\n${error.message}`;
101-
logger.error(errorMessage);
102-
reject(new ErrorHTTP422(errorMessage));
10392
});
93+
94+
const recordIds = results.map((result) => result[primaryKey] || result.id);
95+
const condition = { [primaryKey]: {} };
96+
condition[primaryKey][OPERATORS.IN] = recordIds;
97+
where[OPERATORS.AND].push(condition);
98+
99+
return where;
100+
} catch (error) {
101+
const errorMessage = `Invalid SQL query for this Live Query segment:\n${error.message}`;
102+
logger.error(errorMessage);
103+
throw new ErrorHTTP422(errorMessage);
104104
}
105-
return resolve(where);
106-
});
107-
}
105+
}
108106

107+
return where;
108+
}
109109

110110
async function getRecords() {
111111
fieldNamesRequested = fieldNamesRequested || await getFieldNamesRequested();

0 commit comments

Comments
 (0)