mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 19:28:09 +00:00
refactor: enhance folder retrieval with dynamic sorting options
This commit is contained in:
@@ -426,11 +426,13 @@ export const secretFolderDALFactory = (db: TDbClient) => {
|
|||||||
),
|
),
|
||||||
db.ref("slug").withSchema(TableName.Environment).as("environment")
|
db.ref("slug").withSchema(TableName.Environment).as("environment")
|
||||||
)
|
)
|
||||||
.orderByRaw(
|
.orderByRaw(`CASE WHEN ${TableName.SecretFolder}.?? LIKE '\\_%' THEN ? ELSE ? END`, [
|
||||||
`CASE WHEN ${TableName.SecretFolder}.${orderBy} LIKE '\\_%' THEN ${orderDirection === OrderByDirection.ASC ? "0" : "1"} ELSE ${orderDirection === OrderByDirection.ASC ? "1" : "0"} END`
|
orderBy,
|
||||||
)
|
orderDirection === OrderByDirection.ASC ? 0 : 1,
|
||||||
.orderByRaw(`LOWER(${TableName.SecretFolder}.${orderBy}) ${orderDirection}`)
|
orderDirection === OrderByDirection.ASC ? 1 : 0
|
||||||
.orderByRaw(`${TableName.SecretFolder}.${orderBy} ${orderDirection}`);
|
])
|
||||||
|
.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
|
||||||
@@ -440,11 +442,13 @@ 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(
|
.orderByRaw(`CASE WHEN "w".?? LIKE '\\_%' THEN ? ELSE ? END`, [
|
||||||
`CASE WHEN "w"."${orderBy}" LIKE '\\_%' THEN ${orderDirection === OrderByDirection.ASC ? "0" : "1"} ELSE ${orderDirection === OrderByDirection.ASC ? "1" : "0"} END`
|
orderBy,
|
||||||
)
|
orderDirection === OrderByDirection.ASC ? 0 : 1,
|
||||||
.orderByRaw(`LOWER("w"."${orderBy}") ${orderDirection}`)
|
orderDirection === OrderByDirection.ASC ? 1 : 0
|
||||||
.orderByRaw(`"w"."${orderBy}" ${orderDirection}`);
|
])
|
||||||
|
.orderByRaw(`LOWER("w".??) ${orderDirection}`, [orderBy])
|
||||||
|
.orderByRaw(`"w".?? ${orderDirection}`, [orderBy]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const folders = await query;
|
const folders = await query;
|
||||||
@@ -455,7 +459,10 @@ export const secretFolderDALFactory = (db: TDbClient) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const findByEnvsDeep = async ({ parentIds }: TFindFoldersDeepByParentIdsDTO, tx?: Knex) => {
|
const findByEnvsDeep = async (
|
||||||
|
{ parentIds, orderBy = SecretsOrderBy.Name, orderDirection = OrderByDirection.ASC }: TFindFoldersDeepByParentIdsDTO,
|
||||||
|
tx?: Knex
|
||||||
|
) => {
|
||||||
try {
|
try {
|
||||||
const folders = await (tx || db.replicaNode())
|
const folders = await (tx || db.replicaNode())
|
||||||
.withRecursive("parents", (qb) =>
|
.withRecursive("parents", (qb) =>
|
||||||
@@ -490,7 +497,13 @@ 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")
|
||||||
.orderBy(`name`);
|
.orderByRaw(`CASE WHEN "parents".?? LIKE '\\_%' THEN ? ELSE ? END`, [
|
||||||
|
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) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { PgSqlLock } from "@app/keystore/keystore";
|
|||||||
import { BadRequestError, NotFoundError } from "@app/lib/errors";
|
import { BadRequestError, NotFoundError } from "@app/lib/errors";
|
||||||
import { OrderByDirection, OrgServiceActor } from "@app/lib/types";
|
import { OrderByDirection, OrgServiceActor } from "@app/lib/types";
|
||||||
import { ActorType } from "@app/services/auth/auth-type";
|
import { ActorType } from "@app/services/auth/auth-type";
|
||||||
|
import { SecretsOrderBy } from "@app/services/secret/secret-types";
|
||||||
import { buildFolderPath } from "@app/services/secret-folder/secret-folder-fns";
|
import { buildFolderPath } from "@app/services/secret-folder/secret-folder-fns";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -781,7 +782,11 @@ export const secretFolderServiceFactory = ({
|
|||||||
if (!parentFolder) return [];
|
if (!parentFolder) return [];
|
||||||
|
|
||||||
if (recursive) {
|
if (recursive) {
|
||||||
const recursiveFolders = await folderDAL.findByEnvsDeep({ parentIds: [parentFolder.id] });
|
const recursiveFolders = await folderDAL.findByEnvsDeep({
|
||||||
|
parentIds: [parentFolder.id],
|
||||||
|
orderBy: orderBy || SecretsOrderBy.Name,
|
||||||
|
orderDirection: orderDirection || OrderByDirection.ASC
|
||||||
|
});
|
||||||
// remove the parent folder
|
// remove the parent folder
|
||||||
return recursiveFolders
|
return recursiveFolders
|
||||||
.filter((folder) => {
|
.filter((folder) => {
|
||||||
@@ -800,19 +805,15 @@ export const secretFolderServiceFactory = ({
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
const folders = await folderDAL.find(
|
const folders = await folderDAL.findByMultiEnv({
|
||||||
{
|
environmentIds: [env.id],
|
||||||
envId: env.id,
|
parentIds: [parentFolder.id],
|
||||||
parentId: parentFolder.id,
|
search,
|
||||||
isReserved: false,
|
orderBy: orderBy || SecretsOrderBy.Name,
|
||||||
$search: search ? { name: `%${search}%` } : undefined
|
orderDirection: orderDirection || OrderByDirection.ASC,
|
||||||
},
|
limit,
|
||||||
{
|
offset
|
||||||
sort: orderBy ? [[orderBy, orderDirection ?? OrderByDirection.ASC]] : undefined,
|
});
|
||||||
limit,
|
|
||||||
offset
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (lastSecretModified) {
|
if (lastSecretModified) {
|
||||||
return folders.filter((el) =>
|
return folders.filter((el) =>
|
||||||
el.lastSecretModified ? el.lastSecretModified >= new Date(lastSecretModified) : false
|
el.lastSecretModified ? el.lastSecretModified >= new Date(lastSecretModified) : false
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ export type TGetFoldersDeepByEnvsDTO = {
|
|||||||
|
|
||||||
export type TFindFoldersDeepByParentIdsDTO = {
|
export type TFindFoldersDeepByParentIdsDTO = {
|
||||||
parentIds: string[];
|
parentIds: string[];
|
||||||
|
orderBy?: SecretsOrderBy;
|
||||||
|
orderDirection?: OrderByDirection;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TCreateManyFoldersDTO = {
|
export type TCreateManyFoldersDTO = {
|
||||||
|
|||||||
Reference in New Issue
Block a user