refactor(core): Move some typeorm operators to repositories (no-changelog) (#8139)
Moving some persistence logic to repositories to reduce circular dependencies.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Service } from 'typedi';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { DataSource, In, Repository } from 'typeorm';
|
||||
import { TagEntity } from '../entities/TagEntity';
|
||||
|
||||
@Service()
|
||||
@@ -7,4 +7,11 @@ export class TagRepository extends Repository<TagEntity> {
|
||||
constructor(dataSource: DataSource) {
|
||||
super(TagEntity, dataSource.manager);
|
||||
}
|
||||
|
||||
async findMany(tagIds: string[]) {
|
||||
return this.find({
|
||||
select: ['id', 'name'],
|
||||
where: { id: In(tagIds) },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,22 @@
|
||||
import { Service } from 'typedi';
|
||||
import { DataSource, Repository, type UpdateResult, type FindOptionsWhere } from 'typeorm';
|
||||
import {
|
||||
DataSource,
|
||||
Repository,
|
||||
In,
|
||||
Like,
|
||||
type UpdateResult,
|
||||
type FindOptionsWhere,
|
||||
type FindOptionsSelect,
|
||||
type FindManyOptions,
|
||||
type EntityManager,
|
||||
type DeleteResult,
|
||||
Not,
|
||||
} from 'typeorm';
|
||||
import type { ListQuery } from '@/requests';
|
||||
import { isStringArray } from '@/utils';
|
||||
import config from '@/config';
|
||||
import { WorkflowEntity } from '../entities/WorkflowEntity';
|
||||
import { SharedWorkflow } from '../entities/SharedWorkflow';
|
||||
|
||||
@Service()
|
||||
export class WorkflowRepository extends Repository<WorkflowEntity> {
|
||||
@@ -45,6 +60,29 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
|
||||
return totalTriggerCount ?? 0;
|
||||
}
|
||||
|
||||
async getSharings(
|
||||
transaction: EntityManager,
|
||||
workflowId: string,
|
||||
relations = ['shared'],
|
||||
): Promise<SharedWorkflow[]> {
|
||||
const workflow = await transaction.findOne(WorkflowEntity, {
|
||||
where: { id: workflowId },
|
||||
relations,
|
||||
});
|
||||
return workflow?.shared ?? [];
|
||||
}
|
||||
|
||||
async pruneSharings(
|
||||
transaction: EntityManager,
|
||||
workflowId: string,
|
||||
userIds: string[],
|
||||
): Promise<DeleteResult> {
|
||||
return transaction.delete(SharedWorkflow, {
|
||||
workflowId,
|
||||
userId: Not(In(userIds)),
|
||||
});
|
||||
}
|
||||
|
||||
async updateWorkflowTriggerCount(id: string, triggerCount: number): Promise<UpdateResult> {
|
||||
const qb = this.createQueryBuilder('workflow');
|
||||
return qb
|
||||
@@ -61,4 +99,77 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
|
||||
.where('id = :id', { id })
|
||||
.execute();
|
||||
}
|
||||
|
||||
async getMany(sharedWorkflowIds: string[], options?: ListQuery.Options) {
|
||||
if (sharedWorkflowIds.length === 0) return { workflows: [], count: 0 };
|
||||
|
||||
const where: FindOptionsWhere<WorkflowEntity> = {
|
||||
...options?.filter,
|
||||
id: In(sharedWorkflowIds),
|
||||
};
|
||||
|
||||
const reqTags = options?.filter?.tags;
|
||||
|
||||
if (isStringArray(reqTags)) {
|
||||
where.tags = reqTags.map((tag) => ({ name: tag }));
|
||||
}
|
||||
|
||||
type Select = FindOptionsSelect<WorkflowEntity> & { ownedBy?: true };
|
||||
|
||||
const select: Select = options?.select
|
||||
? { ...options.select } // copy to enable field removal without affecting original
|
||||
: {
|
||||
name: true,
|
||||
active: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
versionId: true,
|
||||
shared: { userId: true, roleId: true },
|
||||
};
|
||||
|
||||
delete select?.ownedBy; // remove non-entity field, handled after query
|
||||
|
||||
const relations: string[] = [];
|
||||
|
||||
const areTagsEnabled = !config.getEnv('workflowTagsDisabled');
|
||||
const isDefaultSelect = options?.select === undefined;
|
||||
const areTagsRequested = isDefaultSelect || options?.select?.tags === true;
|
||||
const isOwnedByIncluded = isDefaultSelect || options?.select?.ownedBy === true;
|
||||
|
||||
if (areTagsEnabled && areTagsRequested) {
|
||||
relations.push('tags');
|
||||
select.tags = { id: true, name: true };
|
||||
}
|
||||
|
||||
if (isOwnedByIncluded) relations.push('shared', 'shared.role', 'shared.user');
|
||||
|
||||
if (typeof where.name === 'string' && where.name !== '') {
|
||||
where.name = Like(`%${where.name}%`);
|
||||
}
|
||||
|
||||
const findManyOptions: FindManyOptions<WorkflowEntity> = {
|
||||
select: { ...select, id: true },
|
||||
where,
|
||||
};
|
||||
|
||||
if (isDefaultSelect || options?.select?.updatedAt === true) {
|
||||
findManyOptions.order = { updatedAt: 'ASC' };
|
||||
}
|
||||
|
||||
if (relations.length > 0) {
|
||||
findManyOptions.relations = relations;
|
||||
}
|
||||
|
||||
if (options?.take) {
|
||||
findManyOptions.skip = options.skip;
|
||||
findManyOptions.take = options.take;
|
||||
}
|
||||
|
||||
const [workflows, count] = (await this.findAndCount(findManyOptions)) as [
|
||||
ListQuery.Workflow.Plain[] | ListQuery.Workflow.WithSharing[],
|
||||
number,
|
||||
];
|
||||
|
||||
return { workflows, count };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user