refactor(core): Move typeorm operators from various sources into repositories (no-changelog) (#8174)
Follow-up to: #8165
This commit is contained in:
@@ -1,16 +1,9 @@
|
||||
import { Service } from 'typedi';
|
||||
import {
|
||||
DataSource,
|
||||
In,
|
||||
Not,
|
||||
Repository,
|
||||
type DeleteResult,
|
||||
type EntityManager,
|
||||
type FindOptionsWhere,
|
||||
Like,
|
||||
} from 'typeorm';
|
||||
import { DataSource, In, Not, Repository, Like } from 'typeorm';
|
||||
import type { FindManyOptions, DeleteResult, EntityManager, FindOptionsWhere } from 'typeorm';
|
||||
import { CredentialsEntity } from '../entities/CredentialsEntity';
|
||||
import { SharedCredentials } from '../entities/SharedCredentials';
|
||||
import type { ListQuery } from '@/requests';
|
||||
|
||||
@Service()
|
||||
export class CredentialsRepository extends Repository<CredentialsEntity> {
|
||||
@@ -36,4 +29,61 @@ export class CredentialsRepository extends Repository<CredentialsEntity> {
|
||||
where: { name: Like(`${credentialName}%`) },
|
||||
});
|
||||
}
|
||||
|
||||
async findMany(listQueryOptions?: ListQuery.Options, credentialIds?: string[]) {
|
||||
const findManyOptions = this.toFindManyOptions(listQueryOptions);
|
||||
|
||||
if (credentialIds) {
|
||||
findManyOptions.where = { ...findManyOptions.where, id: In(credentialIds) };
|
||||
}
|
||||
|
||||
return this.find(findManyOptions);
|
||||
}
|
||||
|
||||
private toFindManyOptions(listQueryOptions?: ListQuery.Options) {
|
||||
const findManyOptions: FindManyOptions<CredentialsEntity> = {};
|
||||
|
||||
type Select = Array<keyof CredentialsEntity>;
|
||||
|
||||
const defaultRelations = ['shared', 'shared.role', 'shared.user'];
|
||||
const defaultSelect: Select = ['id', 'name', 'type', 'nodesAccess', 'createdAt', 'updatedAt'];
|
||||
|
||||
if (!listQueryOptions) return { select: defaultSelect, relations: defaultRelations };
|
||||
|
||||
const { filter, select, take, skip } = listQueryOptions;
|
||||
|
||||
if (typeof filter?.name === 'string' && filter?.name !== '') {
|
||||
filter.name = Like(`%${filter.name}%`);
|
||||
}
|
||||
|
||||
if (typeof filter?.type === 'string' && filter?.type !== '') {
|
||||
filter.type = Like(`%${filter.type}%`);
|
||||
}
|
||||
|
||||
if (filter) findManyOptions.where = filter;
|
||||
if (select) findManyOptions.select = select;
|
||||
if (take) findManyOptions.take = take;
|
||||
if (skip) findManyOptions.skip = skip;
|
||||
|
||||
if (take && select && !select?.id) {
|
||||
findManyOptions.select = { ...findManyOptions.select, id: true }; // pagination requires id
|
||||
}
|
||||
|
||||
if (!findManyOptions.select) {
|
||||
findManyOptions.select = defaultSelect;
|
||||
findManyOptions.relations = defaultRelations;
|
||||
}
|
||||
|
||||
return findManyOptions;
|
||||
}
|
||||
|
||||
async getManyByIds(ids: string[], { withSharings } = { withSharings: false }) {
|
||||
const findManyOptions: FindManyOptions<CredentialsEntity> = { where: { id: In(ids) } };
|
||||
|
||||
if (withSharings) {
|
||||
findManyOptions.relations = ['shared', 'shared.user', 'shared.role'];
|
||||
}
|
||||
|
||||
return this.find(findManyOptions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,17 +187,17 @@ export class ExecutionRepository extends Repository<ExecutionEntity> {
|
||||
where?: FindOptionsWhere<ExecutionEntity>;
|
||||
},
|
||||
): Promise<IExecutionFlattedDb | IExecutionResponse | IExecutionBase | undefined> {
|
||||
const whereClause: FindOneOptions<ExecutionEntity> = {
|
||||
const findOptions: FindOneOptions<ExecutionEntity> = {
|
||||
where: {
|
||||
id,
|
||||
...options?.where,
|
||||
},
|
||||
};
|
||||
if (options?.includeData) {
|
||||
whereClause.relations = ['executionData'];
|
||||
findOptions.relations = ['executionData'];
|
||||
}
|
||||
|
||||
const execution = await this.findOne(whereClause);
|
||||
const execution = await this.findOne(findOptions);
|
||||
|
||||
if (!execution) {
|
||||
return undefined;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Service } from 'typedi';
|
||||
import { DataSource, In, Repository } from 'typeorm';
|
||||
import { DataSource, In, Not, Repository } from 'typeorm';
|
||||
import { SharedCredentials } from '../entities/SharedCredentials';
|
||||
import type { User } from '../entities/User';
|
||||
import type { Role } from '../entities/Role';
|
||||
|
||||
@Service()
|
||||
export class SharedCredentialsRepository extends Repository<SharedCredentials> {
|
||||
@@ -30,4 +31,23 @@ export class SharedCredentialsRepository extends Repository<SharedCredentials> {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async makeOwnerOfAllCredentials(user: User, role: Role) {
|
||||
return this.update({ userId: Not(user.id), roleId: role.id }, { user });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the IDs of all credentials owned by or shared with a user.
|
||||
*/
|
||||
async getAccessibleCredentials(userId: string) {
|
||||
const sharings = await this.find({
|
||||
relations: ['role'],
|
||||
where: {
|
||||
userId,
|
||||
role: { name: In(['owner', 'user']), scope: 'credential' },
|
||||
},
|
||||
});
|
||||
|
||||
return sharings.map((s) => s.credentialsId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { Service } from 'typedi';
|
||||
import { DataSource, type FindOptionsWhere, Repository, In } from 'typeorm';
|
||||
import { DataSource, type FindOptionsWhere, Repository, In, Not } from 'typeorm';
|
||||
import { SharedWorkflow } from '../entities/SharedWorkflow';
|
||||
import { type User } from '../entities/User';
|
||||
import type { Scope } from '@n8n/permissions';
|
||||
import type { Role } from '../entities/Role';
|
||||
|
||||
@Service()
|
||||
export class SharedWorkflowRepository extends Repository<SharedWorkflow> {
|
||||
@@ -41,4 +43,33 @@ export class SharedWorkflowRepository extends Repository<SharedWorkflow> {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findSharing(
|
||||
workflowId: string,
|
||||
user: User,
|
||||
scope: Scope,
|
||||
{ roles, extraRelations }: { roles?: string[]; extraRelations?: string[] } = {},
|
||||
) {
|
||||
const where: FindOptionsWhere<SharedWorkflow> = {
|
||||
workflow: { id: workflowId },
|
||||
};
|
||||
|
||||
if (!user.hasGlobalScope(scope)) {
|
||||
where.user = { id: user.id };
|
||||
}
|
||||
|
||||
if (roles) {
|
||||
where.role = { name: In(roles) };
|
||||
}
|
||||
|
||||
const relations = ['workflow', 'role'];
|
||||
|
||||
if (extraRelations) relations.push(...extraRelations);
|
||||
|
||||
return this.findOne({ relations, where });
|
||||
}
|
||||
|
||||
async makeOwnerOfAllWorkflows(user: User, role: Role) {
|
||||
return this.update({ userId: Not(user.id), roleId: role.id }, { user });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Service } from 'typedi';
|
||||
import { DataSource, In, Repository } from 'typeorm';
|
||||
import { DataSource, In, Not, Repository } from 'typeorm';
|
||||
import { User } from '../entities/User';
|
||||
|
||||
@Service()
|
||||
@@ -14,4 +14,8 @@ export class UserRepository extends Repository<User> {
|
||||
relations: ['globalRole'],
|
||||
});
|
||||
}
|
||||
|
||||
async deleteAllExcept(user: User) {
|
||||
await this.delete({ id: Not(user.id) });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import { isStringArray } from '@/utils';
|
||||
import config from '@/config';
|
||||
import { WorkflowEntity } from '../entities/WorkflowEntity';
|
||||
import { SharedWorkflow } from '../entities/SharedWorkflow';
|
||||
import { WebhookEntity } from '../entities/WebhookEntity';
|
||||
|
||||
@Service()
|
||||
export class WorkflowRepository extends Repository<WorkflowEntity> {
|
||||
@@ -183,4 +184,18 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
|
||||
where: { name: Like(`${workflowName}%`) },
|
||||
});
|
||||
}
|
||||
|
||||
async findIn(workflowIds: string[]) {
|
||||
return this.find({
|
||||
select: ['id', 'name'],
|
||||
where: { id: In(workflowIds) },
|
||||
});
|
||||
}
|
||||
|
||||
async findWebhookBasedActiveWorkflows() {
|
||||
return this.createQueryBuilder('workflow')
|
||||
.select('DISTINCT workflow.id, workflow.name')
|
||||
.innerJoin(WebhookEntity, 'webhook_entity', 'workflow.id = webhook_entity.workflowId')
|
||||
.execute() as Promise<Array<{ id: string; name: string }>>;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user