|
|
|
|
@@ -4,30 +4,30 @@ import { DataSource, Repository } from '@n8n/typeorm';
|
|
|
|
|
|
|
|
|
|
import type { ListQuery } from '@/requests';
|
|
|
|
|
|
|
|
|
|
import type { FolderWithWorkflowsCount } from '../entities/folder';
|
|
|
|
|
import type { FolderWithWorkflowCount } from '../entities/folder';
|
|
|
|
|
import { Folder } from '../entities/folder';
|
|
|
|
|
import { FolderTagMapping } from '../entities/folder-tag-mapping';
|
|
|
|
|
import { TagEntity } from '../entities/tag-entity';
|
|
|
|
|
|
|
|
|
|
@Service()
|
|
|
|
|
export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
export class FolderRepository extends Repository<FolderWithWorkflowCount> {
|
|
|
|
|
constructor(dataSource: DataSource) {
|
|
|
|
|
super(Folder, dataSource.manager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getManyAndCount(
|
|
|
|
|
options: ListQuery.Options = {},
|
|
|
|
|
): Promise<[FolderWithWorkflowsCount[], number]> {
|
|
|
|
|
): Promise<[FolderWithWorkflowCount[], number]> {
|
|
|
|
|
const query = this.getManyQuery(options);
|
|
|
|
|
return await query.getManyAndCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getMany(options: ListQuery.Options = {}): Promise<FolderWithWorkflowsCount[]> {
|
|
|
|
|
async getMany(options: ListQuery.Options = {}): Promise<FolderWithWorkflowCount[]> {
|
|
|
|
|
const query = this.getManyQuery(options);
|
|
|
|
|
return await query.getMany();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getManyQuery(options: ListQuery.Options = {}): SelectQueryBuilder<FolderWithWorkflowsCount> {
|
|
|
|
|
getManyQuery(options: ListQuery.Options = {}): SelectQueryBuilder<FolderWithWorkflowCount> {
|
|
|
|
|
const query = this.createQueryBuilder('folder');
|
|
|
|
|
|
|
|
|
|
this.applySelections(query, options.select);
|
|
|
|
|
@@ -39,7 +39,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applySelections(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
select?: Record<string, boolean>,
|
|
|
|
|
): void {
|
|
|
|
|
if (select) {
|
|
|
|
|
@@ -49,12 +49,12 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyDefaultSelect(query: SelectQueryBuilder<FolderWithWorkflowsCount>): void {
|
|
|
|
|
private applyDefaultSelect(query: SelectQueryBuilder<FolderWithWorkflowCount>): void {
|
|
|
|
|
query
|
|
|
|
|
.leftJoinAndSelect('folder.homeProject', 'homeProject')
|
|
|
|
|
.leftJoinAndSelect('folder.parentFolder', 'parentFolder')
|
|
|
|
|
.leftJoinAndSelect('folder.tags', 'tags')
|
|
|
|
|
.loadRelationCountAndMap('folder.workflowsCount', 'folder.workflows')
|
|
|
|
|
.loadRelationCountAndMap('folder.workflowCount', 'folder.workflows')
|
|
|
|
|
.select([
|
|
|
|
|
'folder',
|
|
|
|
|
...this.getProjectFields('homeProject'),
|
|
|
|
|
@@ -64,7 +64,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyCustomSelect(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
select?: Record<string, boolean>,
|
|
|
|
|
): void {
|
|
|
|
|
const selections = ['folder.id'];
|
|
|
|
|
@@ -82,7 +82,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private addRelationFields(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
selections: string[],
|
|
|
|
|
select?: Record<string, boolean>,
|
|
|
|
|
): void {
|
|
|
|
|
@@ -101,8 +101,8 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
selections.push(...this.getParentFolderFields('parentFolder'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (select?.workflowsCount) {
|
|
|
|
|
query.loadRelationCountAndMap('folder.workflowsCount', 'folder.workflows');
|
|
|
|
|
if (select?.workflowCount) {
|
|
|
|
|
query.loadRelationCountAndMap('folder.workflowCount', 'folder.workflows');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -119,7 +119,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyFilters(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
filter?: ListQuery.Options['filter'],
|
|
|
|
|
): void {
|
|
|
|
|
if (!filter) return;
|
|
|
|
|
@@ -129,7 +129,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyBasicFilters(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
filter: ListQuery.Options['filter'],
|
|
|
|
|
): void {
|
|
|
|
|
if (filter?.folderIds && Array.isArray(filter.folderIds)) {
|
|
|
|
|
@@ -162,7 +162,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyTagsFilter(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
tags?: string[],
|
|
|
|
|
): void {
|
|
|
|
|
if (!Array.isArray(tags) || tags.length === 0) return;
|
|
|
|
|
@@ -176,7 +176,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createTagsSubQuery(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
tags: string[],
|
|
|
|
|
): SelectQueryBuilder<FolderTagMapping> {
|
|
|
|
|
return query
|
|
|
|
|
@@ -191,7 +191,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applySorting(query: SelectQueryBuilder<FolderWithWorkflowsCount>, sortBy?: string): void {
|
|
|
|
|
private applySorting(query: SelectQueryBuilder<FolderWithWorkflowCount>, sortBy?: string): void {
|
|
|
|
|
if (!sortBy) {
|
|
|
|
|
query.orderBy('folder.updatedAt', 'DESC');
|
|
|
|
|
return;
|
|
|
|
|
@@ -207,7 +207,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applySortingByField(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
field: string,
|
|
|
|
|
direction: 'DESC' | 'ASC',
|
|
|
|
|
): void {
|
|
|
|
|
@@ -219,7 +219,7 @@ export class FolderRepository extends Repository<FolderWithWorkflowsCount> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyPagination(
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowsCount>,
|
|
|
|
|
query: SelectQueryBuilder<FolderWithWorkflowCount>,
|
|
|
|
|
options: ListQuery.Options,
|
|
|
|
|
): void {
|
|
|
|
|
if (options?.take) {
|
|
|
|
|
|