mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 07:26:45 +00:00
Merge pull request #5005 from Infisical/improve-pam-func
improvement: pam findByPath function
This commit is contained in:
@@ -71,23 +71,29 @@ export const pamFolderDALFactory = (db: TDbClient) => {
|
|||||||
const findByPath = async (projectId: string, path: string, tx?: Knex) => {
|
const findByPath = async (projectId: string, path: string, tx?: Knex) => {
|
||||||
try {
|
try {
|
||||||
const dbInstance = tx || db.replicaNode();
|
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);
|
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 parentId: string | null = null;
|
||||||
let currentFolder: Awaited<ReturnType<typeof orm.findOne>> | undefined;
|
let currentFolder: (typeof folders)[0] | undefined;
|
||||||
|
|
||||||
for await (const segment of pathSegments) {
|
for (const segment of pathSegments) {
|
||||||
const query = dbInstance(TableName.PamFolder)
|
const childFolders: typeof folders = foldersByParentId.get(parentId) || [];
|
||||||
.where(`${TableName.PamFolder}.projectId`, projectId)
|
currentFolder = childFolders.find((folder) => folder.name === segment);
|
||||||
.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();
|
|
||||||
|
|
||||||
if (!currentFolder) {
|
if (!currentFolder) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user