refactor: simplify folder sorting logic with ICU collation

This commit is contained in:
Piyush Gupta
2025-11-04 21:37:29 +05:30
parent 0d3cb0c018
commit c407acebe4
@@ -419,20 +419,11 @@ export const secretFolderDALFactory = (db: TDbClient) => {
.select( .select(
selectAllTableCols(TableName.SecretFolder), selectAllTableCols(TableName.SecretFolder),
db.raw( db.raw(
`DENSE_RANK() OVER (ORDER BY CASE WHEN ${TableName.SecretFolder}."name" LIKE '\\_%' THEN ${orderDirection === OrderByDirection.ASC ? "0" : "1"} ELSE ${orderDirection === OrderByDirection.ASC ? "1" : "0"} END, `DENSE_RANK() OVER (ORDER BY ${TableName.SecretFolder}."name" COLLATE "en-x-icu" ${orderDirection}) as rank`
LOWER(${TableName.SecretFolder}."name") ${orderDirection},
${TableName.SecretFolder}."name" ${orderDirection}
) as rank`
), ),
db.ref("slug").withSchema(TableName.Environment).as("environment") db.ref("slug").withSchema(TableName.Environment).as("environment")
) )
.orderByRaw(`CASE WHEN ${TableName.SecretFolder}.?? LIKE '\\_%' THEN ? ELSE ? END`, [ .orderByRaw(`${TableName.SecretFolder}.?? COLLATE "en-x-icu" ${orderDirection}`, [orderBy]);
orderBy,
orderDirection === OrderByDirection.ASC ? 0 : 1,
orderDirection === OrderByDirection.ASC ? 1 : 0
])
.orderByRaw(`LOWER(${TableName.SecretFolder}.??) ${orderDirection}`, [orderBy])
.orderByRaw(`${TableName.SecretFolder}.?? ${orderDirection}`, [orderBy]);
if (limit) { if (limit) {
const rankOffset = offset + 1; // ranks start from 1 const rankOffset = offset + 1; // ranks start from 1
@@ -442,13 +433,7 @@ export const secretFolderDALFactory = (db: TDbClient) => {
.from<Awaited<typeof query>[number]>("w") .from<Awaited<typeof query>[number]>("w")
.where("w.rank", ">=", rankOffset) .where("w.rank", ">=", rankOffset)
.andWhere("w.rank", "<", rankOffset + limit) .andWhere("w.rank", "<", rankOffset + limit)
.orderByRaw(`CASE WHEN "w".?? LIKE '\\_%' THEN ? ELSE ? END`, [ .orderByRaw(`"w".?? COLLATE "en-x-icu" ${orderDirection}`, [orderBy]);
orderBy,
orderDirection === OrderByDirection.ASC ? 0 : 1,
orderDirection === OrderByDirection.ASC ? 1 : 0
])
.orderByRaw(`LOWER("w".??) ${orderDirection}`, [orderBy])
.orderByRaw(`"w".?? ${orderDirection}`, [orderBy]);
} }
const folders = await query; const folders = await query;
@@ -497,13 +482,7 @@ export const secretFolderDALFactory = (db: TDbClient) => {
.select<(TSecretFolders & { path: string; depth: number; environment: string })[]>("*") .select<(TSecretFolders & { path: string; depth: number; environment: string })[]>("*")
.from("parents") .from("parents")
.orderBy("depth") .orderBy("depth")
.orderByRaw(`CASE WHEN "parents".?? LIKE '\\_%' THEN ? ELSE ? END`, [ .orderByRaw(`"parents".?? COLLATE "en-x-icu" ${orderDirection}`, [orderBy]);
orderBy,
orderDirection === OrderByDirection.ASC ? 0 : 1,
orderDirection === OrderByDirection.ASC ? 1 : 0
])
.orderByRaw(`LOWER("parents".??) ${orderDirection}`, [orderBy])
.orderByRaw(`"parents".?? ${orderDirection}`, [orderBy]);
return folders; return folders;
} catch (error) { } catch (error) {