@trenskow/pged 4.0.9 → 4.1.0
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/package.json +1 -1
- package/query-builder.js +23 -6
package/package.json
CHANGED
package/query-builder.js
CHANGED
|
@@ -118,9 +118,17 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
|
|
|
118
118
|
return this;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
sorted(
|
|
122
|
-
if (
|
|
123
|
-
|
|
121
|
+
sorted(sortingKeys) {
|
|
122
|
+
if (typeof sortingKeys === 'string') sortingKeys = sortingKeys.split(/, ?/);
|
|
123
|
+
if (!Array.isArray(sortingKeys)) sortingKeys = [sortingKeys];
|
|
124
|
+
sortingKeys = sortingKeys.map((sortingKey) => {
|
|
125
|
+
if (typeof sortingKey === 'string') return {
|
|
126
|
+
key: sortingKey.substring(0, 1) === '-' ? sortingKey.substring(1) : sortingKey,
|
|
127
|
+
order: sortingKey.substring(0, 1) === '-' ? 'desc' : 'asc'
|
|
128
|
+
};
|
|
129
|
+
return sortingKey;
|
|
130
|
+
});
|
|
131
|
+
this._sortingKeys = this._sortingKeys.concat(sortingKeys);
|
|
124
132
|
return this;
|
|
125
133
|
}
|
|
126
134
|
|
|
@@ -419,15 +427,24 @@ module.exports = exports = class QueryBuilder extends CustomPromise {
|
|
|
419
427
|
}
|
|
420
428
|
|
|
421
429
|
_buildSorting() {
|
|
430
|
+
|
|
422
431
|
if (!this._sortingKeys.length) return;
|
|
432
|
+
|
|
423
433
|
const escapeIfNeeded = (value) => {
|
|
424
434
|
if (value.substring(0, 1) == ':') return value.substring(1);
|
|
425
435
|
return this._dbCase(value, true);
|
|
426
436
|
};
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
437
|
+
|
|
438
|
+
return `order by ${this._sortingKeys.map((sortingKey) => {
|
|
439
|
+
let condition = escapeIfNeeded(sortingKey.key);
|
|
440
|
+
if (Array.isArray(sortingKey.values)) {
|
|
441
|
+
condition = `case ${this._dbCase(sortingKey.key)} ${sortingKey.values.map((value, idx) => {
|
|
442
|
+
return `when '${value}' then ${idx}`;
|
|
443
|
+
}).join(' ')} end`;
|
|
444
|
+
}
|
|
445
|
+
return `${condition}${sortingKey.order === 'desc' ? ' desc' : ''}`;
|
|
430
446
|
}).join(', ')}`;
|
|
447
|
+
|
|
431
448
|
}
|
|
432
449
|
|
|
433
450
|
_buildOffset() {
|