Skip to content

Commit b124372

Browse files
committed
Fix enum sorting
Signed-off-by: Denis Bykhov <[email protected]>
1 parent 40be164 commit b124372

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

packages/postgres/CHANGELOG.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
{
22
"name": "@hcengineering/postgres",
33
"entries": [
4+
{
5+
"version": "0.7.19",
6+
"tag": "@hcengineering/postgres_v0.7.19",
7+
"date": "Wed, 12 Nov 2025 07:07:31 GMT",
8+
"comments": {
9+
"patch": [
10+
{
11+
"comment": "Fix enum sorting"
12+
}
13+
]
14+
}
15+
},
416
{
517
"version": "0.7.18",
618
"tag": "@hcengineering/postgres_v0.7.18",

packages/postgres/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log - @hcengineering/postgres
22

3-
This log was last generated on Thu, 06 Nov 2025 10:21:28 GMT and should not be manually modified.
3+
This log was last generated on Wed, 12 Nov 2025 07:07:31 GMT and should not be manually modified.
4+
5+
## 0.7.19
6+
Wed, 12 Nov 2025 07:07:31 GMT
7+
8+
### Patches
9+
10+
- Fix enum sorting
411

512
## 0.7.18
613
Thu, 06 Nov 2025 10:21:28 GMT

packages/postgres/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hcengineering/postgres",
3-
"version": "0.7.18",
3+
"version": "0.7.19",
44
"main": "lib/index.js",
55
"svelte": "src/index.ts",
66
"types": "types/index.d.ts",

packages/postgres/src/storage.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import core, {
1717
AccountRole,
18+
type AnyAttribute,
1819
type AssociationQuery,
1920
type Class,
2021
type Doc,
@@ -28,6 +29,8 @@ import core, {
2829
DOMAIN_RELATION,
2930
DOMAIN_SPACE,
3031
DOMAIN_TX,
32+
type Enum,
33+
type EnumOf,
3134
type FindOptions,
3235
type FindResult,
3336
getClassCollaborators,
@@ -105,7 +108,8 @@ import {
105108
NumericTypes,
106109
parseDoc,
107110
parseDocWithProjection,
108-
parseUpdate
111+
parseUpdate,
112+
simpleEscape
109113
} from './utils'
110114
async function * createCursorGenerator (
111115
client: postgres.Sql,
@@ -973,6 +977,13 @@ abstract class PostgresAdapterBase implements DbAdapter {
973977
}
974978
}
975979

980+
private getEnumVals (attr: AnyAttribute): Enum | undefined {
981+
try {
982+
const ref = (attr.type as EnumOf).of
983+
return this.modelDb.getObject<Enum>(ref)
984+
} catch {}
985+
}
986+
976987
private buildOrder<T extends Doc>(
977988
_class: Ref<Class<T>>,
978989
baseDomain: string,
@@ -981,12 +992,12 @@ abstract class PostgresAdapterBase implements DbAdapter {
981992
): string {
982993
const res: string[] = []
983994
for (const _key in sort) {
995+
const attr = this.hierarchy.findAttribute(_class, _key)
984996
const val = sort[_key]
985997
if (val === undefined) {
986998
continue
987999
}
9881000
if (typeof val === 'number') {
989-
const attr = this.hierarchy.findAttribute(_class, _key)
9901001
const key = escape(_key)
9911002
if (attr !== undefined && NumericTypes.includes(attr.type._class)) {
9921003
res.push(`(${this.getKey(_class, baseDomain, key, joins)})::numeric ${val === 1 ? 'ASC' : 'DESC'}`)
@@ -997,6 +1008,15 @@ abstract class PostgresAdapterBase implements DbAdapter {
9971008
res.push(
9981009
`COALESCE(NULLIF(regexp_replace(${this.getKey(_class, baseDomain, key, joins)}, '.*?(\\d+)$', '\\1'), ''), '0')::INT ${val === 1 ? 'ASC' : 'DESC'}`
9991010
)
1011+
} else if (attr?.type._class === core.class.EnumOf && this.getEnumVals(attr) !== undefined) {
1012+
const enumValues = this.getEnumVals(attr)
1013+
if (enumValues !== undefined) {
1014+
const orderCase = enumValues.enumValues.map((v, i) => `WHEN '${simpleEscape(v)}' THEN ${i + 1}`).join(' ')
1015+
1016+
res.push(
1017+
`CASE ${this.getKey(_class, baseDomain, key, joins)} ${orderCase} ELSE 999 END ${val === 1 ? 'ASC' : 'DESC'}`
1018+
)
1019+
}
10001020
} else {
10011021
res.push(`${this.getKey(_class, baseDomain, key, joins)} ${val === 1 ? 'ASC' : 'DESC'}`)
10021022
}

packages/postgres/src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,10 @@ export function escape<T> (str: T): T {
528528
}
529529
return str
530530
}
531+
532+
export function simpleEscape<T> (str: T): T {
533+
if (typeof str === 'string') {
534+
return str.replace(/'/g, "''") as T
535+
}
536+
return str
537+
}

0 commit comments

Comments
 (0)