Skip to content

Commit 1b14316

Browse files
committed
Add helper for sorting on enum index
1 parent 65c01fd commit 1b14316

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/core/database/query/cypher-functions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const apoc = {
6969
},
7070
coll: {
7171
flatten: fn1('apoc.coll.flatten'),
72+
indexOf: fn('apoc.coll.indexOf'),
7273
},
7374
convert: {
7475
/** Converts Neo4j node to object/map of the node's properties */

src/core/database/query/lists.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { identity } from 'rxjs';
33
import {
44
getDbSortTransformer,
55
ID,
6+
MadeEnum,
67
Order,
78
PaginatedListType,
89
PaginationInput,
910
Resource,
1011
ResourceShape,
1112
} from '~/common';
12-
import { collect } from './cypher-functions';
13+
import { apoc, collect } from './cypher-functions';
1314
import { ACTIVE } from './matching';
1415

1516
/**
@@ -97,7 +98,7 @@ export const sorting =
9798
return query.comment`sorting(${sort})`
9899
.subQuery('*', matcher)
99100
.with('*')
100-
.orderBy(sortTransformer('sortValue'), order);
101+
.orderBy(`${sortTransformer('sortValue')}`, order);
101102
};
102103

103104
const matchPropSort = (prop: string) => (query: Query) =>
@@ -112,6 +113,14 @@ const matchPropSort = (prop: string) => (query: Query) =>
112113
const matchBasePropSort = (prop: string) => (query: Query) =>
113114
query.return(`node.${prop} as sortValue`);
114115

116+
export const sortingForEnumIndex =
117+
<T extends string>(theEnum: MadeEnum<T>) =>
118+
(variable: string) =>
119+
apoc.coll.indexOf(
120+
[...theEnum.values].map((v) => `"${v}"`),
121+
variable,
122+
);
123+
115124
export const whereNotDeletedInChangeset = (changeset?: ID) => (query: Query) =>
116125
changeset
117126
? query.raw(

0 commit comments

Comments
 (0)