pangea-server 3.3.158 → 3.3.159
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/dist/database/db.class.js +28 -5
- package/package.json +1 -1
|
@@ -120,10 +120,7 @@ class Db {
|
|
|
120
120
|
const searchWords = search.split(/\s+/).filter(Boolean);
|
|
121
121
|
searchWhere = {
|
|
122
122
|
[_1.Ops.and]: searchWords.map((word) => ({
|
|
123
|
-
[_1.Ops.or]: searchFields.map((field) =>
|
|
124
|
-
const finalField = field.includes('.') ? `$${field}$` : field;
|
|
125
|
-
return { [finalField]: { [_1.Ops.like]: `%${word}%` } };
|
|
126
|
-
}),
|
|
123
|
+
[_1.Ops.or]: searchFields.map((field) => getSearchFieldCondition(model, field, word)),
|
|
127
124
|
})),
|
|
128
125
|
};
|
|
129
126
|
}
|
|
@@ -231,6 +228,32 @@ function formatField(modelName, field) {
|
|
|
231
228
|
}
|
|
232
229
|
return `${modelName}.${(0, pangea_helpers_1.camelToSnake)(field)}`;
|
|
233
230
|
}
|
|
231
|
+
function getSearchCorrelation(association, parentAlias, childAlias) {
|
|
232
|
+
if (association.associationType === 'BelongsTo') {
|
|
233
|
+
return `${childAlias}.\`${association.targetKeyField || 'id'}\` = ${parentAlias}.\`${association.identifierField}\``;
|
|
234
|
+
}
|
|
235
|
+
return `${childAlias}.\`${association.identifierField}\` = ${parentAlias}.\`${association.sourceKeyField || 'id'}\``;
|
|
236
|
+
}
|
|
237
|
+
function buildSearchExists(parentModel, parentAlias, segments, escapedWord, depth) {
|
|
238
|
+
const [relName, ...rest] = segments;
|
|
239
|
+
const association = parentModel.associations[relName];
|
|
240
|
+
const childModel = association.target;
|
|
241
|
+
const childAlias = `\`__search_${relName}_${depth}\``;
|
|
242
|
+
const conditions = [getSearchCorrelation(association, parentAlias, childAlias)];
|
|
243
|
+
if (parentModel.Relations[relName]?.paranoid)
|
|
244
|
+
conditions.push(`${childAlias}.\`deleted_at\` IS NULL`);
|
|
245
|
+
if (rest.length === 1)
|
|
246
|
+
conditions.push(`${childAlias}.\`${(0, pangea_helpers_1.camelToSnake)(rest[0])}\` LIKE ${escapedWord}`);
|
|
247
|
+
else
|
|
248
|
+
conditions.push(buildSearchExists(childModel, childAlias, rest, escapedWord, depth + 1));
|
|
249
|
+
return `EXISTS (SELECT 1 FROM \`${childModel.tableName}\` AS ${childAlias} WHERE ${conditions.join(' AND ')})`;
|
|
250
|
+
}
|
|
251
|
+
function getSearchFieldCondition(model, field, word) {
|
|
252
|
+
if (!field.includes('.'))
|
|
253
|
+
return { [field]: { [_1.Ops.like]: `%${word}%` } };
|
|
254
|
+
const escapedWord = (0, db_client_1.getDbClient)().escape(`%${word}%`);
|
|
255
|
+
return (0, sequelize_1.literal)(buildSearchExists(model, `\`${model.name}\``, field.split('.'), escapedWord, 0));
|
|
256
|
+
}
|
|
234
257
|
function getFinalAttributes(model, attributes) {
|
|
235
258
|
if (!attributes)
|
|
236
259
|
return;
|
|
@@ -251,7 +274,7 @@ function convertWhereKeys(obj) {
|
|
|
251
274
|
!(obj instanceof RegExp) &&
|
|
252
275
|
!(obj instanceof Map) &&
|
|
253
276
|
!(obj instanceof Set) &&
|
|
254
|
-
!(obj instanceof utils_1.
|
|
277
|
+
!(obj instanceof utils_1.SequelizeMethod) &&
|
|
255
278
|
!(typeof obj === 'function')) {
|
|
256
279
|
const newObj = {};
|
|
257
280
|
for (const key of Reflect.ownKeys(obj)) {
|