@cuboapp/api-backend 1.0.7 → 1.0.8
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/src/helpers/query.ts +17 -15
- package/src/helpers/withes.ts +1 -1
package/package.json
CHANGED
package/src/helpers/query.ts
CHANGED
|
@@ -54,28 +54,30 @@ export class ApiHelpersQuery<T extends CuboApiEntitiesMap<T>> {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
// sort
|
|
57
|
-
if (
|
|
58
|
-
|
|
57
|
+
if (!sorts.length) {
|
|
58
|
+
if (sort !== undefined && typeof sort === 'string') {
|
|
59
|
+
const sortParts = sort.split(',')
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
for (const sortPart of sortParts) {
|
|
62
|
+
const parts = sortPart.replace(CUBO_CRUD_QUERY_KEY_REGEX, '')?.split('.')
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
if (parts.length === 1) {
|
|
65
|
+
const sortDirection = sortPart[0] === '-' ? 'desc' : 'asc'
|
|
66
|
+
const sortKey = sortPart[0] === '-' ? sortPart.slice(1) : sortPart
|
|
66
67
|
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
const conditionField = entity.fields?.find((f) => f.alias === sortKey)
|
|
69
|
+
const defaultField = CUBO_CRUD_DEFAULT_FIELDS.find((f) => f.alias === sortKey)
|
|
69
70
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
if (!conditionField && !defaultField) {
|
|
72
|
+
throw new Error('sortPart field "' + sortKey + '" not found')
|
|
73
|
+
}
|
|
73
74
|
|
|
74
|
-
|
|
75
|
+
sorts.push('t1.' + sortKey + ' ' + sortDirection)
|
|
76
|
+
}
|
|
75
77
|
}
|
|
78
|
+
} else {
|
|
79
|
+
sorts.push('t1.id desc')
|
|
76
80
|
}
|
|
77
|
-
} else {
|
|
78
|
-
sorts.push('t1.id desc')
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
// limit & offset
|
package/src/helpers/withes.ts
CHANGED
|
@@ -238,9 +238,9 @@ export class ApiHelpersWithes<T extends CuboApiEntitiesMap<T>> {
|
|
|
238
238
|
const havings: string[] = [...(opts?.queryOptions?.havings || [])]
|
|
239
239
|
const replacements: Record<string, any> = { ...(opts?.queryOptions?.replacements || {}) }
|
|
240
240
|
const groupBy: string = opts?.queryOptions?.groupBy || undefined
|
|
241
|
+
const sorts: string[] = opts?.queryOptions?.sorts || []
|
|
241
242
|
|
|
242
243
|
const withes: CuboCrudWith[] = []
|
|
243
|
-
const sorts: string[] = []
|
|
244
244
|
|
|
245
245
|
// from sort key
|
|
246
246
|
if (_sort) {
|