mirror of
https://github.com/Infisical/infisical.git
synced 2026-01-09 07:28:09 -05:00
imrpove findByPath
This commit is contained in:
@@ -71,23 +71,29 @@ export const pamFolderDALFactory = (db: TDbClient) => {
|
||||
const findByPath = async (projectId: string, path: string, tx?: Knex) => {
|
||||
try {
|
||||
const dbInstance = tx || db.replicaNode();
|
||||
|
||||
const folders = await dbInstance(TableName.PamFolder)
|
||||
.where(`${TableName.PamFolder}.projectId`, projectId)
|
||||
.select(selectAllTableCols(TableName.PamFolder));
|
||||
|
||||
const pathSegments = path.split("/").filter(Boolean);
|
||||
if (pathSegments.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const foldersByParentId = new Map<string | null, typeof folders>();
|
||||
for (const folder of folders) {
|
||||
const children = foldersByParentId.get(folder.parentId ?? null) ?? [];
|
||||
children.push(folder);
|
||||
foldersByParentId.set(folder.parentId ?? null, children);
|
||||
}
|
||||
|
||||
let parentId: string | null = null;
|
||||
let currentFolder: Awaited<ReturnType<typeof orm.findOne>> | undefined;
|
||||
let currentFolder: (typeof folders)[0] | undefined;
|
||||
|
||||
for await (const segment of pathSegments) {
|
||||
const query = dbInstance(TableName.PamFolder)
|
||||
.where(`${TableName.PamFolder}.projectId`, projectId)
|
||||
.where(`${TableName.PamFolder}.name`, segment);
|
||||
|
||||
if (parentId) {
|
||||
void query.where(`${TableName.PamFolder}.parentId`, parentId);
|
||||
} else {
|
||||
void query.whereNull(`${TableName.PamFolder}.parentId`);
|
||||
}
|
||||
|
||||
currentFolder = await query.first();
|
||||
for (const segment of pathSegments) {
|
||||
const childFolders: typeof folders = foldersByParentId.get(parentId) || [];
|
||||
currentFolder = childFolders.find((folder) => folder.name === segment);
|
||||
|
||||
if (!currentFolder) {
|
||||
return undefined;
|
||||
|
||||
Reference in New Issue
Block a user